From 3d6dcb7a51dbaa4f6e2f16f18d9c921a3f81205b Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 31 May 2007 12:19:19 +0000 Subject: [PATCH] Fixed SWS-133: CastorMarshaller ignoreExtraAttributes & Elements --- oxm/build-maven2.xml | 2 +- oxm/pom.xml | 18 +- .../oxm/AbstractMarshaller.java | 43 +-- .../oxm/castor/CastorMarshaller.java | 337 +++++++++++------- .../oxm/xmlbeans/XmlBeansMarshaller.java | 115 +++--- pom.xml | 5 +- src/changes/changes.xml | 19 +- src/site/site.xml | 5 +- 8 files changed, 309 insertions(+), 235 deletions(-) diff --git a/oxm/build-maven2.xml b/oxm/build-maven2.xml index c275f8f9..c5a8a1fc 100644 --- a/oxm/build-maven2.xml +++ b/oxm/build-maven2.xml @@ -32,7 +32,7 @@ - diff --git a/oxm/pom.xml b/oxm/pom.xml index bd52ec3b..23f0b5ed 100644 --- a/oxm/pom.xml +++ b/oxm/pom.xml @@ -1,5 +1,6 @@ - + spring-ws org.springframework.ws @@ -29,8 +30,8 @@ - - + + ${project.build.directory}/generated-sources/test/java @@ -45,8 +46,8 @@ - - + + @@ -55,6 +56,13 @@ + + + org.codehaus.castor + castor-codegen-anttask + 1.1 + + diff --git a/oxm/src/main/java/org/springframework/oxm/AbstractMarshaller.java b/oxm/src/main/java/org/springframework/oxm/AbstractMarshaller.java index 5fc589a3..b719a5c2 100644 --- a/oxm/src/main/java/org/springframework/oxm/AbstractMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/AbstractMarshaller.java @@ -59,38 +59,11 @@ import org.xml.sax.helpers.XMLReaderFactory; */ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { - /** - * Logger available to subclasses. - */ + /** Logger available to subclasses. */ protected final Log logger = LogFactory.getLog(getClass()); - private boolean validating = false; - - private boolean namespaceAware = true; - private DocumentBuilderFactory documentBuilderFactory; - /** - * Set whether or not the XML parser should be XML namespace aware. Default is true. - */ - public void setNamespaceAware(boolean namespaceAware) { - this.namespaceAware = namespaceAware; - } - - /** - * Set if the XML parser should validate the document. Default is false. - */ - public void setValidating(boolean validating) { - this.validating = validating; - } - - /** - * Returns whether the XML parser should validate the document. Default is false. - */ - public boolean isValidating() { - return validating; - } - /** * Marshals the object graph with the given root into the provided javax.xml.transform.Result. *

@@ -103,9 +76,9 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { * @throws IOException if an I/O exception occurs * @throws IllegalArgumentException if result if neither a DOMResult, * SAXResult, StreamResult - * @see #marshalDomResult(Object, javax.xml.transform.dom.DOMResult) - * @see #marshalSaxResult(Object, javax.xml.transform.sax.SAXResult) - * @see #marshalStreamResult(Object, javax.xml.transform.stream.StreamResult) + * @see #marshalDomResult(Object,javax.xml.transform.dom.DOMResult) + * @see #marshalSaxResult(Object,javax.xml.transform.sax.SAXResult) + * @see #marshalStreamResult(Object,javax.xml.transform.stream.StreamResult) */ public final void marshal(Object graph, Result result) throws XmlMappingException, IOException { if (result instanceof DOMResult) { @@ -183,8 +156,8 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { */ protected DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setValidating(validating); - factory.setNamespaceAware(namespaceAware); + factory.setValidating(false); + factory.setNamespaceAware(true); return factory; } @@ -246,7 +219,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { * @param graph the root of the object graph to marshal * @param saxResult the 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) + * @see #marshalSaxHandlers(Object,org.xml.sax.ContentHandler,org.xml.sax.ext.LexicalHandler) */ protected void marshalSaxResult(Object graph, SAXResult saxResult) throws XmlMappingException { ContentHandler contentHandler = saxResult.getHandler(); @@ -340,7 +313,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { * @return the object graph * @throws XmlMappingException if the given source cannot be mapped to an object * @throws IOException if an I/O Exception occurs - * @see #unmarshalSaxReader(org.xml.sax.XMLReader, org.xml.sax.InputSource) + * @see #unmarshalSaxReader(org.xml.sax.XMLReader,org.xml.sax.InputSource) */ protected Object unmarshalSaxSource(SAXSource saxSource) throws XmlMappingException, IOException { if (saxSource.getXMLReader() == null) { diff --git a/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java b/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java index d6f20ce5..a6f8f5dd 100644 --- a/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java @@ -91,22 +91,65 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing private boolean whitespacePreserve = false; + private boolean ignoreExtraAttributes = true; + + private boolean ignoreExtraElements = false; + /** - * Set if the Castor Unmarshaller should validate the incoming document. Default is - * false. + * Returns whether the Castor {@link Unmarshaller} should ignore attributes that do not match + * a specific field. */ - public void setValidating(boolean validating) { - this.validating = validating; + public boolean getIgnoreExtraAttributes() { + return ignoreExtraAttributes; } /** - * Indicates whether the Castor Unmarshaller should preserve "ignorable" whitespace. Default is + * Sets whether the Castor {@link Unmarshaller} should ignore attributes that do not match + * a specific field. Default is true: extra attributes are ignored. + */ + public void setIgnoreExtraAttributes(boolean ignoreExtraAttributes) { + this.ignoreExtraAttributes = ignoreExtraAttributes; + } + + /** + * Returns whether the Castor {@link Unmarshaller} should ignore elements that do not match + * a specific field. + */ + public boolean getIgnoreExtraElements() { + return ignoreExtraElements; + } + + /** + * Sets whether the Castor {@link Unmarshaller} should ignore elements that do not match + * a specific field. Default is false, extra attributes are flagged as an error. + */ + public void setIgnoreExtraElements(boolean ignoreExtraElements) { + this.ignoreExtraElements = ignoreExtraElements; + } + + /** Returns whether the Castor {@link Unmarshaller} should preserve "ignorable" whitespace. */ + public boolean getWhitespacePreserve() { + return whitespacePreserve; + } + + /** + * Sets whether the Castor {@link Unmarshaller} should preserve "ignorable" whitespace. Default is * false. */ public void setWhitespacePreserve(boolean whitespacePreserve) { this.whitespacePreserve = whitespacePreserve; } + /** Returns whether this marshaller should validate in- and outgoing documents. */ + public boolean isValidating() { + return validating; + } + + /** Sets whether this marshaller should validate in- and outgoing documents. Default is false. */ + public void setValidating(boolean validating) { + this.validating = validating; + } + /** * Sets the encoding to be used for stream access. If this property is not set, the default encoding is used. * @@ -116,16 +159,6 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing this.encoding = encoding; } - /** - * Sets the Castor target class. If this property is set, this CastorMarshaller is tied to this one - * specific class. Use a mapping file for unmarshalling multiple classes. - *

- * You cannot set both this property and the mapping (location). - */ - public void setTargetClass(Class targetClass) { - this.targetClass = targetClass; - } - /** Sets the locations of the Castor XML Mapping files. */ public void setMappingLocation(Resource mappingLocation) { mappingLocations = new Resource[]{mappingLocation}; @@ -136,6 +169,16 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing this.mappingLocations = mappingLocations; } + /** + * Sets the Castor target class. If this property is set, this CastorMarshaller is tied to this one + * specific class. Use a mapping file for unmarshalling multiple classes. + *

+ * You cannot set both this property and the mapping (location). + */ + public void setTargetClass(Class targetClass) { + this.targetClass = targetClass; + } + public final void afterPropertiesSet() throws IOException { if (mappingLocations != null && targetClass != null) { throw new IllegalArgumentException("Cannot set both the 'mappingLocations' and 'targetClass' property. " + @@ -160,10 +203,103 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } } + /** Returns true for all classes, i.e. Castor supports arbitrary classes. */ public boolean supports(Class clazz) { return true; } + protected final void marshalDomNode(Object graph, Node node) throws XmlMappingException { + marshalSaxHandlers(graph, new DomContentHandler(node), null); + } + + protected final void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) + throws XmlMappingException { + try { + marshal(graph, new Marshaller(contentHandler)); + } + catch (IOException ex) { + throw new CastorSystemException("Could not construct Castor ContentHandler Marshaller", ex); + } + } + + protected final void marshalOutputStream(Object graph, OutputStream outputStream) + throws XmlMappingException, IOException { + marshalWriter(graph, new OutputStreamWriter(outputStream, encoding)); + } + + protected final void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException { + marshal(graph, new Marshaller(writer)); + } + + protected final void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException { + marshalSaxHandlers(graph, new StaxEventContentHandler(eventWriter), null); + } + + protected final void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { + marshalSaxHandlers(graph, new StaxStreamContentHandler(streamWriter), null); + } + + protected final Object unmarshalDomNode(Node node) throws XmlMappingException { + try { + return createUnmarshaller().unmarshal(node); + } + catch (XMLException ex) { + throw convertCastorException(ex, false); + } + } + + protected final Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { + try { + return createUnmarshaller().unmarshal(new InputSource(inputStream)); + } + catch (XMLException ex) { + throw convertCastorException(ex, false); + } + } + + protected final Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { + try { + return createUnmarshaller().unmarshal(new InputSource(reader)); + } + catch (XMLException ex) { + throw convertCastorException(ex, false); + } + } + + protected final Object unmarshalXmlEventReader(XMLEventReader eventReader) { + XMLReader reader = new StaxEventXmlReader(eventReader); + try { + return unmarshalSaxReader(reader, new InputSource()); + } + catch (IOException ex) { + throw new CastorUnmarshallingFailureException(new MarshalException(ex)); + } + } + + protected final Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) + throws XmlMappingException, IOException { + UnmarshalHandler unmarshalHandler = createUnmarshaller().createHandler(); + try { + ContentHandler contentHandler = Unmarshaller.getContentHandler(unmarshalHandler); + xmlReader.setContentHandler(contentHandler); + xmlReader.parse(inputSource); + return unmarshalHandler.getObject(); + } + catch (SAXException ex) { + throw new CastorUnmarshallingFailureException(ex); + } + } + + protected final Object unmarshalXmlStreamReader(XMLStreamReader streamReader) { + XMLReader reader = new StaxStreamXmlReader(streamReader); + try { + return unmarshalSaxReader(reader, new InputSource()); + } + catch (IOException ex) { + throw new CastorUnmarshallingFailureException(new MarshalException(ex)); + } + } + /** * Creates the Castor XMLClassDescriptorResolver. Subclasses can override this to create a custom * resolver. @@ -194,6 +330,55 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing return classDescriptorResolver; } + private Unmarshaller createUnmarshaller() { + Unmarshaller unmarshaller = null; + if (targetClass != null) { + unmarshaller = new Unmarshaller(targetClass); + } + else { + unmarshaller = new Unmarshaller(); + } + unmarshaller.setResolver(classDescriptorResolver); + customizeUnmarshaller(unmarshaller); + return unmarshaller; + } + + /** + * Template method that allows for customizing of the given Castor {@link Unmarshaller}. + *

+ * Default implementation invokes {@link Unmarshaller#setValidation(boolean)}, + * {@link Unmarshaller#setWhitespacePreserve(boolean)}, + * {@link Unmarshaller#setIgnoreExtraAttributes(boolean)}, and {@link Unmarshaller#setIgnoreExtraElements(boolean)} + * with the properties set on this marshaller. + */ + protected void customizeUnmarshaller(Unmarshaller unmarshaller) { + unmarshaller.setValidation(isValidating()); + unmarshaller.setWhitespacePreserve(getWhitespacePreserve()); + unmarshaller.setIgnoreExtraAttributes(getIgnoreExtraAttributes()); + unmarshaller.setIgnoreExtraElements(getIgnoreExtraElements()); + } + + private void marshal(Object graph, Marshaller marshaller) { + try { + marshaller.setResolver(classDescriptorResolver); + customizeMarshaller(marshaller); + marshaller.marshal(graph); + } + catch (XMLException ex) { + throw convertCastorException(ex, true); + } + } + + /** + * Template method that allows for customizing of the given Castor {@link Marshaller}. + *

+ * Default implementation invokes {@link Marshaller#setValidation(boolean)} with the property set on this + * marshaller. + */ + protected void customizeMarshaller(Marshaller marshaller) { + marshaller.setValidation(isValidating()); + } + /** * Converts the given CastorException to an appropriate exception from the * org.springframework.oxm hierarchy. @@ -213,125 +398,5 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing return CastorUtils.convertXmlException(ex, marshalling); } - private Unmarshaller createUnmarshaller() { - Unmarshaller unmarshaller = null; - if (targetClass != null) { - unmarshaller = new Unmarshaller(targetClass); - } - else { - unmarshaller = new Unmarshaller(); - } - unmarshaller.setResolver(classDescriptorResolver); - unmarshaller.setValidation(validating); - unmarshaller.setWhitespacePreserve(whitespacePreserve); - return unmarshaller; - } - - private void marshal(Object graph, Marshaller marshaller) { - try { - marshaller.setResolver(classDescriptorResolver); - marshaller.marshal(graph); - } - catch (XMLException ex) { - throw convertCastorException(ex, true); - } - } - - protected void marshalDomNode(Object graph, Node node) throws XmlMappingException { - ContentHandler contentHandler = new DomContentHandler(node); - marshalSaxHandlers(graph, contentHandler, null); - } - - protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) throws XmlMappingException { - ContentHandler contentHandler = new StaxEventContentHandler(eventWriter); - marshalSaxHandlers(graph, contentHandler, null); - } - - protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { - ContentHandler contentHandler = new StaxStreamContentHandler(streamWriter); - marshalSaxHandlers(graph, contentHandler, null); - } - - protected void marshalOutputStream(Object graph, OutputStream outputStream) - throws XmlMappingException, IOException { - OutputStreamWriter writer = new OutputStreamWriter(outputStream, encoding); - marshalWriter(graph, writer); - } - - protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) - throws XmlMappingException { - try { - Marshaller marshaller = new Marshaller(contentHandler); - marshal(graph, marshaller); - } - catch (IOException ex) { - throw new CastorSystemException("Could not construct Castor ContentHandler Marshaller", ex); - } - } - - protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException { - Marshaller marshaller = new Marshaller(writer); - marshal(graph, marshaller); - } - - protected Object unmarshalDomNode(Node node) throws XmlMappingException { - try { - return createUnmarshaller().unmarshal(node); - } - catch (XMLException ex) { - throw convertCastorException(ex, false); - } - } - - protected Object unmarshalXmlEventReader(XMLEventReader eventReader) { - XMLReader reader = new StaxEventXmlReader(eventReader); - try { - return unmarshalSaxReader(reader, new InputSource()); - } - catch (IOException ex) { - throw new CastorUnmarshallingFailureException(new MarshalException(ex)); - } - } - - protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) { - XMLReader reader = new StaxStreamXmlReader(streamReader); - try { - return unmarshalSaxReader(reader, new InputSource()); - } - catch (IOException ex) { - throw new CastorUnmarshallingFailureException(new MarshalException(ex)); - } - } - - protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) - throws XmlMappingException, IOException { - UnmarshalHandler unmarshalHandler = createUnmarshaller().createHandler(); - try { - ContentHandler contentHandler = Unmarshaller.getContentHandler(unmarshalHandler); - xmlReader.setContentHandler(contentHandler); - xmlReader.parse(inputSource); - return unmarshalHandler.getObject(); - } - catch (SAXException ex) { - throw new CastorUnmarshallingFailureException(ex); - } - } - - protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { - try { - return createUnmarshaller().unmarshal(new InputSource(inputStream)); - } - catch (XMLException ex) { - throw convertCastorException(ex, false); - } - } - - protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { - try { - return createUnmarshaller().unmarshal(new InputSource(reader)); - } - catch (XMLException ex) { - throw convertCastorException(ex, false); - } - } +// abstract } diff --git a/oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java b/oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java index f193445b..77b23981 100644 --- a/oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java @@ -20,6 +20,7 @@ import org.apache.xmlbeans.XmlOptions; import org.apache.xmlbeans.XmlSaxHandler; import org.apache.xmlbeans.XmlValidationError; import org.springframework.oxm.AbstractMarshaller; +import org.springframework.oxm.Marshaller; import org.springframework.oxm.XmlMappingException; import org.springframework.xml.stream.StaxEventContentHandler; import org.springframework.xml.stream.StaxEventXmlReader; @@ -36,61 +37,60 @@ import org.xml.sax.XMLReader; import org.xml.sax.ext.LexicalHandler; /** - * Implementation of the Marshaller interface for XMLBeans. Further options can be set by setting the - * xmlOptions property. A XmlOptionsFactoryBean is provided to easily wire up - * XmlObjects instances. + * Implementation of the {@link Marshaller} interface for XMLBeans. Further options can be set by setting the + * xmlOptions property. The {@link XmlOptionsFactoryBean} is provided to easily wire up + * {@link XmlOptions} instances. *

* Unmarshalled objects can be validated by setting the validating property, or by calling the - * validiate() method directly. Invalid objects will result in an XmlBeansValidationFailureException. + * {@link #validate(XmlObject)} method directly. Invalid objects will result in an + * {@link XmlBeansValidationFailureException}. *

* Note that due to the nature of XMLBeans, this marshaller requires all passed objects to be of type - * XmlObject. + * {@link XmlObject}. * * @author Arjen Poutsma * @see #setXmlOptions(org.apache.xmlbeans.XmlOptions) * @see XmlOptionsFactoryBean * @see #setValidating(boolean) - * @see org.apache.xmlbeans.XmlObject */ public class XmlBeansMarshaller extends AbstractMarshaller { private XmlOptions xmlOptions; + private boolean validating = false; + + /** Returns the XmlOptions. */ + public XmlOptions getXmlOptions() { + return xmlOptions; + } + /** * Sets the XmlOptions. * - * @param xmlOptions the xml options + * @see XmlOptionsFactoryBean */ public void setXmlOptions(XmlOptions xmlOptions) { this.xmlOptions = xmlOptions; } + /** Returns whether this marshaller should validate in- and outgoing documents. */ + public boolean isValidating() { + return validating; + } + + /** Sets whether this marshaller should validate in- and outgoing documents. Default is false. */ + public void setValidating(boolean validating) { + this.validating = validating; + } + + /** Returns true if the given class is an implementation of {@link XmlObject}. */ public boolean supports(Class clazz) { return XmlObject.class.isAssignableFrom(clazz); } - /** - * Converts the given XMLBeans exception to an appropriate exception from the org.springframework.oxm - * hierarchy. - *

- * The default implementation delegates to XmlBeansUtils. Can be overridden in subclasses. - *

- * A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since - * XMLBeans itself does not make this distinction in its exception hierarchy. - * - * @param ex XMLBeans Exception that occured - * @param marshalling indicates whether the exception occurs during marshalling (true), or - * unmarshalling (false) - * @return the corresponding XmlMappingException - * @see XmlBeansUtils#convertXmlBeansException(Exception,boolean) - */ - public XmlMappingException convertXmlBeansException(Exception ex, boolean marshalling) { - return XmlBeansUtils.convertXmlBeansException(ex, marshalling); - } - - protected void marshalDomNode(Object graph, Node node) throws XmlMappingException { + protected final void marshalDomNode(Object graph, Node node) throws XmlMappingException { Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node : node.getOwnerDocument(); - Node xmlBeansNode = ((XmlObject) graph).newDomNode(xmlOptions); + Node xmlBeansNode = ((XmlObject) graph).newDomNode(getXmlOptions()); NodeList xmlBeansChildNodes = xmlBeansNode.getChildNodes(); for (int i = 0; i < xmlBeansChildNodes.getLength(); i++) { Node xmlBeansChildNode = xmlBeansChildNodes.item(i); @@ -99,38 +99,38 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } } - protected void marshalOutputStream(Object graph, OutputStream outputStream) + protected final void marshalOutputStream(Object graph, OutputStream outputStream) throws XmlMappingException, IOException { - ((XmlObject) graph).save(outputStream, xmlOptions); + ((XmlObject) graph).save(outputStream, getXmlOptions()); } - protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) + protected final void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XmlMappingException { try { - ((XmlObject) graph).save(contentHandler, lexicalHandler, xmlOptions); + ((XmlObject) graph).save(contentHandler, lexicalHandler, getXmlOptions()); } catch (SAXException ex) { throw convertXmlBeansException(ex, true); } } - protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException { - ((XmlObject) graph).save(writer, xmlOptions); + protected final void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException { + ((XmlObject) graph).save(writer, getXmlOptions()); } - protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) { + protected final void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) { ContentHandler contentHandler = new StaxEventContentHandler(eventWriter); marshalSaxHandlers(graph, contentHandler, null); } - protected void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { + protected final void marshalXmlStreamWriter(Object graph, XMLStreamWriter streamWriter) throws XmlMappingException { ContentHandler contentHandler = new StaxStreamContentHandler(streamWriter); marshalSaxHandlers(graph, contentHandler, null); } - protected Object unmarshalDomNode(Node node) throws XmlMappingException { + protected final Object unmarshalDomNode(Node node) throws XmlMappingException { try { - XmlObject object = XmlObject.Factory.parse(node, xmlOptions); + XmlObject object = XmlObject.Factory.parse(node, getXmlOptions()); validate(object); return object; } @@ -139,9 +139,9 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } } - protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { + protected final Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException { try { - XmlObject object = XmlObject.Factory.parse(inputStream, xmlOptions); + XmlObject object = XmlObject.Factory.parse(inputStream, getXmlOptions()); validate(object); return object; } @@ -150,9 +150,9 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } } - protected Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { + protected final Object unmarshalReader(Reader reader) throws XmlMappingException, IOException { try { - XmlObject object = XmlObject.Factory.parse(reader, xmlOptions); + XmlObject object = XmlObject.Factory.parse(reader, getXmlOptions()); validate(object); return object; } @@ -161,9 +161,9 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } } - protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) + protected final Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) throws XmlMappingException, IOException { - XmlSaxHandler saxHandler = XmlObject.Factory.newXmlSaxHandler(xmlOptions); + XmlSaxHandler saxHandler = XmlObject.Factory.newXmlSaxHandler(getXmlOptions()); xmlReader.setContentHandler(saxHandler.getContentHandler()); try { xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", saxHandler.getLexicalHandler()); @@ -188,7 +188,7 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } } - protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException { + protected final Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException { XMLReader reader = new StaxEventXmlReader(eventReader); try { return unmarshalSaxReader(reader, new InputSource()); @@ -198,9 +198,9 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } } - protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException { + protected final Object unmarshalXmlStreamReader(XMLStreamReader streamReader) throws XmlMappingException { try { - XmlObject object = XmlObject.Factory.parse(streamReader, xmlOptions); + XmlObject object = XmlObject.Factory.parse(streamReader, getXmlOptions()); validate(object); return object; } @@ -209,6 +209,25 @@ public class XmlBeansMarshaller extends AbstractMarshaller { } } + /** + * Converts the given XMLBeans exception to an appropriate exception from the org.springframework.oxm + * hierarchy. + *

+ * The default implementation delegates to XmlBeansUtils. Can be overridden in subclasses. + *

+ * A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since + * XMLBeans itself does not make this distinction in its exception hierarchy. + * + * @param ex XMLBeans Exception that occured + * @param marshalling indicates whether the exception occurs during marshalling (true), or + * unmarshalling (false) + * @return the corresponding XmlMappingException + * @see XmlBeansUtils#convertXmlBeansException(Exception,boolean) + */ + public XmlMappingException convertXmlBeansException(Exception ex, boolean marshalling) { + return XmlBeansUtils.convertXmlBeansException(ex, marshalling); + } + /** * Validates the given XmlObject. * @@ -219,7 +238,7 @@ public class XmlBeansMarshaller extends AbstractMarshaller { public void validate(XmlObject object) throws XmlBeansValidationFailureException { if (isValidating() && object != null) { // create a temporary xmlOptions just for validation - XmlOptions validateOptions = xmlOptions != null ? xmlOptions : new XmlOptions(); + XmlOptions validateOptions = getXmlOptions() != null ? getXmlOptions() : new XmlOptions(); List errorsList = new ArrayList(); validateOptions.setErrorListener(errorsList); if (!object.validate(validateOptions)) { diff --git a/pom.xml b/pom.xml index 03e910c5..7775aa97 100644 --- a/pom.xml +++ b/pom.xml @@ -505,11 +505,12 @@ xpp3 1.1.3.4.O - + + org.codehaus.castor castor - 1.0.5 + 1.1.1 diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 82f71708..79732d97 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -6,13 +6,20 @@ + Added ignoreExtraAttributes and ignoreExtraElements + properties to CastorMarshaller + Moved SimpleMethodEndpointMapping from sandbox - Moved PayloadMethodEndpointAdapter and MessageMethodEndpointAdapter from sandbox - CommonsHttpMessageSender should expose setters for username and password - A MessageSender setter on WebServicesGatewaySupport - A SOAP:Fault does not set the the response code to 500 + Moved PayloadMethodEndpointAdapter and MessageMethodEndpointAdapter from + sandbox + + CommonsHttpMessageSender should expose setters for username + and password + + A MessageSender setter on WebServicesGatewaySupport + + A SOAP:Fault does not set the the response code to 500 + Changing URL in CommonsHttpMessageSender diff --git a/src/site/site.xml b/src/site/site.xml index 252c0a7e..8dfd9fa2 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -7,13 +7,14 @@ + Spring Web Services images/spring-ws.png http://www.springframework.org/spring-ws - - + +