Search in sources :

Example 1 with MarshallingFailureException

use of org.springframework.oxm.MarshallingFailureException in project spring-framework by spring-projects.

the class JibxMarshaller method transformAndMarshal.

private void transformAndMarshal(Object graph, Result result) throws IOException {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        marshalOutputStream(graph, os);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        Transformer transformer = this.transformerFactory.newTransformer();
        transformer.transform(new StreamSource(is), result);
    } catch (TransformerException ex) {
        throw new MarshallingFailureException("Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) MarshallingFailureException(org.springframework.oxm.MarshallingFailureException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerException(javax.xml.transform.TransformerException)

Example 2 with MarshallingFailureException

use of org.springframework.oxm.MarshallingFailureException in project spring-framework by spring-projects.

the class JibxMarshaller method marshalDomNode.

// Unsupported marshalling
@Override
protected void marshalDomNode(Object graph, Node node) throws XmlMappingException {
    try {
        // JiBX does not support DOM natively, so we write to a buffer first, and transform that to the Node
        Result result = new DOMResult(node);
        transformAndMarshal(graph, result);
    } catch (IOException ex) {
        throw new MarshallingFailureException("JiBX marshalling exception", ex);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) MarshallingFailureException(org.springframework.oxm.MarshallingFailureException) IOException(java.io.IOException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) DOMResult(javax.xml.transform.dom.DOMResult)

Example 3 with MarshallingFailureException

use of org.springframework.oxm.MarshallingFailureException in project spring-framework by spring-projects.

the class JibxMarshaller method transformAndUnmarshal.

private Object transformAndUnmarshal(Source source, String encoding) throws IOException {
    try {
        Transformer transformer = this.transformerFactory.newTransformer();
        if (encoding != null) {
            transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        transformer.transform(source, new StreamResult(os));
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        return unmarshalInputStream(is);
    } catch (TransformerException ex) {
        throw new MarshallingFailureException("Could not transform from [" + ClassUtils.getShortName(source.getClass()) + "]", ex);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) MarshallingFailureException(org.springframework.oxm.MarshallingFailureException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerException(javax.xml.transform.TransformerException)

Example 4 with MarshallingFailureException

use of org.springframework.oxm.MarshallingFailureException in project spring-framework by spring-projects.

the class JibxMarshaller method marshalSaxHandlers.

@Override
protected void marshalSaxHandlers(Object graph, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XmlMappingException {
    try {
        // JiBX does not support SAX natively, so we write to a buffer first, and transform that to the handlers
        SAXResult saxResult = new SAXResult(contentHandler);
        saxResult.setLexicalHandler(lexicalHandler);
        transformAndMarshal(graph, saxResult);
    } catch (IOException ex) {
        throw new MarshallingFailureException("JiBX marshalling exception", ex);
    }
}
Also used : SAXResult(javax.xml.transform.sax.SAXResult) MarshallingFailureException(org.springframework.oxm.MarshallingFailureException) IOException(java.io.IOException)

Example 5 with MarshallingFailureException

use of org.springframework.oxm.MarshallingFailureException in project spring-integration-samples by spring-projects.

the class WeatherMarshaller method unmarshal.

public Object unmarshal(Source source) throws IOException, XmlMappingException {
    // this.writeXml(((DOMSource)source).getNode().getOwnerDocument());
    DOMResult result = null;
    try {
        Transformer transformer = transformerFactory.newTransformer();
        result = new DOMResult();
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new MarshallingFailureException("Failed to unmarshal SOAP Response", e);
    }
    Weather weather = new Weather();
    String expression = xpathPrefix + "p:City";
    String city = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode());
    weather.setCity(city);
    expression = xpathPrefix + "p:State";
    String state = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode());
    weather.setState(state);
    expression = xpathPrefix + "p:Temperature";
    String temperature = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode());
    weather.setTemperature(temperature);
    expression = xpathPrefix + "p:Description";
    String description = XPathExpressionFactory.createXPathExpression(expression, namespacePrefixes).evaluateAsString(result.getNode());
    weather.setDescription(description);
    return weather;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) Transformer(javax.xml.transform.Transformer) MarshallingFailureException(org.springframework.oxm.MarshallingFailureException) MarshallingFailureException(org.springframework.oxm.MarshallingFailureException) IOException(java.io.IOException) XmlMappingException(org.springframework.oxm.XmlMappingException)

Aggregations

MarshallingFailureException (org.springframework.oxm.MarshallingFailureException)6 IOException (java.io.IOException)3 Transformer (javax.xml.transform.Transformer)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Result (javax.xml.transform.Result)2 TransformerException (javax.xml.transform.TransformerException)2 DOMResult (javax.xml.transform.dom.DOMResult)2 SAXResult (javax.xml.transform.sax.SAXResult)2 StreamResult (javax.xml.transform.stream.StreamResult)2 StreamSource (javax.xml.transform.stream.StreamSource)1 Test (org.junit.jupiter.api.Test)1 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)1 Marshaller (org.springframework.oxm.Marshaller)1 XmlMappingException (org.springframework.oxm.XmlMappingException)1