/** * Convert the given {@code JAXBException} to an appropriate exception from the * {@code org.springframework.oxm} hierarchy. * @param ex {@code JAXBException} that occurred * @return the corresponding {@code XmlMappingException} */ protected XmlMappingException convertJaxbException(JAXBException ex) { if (ex instanceof ValidationException) { return new ValidationFailureException("JAXB validation exception", ex); } else if (ex instanceof MarshalException) { return new MarshallingFailureException("JAXB marshalling exception", ex); } else if (ex instanceof UnmarshalException) { return new UnmarshallingFailureException("JAXB unmarshalling exception", ex); } else { // fallback return new UncategorizedMappingException("Unknown JAXB exception", ex); } }
/** * Convert the given {@code JiBXException} to an appropriate exception from the * {@code org.springframework.oxm} hierarchy. * <p>A boolean flag is used to indicate whether this exception occurs during marshalling or * unmarshalling, since JiBX itself does not make this distinction in its exception hierarchy. * @param ex {@code JiBXException} that occurred * @param marshalling indicates whether the exception occurs during marshalling ({@code true}), * or unmarshalling ({@code false}) * @return the corresponding {@code XmlMappingException} */ public XmlMappingException convertJibxException(JiBXException ex, boolean marshalling) { if (ex instanceof ValidationException) { return new ValidationFailureException("JiBX validation exception", ex); } else { if (marshalling) { return new MarshallingFailureException("JiBX marshalling exception", ex); } else { return new UnmarshallingFailureException("JiBX unmarshalling exception", ex); } } }
/** * Convert the given XStream exception to an appropriate exception from the * {@code org.springframework.oxm} hierarchy. * <p>A boolean flag is used to indicate whether this exception occurs during marshalling or * unmarshalling, since XStream itself does not make this distinction in its exception hierarchy. * @param ex the XStream exception that occurred * @param marshalling indicates whether the exception occurs during marshalling ({@code true}), * or unmarshalling ({@code false}) * @return the corresponding {@code XmlMappingException} */ protected XmlMappingException convertXStreamException(Exception ex, boolean marshalling) { if (ex instanceof StreamException || ex instanceof CannotResolveClassException || ex instanceof ConversionException) { if (marshalling) { return new MarshallingFailureException("XStream marshalling exception", ex); } else { return new UnmarshallingFailureException("XStream unmarshalling exception", ex); } } else { // fallback return new UncategorizedMappingException("Unknown XStream exception", ex); } }
@Test public void unmarshalStreamSourceReader() throws Exception { StreamSource source = new StreamSource(new StringReader(INPUT_STRING)); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void marshalStreamResultWriter() throws Exception { StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); marshaller.marshal(flights, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
@Override protected void writeToResult(Object o, HttpHeaders headers, Result result) throws Exception { Assert.notNull(this.marshaller, "Property 'marshaller' is required"); this.marshaller.marshal(o, result); }
@Override protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) { return (supportsMimeType(message.getHeaders()) && this.unmarshaller != null && this.unmarshaller.supports(targetClass)); }
@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); } }
@Override protected Object unmarshalDomNode(Node node) throws XmlMappingException { try { return transformAndUnmarshal(new DOMSource(node), null); } catch (IOException ex) { throw new UnmarshallingFailureException("JiBX unmarshalling exception", ex); } }
@Before public final void setUp() throws Exception { marshaller = createMarshaller(); flights = createFlights(); }
@Before public final void setUp() throws Exception { unmarshaller = createUnmarshaller(); }
@Test public void unmarshalStreamSourceInputStream() throws Exception { StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8"))); Object flights = unmarshaller.unmarshal(source); testFlights(flights); }
@Test public void marshalStreamResultOutputStream() throws Exception { ByteArrayOutputStream os = new ByteArrayOutputStream(); StreamResult result = new StreamResult(os); marshaller.marshal(flights, result); assertThat("Marshaller writes invalid StreamResult", new String(os.toByteArray(), "UTF-8"), isSimilarTo(EXPECTED_STRING)); }
@Test public void unmarshalJaxp14StaxSourceXmlEventReader() 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); }
@Test public void marshalJaxp14StaxResultStreamWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); StAXResult result = new StAXResult(streamWriter); marshaller.marshal(flights, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
@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 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); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
@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 marshalJaxp14StaxResultEventWriter() throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); StringWriter writer = new StringWriter(); XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer); StAXResult result = new StAXResult(eventWriter); marshaller.marshal(flights, result); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING)); }
@Test public void unmarshalJaxp14StaxSourceXmlStreamReader() 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); }