Added support for unmarshalling XML fragments, requested by Spring Batch.
This commit is contained in:
@@ -247,22 +247,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
|
||||
// Unmarshalling
|
||||
//
|
||||
|
||||
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
|
||||
try {
|
||||
Transformer transformer = transfomerFactory.newTransformer();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
transformer.transform(new DOMSource(node), new StreamResult(os));
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
return unmarshalInputStream(is);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
|
||||
try {
|
||||
IUnmarshallingContext unmarshallingContext = createUnmarshallingContext();
|
||||
@@ -283,6 +267,48 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) {
|
||||
try {
|
||||
UnmarshallingContext unmarshallingContext = (UnmarshallingContext) createUnmarshallingContext();
|
||||
IXMLReader xmlReader = new StAXReaderWrapper(streamReader, null, true);
|
||||
unmarshallingContext.setDocument(xmlReader);
|
||||
return unmarshallingContext.unmarshalElement();
|
||||
}
|
||||
catch (JiBXException ex) {
|
||||
throw convertJibxException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) {
|
||||
try {
|
||||
XMLStreamReader streamReader = new XmlEventStreamReader(eventReader);
|
||||
return unmarshalXmlStreamReader(streamReader);
|
||||
}
|
||||
catch (XMLStreamException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Unsupported Unmarshalling
|
||||
//
|
||||
|
||||
protected Object unmarshalDomNode(Node node) throws XmlMappingException {
|
||||
try {
|
||||
Transformer transformer = transfomerFactory.newTransformer();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
transformer.transform(new DOMSource(node), new StreamResult(os));
|
||||
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
|
||||
return unmarshalInputStream(is);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource)
|
||||
throws XmlMappingException, IOException {
|
||||
try {
|
||||
@@ -300,28 +326,6 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlEventReader(XMLEventReader eventReader) {
|
||||
try {
|
||||
XMLStreamReader streamReader = new XmlEventStreamReader(eventReader);
|
||||
return unmarshalXmlStreamReader(streamReader);
|
||||
}
|
||||
catch (XMLStreamException ex) {
|
||||
throw new JibxSystemException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unmarshalXmlStreamReader(XMLStreamReader streamReader) {
|
||||
try {
|
||||
UnmarshallingContext unmarshallingContext = (UnmarshallingContext) createUnmarshallingContext();
|
||||
IXMLReader xmlReader = new StAXReaderWrapper(streamReader, null, true);
|
||||
unmarshallingContext.setDocument(xmlReader);
|
||||
return unmarshallingContext.unmarshalElement();
|
||||
}
|
||||
catch (JiBXException ex) {
|
||||
throw convertJibxException(ex, false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new <code>IMarshallingContext</code>, set with the correct indentation.
|
||||
*
|
||||
|
||||
@@ -17,7 +17,7 @@ package org.springframework.oxm;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.StringReader;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
@@ -28,6 +28,7 @@ import javax.xml.transform.sax.SAXSource;
|
||||
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;
|
||||
@@ -35,8 +36,6 @@ 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;
|
||||
@@ -53,6 +52,8 @@ public abstract class AbstractUnmarshallerTestCase extends TestCase {
|
||||
|
||||
protected abstract void testFlights(Object o);
|
||||
|
||||
protected abstract void testFlight(Object o);
|
||||
|
||||
public void testUnmarshalDomSource() throws Exception {
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
@@ -103,4 +104,18 @@ public abstract class AbstractUnmarshallerTestCase extends TestCase {
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
testFlights(flights);
|
||||
}
|
||||
|
||||
public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
streamReader.nextTag(); // skip to flights
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"),
|
||||
streamReader.getName());
|
||||
streamReader.nextTag(); // skip to flight
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"),
|
||||
streamReader.getName());
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
Object flight = unmarshaller.unmarshal(source);
|
||||
testFlight(flight);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,11 @@ public class CastorUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlightCount());
|
||||
Flight flight = flights.getFlight()[0];
|
||||
testFlight(flights.getFlight()[0]);
|
||||
}
|
||||
|
||||
protected void testFlight(Object o) {
|
||||
Flight flight = (Flight) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
@@ -34,7 +34,11 @@ public class Jaxb1UnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
|
||||
FlightType flight = (FlightType) flights.getFlight().get(0);
|
||||
testFlight(flights.getFlight().get(0));
|
||||
}
|
||||
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = (FlightType) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
@@ -31,10 +31,16 @@ public class JibxUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.sizeFlightList());
|
||||
FlightType flight = (FlightType) flights.getFlight(0);
|
||||
testFlight(flights.getFlight(0));
|
||||
}
|
||||
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = (FlightType) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
|
||||
public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
// JiBX does not support reading XML fragments, hence the override here
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,18 @@
|
||||
*/
|
||||
package org.springframework.oxm.xmlbeans;
|
||||
|
||||
import java.io.StringReader;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.samples.flight.FlightDocument;
|
||||
import org.springframework.samples.flight.FlightType;
|
||||
import org.springframework.samples.flight.FlightsDocument;
|
||||
import org.springframework.samples.flight.FlightsDocument.Flights;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
public class XmlBeansUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
@@ -33,11 +40,36 @@ public class XmlBeansUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||
assertNotNull("FlightsDocument is null", flightsDocument);
|
||||
Flights flights = flightsDocument.getFlights();
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.sizeOfFlightArray());
|
||||
FlightType flight = flights.getFlightArray(0);
|
||||
testFlight(flights.getFlightArray(0));
|
||||
}
|
||||
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = null;
|
||||
if (o instanceof FlightType) {
|
||||
flight = (FlightType) o;
|
||||
}
|
||||
else if (o instanceof FlightDocument) {
|
||||
FlightDocument flightDocument = (FlightDocument) o;
|
||||
flight = flightDocument.getFlight();
|
||||
}
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
public void testUnmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||
streamReader.nextTag(); // skip to flights
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"),
|
||||
streamReader.getName());
|
||||
streamReader.nextTag(); // skip to flight
|
||||
assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"),
|
||||
streamReader.getName());
|
||||
StaxSource source = new StaxSource(streamReader);
|
||||
Object flight = unmarshaller.unmarshal(source);
|
||||
testFlight(flight);
|
||||
}
|
||||
|
||||
public void testValidate() throws Exception {
|
||||
((XmlBeansMarshaller) unmarshaller).setValidating(true);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user