Search in sources :

Example 6 with XmlConverter

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

the class DumpModelAsXmlTransformRouteConstantTest method testDumpModelAsXml.

public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);
    Document doc = new XmlConverter().toDOMDocument(xml);
    NodeList nodes = doc.getElementsByTagName("constant");
    assertEquals(1, nodes.getLength());
    Element node = (Element) nodes.item(0);
    assertNotNull("Node <simple> expected to be instanceof Element", node);
    assertEquals("Hello World", node.getTextContent());
    nodes = doc.getElementsByTagName("to");
    assertEquals(1, nodes.getLength());
    node = (Element) nodes.item(0);
    assertNotNull("Node <to> expected to be instanceof Element", node);
    assertEquals("mock:result", node.getAttribute("uri"));
    assertEquals("myMock", node.getAttribute("id"));
    assertEquals("true", node.getAttribute("customId"));
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 7 with XmlConverter

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

the class DumpModelAsXmlTransformRouteLanguageTest method testDumpModelAsXml.

public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);
    Document doc = new XmlConverter().toDOMDocument(xml);
    NodeList nodes = doc.getElementsByTagName("language");
    assertEquals(1, nodes.getLength());
    Element node = (Element) nodes.item(0);
    assertNotNull("Node <simple> expected to be instanceof Element", node);
    assertEquals("constant", node.getAttribute("language"));
    assertEquals("Hello World", node.getTextContent());
    nodes = doc.getElementsByTagName("to");
    assertEquals(1, nodes.getLength());
    node = (Element) nodes.item(0);
    assertNotNull("Node <to> expected to be instanceof Element", node);
    assertEquals("mock:result", node.getAttribute("uri"));
    assertEquals("myMock", node.getAttribute("id"));
    assertEquals("true", node.getAttribute("customId"));
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 8 with XmlConverter

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

the class DumpModelAsXmlTransformRouteTest method testDumpModelAsXml.

public void testDumpModelAsXml() throws Exception {
    String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
    assertNotNull(xml);
    log.info(xml);
    Document doc = new XmlConverter().toDOMDocument(xml);
    NodeList nodes = doc.getElementsByTagName("simple");
    assertEquals(1, nodes.getLength());
    Element node = (Element) nodes.item(0);
    assertNotNull("Node <simple> expected to be instanceof Element", node);
    assertEquals("Hello ${body}", node.getTextContent());
    nodes = doc.getElementsByTagName("to");
    assertEquals(1, nodes.getLength());
    node = (Element) nodes.item(0);
    assertNotNull("Node <to> expected to be instanceof Element", node);
    assertEquals("mock:result", node.getAttribute("uri"));
    assertEquals("myMock", node.getAttribute("id"));
    assertEquals("true", node.getAttribute("customId"));
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 9 with XmlConverter

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

the class CacheBasedXPathReplacer method process.

public void process(Exchange exchange) throws Exception {
    String cacheKey = key.evaluate(exchange, String.class);
    if (isValid(cacheManager, cacheName, cacheKey)) {
        Ehcache cache = cacheManager.getCache(cacheName);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Replacing XPath value {} in Message with value stored against key {} in CacheName {}", new Object[] { xpath, cacheKey, cacheName });
        }
        exchange.getIn().setHeader(CacheConstants.CACHE_KEY, cacheKey);
        Object body = exchange.getIn().getBody();
        InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
        Document document;
        try {
            document = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, is);
        } finally {
            IOHelper.close(is, "is", LOG);
        }
        InputStream cis = exchange.getContext().getTypeConverter().convertTo(InputStream.class, cache.get(cacheKey).getObjectValue());
        try {
            Document cacheValueDocument = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, cis);
            // Create/setup the Transformer
            XmlConverter xmlConverter = new XmlConverter();
            String xslString = IOConverter.toString(new File("./src/main/resources/xpathreplacer.xsl"), exchange);
            xslString = xslString.replace("##match_token##", xpath);
            Source xslSource = xmlConverter.toStreamSource(new StringReader(xslString));
            TransformerFactory transformerFactory = xmlConverter.createTransformerFactory();
            Transformer transformer = transformerFactory.newTransformer(xslSource);
            DOMSource source = xmlConverter.toDOMSource(document);
            DOMResult result = new DOMResult();
            transformer.setParameter("cacheValue", cacheValueDocument);
            transformer.transform(source, result);
            // DOMSource can be converted to byte[] by camel type converter mechanism
            DOMSource dom = new DOMSource(result.getNode());
            exchange.getIn().setBody(dom, byte[].class);
        } finally {
            IOHelper.close(cis, "cis", LOG);
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) InputStream(java.io.InputStream) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) StringReader(java.io.StringReader) Ehcache(net.sf.ehcache.Ehcache) File(java.io.File)

Example 10 with XmlConverter

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

the class ModelHelper method loadRoutesDefinition.

/**
     * Marshal the xml to the model definition
     *
     * @param context the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use
     * @param inputStream the xml stream
     * @throws Exception is thrown if an error is encountered unmarshalling from xml to model
     */
public static RoutesDefinition loadRoutesDefinition(CamelContext context, InputStream inputStream) throws Exception {
    XmlConverter xmlConverter = newXmlConverter(context);
    Document dom = xmlConverter.toDOMDocument(inputStream, null);
    return loadRoutesDefinition(context, dom);
}
Also used : Document(org.w3c.dom.Document) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

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