diff --git a/oxm-tiger/build-maven2.xml b/oxm-tiger/build-maven2.xml index 1567fdbc..d33c15f6 100644 --- a/oxm-tiger/build-maven2.xml +++ b/oxm-tiger/build-maven2.xml @@ -22,7 +22,7 @@ + classpathref="maven.plugin.classpath"/> diff --git a/oxm-tiger/pom.xml b/oxm-tiger/pom.xml index dbaa9595..0f21224a 100644 --- a/oxm-tiger/pom.xml +++ b/oxm-tiger/pom.xml @@ -1,4 +1,5 @@ - + spring-ws-parent org.springframework.ws @@ -27,8 +28,8 @@ - - + + ${project.build.directory}/generated-sources/test/java @@ -38,6 +39,18 @@ + + + com.sun.xml.bind + jaxb-impl + 2.1.5 + + + com.sun.xml.bind + jaxb-xjc + 2.0.3 + + org.apache.felix @@ -68,7 +81,6 @@ - @@ -106,44 +118,42 @@ test - - stax - stax-api - org.codehaus.woodstox wstx-asl - provided + test - - javax.xml.bind - jaxb-api - provided - true - 2.1 - - - javax.xml.stream - stax-api - - - javax.activation - activation - - - - - com.sun.xml.bind - jaxb-impl - 2.1.5 - provided - + com.sun.xml.bind jaxb-xjc - 2.1.5 + 2.0.3 test 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 8f737853..fbc7d999 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 @@ -16,7 +16,7 @@ package org.springframework.oxm.jaxb; -import java.awt.Image; +import java.awt.*; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -52,6 +52,10 @@ import javax.xml.bind.attachment.AttachmentUnmarshaller; import javax.xml.datatype.Duration; import javax.xml.datatype.XMLGregorianCalendar; import javax.xml.namespace.QName; +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 javax.xml.validation.Schema; @@ -69,8 +73,7 @@ import org.springframework.util.ClassUtils; import org.springframework.util.FileCopyUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; -import org.springframework.xml.transform.StaxResult; -import org.springframework.xml.transform.StaxSource; +import org.springframework.xml.transform.TraxUtils; import org.springframework.xml.validation.SchemaLoaderUtils; /** @@ -369,8 +372,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller if (mtomEnabled && mimeContainer != null) { marshaller.setAttachmentMarshaller(new Jaxb2AttachmentMarshaller(mimeContainer)); } - if (result instanceof StaxResult) { - marshalStaxResult(marshaller, graph, (StaxResult) result); + if (TraxUtils.isStaxResult(result)) { + marshalStaxResult(marshaller, graph, result); } else { marshaller.marshal(graph, result); @@ -381,16 +384,19 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller } } - private void marshalStaxResult(Marshaller jaxbMarshaller, Object graph, StaxResult staxResult) - throws JAXBException { - if (staxResult.getXMLStreamWriter() != null) { - jaxbMarshaller.marshal(graph, staxResult.getXMLStreamWriter()); - } - else if (staxResult.getXMLEventWriter() != null) { - jaxbMarshaller.marshal(graph, staxResult.getXMLEventWriter()); + private void marshalStaxResult(Marshaller jaxbMarshaller, Object graph, Result staxResult) throws JAXBException { + XMLStreamWriter streamWriter = TraxUtils.getXMLStreamWriter(staxResult); + if (streamWriter != null) { + jaxbMarshaller.marshal(graph, streamWriter); } else { - throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer"); + XMLEventWriter eventWriter = TraxUtils.getXMLEventWriter(staxResult); + if (eventWriter != null) { + jaxbMarshaller.marshal(graph, eventWriter); + } + else { + throw new IllegalArgumentException("StAX Result contains neither XMLStreamWriter nor XMLEventConsumer"); + } } } @@ -408,8 +414,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller if (mtomEnabled && mimeContainer != null) { unmarshaller.setAttachmentUnmarshaller(new Jaxb2AttachmentUnmarshaller(mimeContainer)); } - if (source instanceof StaxSource) { - return unmarshalStaxSource(unmarshaller, (StaxSource) source); + if (TraxUtils.isStaxSource(source)) { + return unmarshalStaxSource(unmarshaller, source); } else { return unmarshaller.unmarshal(source); @@ -420,15 +426,19 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller } } - private Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, StaxSource staxSource) throws JAXBException { - if (staxSource.getXMLStreamReader() != null) { - return jaxbUnmarshaller.unmarshal(staxSource.getXMLStreamReader()); - } - else if (staxSource.getXMLEventReader() != null) { - return jaxbUnmarshaller.unmarshal(staxSource.getXMLEventReader()); + private Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource) throws JAXBException { + XMLStreamReader streamReader = TraxUtils.getXMLStreamReader(staxSource); + if (streamReader != null) { + return jaxbUnmarshaller.unmarshal(streamReader); } else { - throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader"); + XMLEventReader eventReader = TraxUtils.getXMLEventReader(staxSource); + if (eventReader != null) { + return jaxbUnmarshaller.unmarshal(eventReader); + } + else { + throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader"); + } } } 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 fdae544d..9172e67d 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 @@ -16,25 +16,19 @@ package org.springframework.oxm.jaxb; -import org.custommonkey.xmlunit.XMLTestCase; -import static org.easymock.EasyMock.*; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; -import org.springframework.oxm.XmlMappingException; -import org.springframework.oxm.jaxb2.FlightType; -import org.springframework.oxm.jaxb2.Flights; -import org.springframework.oxm.jaxb2.ObjectFactory; -import org.springframework.oxm.mime.MimeContainer; -import org.springframework.util.FileCopyUtils; -import org.springframework.xml.transform.StaxResult; -import org.springframework.xml.transform.StringResult; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Text; -import org.xml.sax.Attributes; -import org.xml.sax.ContentHandler; -import org.xml.sax.Locator; - +import java.awt.*; +import java.io.ByteArrayOutputStream; +import java.io.StringWriter; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.URI; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import java.util.UUID; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.xml.bind.JAXBElement; @@ -50,20 +44,28 @@ import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stax.StAXResult; import javax.xml.transform.stream.StreamResult; -import java.awt.*; -import java.io.ByteArrayOutputStream; -import java.io.StringWriter; -import java.lang.reflect.Method; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.net.URI; -import java.util.Calendar; -import java.util.Collections; -import java.util.Date; -import java.util.UUID; + +import org.custommonkey.xmlunit.XMLTestCase; +import static org.easymock.EasyMock.*; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Text; +import org.xml.sax.Attributes; +import org.xml.sax.ContentHandler; +import org.xml.sax.Locator; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.oxm.XmlMappingException; +import org.springframework.oxm.jaxb2.FlightType; +import org.springframework.oxm.jaxb2.Flights; +import org.springframework.oxm.jaxb2.ObjectFactory; +import org.springframework.oxm.mime.MimeContainer; +import org.springframework.util.FileCopyUtils; +import org.springframework.xml.transform.StaxResult; +import org.springframework.xml.transform.StringResult; public class Jaxb2MarshallerTest extends XMLTestCase { @@ -138,6 +140,24 @@ public class Jaxb2MarshallerTest extends XMLTestCase { assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString()); } + public void testMarshalStaxResultXMLStreamWriterJaxp14() 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 testMarshalStaxResultXMLEventWriterJaxp14() 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()); + } + public void testProperties() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setContextPath(CONTEXT_PATH); @@ -236,8 +256,9 @@ public class Jaxb2MarshallerTest extends XMLTestCase { } public void testSupportsPrimitives() throws Exception { - Method primitives = getClass().getDeclaredMethod("primitives", JAXBElement.class, JAXBElement.class, JAXBElement.class, - JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class); + Method primitives = getClass().getDeclaredMethod("primitives", JAXBElement.class, JAXBElement.class, + JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, + JAXBElement.class); Type[] types = primitives.getGenericParameterTypes(); for (int i = 0; i < types.length; i++) { ParameterizedType type = (ParameterizedType) types[i]; @@ -246,9 +267,10 @@ public class Jaxb2MarshallerTest extends XMLTestCase { } public void testSupportsStandards() throws Exception { - Method standards = getClass().getDeclaredMethod("standards", JAXBElement.class, JAXBElement.class, JAXBElement.class, - JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, - JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class); + Method standards = getClass().getDeclaredMethod("standards", JAXBElement.class, JAXBElement.class, + JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, + JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, + JAXBElement.class, JAXBElement.class); Type[] types = standards.getGenericParameterTypes(); for (int i = 0; i < types.length; i++) { ParameterizedType type = (ParameterizedType) types[i]; @@ -279,15 +301,29 @@ public class Jaxb2MarshallerTest extends XMLTestCase { assertTrue("No XML written", result.toString().length() > 0); } - private void primitives(JAXBElement bool, JAXBElement aByte, JAXBElement aShort, - JAXBElement anInteger, JAXBElement aLong, JAXBElement aFloat, - JAXBElement aDouble, JAXBElement byteArray) { + private void primitives(JAXBElement bool, + JAXBElement aByte, + JAXBElement aShort, + JAXBElement anInteger, + JAXBElement aLong, + JAXBElement aFloat, + JAXBElement aDouble, + JAXBElement byteArray) { } - private void standards(JAXBElement string, JAXBElement integer, JAXBElement decimal, - JAXBElement calendar, JAXBElement date, JAXBElement qName, - JAXBElement uri, JAXBElement xmlGregorianCalendar, - JAXBElement duration, JAXBElement object, JAXBElement image, - JAXBElement dataHandler, JAXBElement source, JAXBElement uuid) { + private void standards(JAXBElement string, + JAXBElement integer, + JAXBElement decimal, + JAXBElement calendar, + JAXBElement date, + JAXBElement qName, + JAXBElement uri, + JAXBElement xmlGregorianCalendar, + JAXBElement duration, + JAXBElement object, + JAXBElement image, + JAXBElement dataHandler, + JAXBElement source, + JAXBElement uuid) { } } diff --git a/oxm-tiger/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTest.java b/oxm-tiger/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTest.java index 042ca26f..a0689559 100644 --- a/oxm-tiger/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTest.java +++ b/oxm-tiger/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTest.java @@ -29,10 +29,18 @@ import javax.xml.stream.XMLStreamReader; import javax.xml.transform.Source; 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 static org.easymock.EasyMock.*; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Text; +import org.xml.sax.InputSource; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.oxm.jaxb2.FlightType; @@ -40,12 +48,6 @@ import org.springframework.oxm.jaxb2.Flights; import org.springframework.oxm.mime.MimeContainer; import org.springframework.xml.transform.StaxSource; import org.springframework.xml.transform.StringSource; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Text; -import org.xml.sax.InputSource; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.XMLReaderFactory; public class Jaxb2UnmarshallerTest extends TestCase { @@ -112,6 +114,22 @@ public class Jaxb2UnmarshallerTest extends TestCase { testFlights(flights); } + public void testUnmarshalStaxSourceXmlStreamReaderJaxp14() 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 testUnmarshalStaxSourceXmlEventReaderJaxp14() 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 testMarshalAttachments() throws Exception { unmarshaller = new Jaxb2Marshaller(); unmarshaller.setClassesToBeBound(new Class[]{BinaryObject.class});