Working on JAXP 1.4 support.

This commit is contained in:
Arjen Poutsma
2007-12-20 02:45:13 +00:00
parent 3e11c052b0
commit 0da5da9546
5 changed files with 125 additions and 46 deletions

View File

@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>spring-ws-parent</artifactId>
<groupId>org.springframework.ws</groupId>
@@ -21,8 +22,8 @@
<configuration>
<tasks>
<ant antfile="${basedir}/build-maven2.xml" inheritRefs="true">
<property name="maven.test.skip" value="${maven.test.skip}" />
<target name="generate-test-sources" />
<property name="maven.test.skip" value="${maven.test.skip}"/>
<target name="generate-test-sources"/>
</ant>
</tasks>
<testSourceRoot>${project.build.directory}/generated-sources/test/java</testSourceRoot>
@@ -37,8 +38,8 @@
<configuration>
<tasks>
<ant antfile="${basedir}/build-maven2.xml" inheritRefs="true">
<property name="maven.test.skip" value="${maven.test.skip}" />
<target name="test-compile" />
<property name="maven.test.skip" value="${maven.test.skip}"/>
<target name="test-compile"/>
</ant>
</tasks>
</configuration>
@@ -99,24 +100,16 @@
<scope>test</scope>
</dependency>
<!-- XML handling dependencies -->
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
<scope>provided</scope>
<scope>test</scope>
</dependency>
<dependency>
<!-- Xerces required for Castor -->
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- O/X Mapping dependencies -->
<!-- Castor -->

View File

@@ -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 <code>Marshaller</code> and <code>Unmarshaller</code> interface. This implementation
* inspects the given <code>Source</code> or <code>Result</code>, 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 <code>StaxResult</code>.
*
* @param graph the root of the object graph to marshal
* @param staxResult the <code>StaxResult</code>
* @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 <code>domResult</code> 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");
}
}
}

View File

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

View File

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

View File

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