Marshaller-supports

This commit is contained in:
Arjen Poutsma
2007-04-03 16:58:48 +00:00
parent 31e518f079
commit 5a4ccca5f9
13 changed files with 106 additions and 50 deletions

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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.

View File

@@ -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");

View File

@@ -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.

View File

@@ -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);

View File

@@ -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.

View File

@@ -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));
}
}

View File

@@ -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));
}
}

View File

@@ -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));
}
}