From 5a4ccca5f9a27cb931746ea3d5b3c065056e8b86 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 3 Apr 2007 16:58:48 +0000 Subject: [PATCH] Marshaller-supports --- changelog.txt | 3 ++ .../oxm/jaxb/Jaxb2Marshaller.java | 34 +++++++++---------- .../oxm/jaxb/Jaxb2MarshallerTest.java | 16 +++++---- .../org/springframework/oxm/Marshaller.java | 26 +++++++++----- .../org/springframework/oxm/Unmarshaller.java | 19 ++++++++--- .../oxm/castor/CastorMarshaller.java | 4 +++ .../oxm/jaxb/Jaxb1Marshaller.java | 9 +++-- .../oxm/jibx/JibxMarshaller.java | 4 +++ .../oxm/xmlbeans/XmlBeansMarshaller.java | 8 ++++- .../oxm/xstream/XStreamMarshaller.java | 8 +++-- .../oxm/jaxb/Jaxb1MarshallerTest.java | 7 ++++ .../oxm/jibx/JibxMarshallerTest.java | 5 +++ .../oxm/xmlbeans/XmlBeansMarshallerTest.java | 13 ++++--- 13 files changed, 106 insertions(+), 50 deletions(-) diff --git a/changelog.txt b/changelog.txt index 13bc5d16..50faca03 100644 --- a/changelog.txt +++ b/changelog.txt @@ -4,6 +4,9 @@ http://www.springframework.org/spring-ws Changes in version 1.0-RC1 ------------------------- +Package org.springframework.oxm.castor +* added supports method to Marshaller and Unmarshaller + Package org.springframework.oxm.castor * fixed #SWS-88: Allow CastorMarshaller to accept more than one mapping file * fixed #SWS-96: set WhitespacePreserve flag in CastorMarshaller diff --git a/oxm-tiger/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java b/oxm-tiger/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java index 67801d52..ea2798aa 100644 --- a/oxm-tiger/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java +++ b/oxm-tiger/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java @@ -20,9 +20,11 @@ import java.io.IOException; import java.util.Map; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; +import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlAdapter; import javax.xml.transform.Result; import javax.xml.transform.Source; @@ -72,7 +74,7 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller { private Class[] classesToBeBound; - private Map jaxbContextProperties; + private Map jaxbContextProperties; /** * Sets the XmlAdapters to be registered with the JAXB Marshaller and @@ -96,24 +98,15 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller { * Sets the JAXBContext properties. These implementation-specific properties will be set on the * JAXBContext. */ - public void setJaxbContextProperties(Map jaxbContextProperties) { + public void setJaxbContextProperties(Map jaxbContextProperties) { this.jaxbContextProperties = jaxbContextProperties; } - /** - * Sets the Marshaller.Listener to be registered with the JAXB Marshaller. - */ + /** Sets the Marshaller.Listener to be registered with the JAXB Marshaller. */ public void setMarshallerListener(Marshaller.Listener marshallerListener) { this.marshallerListener = marshallerListener; } - /** - * Sets the schema resource to use for validation. - */ - public void setSchema(Resource schemaResource) { - schemaResources = new Resource[]{schemaResource}; - } - /** * Sets the schema language. Default is the W3C XML Schema: http://www.w3.org/2001/XMLSchema". * @@ -124,20 +117,25 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller { this.schemaLanguage = schemaLanguage; } - /** - * Sets the schema resources to use for validation. - */ + /** Sets the schema resource to use for validation. */ + public void setSchema(Resource schemaResource) { + schemaResources = new Resource[]{schemaResource}; + } + + /** Sets the schema resources to use for validation. */ public void setSchemas(Resource[] schemaResources) { this.schemaResources = schemaResources; } - /** - * Sets the Unmarshaller.Listener to be registered with the JAXB Unmarshaller. - */ + /** Sets the Unmarshaller.Listener to be registered with the JAXB Unmarshaller. */ public void setUnmarshallerListener(Unmarshaller.Listener unmarshallerListener) { this.unmarshallerListener = unmarshallerListener; } + public boolean supports(Class clazz) { + return clazz.getAnnotation(XmlRootElement.class) != null || JAXBElement.class.isAssignableFrom(clazz); + } + public void marshal(Object graph, Result result) { try { if (result instanceof StaxResult) { diff --git a/oxm-tiger/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTest.java b/oxm-tiger/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTest.java index ac6eabc7..c4095708 100644 --- a/oxm-tiger/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTest.java +++ b/oxm-tiger/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTest.java @@ -19,7 +19,7 @@ package org.springframework.oxm.jaxb; import java.io.ByteArrayOutputStream; import java.io.StringWriter; import java.util.Collections; - +import javax.xml.bind.JAXBElement; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.stream.XMLEventWriter; @@ -32,15 +32,14 @@ import javax.xml.transform.stream.StreamResult; import org.custommonkey.xmlunit.XMLTestCase; import org.easymock.MockControl; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Text; -import org.xml.sax.ContentHandler; - import org.springframework.oxm.XmlMappingException; import org.springframework.oxm.jaxb2.FlightType; import org.springframework.oxm.jaxb2.Flights; import org.springframework.xml.transform.StaxResult; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Text; +import org.xml.sax.ContentHandler; public class Jaxb2MarshallerTest extends XMLTestCase { @@ -185,4 +184,9 @@ public class Jaxb2MarshallerTest extends XMLTestCase { marshaller.marshal(flights, result); handlerControl.verify(); } + + public void testSupports() throws Exception { + assertTrue("Jaxb2Marshaller does not support Flights", marshaller.supports(Flights.class)); + assertTrue("Jaxb2Marshaller does not support JAXBElement", marshaller.supports(JAXBElement.class)); + } } diff --git a/oxm/src/main/java/org/springframework/oxm/Marshaller.java b/oxm/src/main/java/org/springframework/oxm/Marshaller.java index 3bb7518d..09ac08da 100644 --- a/oxm/src/main/java/org/springframework/oxm/Marshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/Marshaller.java @@ -16,29 +16,37 @@ package org.springframework.oxm; import java.io.IOException; - import javax.xml.transform.Result; /** * Defines the contract for Object XML Mapping Marshallers. Implementations of this interface can serialize a given * Object to a XML Stream. - *

- * Although the marshal method accepts a java.lang.Object as its first parameter, - * most Marshaller implementations cannot handle arbitrary java.lang.Object. Instead, a - * object class must be registered with the marshaller, or have a common base class. - * + *

+ * Although the marshal method accepts a java.lang.Object as its first parameter, most + * Marshaller implementations cannot handle arbitrary java.lang.Object. Instead, a object + * class must be registered with the marshaller, or have a common base class. + * * @author Arjen Poutsma */ public interface Marshaller { /** * Marshals the object graph with the given root into the provided javax.xml.transform.Result. - * - * @param graph the root of the object graph to marshal + * + * @param graph the root of the object graph to marshal * @param result the result to marshal to * @throws XmlMappingException if the given object cannot be marshalled to the result - * @throws IOException if an I/O exception occurs + * @throws IOException if an I/O exception occurs */ void marshal(Object graph, Result result) throws XmlMappingException, IOException; + /** + * Indicates whether this marshaller can marshal instances of the supplied type. + * + * @param clazz the class that this marshaller is being asked if it can marshal + * @return true if this marshaller can indeed marshal instances of the supplied class; + * false otherwise + */ + boolean supports(Class clazz); + } diff --git a/oxm/src/main/java/org/springframework/oxm/Unmarshaller.java b/oxm/src/main/java/org/springframework/oxm/Unmarshaller.java index 45711b8b..14ddb47f 100644 --- a/oxm/src/main/java/org/springframework/oxm/Unmarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/Unmarshaller.java @@ -16,24 +16,33 @@ package org.springframework.oxm; import java.io.IOException; - import javax.xml.transform.Source; /** * Defines the contract for Object XML Mapping unmarshallers. Implementations of this interface can deserialize a given * XML Stream to an Object graph. - * + * * @author Arjen Poutsma */ public interface Unmarshaller { /** * Unmarshals the given provided javax.xml.transform.Source into an object graph. - * + * * @param source the source to marshal from * @return the object graph - * @throws XmlMappingException if the given source cannot be mapped to an object - * @throws IOException if an I/O Exception occurs + * @throws XmlMappingException if the given source cannot be mapped to an object + * @throws IOException if an I/O Exception occurs */ Object unmarshal(Source source) throws XmlMappingException, IOException; + + /** + * Indicates whether this unmarshaller can unmarshal instances of the supplied type. + * + * @param clazz the class that this unmarshaller is being asked if it can marshal + * @return true if this unmarshaller can indeed unmarshal to the supplied class; false + * otherwise + */ + boolean supports(Class clazz); + } 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 743fed61..d6f20ce5 100644 --- a/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java @@ -160,6 +160,10 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } } + public boolean supports(Class clazz) { + return true; + } + /** * Creates the Castor XMLClassDescriptorResolver. Subclasses can override this to create a custom * resolver. diff --git a/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java b/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java index 56ace1ab..6e2be20b 100644 --- a/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java @@ -15,6 +15,7 @@ */ package org.springframework.oxm.jaxb; +import javax.xml.bind.Element; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; @@ -40,13 +41,15 @@ public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements Initializ private boolean validating = false; - /** - * Set if the JAXB Unmarshaller should validate the incoming document. Default is false. - */ + /** Set if the JAXB Unmarshaller should validate the incoming document. Default is false. */ public void setValidating(boolean validating) { this.validating = validating; } + public boolean supports(Class clazz) { + return Element.class.isAssignableFrom(clazz); + } + protected final JAXBContext createJaxbContext() throws JAXBException { if (!StringUtils.hasLength(getContextPath())) { throw new IllegalArgumentException("contextPath is required"); diff --git a/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java b/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java index dfe449a5..5589eee7 100644 --- a/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java @@ -137,6 +137,10 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe transfomerFactory = TransformerFactory.newInstance(); } + public boolean supports(Class clazz) { + return targetClass.isAssignableFrom(clazz); + } + /** * Convert the given JiBXException to an appropriate exception from the * org.springframework.oxm hierarchy. 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 c7465dd2..f193445b 100644 --- a/oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/xmlbeans/XmlBeansMarshaller.java @@ -65,6 +65,10 @@ public class XmlBeansMarshaller extends AbstractMarshaller { this.xmlOptions = xmlOptions; } + 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. @@ -78,7 +82,7 @@ public class XmlBeansMarshaller extends AbstractMarshaller { * @param marshalling indicates whether the exception occurs during marshalling (true), or * unmarshalling (false) * @return the corresponding XmlMappingException - * @see XmlBeansUtils#convertXmlBeansException(Exception, boolean) + * @see XmlBeansUtils#convertXmlBeansException(Exception,boolean) */ public XmlMappingException convertXmlBeansException(Exception ex, boolean marshalling) { return XmlBeansUtils.convertXmlBeansException(ex, marshalling); @@ -165,8 +169,10 @@ public class XmlBeansMarshaller extends AbstractMarshaller { xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", saxHandler.getLexicalHandler()); } catch (SAXNotRecognizedException e) { + // ignore } catch (SAXNotSupportedException e) { + // ignore } try { xmlReader.parse(inputSource); diff --git a/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java b/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java index e101b636..cb50f026 100644 --- a/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java @@ -77,9 +77,7 @@ import org.xml.sax.ext.LexicalHandler; */ public class XStreamMarshaller extends AbstractMarshaller { - /** - * The default encoding used for stream access. - */ + /** The default encoding used for stream access. */ public static final String DEFAULT_ENCODING = "UTF-8"; private XStream xstream = new XStream(); @@ -169,6 +167,10 @@ public class XStreamMarshaller extends AbstractMarshaller { xstream.alias(name, type); } + public boolean supports(Class clazz) { + return true; + } + /** * Convert the given XStream exception to an appropriate exception from the org.springframework.oxm * hierarchy. diff --git a/oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb1MarshallerTest.java b/oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb1MarshallerTest.java index fd5a0401..063ae1f5 100644 --- a/oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb1MarshallerTest.java +++ b/oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb1MarshallerTest.java @@ -21,6 +21,7 @@ import org.springframework.oxm.Marshaller; import org.springframework.oxm.XmlMappingException; import org.springframework.oxm.jaxb1.FlightType; import org.springframework.oxm.jaxb1.Flights; +import org.springframework.oxm.jaxb1.FlightsType; import org.springframework.oxm.jaxb1.impl.FlightTypeImpl; import org.springframework.oxm.jaxb1.impl.FlightsImpl; @@ -72,4 +73,10 @@ public class Jaxb1MarshallerTest extends AbstractJaxbMarshallerTestCase { } } + public void testSupports() throws Exception { + assertTrue("Jaxb1Marshaller does not support Flights", marshaller.supports(Flights.class)); + assertFalse("Jaxb1Marshaller supports FlightsType", marshaller.supports(FlightsType.class)); + } + + } diff --git a/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java b/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java index 98398380..e093fa2b 100644 --- a/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java +++ b/oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTest.java @@ -68,4 +68,9 @@ public class JibxMarshallerTest extends AbstractMarshallerTestCase { result.toString().startsWith("")); } + public void testSupports() throws Exception { + assertTrue("Jaxb2Marshaller does not support Flights", marshaller.supports(Flights.class)); + } + + } diff --git a/oxm/src/test/java/org/springframework/oxm/xmlbeans/XmlBeansMarshallerTest.java b/oxm/src/test/java/org/springframework/oxm/xmlbeans/XmlBeansMarshallerTest.java index eb2d4545..5a390a77 100644 --- a/oxm/src/test/java/org/springframework/oxm/xmlbeans/XmlBeansMarshallerTest.java +++ b/oxm/src/test/java/org/springframework/oxm/xmlbeans/XmlBeansMarshallerTest.java @@ -16,9 +16,9 @@ package org.springframework.oxm.xmlbeans; import java.io.ByteArrayOutputStream; - import javax.xml.transform.stream.StreamResult; +import org.apache.xmlbeans.XmlObject; import org.springframework.oxm.AbstractMarshallerTestCase; import org.springframework.oxm.Marshaller; import org.springframework.samples.flight.FlightType; @@ -33,7 +33,7 @@ public class XmlBeansMarshallerTest extends AbstractMarshallerTestCase { public void testMarshalNonXmlObject() throws Exception { try { - this.marshaller.marshal(new Object(), new StreamResult(new ByteArrayOutputStream())); + marshaller.marshal(new Object(), new StreamResult(new ByteArrayOutputStream())); fail("XmlBeansMarshaller did not throw ClassCastException for non-XmlObject"); } catch (ClassCastException e) { @@ -49,8 +49,11 @@ public class XmlBeansMarshallerTest extends AbstractMarshallerTestCase { return flightsDocument; } - public void testMarshalStaxResultXMLStreamWriter() throws Exception { - // Unfortu + public void testSupports() throws Exception { + assertTrue("XmlBeansMarshaller does not support XmlObject", marshaller.supports(XmlObject.class)); + assertFalse("XmlBeansMarshaller supports other objects", marshaller.supports(Object.class)); + assertTrue("XmlBeansMarshaller does not support FlightsDocument", marshaller.supports(FlightsDocument.class)); + assertTrue("XmlBeansMarshaller does not support Flights", marshaller.supports(Flights.class)); + assertTrue("XmlBeansMarshaller does not support FlightType", marshaller.supports(FlightType.class)); } - }