Search in sources :

Example 21 with Document

use of org.w3c.dom.Document 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 22 with Document

use of org.w3c.dom.Document 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 23 with Document

use of org.w3c.dom.Document 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 24 with Document

use of org.w3c.dom.Document in project camel by apache.

the class CamelNamespaceHandler method doBeforeParse.

/**
     * Prepares the nodes before parsing.
     */
public static void doBeforeParse(Node node, String fromNamespace, String toNamespace) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Document doc = node.getOwnerDocument();
        if (node.getNamespaceURI().equals(fromNamespace)) {
            doc.renameNode(node, toNamespace, node.getLocalName());
        }
        // remove whitespace noise from uri, xxxUri attributes, eg new lines, and tabs etc, which allows end users to format
        // their Camel routes in more human readable format, but at runtime those attributes must be trimmed
        // the parser removes most of the noise, but keeps double spaces in the attribute values
        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            Node att = map.item(i);
            if (att.getNodeName().equals("uri") || att.getNodeName().endsWith("Uri")) {
                final String value = att.getNodeValue();
                String before = ObjectHelper.before(value, "?");
                String after = ObjectHelper.after(value, "?");
                if (before != null && after != null) {
                    // remove all double spaces in the uri parameters
                    String changed = after.replaceAll("\\s{2,}", "");
                    if (!after.equals(changed)) {
                        String newAtr = before.trim() + "?" + changed.trim();
                        LOG.debug("Removed whitespace noise from attribute {} -> {}", value, newAtr);
                        att.setNodeValue(newAtr);
                    }
                }
            }
        }
    }
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        doBeforeParse(list.item(i), fromNamespace, toNamespace);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Endpoint(org.apache.camel.Endpoint)

Example 25 with Document

use of org.w3c.dom.Document 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

Document (org.w3c.dom.Document)2446 Element (org.w3c.dom.Element)990 DocumentBuilder (javax.xml.parsers.DocumentBuilder)719 NodeList (org.w3c.dom.NodeList)648 Node (org.w3c.dom.Node)545 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)528 IOException (java.io.IOException)425 SAXException (org.xml.sax.SAXException)301 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)299 InputSource (org.xml.sax.InputSource)250 Test (org.junit.Test)233 File (java.io.File)190 StringReader (java.io.StringReader)182 ArrayList (java.util.ArrayList)174 InputStream (java.io.InputStream)167 DOMSource (javax.xml.transform.dom.DOMSource)161 ByteArrayInputStream (java.io.ByteArrayInputStream)154 Attr (org.w3c.dom.Attr)134 DOMException (org.w3c.dom.DOMException)129 XPath (javax.xml.xpath.XPath)107