/** * Marshals the object graph with the given root into the provided {@code javax.xml.transform.Result}. * <p>This implementation inspects the given result, and calls {@code marshalDomResult}, * {@code marshalSaxResult}, or {@code marshalStreamResult}. * @param graph the root of the object graph to marshal * @param result the result to marshal to * @throws IOException if an I/O exception occurs * @throws XmlMappingException if the given object cannot be marshalled to the result * @throws IllegalArgumentException if {@code result} if neither a {@code DOMResult}, * a {@code SAXResult}, nor a {@code StreamResult} * @see #marshalDomResult(Object, javax.xml.transform.dom.DOMResult) * @see #marshalSaxResult(Object, javax.xml.transform.sax.SAXResult) * @see #marshalStreamResult(Object, javax.xml.transform.stream.StreamResult) */ @Override public final void marshal(Object graph, Result result) throws IOException, XmlMappingException { if (result instanceof DOMResult) { marshalDomResult(graph, (DOMResult) result); } else if (StaxUtils.isStaxResult(result)) { marshalStaxResult(graph, result); } else if (result instanceof SAXResult) { marshalSaxResult(graph, (SAXResult) result); } else if (result instanceof StreamResult) { marshalStreamResult(graph, (StreamResult) result); } else { throw new IllegalArgumentException("Unknown Result type: " + result.getClass()); } }
/** * Template method for handling {@code StaxResult}s. * <p>This implementation delegates to {@code marshalXMLSteamWriter} or * {@code marshalXMLEventConsumer}, depending on what is contained in the * {@code StaxResult}. * @param graph the root of the object graph to marshal * @param staxResult a JAXP 1.4 {@link StAXSource} * @throws XmlMappingException if the given object cannot be marshalled to the result * @throws IllegalArgumentException if the {@code domResult} is empty * @see #marshalDomNode(Object, org.w3c.dom.Node) */ protected void marshalStaxResult(Object graph, Result staxResult) throws XmlMappingException { XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(staxResult); if (streamWriter != null) { marshalXmlStreamWriter(graph, streamWriter); } else { XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(staxResult); if (eventWriter != null) { marshalXmlEventWriter(graph, eventWriter); } else { throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer"); } } }
/** * Unmarshals the given provided {@code javax.xml.transform.Source} into an object graph. * <p>This implementation inspects the given result, and calls {@code unmarshalDomSource}, * {@code unmarshalSaxSource}, or {@code unmarshalStreamSource}. * @param source the source to marshal from * @return the object graph * @throws IOException if an I/O Exception occurs * @throws XmlMappingException if the given source cannot be mapped to an object * @throws IllegalArgumentException if {@code source} is neither a {@code DOMSource}, * a {@code SAXSource}, nor a {@code StreamSource} * @see #unmarshalDomSource(javax.xml.transform.dom.DOMSource) * @see #unmarshalSaxSource(javax.xml.transform.sax.SAXSource) * @see #unmarshalStreamSource(javax.xml.transform.stream.StreamSource) */ @Override public final Object unmarshal(Source source) throws IOException, XmlMappingException { if (source instanceof DOMSource) { return unmarshalDomSource((DOMSource) source); } else if (StaxUtils.isStaxSource(source)) { return unmarshalStaxSource(source); } else if (source instanceof SAXSource) { return unmarshalSaxSource((SAXSource) source); } else if (source instanceof StreamSource) { return unmarshalStreamSource((StreamSource) source); } else { throw new IllegalArgumentException("Unknown Source type: " + source.getClass()); } }
/** * Template method for handling {@code StaxSource}s. * <p>This implementation delegates to {@code unmarshalXmlStreamReader} or * {@code unmarshalXmlEventReader}. * @param staxSource the {@code StaxSource} * @return the object graph * @throws XmlMappingException if the given source cannot be mapped to an object */ protected Object unmarshalStaxSource(Source staxSource) throws XmlMappingException { XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(staxSource); if (streamReader != null) { return unmarshalXmlStreamReader(streamReader); } else { XMLEventReader eventReader = StaxUtils.getXMLEventReader(staxSource); if (eventReader != null) { return unmarshalXmlEventReader(eventReader); } else { throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader"); } } }
@Override public void marshal(Object graph, Result result, MimeContainer mimeContainer) throws XmlMappingException { try { Marshaller marshaller = createMarshaller(); if (this.mtomEnabled && mimeContainer != null) { marshaller.setAttachmentMarshaller(new Jaxb2AttachmentMarshaller(mimeContainer)); } if (StaxUtils.isStaxResult(result)) { marshalStaxResult(marshaller, graph, result); } else { marshaller.marshal(graph, result); } } catch (JAXBException ex) { throw convertJaxbException(ex); } }
protected Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource) throws JAXBException { XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(staxSource); if (streamReader != null) { return (this.mappedClass != null ? jaxbUnmarshaller.unmarshal(streamReader, this.mappedClass).getValue() : jaxbUnmarshaller.unmarshal(streamReader)); } else { XMLEventReader eventReader = StaxUtils.getXMLEventReader(staxSource); if (eventReader != null) { return (this.mappedClass != null ? jaxbUnmarshaller.unmarshal(eventReader, this.mappedClass).getValue() : jaxbUnmarshaller.unmarshal(eventReader)); } else { throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader"); } } }
/** * Move to next fragment and map it to item. * @return an item * @throws Exception if there is a problem reading from the resource */ protected final T doRead() throws Exception { if (noInput) { return null; } T item = null; if (moveCursorToNextFragment(fragmentReader)) { fragmentReader.markStartFragment(); @SuppressWarnings("unchecked") T mappedFragment = (T) unmarshaller.unmarshal(StaxUtils .createStaxSource(fragmentReader)); item = mappedFragment; fragmentReader.markFragmentProcessed(); } return item; }
/** * Marshals the object graph with the given root into the provided {@code javax.xml.transform.Result}. * <p>This implementation inspects the given result, and calls {@code marshalDomResult}, * {@code marshalSaxResult}, or {@code marshalStreamResult}. * @param graph the root of the object graph to marshal * @param result the result to marshal to * @throws IOException if an I/O exception occurs * @throws XmlMappingException if the given object cannot be marshalled to the result * @throws IllegalArgumentException if {@code result} if neither a {@code DOMResult}, * a {@code SAXResult}, nor a {@code StreamResult} * @see #marshalDomResult(Object, javax.xml.transform.dom.DOMResult) * @see #marshalSaxResult(Object, javax.xml.transform.sax.SAXResult) * @see #marshalStreamResult(Object, javax.xml.transform.stream.StreamResult) */ public final void marshal(Object graph, Result result) throws IOException, XmlMappingException { if (result instanceof DOMResult) { marshalDomResult(graph, (DOMResult) result); } else if (StaxUtils.isStaxResult(result)) { marshalStaxResult(graph, result); } else if (result instanceof SAXResult) { marshalSaxResult(graph, (SAXResult) result); } else if (result instanceof StreamResult) { marshalStreamResult(graph, (StreamResult) result); } else { throw new IllegalArgumentException("Unknown Result type: " + result.getClass()); } }
/** * Template method for handling {@code StaxResult}s. * <p>This implementation delegates to {@code marshalXMLSteamWriter} or * {@code marshalXMLEventConsumer}, depending on what is contained in the * {@code StaxResult}. * @param graph the root of the object graph to marshal * @param staxResult a Spring {@link org.springframework.util.xml.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} is empty * @see #marshalDomNode(Object, org.w3c.dom.Node) */ protected void marshalStaxResult(Object graph, Result staxResult) throws XmlMappingException { XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(staxResult); if (streamWriter != null) { marshalXmlStreamWriter(graph, streamWriter); } else { XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(staxResult); if (eventWriter != null) { marshalXmlEventWriter(graph, eventWriter); } else { throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer"); } } }
/** * Unmarshals the given provided {@code javax.xml.transform.Source} into an object graph. * <p>This implementation inspects the given result, and calls {@code unmarshalDomSource}, * {@code unmarshalSaxSource}, or {@code unmarshalStreamSource}. * @param source the source to marshal from * @return the object graph * @throws IOException if an I/O Exception occurs * @throws XmlMappingException if the given source cannot be mapped to an object * @throws IllegalArgumentException if {@code source} is neither a {@code DOMSource}, * a {@code SAXSource}, nor a {@code StreamSource} * @see #unmarshalDomSource(javax.xml.transform.dom.DOMSource) * @see #unmarshalSaxSource(javax.xml.transform.sax.SAXSource) * @see #unmarshalStreamSource(javax.xml.transform.stream.StreamSource) */ public final Object unmarshal(Source source) throws IOException, XmlMappingException { if (source instanceof DOMSource) { return unmarshalDomSource((DOMSource) source); } else if (StaxUtils.isStaxSource(source)) { return unmarshalStaxSource(source); } else if (source instanceof SAXSource) { return unmarshalSaxSource((SAXSource) source); } else if (source instanceof StreamSource) { return unmarshalStreamSource((StreamSource) source); } else { throw new IllegalArgumentException("Unknown Source type: " + source.getClass()); } }
public void marshal(Object graph, Result result, MimeContainer mimeContainer) throws XmlMappingException { try { Marshaller marshaller = createMarshaller(); if (this.mtomEnabled && mimeContainer != null) { marshaller.setAttachmentMarshaller(new Jaxb2AttachmentMarshaller(mimeContainer)); } if (StaxUtils.isStaxResult(result)) { marshalStaxResult(marshaller, graph, result); } else { marshaller.marshal(graph, result); } } catch (JAXBException ex) { throw convertJaxbException(ex); } }
public Object unmarshal(Source source, MimeContainer mimeContainer) throws XmlMappingException { source = processSource(source); try { Unmarshaller unmarshaller = createUnmarshaller(); if (this.mtomEnabled && mimeContainer != null) { unmarshaller.setAttachmentUnmarshaller(new Jaxb2AttachmentUnmarshaller(mimeContainer)); } if (StaxUtils.isStaxSource(source)) { return unmarshalStaxSource(unmarshaller, source); } else if (this.mappedClass != null) { return unmarshaller.unmarshal(source, this.mappedClass).getValue(); } else { return unmarshaller.unmarshal(source); } } catch (JAXBException ex) { throw convertJaxbException(ex); } }
@Override protected Object unmarshalXmlEventReader(XMLEventReader eventReader) { try { XMLStreamReader streamReader = StaxUtils.createEventStreamReader(eventReader); return unmarshalXmlStreamReader(streamReader); } catch (XMLStreamException ex) { return new UnmarshallingFailureException("JiBX unmarshalling exception", ex); } }
private void marshalStaxResult(Marshaller jaxbMarshaller, Object graph, Result staxResult) throws JAXBException { XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(staxResult); if (streamWriter != null) { jaxbMarshaller.marshal(graph, streamWriter); } else { XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(staxResult); if (eventWriter != null) { jaxbMarshaller.marshal(graph, eventWriter); } else { throw new IllegalArgumentException("StAX Result contains neither XMLStreamWriter nor XMLEventConsumer"); } } }
@Override protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException { try { XMLStreamReader streamReader = StaxUtils.createEventStreamReader(eventReader); return unmarshalXmlStreamReader(streamReader); } catch (XMLStreamException ex) { throw convertXStreamException(ex, false); } }
@Override protected Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException { XMLReader reader = StaxUtils.createXMLReader(eventReader); try { return unmarshalSaxReader(reader, new InputSource()); } catch (IOException ex) { throw convertXmlBeansException(ex, false); } }
@Test public void unmarshalStaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); Source source = StaxUtils.createStaxSource(streamReader); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void unmarshalStaxSourceXmlEventReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING)); Source source = StaxUtils.createStaxSource(eventReader); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void unmarshalPartialStaxSourceXmlStreamReader() 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()); Source source = StaxUtils.createStaxSource(streamReader); Object flight = unmarshaller.unmarshal(source); testFlight(flight); }
@Test @Override @SuppressWarnings("unchecked") public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); streamReader.nextTag(); // skip to flights streamReader.nextTag(); // skip to flight Source source = StaxUtils.createStaxSource(streamReader); JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller.unmarshal(source); FlightType flight = element.getValue(); testFlight(flight); }
@Test public void marshalStaxResultXMLStreamWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); Result result = StaxUtils.createStaxResult(streamWriter); marshaller.marshal(flight, result); assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString()); }
@Test public void marshalStaxResultXMLEventWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer); Result result = StaxUtils.createStaxResult(eventWriter); marshaller.marshal(flight, result); assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString()); }
@Test public void unmarshalStaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); Source source = StaxUtils.createStaxSource(streamReader); Object flights = unmarshaller.unmarshal(source); testFlight(flights); }
@Test @Override public void unmarshalPartialStaxSourceXmlStreamReader() 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()); Source source = StaxUtils.createStaxSource(streamReader); Object flight = unmarshaller.unmarshal(source); testFlight(flight); }
@Test public void marshalStaxResultStreamWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); Result result = StaxUtils.createStaxResult(streamWriter); marshaller.marshal(flights, result); assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString()); }
@Test public void marshalStaxResultEventWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer); Result result = StaxUtils.createStaxResult(eventWriter); marshaller.marshal(flights, result); assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString()); }
@Override protected final Object unmarshalXmlEventReader(XMLEventReader eventReader) throws XmlMappingException { XMLReader reader = StaxUtils.createXMLReader(eventReader); try { return unmarshalSaxReader(reader, new InputSource()); } catch (IOException ex) { throw convertXmlBeansException(ex, false); } }
@Override public void unmarshalPartialStaxSourceXmlStreamReader() 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()); Source source = StaxUtils.createStaxSource(streamReader); Object flight = unmarshaller.unmarshal(source); testFlight(flight); }