Marshaller-supports
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<java.lang.String, ?> jaxbContextProperties;
|
||||
|
||||
/**
|
||||
* Sets the <code>XmlAdapter</code>s to be registered with the JAXB <code>Marshaller</code> and
|
||||
@@ -96,24 +98,15 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller {
|
||||
* Sets the <code>JAXBContext</code> properties. These implementation-specific properties will be set on the
|
||||
* <code>JAXBContext</code>.
|
||||
*/
|
||||
public void setJaxbContextProperties(Map jaxbContextProperties) {
|
||||
public void setJaxbContextProperties(Map<String, ?> jaxbContextProperties) {
|
||||
this.jaxbContextProperties = jaxbContextProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the <code>Marshaller.Listener</code> to be registered with the JAXB <code>Marshaller</code>.
|
||||
*/
|
||||
/** Sets the <code>Marshaller.Listener</code> to be registered with the JAXB <code>Marshaller</code>. */
|
||||
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: <code>http://www.w3.org/2001/XMLSchema"</code>.
|
||||
*
|
||||
@@ -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 <code>Unmarshaller.Listener</code> to be registered with the JAXB <code>Unmarshaller</code>.
|
||||
*/
|
||||
/** Sets the <code>Unmarshaller.Listener</code> to be registered with the JAXB <code>Unmarshaller</code>. */
|
||||
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) {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
* <p>
|
||||
* Although the <code>marshal</code> method accepts a <code>java.lang.Object</code> as its first parameter,
|
||||
* most <code>Marshaller</code> implementations cannot handle arbitrary <code>java.lang.Object</code>. Instead, a
|
||||
* object class must be registered with the marshaller, or have a common base class.
|
||||
*
|
||||
* <p/>
|
||||
* Although the <code>marshal</code> method accepts a <code>java.lang.Object</code> as its first parameter, most
|
||||
* <code>Marshaller</code> implementations cannot handle arbitrary <code>java.lang.Object</code>. 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 <code>javax.xml.transform.Result</code>.
|
||||
*
|
||||
* @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 <code>true</code> if this marshaller can indeed marshal instances of the supplied class;
|
||||
* <code>false</code> otherwise
|
||||
*/
|
||||
boolean supports(Class clazz);
|
||||
|
||||
}
|
||||
|
||||
@@ -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 <code>javax.xml.transform.Source</code> 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 <code>true</code> if this unmarshaller can indeed unmarshal to the supplied class; <code>false</code>
|
||||
* otherwise
|
||||
*/
|
||||
boolean supports(Class clazz);
|
||||
|
||||
}
|
||||
|
||||
@@ -160,6 +160,10 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
|
||||
}
|
||||
}
|
||||
|
||||
public boolean supports(Class clazz) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the Castor <code>XMLClassDescriptorResolver</code>. Subclasses can override this to create a custom
|
||||
* resolver.
|
||||
|
||||
@@ -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 <code>Unmarshaller</code> should validate the incoming document. Default is <code>false</code>.
|
||||
*/
|
||||
/** Set if the JAXB <code>Unmarshaller</code> should validate the incoming document. Default is <code>false</code>. */
|
||||
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");
|
||||
|
||||
@@ -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 <code>JiBXException</code> to an appropriate exception from the
|
||||
* <code>org.springframework.oxm</code> hierarchy.
|
||||
|
||||
@@ -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 <code>org.springframework.oxm</code>
|
||||
* hierarchy.
|
||||
@@ -78,7 +82,7 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
|
||||
* @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
|
||||
* unmarshalling (<code>false</code>)
|
||||
* @return the corresponding <code>XmlMappingException</code>
|
||||
* @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);
|
||||
|
||||
@@ -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 <code>org.springframework.oxm</code>
|
||||
* hierarchy.
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -68,4 +68,9 @@ public class JibxMarshallerTest extends AbstractMarshallerTestCase {
|
||||
result.toString().startsWith("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>"));
|
||||
}
|
||||
|
||||
public void testSupports() throws Exception {
|
||||
assertTrue("Jaxb2Marshaller does not support Flights", marshaller.supports(Flights.class));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user