Consistent use of varargs, consistent template method order

This commit is contained in:
Juergen Hoeller 2013-08-09 11:43:20 +02:00
parent 92e3c52a48
commit b27e240fdb
5 changed files with 192 additions and 187 deletions

View File

@ -97,7 +97,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
private String encoding = DEFAULT_ENCODING; private String encoding = DEFAULT_ENCODING;
private Class[] targetClasses; private Class<?>[] targetClasses;
private String[] targetPackages; private String[] targetPackages;
@ -172,7 +172,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
/** /**
* Set the locations of the Castor XML Mapping files. * Set the locations of the Castor XML Mapping files.
*/ */
public void setMappingLocations(Resource[] mappingLocations) { public void setMappingLocations(Resource... mappingLocations) {
this.mappingLocations = mappingLocations; this.mappingLocations = mappingLocations;
} }
@ -180,15 +180,15 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
* Set the Castor target class. Alternative means of configuring {@code CastorMarshaller} for unmarshalling * Set the Castor target class. Alternative means of configuring {@code CastorMarshaller} for unmarshalling
* multiple classes include use of mapping files, and specifying packages with Castor descriptor classes. * multiple classes include use of mapping files, and specifying packages with Castor descriptor classes.
*/ */
public void setTargetClass(Class targetClass) { public void setTargetClass(Class<?> targetClass) {
this.targetClasses = new Class[]{targetClass}; this.targetClasses = new Class<?>[] {targetClass};
} }
/** /**
* Set the Castor target classes. Alternative means of configuring {@code CastorMarshaller} for unmarshalling * Set the Castor target classes. Alternative means of configuring {@code CastorMarshaller} for unmarshalling
* multiple classes include use of mapping files, and specifying packages with Castor descriptor classes. * multiple classes include use of mapping files, and specifying packages with Castor descriptor classes.
*/ */
public void setTargetClasses(Class[] targetClasses) { public void setTargetClasses(Class<?>... targetClasses) {
this.targetClasses = targetClasses; this.targetClasses = targetClasses;
} }
@ -202,7 +202,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
/** /**
* Set the names of packages with the Castor descriptor classes. * Set the names of packages with the Castor descriptor classes.
*/ */
public void setTargetPackages(String[] targetPackages) { public void setTargetPackages(String... targetPackages) {
this.targetPackages = targetPackages; this.targetPackages = targetPackages;
} }
@ -458,8 +458,8 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
* @see XMLContext#addMapping(org.exolab.castor.mapping.Mapping) * @see XMLContext#addMapping(org.exolab.castor.mapping.Mapping)
* @see XMLContext#addClass(Class) * @see XMLContext#addClass(Class)
*/ */
protected XMLContext createXMLContext(Resource[] mappingLocations, Class[] targetClasses, String[] targetPackages) protected XMLContext createXMLContext(Resource[] mappingLocations, Class<?>[] targetClasses,
throws MappingException, ResolverException, IOException { String[] targetPackages) throws MappingException, ResolverException, IOException {
XMLContext context = new XMLContext(); XMLContext context = new XMLContext();
if (!ObjectUtils.isEmpty(mappingLocations)) { if (!ObjectUtils.isEmpty(mappingLocations)) {
@ -492,47 +492,46 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
return true; return true;
} }
// Marshalling // Marshalling
@Override @Override
protected final void marshalDomNode(Object graph, Node node) throws XmlMappingException { protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
marshalSaxHandlers(graph, DomUtils.createContentHandler(node), null); marshalSaxHandlers(graph, DomUtils.createContentHandler(node), null);
} }
@Override @Override
protected final void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException {
throws XmlMappingException {
Marshaller marshaller = xmlContext.createMarshaller();
marshaller.setContentHandler(contentHandler);
marshal(graph, marshaller);
}
@Override
protected final void marshalOutputStream(Object graph, OutputStream outputStream)
throws XmlMappingException, IOException {
marshalWriter(graph, new OutputStreamWriter(outputStream, encoding));
}
@Override
protected final void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
Marshaller marshaller = xmlContext.createMarshaller();
marshaller.setWriter(writer);
marshal(graph, marshaller);
}
@Override
protected final void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException {
marshalSaxHandlers(graph, StaxUtils.createContentHandler(eventWriter), null); marshalSaxHandlers(graph, StaxUtils.createContentHandler(eventWriter), null);
} }
@Override @Override
protected final void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
marshalSaxHandlers(graph, StaxUtils.createContentHandler(streamWriter), null); marshalSaxHandlers(graph, StaxUtils.createContentHandler(streamWriter), null);
} }
private void marshal(Object graph, Marshaller marshaller) { @Override
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
throws XmlMappingException {
Marshaller marshaller = xmlContext.createMarshaller();
marshaller.setContentHandler(contentHandler);
doMarshal(graph, marshaller);
}
@Override
protected void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException {
marshalWriter(graph, new OutputStreamWriter(outputStream, encoding));
}
@Override
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
Marshaller marshaller = xmlContext.createMarshaller();
marshaller.setWriter(writer);
doMarshal(graph, marshaller);
}
private void doMarshal(Object graph, Marshaller marshaller) {
try { try {
customizeMarshaller(marshaller); customizeMarshaller(marshaller);
marshaller.marshal(graph); marshaller.marshal(graph);
@ -572,10 +571,11 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
} }
} }
// Unmarshalling // Unmarshalling
@Override @Override
protected final Object unmarshalDomNode(Node node) throws XmlMappingException { protected Object unmarshalDomNode(Node node) throws XmlMappingException {
try { try {
return createUnmarshaller().unmarshal(node); return createUnmarshaller().unmarshal(node);
} }
@ -585,9 +585,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
} }
@Override @Override
protected final Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { protected Object unmarshalXmlEventReader(XMLEventReader eventReader) {
try { try {
return createUnmarshaller().unmarshal(new InputSource(inputStream)); return createUnmarshaller().unmarshal(eventReader);
} }
catch (XMLException ex) { catch (XMLException ex) {
throw convertCastorException(ex, false); throw convertCastorException(ex, false);
@ -595,9 +595,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
} }
@Override @Override
protected final Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) {
try { try {
return createUnmarshaller().unmarshal(new InputSource(reader)); return createUnmarshaller().unmarshal(streamReader);
} }
catch (XMLException ex) { catch (XMLException ex) {
throw convertCastorException(ex, false); throw convertCastorException(ex, false);
@ -605,7 +605,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
} }
@Override @Override
protected final Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
throws XmlMappingException, IOException { throws XmlMappingException, IOException {
UnmarshalHandler unmarshalHandler = createUnmarshaller().createHandler(); UnmarshalHandler unmarshalHandler = createUnmarshaller().createHandler();
@ -621,9 +621,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
} }
@Override @Override
protected final Object unmarshalXmlEventReader(XMLEventReader eventReader) { protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
try { try {
return createUnmarshaller().unmarshal(eventReader); return createUnmarshaller().unmarshal(new InputSource(inputStream));
} }
catch (XMLException ex) { catch (XMLException ex) {
throw convertCastorException(ex, false); throw convertCastorException(ex, false);
@ -631,9 +631,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
} }
@Override @Override
protected final Object unmarshalXmlStreamReader(XMLStreamReader streamReader) { protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
try { try {
return createUnmarshaller().unmarshal(streamReader); return createUnmarshaller().unmarshal(new InputSource(reader));
} }
catch (XMLException ex) { catch (XMLException ex) {
throw convertCastorException(ex, false); throw convertCastorException(ex, false);
@ -679,6 +679,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
} }
} }
/** /**
* Convert the given {@code XMLException} to an appropriate exception from the * Convert the given {@code XMLException} to an appropriate exception from the
* {@code org.springframework.oxm} hierarchy. * {@code org.springframework.oxm} hierarchy.

View File

@ -297,7 +297,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
* Specify the {@code XmlAdapter}s to be registered with the JAXB {@code Marshaller} * Specify the {@code XmlAdapter}s to be registered with the JAXB {@code Marshaller}
* and {@code Unmarshaller} * and {@code Unmarshaller}
*/ */
public void setAdapters(XmlAdapter<?, ?>[] adapters) { public void setAdapters(XmlAdapter<?, ?>... adapters) {
this.adapters = adapters; this.adapters = adapters;
} }
@ -311,7 +311,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
/** /**
* Set the schema resources to use for validation. * Set the schema resources to use for validation.
*/ */
public void setSchemas(Resource[] schemaResources) { public void setSchemas(Resource... schemaResources) {
this.schemaResources = schemaResources; this.schemaResources = schemaResources;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -84,6 +84,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
private static final String DEFAULT_BINDING_NAME = "binding"; private static final String DEFAULT_BINDING_NAME = "binding";
private Class<?> targetClass; private Class<?> targetClass;
private String targetPackage; private String targetPackage;
@ -112,7 +113,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
/** /**
* Set the target class for this instance. Setting either this property or the * Set the target class for this instance. Setting either this property or the
* {@link #setTargetPackage(String) targetPackage} property is required. * {@link #setTargetPackage(String) targetPackage} property is required.
*
* <p>If this property is set, {@link #setTargetPackage(String) targetPackage} is ignored. * <p>If this property is set, {@link #setTargetPackage(String) targetPackage} is ignored.
*/ */
public void setTargetClass(Class<?> targetClass) { public void setTargetClass(Class<?> targetClass) {
@ -122,7 +122,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
/** /**
* Set the target package for this instance. Setting either this property or the * Set the target package for this instance. Setting either this property or the
* {@link #setTargetClass(Class) targetClass} property is required. * {@link #setTargetClass(Class) targetClass} property is required.
*
* <p>If {@link #setTargetClass(Class) targetClass} is set, this property is ignored. * <p>If {@link #setTargetClass(Class) targetClass} is set, this property is ignored.
*/ */
public void setTargetPackage(String targetPackage) { public void setTargetPackage(String targetPackage) {
@ -157,10 +156,9 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
/** /**
* Sets the root element name for the DTD declaration written when marshalling. By default, this is * Set the root element name for the DTD declaration written when marshalling.
* {@code null} (i.e. no DTD declaration is written). If set to a value, the system ID or public ID also need to * By default, this is {@code null} (i.e. no DTD declaration is written).
* be set. * <p>If set to a value, the system ID or public ID also need to be set.
*
* @see #setDocTypeSystemId(String) * @see #setDocTypeSystemId(String)
* @see #setDocTypePublicId(String) * @see #setDocTypePublicId(String)
*/ */
@ -169,10 +167,9 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
/** /**
* Sets the system Id for the DTD declaration written when marshalling. By default, this is * Set the system Id for the DTD declaration written when marshalling.
* {@code null}. Only used when the root element also has been set. Set either this property or * By default, this is {@code null}. Only used when the root element also has been set.
* {@code docTypePublicId}, not both. * <p>Set either this property or {@code docTypePublicId}, not both.
*
* @see #setDocTypeRootElementName(String) * @see #setDocTypeRootElementName(String)
*/ */
public void setDocTypeSystemId(String docTypeSystemId) { public void setDocTypeSystemId(String docTypeSystemId) {
@ -180,10 +177,9 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
/** /**
* Sets the public Id for the DTD declaration written when marshalling. By default, this is * Set the public Id for the DTD declaration written when marshalling.
* {@code null}. Only used when the root element also has been set. Set either this property or * By default, this is {@code null}. Only used when the root element also has been set.
* {@code docTypeSystemId}, not both. * <p>Set either this property or {@code docTypeSystemId}, not both.
*
* @see #setDocTypeRootElementName(String) * @see #setDocTypeRootElementName(String)
*/ */
public void setDocTypePublicId(String docTypePublicId) { public void setDocTypePublicId(String docTypePublicId) {
@ -191,15 +187,15 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
/** /**
* Sets the internal subset Id for the DTD declaration written when marshalling. By default, this is * Set the internal subset Id for the DTD declaration written when marshalling.
* {@code null}. Only used when the root element also has been set. * By default, this is {@code null}. Only used when the root element also has been set.
*
* @see #setDocTypeRootElementName(String) * @see #setDocTypeRootElementName(String)
*/ */
public void setDocTypeInternalSubset(String docTypeInternalSubset) { public void setDocTypeInternalSubset(String docTypeInternalSubset) {
this.docTypeInternalSubset = docTypeInternalSubset; this.docTypeInternalSubset = docTypeInternalSubset;
} }
@Override @Override
public void afterPropertiesSet() throws JiBXException { public void afterPropertiesSet() throws JiBXException {
if (this.targetClass != null) { if (this.targetClass != null) {
@ -215,7 +211,8 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
this.bindingFactory = BindingDirectory.getFactory(this.targetClass); this.bindingFactory = BindingDirectory.getFactory(this.targetClass);
} }
} else if (this.targetPackage != null) { }
else if (this.targetPackage != null) {
if (!StringUtils.hasLength(bindingName)) { if (!StringUtils.hasLength(bindingName)) {
bindingName = DEFAULT_BINDING_NAME; bindingName = DEFAULT_BINDING_NAME;
} }
@ -223,7 +220,8 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
logger.info("Configured for target package [" + targetPackage + "] using binding [" + bindingName + "]"); logger.info("Configured for target package [" + targetPackage + "] using binding [" + bindingName + "]");
} }
this.bindingFactory = BindingDirectory.getFactory(bindingName, targetPackage); this.bindingFactory = BindingDirectory.getFactory(bindingName, targetPackage);
} else { }
else {
throw new IllegalArgumentException("either 'targetClass' or 'targetPackage' is required"); throw new IllegalArgumentException("either 'targetClass' or 'targetPackage' is required");
} }
} }
@ -246,7 +244,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
// Supported Marshalling // Supported marshalling
@Override @Override
protected void marshalOutputStream(Object graph, OutputStream outputStream) protected void marshalOutputStream(Object graph, OutputStream outputStream)
@ -273,8 +271,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
} }
private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, JiBXException {
JiBXException {
if (StringUtils.hasLength(docTypeRootElementName)) { if (StringUtils.hasLength(docTypeRootElementName)) {
IXMLWriter xmlWriter = marshallingContext.getXmlWriter(); IXMLWriter xmlWriter = marshallingContext.getXmlWriter();
xmlWriter.writeDocType(docTypeRootElementName, docTypeSystemId, docTypePublicId, docTypeInternalSubset); xmlWriter.writeDocType(docTypeRootElementName, docTypeSystemId, docTypePublicId, docTypeInternalSubset);
@ -282,6 +279,27 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
marshallingContext.marshalDocument(graph); marshallingContext.marshalDocument(graph);
} }
// Unsupported marshalling
@Override
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
try {
// JiBX does not support DOM natively, so we write to a buffer first, and transform that to the Node
Result result = new DOMResult(node);
transformAndMarshal(graph, result);
}
catch (IOException ex) {
throw new MarshallingFailureException("JiBX marshalling exception", ex);
}
}
@Override
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) {
XMLStreamWriter streamWriter = StaxUtils.createEventStreamWriter(eventWriter);
marshalXmlStreamWriter(graph, streamWriter);
}
@Override @Override
protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
try { try {
@ -295,20 +313,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
} }
// Unsupported Marshalling
@Override
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
try {
// JiBX does not support DOM natively, so we write to a buffer first, and transform that to the Node
Result result = new DOMResult(node);
transformAndMarshal(graph, result);
}
catch (IOException ex) {
throw new MarshallingFailureException("JiBX marshalling exception", ex);
}
}
@Override @Override
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
throws XmlMappingException { throws XmlMappingException {
@ -338,15 +342,33 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
@Override
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) {
XMLStreamWriter streamWriter = StaxUtils.createEventStreamWriter(eventWriter);
marshalXmlStreamWriter(graph, streamWriter);
}
// Unmarshalling // Unmarshalling
@Override
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) {
try {
XMLStreamReader streamReader = StaxUtils.createEventStreamReader(eventReader);
return unmarshalXmlStreamReader(streamReader);
}
catch (XMLStreamException ex) {
return new UnmarshallingFailureException("JiBX unmarshalling exception", ex);
}
}
@Override
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) {
try {
UnmarshallingContext unmarshallingContext = (UnmarshallingContext) createUnmarshallingContext();
IXMLReader xmlReader = new StAXReaderWrapper(streamReader, null, true);
unmarshallingContext.setDocument(xmlReader);
return unmarshallingContext.unmarshalElement();
}
catch (JiBXException ex) {
throw convertJibxException(ex, false);
}
}
@Override @Override
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
try { try {
@ -369,30 +391,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
} }
} }
@Override
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) {
try {
UnmarshallingContext unmarshallingContext = (UnmarshallingContext) createUnmarshallingContext();
IXMLReader xmlReader = new StAXReaderWrapper(streamReader, null, true);
unmarshallingContext.setDocument(xmlReader);
return unmarshallingContext.unmarshalElement();
}
catch (JiBXException ex) {
throw convertJibxException(ex, false);
}
}
@Override
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) {
try {
XMLStreamReader streamReader = StaxUtils.createEventStreamReader(eventReader);
return unmarshalXmlStreamReader(streamReader);
}
catch (XMLStreamException ex) {
return new UnmarshallingFailureException("JiBX unmarshalling exception", ex);
}
}
// Unsupported Unmarshalling // Unsupported Unmarshalling

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -170,7 +170,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
} }
/** /**
* Create a {@code XMLReader} that this marshaller will when passed an empty {@code SAXSource}. * Create an {@code XMLReader} that this marshaller will when passed an empty {@code SAXSource}.
* @return the XMLReader * @return the XMLReader
* @throws SAXException if thrown by JAXP methods * @throws SAXException if thrown by JAXP methods
*/ */
@ -215,7 +215,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
* {@code marshalXMLEventConsumer}, depending on what is contained in the * {@code marshalXMLEventConsumer}, depending on what is contained in the
* {@code StaxResult}. * {@code StaxResult}.
* @param graph the root of the object graph to marshal * @param graph the root of the object graph to marshal
* @param staxResult a Spring {@link org.springframework.util.xml.StaxSource} or JAXP 1.4 {@link StAXSource} * @param staxResult a JAXP 1.4 {@link StAXSource}
* @throws XmlMappingException if the given object cannot be marshalled to the result * @throws XmlMappingException if the given object cannot be marshalled to the result
* @throws IllegalArgumentException if the {@code domResult} is empty * @throws IllegalArgumentException if the {@code domResult} is empty
* @see #marshalDomNode(Object, org.w3c.dom.Node) * @see #marshalDomNode(Object, org.w3c.dom.Node)
@ -412,16 +412,6 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
protected abstract void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) protected abstract void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter)
throws XmlMappingException; throws XmlMappingException;
/**
* Abstract template method for marshalling the given object graph to a {@code OutputStream}.
* @param graph the root of the object graph to marshal
* @param outputStream the {@code OutputStream} to write to
* @throws XmlMappingException if the given object cannot be marshalled to the writer
* @throws IOException if an I/O exception occurs
*/
protected abstract void marshalOutputStream(Object graph, OutputStream outputStream)
throws XmlMappingException, IOException;
/** /**
* Abstract template method for marshalling the given object graph to a SAX {@code ContentHandler}. * Abstract template method for marshalling the given object graph to a SAX {@code ContentHandler}.
* @param graph the root of the object graph to marshal * @param graph the root of the object graph to marshal
@ -433,6 +423,16 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
throws XmlMappingException; throws XmlMappingException;
/**
* Abstract template method for marshalling the given object graph to a {@code OutputStream}.
* @param graph the root of the object graph to marshal
* @param outputStream the {@code OutputStream} to write to
* @throws XmlMappingException if the given object cannot be marshalled to the writer
* @throws IOException if an I/O exception occurs
*/
protected abstract void marshalOutputStream(Object graph, OutputStream outputStream)
throws XmlMappingException, IOException;
/** /**
* Abstract template method for marshalling the given object graph to a {@code Writer}. * Abstract template method for marshalling the given object graph to a {@code Writer}.
* @param graph the root of the object graph to marshal * @param graph the root of the object graph to marshal
@ -443,6 +443,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
protected abstract void marshalWriter(Object graph, Writer writer) protected abstract void marshalWriter(Object graph, Writer writer)
throws XmlMappingException, IOException; throws XmlMappingException, IOException;
/** /**
* Abstract template method for unmarshalling from a given DOM {@code Node}. * Abstract template method for unmarshalling from a given DOM {@code Node}.
* @param node the DOM node that contains the objects to be unmarshalled * @param node the DOM node that contains the objects to be unmarshalled
@ -469,6 +470,18 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
protected abstract Object unmarshalXmlStreamReader(XMLStreamReader streamReader) protected abstract Object unmarshalXmlStreamReader(XMLStreamReader streamReader)
throws XmlMappingException; throws XmlMappingException;
/**
* Abstract template method for unmarshalling using a given SAX {@code XMLReader}
* and {@code InputSource}.
* @param xmlReader the SAX {@code XMLReader} to parse with
* @param inputSource the input source to parse from
* @return the object graph
* @throws XmlMappingException if the given reader and input source cannot be converted to an object
* @throws IOException if an I/O exception occurs
*/
protected abstract Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
throws XmlMappingException, IOException;
/** /**
* Abstract template method for unmarshalling from a given {@code InputStream}. * Abstract template method for unmarshalling from a given {@code InputStream}.
* @param inputStream the {@code InputStreamStream} to read from * @param inputStream the {@code InputStreamStream} to read from
@ -489,16 +502,4 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller {
protected abstract Object unmarshalReader(Reader reader) protected abstract Object unmarshalReader(Reader reader)
throws XmlMappingException, IOException; throws XmlMappingException, IOException;
/**
* Abstract template method for unmarshalling using a given SAX {@code XMLReader}
* and {@code InputSource}.
* @param xmlReader the SAX {@code XMLReader} to parse with
* @param inputSource the input source to parse from
* @return the object graph
* @throws XmlMappingException if the given reader and input source cannot be converted to an object
* @throws IOException if an I/O exception occurs
*/
protected abstract Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
throws XmlMappingException, IOException;
} }

View File

@ -122,8 +122,9 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
return XmlObject.class.isAssignableFrom(clazz); return XmlObject.class.isAssignableFrom(clazz);
} }
@Override @Override
protected final void marshalDomNode(Object graph, Node node) throws XmlMappingException { protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument(); Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument();
Node xmlBeansNode = ((XmlObject) graph).newDomNode(getXmlOptions()); Node xmlBeansNode = ((XmlObject) graph).newDomNode(getXmlOptions());
NodeList xmlBeansChildNodes = xmlBeansNode.getChildNodes(); NodeList xmlBeansChildNodes = xmlBeansNode.getChildNodes();
@ -135,14 +136,19 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
} }
@Override @Override
protected final void marshalOutputStream(Object graph, OutputStream outputStream) protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) {
throws XmlMappingException, IOException { ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter);
marshalSaxHandlers(graph, contentHandler, null);
((XmlObject) graph).save(outputStream, getXmlOptions());
} }
@Override @Override
protected final void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException {
ContentHandler contentHandler = StaxUtils.createContentHandler(streamWriter);
marshalSaxHandlers(graph, contentHandler, null);
}
@Override
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler)
throws XmlMappingException { throws XmlMappingException {
try { try {
((XmlObject) graph).save(contentHandler, lexicalHandler, getXmlOptions()); ((XmlObject) graph).save(contentHandler, lexicalHandler, getXmlOptions());
@ -153,24 +159,20 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
} }
@Override @Override
protected final void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException { protected void marshalOutputStream(Object graph, OutputStream outputStream)
throws XmlMappingException, IOException {
((XmlObject) graph).save(outputStream, getXmlOptions());
}
@Override
protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
((XmlObject) graph).save(writer, getXmlOptions()); ((XmlObject) graph).save(writer, getXmlOptions());
} }
@Override
protected final void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) {
ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter);
marshalSaxHandlers(graph, contentHandler, null);
}
@Override @Override
protected final void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { protected Object unmarshalDomNode(Node node) throws XmlMappingException {
ContentHandler contentHandler = StaxUtils.createContentHandler(streamWriter);
marshalSaxHandlers(graph, contentHandler, null);
}
@Override
protected final Object unmarshalDomNode(Node node) throws XmlMappingException {
try { try {
XmlObject object = XmlObject.Factory.parse(node, getXmlOptions()); XmlObject object = XmlObject.Factory.parse(node, getXmlOptions());
validate(object); validate(object);
@ -182,10 +184,20 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
} }
@Override @Override
protected final Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException {
XMLReader reader = StaxUtils.createXMLReader(eventReader);
try { try {
InputStream nonClosingInputStream = new NonClosingInputStream(inputStream); return unmarshalSaxReader(reader, new InputSource());
XmlObject object = XmlObject.Factory.parse(nonClosingInputStream, getXmlOptions()); }
catch (IOException ex) {
throw convertXmlBeansException(ex, false);
}
}
@Override
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException {
try {
XmlObject object = XmlObject.Factory.parse(streamReader, getXmlOptions());
validate(object); validate(object);
return object; return object;
} }
@ -195,30 +207,18 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
} }
@Override @Override
protected final Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
try {
Reader nonClosingReader = new NonClosingReader(reader);
XmlObject object = XmlObject.Factory.parse(nonClosingReader, getXmlOptions());
validate(object);
return object;
}
catch (XmlException ex) {
throw convertXmlBeansException(ex, false);
}
}
@Override
protected final Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
throws XmlMappingException, IOException { throws XmlMappingException, IOException {
XmlSaxHandler saxHandler = XmlObject.Factory.newXmlSaxHandler(getXmlOptions()); XmlSaxHandler saxHandler = XmlObject.Factory.newXmlSaxHandler(getXmlOptions());
xmlReader.setContentHandler(saxHandler.getContentHandler()); xmlReader.setContentHandler(saxHandler.getContentHandler());
try { try {
xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", saxHandler.getLexicalHandler()); xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", saxHandler.getLexicalHandler());
} }
catch (SAXNotRecognizedException e) { catch (SAXNotRecognizedException ex) {
// ignore // ignore
} }
catch (SAXNotSupportedException e) { catch (SAXNotSupportedException ex) {
// ignore // ignore
} }
try { try {
@ -236,20 +236,23 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
} }
@Override @Override
protected final Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException { protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
XMLReader reader = StaxUtils.createXMLReader(eventReader);
try { try {
return unmarshalSaxReader(reader, new InputSource()); InputStream nonClosingInputStream = new NonClosingInputStream(inputStream);
XmlObject object = XmlObject.Factory.parse(nonClosingInputStream, getXmlOptions());
validate(object);
return object;
} }
catch (IOException ex) { catch (XmlException ex) {
throw convertXmlBeansException(ex, false); throw convertXmlBeansException(ex, false);
} }
} }
@Override @Override
protected final Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException { protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException {
try { try {
XmlObject object = XmlObject.Factory.parse(streamReader, getXmlOptions()); Reader nonClosingReader = new NonClosingReader(reader);
XmlObject object = XmlObject.Factory.parse(nonClosingReader, getXmlOptions());
validate(object); validate(object);
return object; return object;
} }
@ -312,6 +315,7 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
} }
} }
/** /**
* See SPR-7034 * See SPR-7034
*/ */
@ -388,6 +392,7 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
} }
} }
private static class NonClosingReader extends Reader { private static class NonClosingReader extends Reader {
private final WeakReference<Reader> reader; private final WeakReference<Reader> reader;