Search in sources :

Example 26 with XmlConverter

use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.

the class ModelHelper method modelToXml.

private static <T extends NamedNode> T modelToXml(CamelContext context, InputStream is, String xml, Class<T> type) throws JAXBException {
    JAXBContext jaxbContext = getJAXBContext(context);
    XmlConverter xmlConverter = newXmlConverter(context);
    Document dom = null;
    try {
        if (is != null) {
            dom = xmlConverter.toDOMDocument(is, null);
        } else if (xml != null) {
            dom = xmlConverter.toDOMDocument(xml, null);
        }
    } catch (Exception e) {
        throw new TypeConversionException(xml, Document.class, e);
    }
    if (dom == null) {
        throw new IllegalArgumentException("InputStream and XML is both null");
    }
    Map<String, String> namespaces = new LinkedHashMap<>();
    extractNamespaces(dom, namespaces);
    Binder<Node> binder = jaxbContext.createBinder();
    Object result = binder.unmarshal(dom);
    if (result == null) {
        throw new JAXBException("Cannot unmarshal to " + type + " using JAXB");
    }
    // Restore namespaces to anything that's NamespaceAware
    if (result instanceof RoutesDefinition) {
        List<RouteDefinition> routes = ((RoutesDefinition) result).getRoutes();
        for (RouteDefinition route : routes) {
            applyNamespaces(route, namespaces);
        }
    } else if (result instanceof RouteDefinition) {
        RouteDefinition route = (RouteDefinition) result;
        applyNamespaces(route, namespaces);
    }
    return type.cast(result);
}
Also used : TypeConversionException(org.apache.camel.TypeConversionException) Node(org.w3c.dom.Node) NamedNode(org.apache.camel.NamedNode) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) JAXBException(javax.xml.bind.JAXBException) TypeConversionException(org.apache.camel.TypeConversionException) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) LinkedHashMap(java.util.LinkedHashMap)

Example 27 with XmlConverter

use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.

the class CxfConsumerProviderTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            errorHandler(noErrorHandler());
            from(getFromEndpointUri()).process(new Processor() {

                public void process(final Exchange exchange) {
                    Message in = exchange.getIn();
                    Node node = in.getBody(Node.class);
                    assertNotNull(node);
                    XmlConverter xmlConverter = new XmlConverter();
                    // Put the result back
                    exchange.getOut().setBody(xmlConverter.toSource(RESPONSE));
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(org.apache.camel.Message) Node(org.w3c.dom.Node) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 28 with XmlConverter

use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.

the class XsltBuilderTest method testXsltSetConverter.

public void testXsltSetConverter() throws Exception {
    URL styleSheet = getClass().getResource("example.xsl");
    XsltBuilder builder = XsltBuilder.xslt(styleSheet);
    XmlConverter converter = new XmlConverter();
    builder.setConverter(converter);
    assertSame(converter, builder.getConverter());
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("<hello>world!</hello>");
    builder.process(exchange);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>", exchange.getOut().getBody());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) URL(java.net.URL) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 29 with XmlConverter

use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.

the class MyCxfCustomerConverter method cxfPayloadToString.

@Converter
public static String cxfPayloadToString(final CxfPayload<?> payload) {
    XmlConverter converter = new XmlConverter();
    StringBuilder buf = new StringBuilder();
    for (Object element : payload.getBody()) {
        String elementString = "";
        try {
            elementString = converter.toString((Element) element, null);
        } catch (TransformerException e) {
            elementString = element.toString();
        }
        buf.append(elementString);
    }
    return buf.toString();
}
Also used : Element(org.w3c.dom.Element) TransformerException(javax.xml.transform.TransformerException) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) Converter(org.apache.camel.Converter)

Aggregations

XmlConverter (org.apache.camel.converter.jaxp.XmlConverter)29 Element (org.w3c.dom.Element)14 Exchange (org.apache.camel.Exchange)10 Document (org.w3c.dom.Document)10 Source (javax.xml.transform.Source)9 Processor (org.apache.camel.Processor)7 RouteBuilder (org.apache.camel.builder.RouteBuilder)6 NodeList (org.w3c.dom.NodeList)4 StringReader (java.io.StringReader)3 List (java.util.List)3 TransformerException (javax.xml.transform.TransformerException)3 DOMSource (javax.xml.transform.dom.DOMSource)3 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 JAXBContext (javax.xml.bind.JAXBContext)2