diff --git a/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java index ab78c0bd22..7e8ecfc42e 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java @@ -97,7 +97,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing private String encoding = DEFAULT_ENCODING; - private Class[] targetClasses; + private Class[] targetClasses; private String[] targetPackages; @@ -172,7 +172,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing /** * Set the locations of the Castor XML Mapping files. */ - public void setMappingLocations(Resource[] mappingLocations) { + public void setMappingLocations(Resource... 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 * multiple classes include use of mapping files, and specifying packages with Castor descriptor classes. */ - public void setTargetClass(Class targetClass) { - this.targetClasses = new Class[]{targetClass}; + public void setTargetClass(Class targetClass) { + this.targetClasses = new Class[] {targetClass}; } /** * 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. */ - public void setTargetClasses(Class[] targetClasses) { + public void setTargetClasses(Class... 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. */ - public void setTargetPackages(String[] targetPackages) { + public void setTargetPackages(String... 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#addClass(Class) */ - protected XMLContext createXMLContext(Resource[] mappingLocations, Class[] targetClasses, String[] targetPackages) - throws MappingException, ResolverException, IOException { + protected XMLContext createXMLContext(Resource[] mappingLocations, Class[] targetClasses, + String[] targetPackages) throws MappingException, ResolverException, IOException { XMLContext context = new XMLContext(); if (!ObjectUtils.isEmpty(mappingLocations)) { @@ -492,47 +492,46 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing return true; } + // Marshalling @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); } @Override - protected final void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) - 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 { + protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException { marshalSaxHandlers(graph, StaxUtils.createContentHandler(eventWriter), null); } @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); } - 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 { customizeMarshaller(marshaller); marshaller.marshal(graph); @@ -572,10 +571,11 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } } + // Unmarshalling @Override - protected final Object unmarshalDomNode(Node node) throws XmlMappingException { + protected Object unmarshalDomNode(Node node) throws XmlMappingException { try { return createUnmarshaller().unmarshal(node); } @@ -585,9 +585,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } @Override - protected final Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { + protected Object unmarshalXmlEventReader(XMLEventReader eventReader) { try { - return createUnmarshaller().unmarshal(new InputSource(inputStream)); + return createUnmarshaller().unmarshal(eventReader); } catch (XMLException ex) { throw convertCastorException(ex, false); @@ -595,9 +595,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } @Override - protected final Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { + protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) { try { - return createUnmarshaller().unmarshal(new InputSource(reader)); + return createUnmarshaller().unmarshal(streamReader); } catch (XMLException ex) { throw convertCastorException(ex, false); @@ -605,7 +605,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } @Override - protected final Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) + protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException { UnmarshalHandler unmarshalHandler = createUnmarshaller().createHandler(); @@ -621,9 +621,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } @Override - protected final Object unmarshalXmlEventReader(XMLEventReader eventReader) { + protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { try { - return createUnmarshaller().unmarshal(eventReader); + return createUnmarshaller().unmarshal(new InputSource(inputStream)); } catch (XMLException ex) { throw convertCastorException(ex, false); @@ -631,9 +631,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } @Override - protected final Object unmarshalXmlStreamReader(XMLStreamReader streamReader) { + protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { try { - return createUnmarshaller().unmarshal(streamReader); + return createUnmarshaller().unmarshal(new InputSource(reader)); } catch (XMLException ex) { 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 * {@code org.springframework.oxm} hierarchy. diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java index 9e513f4359..d182b0938f 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java @@ -297,7 +297,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi * Specify the {@code XmlAdapter}s to be registered with the JAXB {@code Marshaller} * and {@code Unmarshaller} */ - public void setAdapters(XmlAdapter[] adapters) { + public void setAdapters(XmlAdapter... adapters) { this.adapters = adapters; } @@ -311,7 +311,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi /** * Set the schema resources to use for validation. */ - public void setSchemas(Resource[] schemaResources) { + public void setSchemas(Resource... schemaResources) { this.schemaResources = schemaResources; } diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java index b374b52f09..494ac097f9 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java @@ -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"); * 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 Class targetClass; 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 * {@link #setTargetPackage(String) targetPackage} property is required. - * *

If this property is set, {@link #setTargetPackage(String) targetPackage} is ignored. */ 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 * {@link #setTargetClass(Class) targetClass} property is required. - * *

If {@link #setTargetClass(Class) targetClass} is set, this property is ignored. */ 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 - * {@code null} (i.e. no DTD declaration is written). If set to a value, the system ID or public ID also need to - * be set. - * + * Set the root element name for the DTD declaration written when marshalling. + * By default, this is {@code null} (i.e. no DTD declaration is written). + *

If set to a value, the system ID or public ID also need to be set. * @see #setDocTypeSystemId(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 - * {@code null}. Only used when the root element also has been set. Set either this property or - * {@code docTypePublicId}, not both. - * + * Set the system Id for the DTD declaration written when marshalling. + * By default, this is {@code null}. Only used when the root element also has been set. + *

Set either this property or {@code docTypePublicId}, not both. * @see #setDocTypeRootElementName(String) */ 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 - * {@code null}. Only used when the root element also has been set. Set either this property or - * {@code docTypeSystemId}, not both. - * + * Set the public Id for the DTD declaration written when marshalling. + * By default, this is {@code null}. Only used when the root element also has been set. + *

Set either this property or {@code docTypeSystemId}, not both. * @see #setDocTypeRootElementName(String) */ 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 - * {@code null}. Only used when the root element also has been set. - * + * Set the internal subset Id for the DTD declaration written when marshalling. + * By default, this is {@code null}. Only used when the root element also has been set. * @see #setDocTypeRootElementName(String) */ public void setDocTypeInternalSubset(String docTypeInternalSubset) { this.docTypeInternalSubset = docTypeInternalSubset; } + @Override public void afterPropertiesSet() throws JiBXException { if (this.targetClass != null) { @@ -215,7 +211,8 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe } this.bindingFactory = BindingDirectory.getFactory(this.targetClass); } - } else if (this.targetPackage != null) { + } + else if (this.targetPackage != null) { if (!StringUtils.hasLength(bindingName)) { 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 + "]"); } this.bindingFactory = BindingDirectory.getFactory(bindingName, targetPackage); - } else { + } + else { 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 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, - JiBXException { + private void marshalDocument(IMarshallingContext marshallingContext, Object graph) throws IOException, JiBXException { if (StringUtils.hasLength(docTypeRootElementName)) { IXMLWriter xmlWriter = marshallingContext.getXmlWriter(); xmlWriter.writeDocType(docTypeRootElementName, docTypeSystemId, docTypePublicId, docTypeInternalSubset); @@ -282,6 +279,27 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe 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 protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { 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 protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) 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 + @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 protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { 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 diff --git a/spring-oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java index d42cb72a49..a118775137 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java @@ -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"); * 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 * @throws SAXException if thrown by JAXP methods */ @@ -184,7 +184,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { /** * Template method for handling {@code DOMResult}s. *

This implementation delegates to {@code marshalDomNode}. - * @param graph the root of the object graph to marshal + * @param graph the root of the object graph to marshal * @param domResult the {@code DOMResult} * @throws XmlMappingException if the given object cannot be marshalled to the result * @throws IllegalArgumentException if the {@code domResult} is empty @@ -214,8 +214,8 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { *

This implementation delegates to {@code marshalXMLSteamWriter} or * {@code marshalXMLEventConsumer}, depending on what is contained in the * {@code StaxResult}. - * @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 graph the root of the object graph to marshal + * @param staxResult a JAXP 1.4 {@link StAXSource} * @throws XmlMappingException if the given object cannot be marshalled to the result * @throws IllegalArgumentException if the {@code domResult} is empty * @see #marshalDomNode(Object, org.w3c.dom.Node) @@ -239,7 +239,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { /** * Template method for handling {@code SAXResult}s. *

This implementation delegates to {@code marshalSaxHandlers}. - * @param graph the root of the object graph to marshal + * @param graph the root of the object graph to marshal * @param saxResult the {@code SAXResult} * @throws XmlMappingException if the given object cannot be marshalled to the result * @see #marshalSaxHandlers(Object, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler) @@ -396,7 +396,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { /** * Abstract template method for marshalling the given object to a StAX {@code XMLEventWriter}. - * @param graph the root of the object graph to marshal + * @param graph the root of the object graph to marshal * @param eventWriter the {@code XMLEventWriter} to write to * @throws XmlMappingException if the given object cannot be marshalled to the DOM node */ @@ -412,16 +412,6 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { protected abstract void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) 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}. * @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) 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}. * @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) throws XmlMappingException, IOException; + /** * Abstract template method for unmarshalling from a given DOM {@code Node}. * @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) 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}. * @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) 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; - } diff --git a/spring-oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java index 83a4639363..780f5efa83 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java @@ -122,8 +122,9 @@ public class XmlBeansMarshaller extends AbstractMarshaller { return XmlObject.class.isAssignableFrom(clazz); } + @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(); Node xmlBeansNode = ((XmlObject) graph).newDomNode(getXmlOptions()); NodeList xmlBeansChildNodes = xmlBeansNode.getChildNodes(); @@ -135,14 +136,19 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } @Override - protected final void marshalOutputStream(Object graph, OutputStream outputStream) - throws XmlMappingException, IOException { - - ((XmlObject) graph).save(outputStream, getXmlOptions()); + protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) { + ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter); + marshalSaxHandlers(graph, contentHandler, null); } @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 { try { ((XmlObject) graph).save(contentHandler, lexicalHandler, getXmlOptions()); @@ -153,24 +159,20 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } @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()); } - @Override - protected final void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) { - ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter); - marshalSaxHandlers(graph, contentHandler, null); - } @Override - protected final void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { - ContentHandler contentHandler = StaxUtils.createContentHandler(streamWriter); - marshalSaxHandlers(graph, contentHandler, null); - } - - @Override - protected final Object unmarshalDomNode(Node node) throws XmlMappingException { + protected Object unmarshalDomNode(Node node) throws XmlMappingException { try { XmlObject object = XmlObject.Factory.parse(node, getXmlOptions()); validate(object); @@ -182,10 +184,20 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } @Override - protected final Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { + protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException { + XMLReader reader = StaxUtils.createXMLReader(eventReader); try { - InputStream nonClosingInputStream = new NonClosingInputStream(inputStream); - XmlObject object = XmlObject.Factory.parse(nonClosingInputStream, getXmlOptions()); + return unmarshalSaxReader(reader, new InputSource()); + } + 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); return object; } @@ -195,30 +207,18 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } @Override - protected final Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { - 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) + protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException { + XmlSaxHandler saxHandler = XmlObject.Factory.newXmlSaxHandler(getXmlOptions()); xmlReader.setContentHandler(saxHandler.getContentHandler()); try { xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", saxHandler.getLexicalHandler()); } - catch (SAXNotRecognizedException e) { + catch (SAXNotRecognizedException ex) { // ignore } - catch (SAXNotSupportedException e) { + catch (SAXNotSupportedException ex) { // ignore } try { @@ -236,20 +236,23 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } @Override - protected final Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException { - XMLReader reader = StaxUtils.createXMLReader(eventReader); + protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { 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); } } @Override - protected final Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException { + protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { try { - XmlObject object = XmlObject.Factory.parse(streamReader, getXmlOptions()); + Reader nonClosingReader = new NonClosingReader(reader); + XmlObject object = XmlObject.Factory.parse(nonClosingReader, getXmlOptions()); validate(object); return object; } @@ -312,6 +315,7 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } } + /** * See SPR-7034 */ @@ -388,6 +392,7 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } } + private static class NonClosingReader extends Reader { private final WeakReference reader;