diff --git a/oxm/pom.xml b/oxm/pom.xml index ae640af7..613a05e2 100644 --- a/oxm/pom.xml +++ b/oxm/pom.xml @@ -1,4 +1,5 @@ - + spring-ws-parent org.springframework.ws @@ -21,8 +22,8 @@ - - + + ${project.build.directory}/generated-sources/test/java @@ -37,8 +38,8 @@ - - + + @@ -99,24 +100,16 @@ test - - xml-apis - xml-apis - provided - - - stax - stax-api - org.codehaus.woodstox wstx-asl - provided + test + xerces xercesImpl - provided + true diff --git a/oxm/src/main/java/org/springframework/oxm/AbstractMarshaller.java b/oxm/src/main/java/org/springframework/oxm/AbstractMarshaller.java index f2440483..fc35d2fa 100644 --- a/oxm/src/main/java/org/springframework/oxm/AbstractMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/AbstractMarshaller.java @@ -34,14 +34,12 @@ import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.stax.StAXSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.util.Assert; -import org.springframework.xml.transform.StaxResult; -import org.springframework.xml.transform.StaxSource; import org.w3c.dom.Node; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; @@ -50,6 +48,10 @@ import org.xml.sax.XMLReader; import org.xml.sax.ext.LexicalHandler; import org.xml.sax.helpers.XMLReaderFactory; +import org.springframework.util.Assert; +import org.springframework.xml.transform.StaxSource; +import org.springframework.xml.transform.TraxUtils; + /** * Abstract implementation of the Marshaller and Unmarshaller interface. This implementation * inspects the given Source or Result, and defers further handling to overridable template @@ -60,9 +62,7 @@ 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 DocumentBuilderFactory documentBuilderFactory; @@ -87,8 +87,8 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { if (result instanceof DOMResult) { marshalDomResult(graph, (DOMResult) result); } - else if (result instanceof StaxResult) { - marshalStaxResult(graph, (StaxResult) result); + else if (TraxUtils.isStaxResult(result)) { + marshalStaxResult(graph, result); } else if (result instanceof SAXResult) { marshalSaxResult(graph, (SAXResult) result); @@ -121,8 +121,8 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { if (source instanceof DOMSource) { return unmarshalDomSource((DOMSource) source); } - else if (source instanceof StaxSource) { - return unmarshalStaxSource((StaxSource) source); + else if (TraxUtils.isStaxSource(source)) { + return unmarshalStaxSource(source); } else if (source instanceof SAXSource) { return unmarshalSaxSource((SAXSource) source); @@ -198,20 +198,24 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { * the StaxResult. * * @param graph the root of the object graph to marshal - * @param staxResult the StaxResult + * @param staxResult a Spring-WS {@link StaxSource} or JAXP 1.4 {@link StAXSource} * @throws XmlMappingException if the given object cannot be marshalled to the result * @throws IllegalArgumentException if the domResult is empty * @see #marshalDomNode(Object,org.w3c.dom.Node) */ - protected void marshalStaxResult(Object graph, StaxResult staxResult) throws XmlMappingException { - if (staxResult.getXMLStreamWriter() != null) { - marshalXmlStreamWriter(graph, staxResult.getXMLStreamWriter()); - } - else if (staxResult.getXMLEventWriter() != null) { - marshalXmlEventWriter(graph, staxResult.getXMLEventWriter()); + protected void marshalStaxResult(Object graph, Result staxResult) throws XmlMappingException { + XMLStreamWriter streamWriter = TraxUtils.getXMLStreamWriter(staxResult); + if (streamWriter != null) { + marshalXmlStreamWriter(graph, streamWriter); } else { - throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer"); + XMLEventWriter eventWriter = TraxUtils.getXMLEventWriter(staxResult); + if (eventWriter != null) { + marshalXmlEventWriter(graph, eventWriter); + } + else { + throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer"); + } } } @@ -296,15 +300,19 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { * @return the object graph * @throws XmlMappingException if the given source cannot be mapped to an object */ - protected Object unmarshalStaxSource(StaxSource staxSource) throws XmlMappingException { - if (staxSource.getXMLStreamReader() != null) { - return unmarshalXmlStreamReader(staxSource.getXMLStreamReader()); - } - else if (staxSource.getXMLEventReader() != null) { - return unmarshalXmlEventReader(staxSource.getXMLEventReader()); + protected Object unmarshalStaxSource(Source staxSource) throws XmlMappingException { + XMLStreamReader streamReader = TraxUtils.getXMLStreamReader(staxSource); + if (streamReader != null) { + return unmarshalXmlStreamReader(streamReader); } else { - throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader"); + XMLEventReader eventReader = TraxUtils.getXMLEventReader(staxSource); + if (eventReader != null) { + return unmarshalXmlEventReader(eventReader); + } + else { + throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader"); + } } } 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 c3c172a8..b0a79196 100644 --- a/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java @@ -19,12 +19,19 @@ import javax.xml.bind.Element; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; import javax.xml.transform.Result; import javax.xml.transform.Source; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; +import org.springframework.xml.transform.StaxResult; +import org.springframework.xml.transform.StaxSource; +import org.springframework.xml.transform.TraxUtils; /** * Implementation of the Marshaller interface for JAXB 1.0. @@ -93,6 +100,22 @@ public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements BeanClass } public void marshal(Object graph, Result result) { + if (TraxUtils.isStaxResult(result)) { + XMLStreamWriter streamWriter = TraxUtils.getXMLStreamWriter(result); + if (streamWriter != null) { + result = new StaxResult(streamWriter); + } + else { + XMLEventWriter eventWriter = TraxUtils.getXMLEventWriter(result); + if (eventWriter != null) { + result = new StaxResult(eventWriter); + } + else { + throw new IllegalArgumentException( + "StAXResult contains neither XMLStreamWriter nor XMLEventWriter"); + } + } + } try { createMarshaller().marshal(graph, result); } @@ -102,6 +125,22 @@ public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements BeanClass } public Object unmarshal(Source source) { + if (TraxUtils.isStaxSource(source)) { + XMLStreamReader streamReader = TraxUtils.getXMLStreamReader(source); + if (streamReader != null) { + source = new StaxSource(streamReader); + } + else { + XMLEventReader eventReader = TraxUtils.getXMLEventReader(source); + if (eventReader != null) { + source = new StaxSource(eventReader); + } + else { + throw new IllegalArgumentException( + "StAXSource contains neither XMLStreamReader nor XMLEventReader"); + } + } + } try { return createUnmarshaller().unmarshal(source); } @@ -109,4 +148,5 @@ public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements BeanClass throw convertJaxbException(ex); } } + } diff --git a/oxm/src/test/java/org/springframework/oxm/AbstractMarshallerTestCase.java b/oxm/src/test/java/org/springframework/oxm/AbstractMarshallerTestCase.java index 09e64e55..033ddc83 100644 --- a/oxm/src/test/java/org/springframework/oxm/AbstractMarshallerTestCase.java +++ b/oxm/src/test/java/org/springframework/oxm/AbstractMarshallerTestCase.java @@ -23,15 +23,17 @@ import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.stax.StAXResult; import javax.xml.transform.stream.StreamResult; import org.custommonkey.xmlunit.XMLTestCase; import org.custommonkey.xmlunit.XMLUnit; -import org.springframework.xml.transform.StaxResult; +import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; -import org.w3c.dom.Attr; + +import org.springframework.xml.transform.StaxResult; public abstract class AbstractMarshallerTestCase extends XMLTestCase { @@ -90,7 +92,7 @@ public abstract class AbstractMarshallerTestCase extends XMLTestCase { new String(os.toByteArray(), "UTF-8")); } - public void testMarshalStaxResultXMLStreamWriter() throws Exception { + public void testMarshalStaxResultStreamWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); @@ -99,7 +101,7 @@ public abstract class AbstractMarshallerTestCase extends XMLTestCase { assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString()); } - public void testMarshalStaxResultXMLEventWriter() throws Exception { + public void testMarshalStaxResultEventWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer); @@ -107,4 +109,22 @@ public abstract class AbstractMarshallerTestCase extends XMLTestCase { marshaller.marshal(flights, result); assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString()); } + + public void testMarshalJaxp14StaxResultStreamWriter() throws Exception { + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + StringWriter writer = new StringWriter(); + XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); + StAXResult result = new StAXResult(streamWriter); + marshaller.marshal(flights, result); + assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString()); + } + + public void testMarshalJaxp14StaxResultEventWriter() throws Exception { + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + StringWriter writer = new StringWriter(); + XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer); + StAXResult result = new StAXResult(eventWriter); + marshaller.marshal(flights, result); + assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString()); + } } diff --git a/oxm/src/test/java/org/springframework/oxm/AbstractUnmarshallerTestCase.java b/oxm/src/test/java/org/springframework/oxm/AbstractUnmarshallerTestCase.java index 577fd776..a9b0a665 100644 --- a/oxm/src/test/java/org/springframework/oxm/AbstractUnmarshallerTestCase.java +++ b/oxm/src/test/java/org/springframework/oxm/AbstractUnmarshallerTestCase.java @@ -25,10 +25,10 @@ import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.stax.StAXSource; import javax.xml.transform.stream.StreamSource; import junit.framework.TestCase; -import org.springframework.xml.transform.StaxSource; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; @@ -36,6 +36,8 @@ import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderFactory; +import org.springframework.xml.transform.StaxSource; + public abstract class AbstractUnmarshallerTestCase extends TestCase { protected Unmarshaller unmarshaller; @@ -105,6 +107,22 @@ public abstract class AbstractUnmarshallerTestCase extends TestCase { testFlights(flights); } + public void testUnmarshalJaxp14StaxSourceXmlStreamReader() throws Exception { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); + StAXSource source = new StAXSource(streamReader); + Object flights = unmarshaller.unmarshal(source); + testFlights(flights); + } + + public void testUnmarshalJaxp14StaxSourceXmlEventReader() throws Exception { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING)); + StAXSource source = new StAXSource(eventReader); + Object flights = unmarshaller.unmarshal(source); + testFlights(flights); + } + public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));