Search in sources :

Example 11 with NodeList

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

the class CamelNamespaceHandler method injectNamespaces.

protected void injectNamespaces(Element element, Binder<Node> binder) {
    NodeList list = element.getChildNodes();
    Namespaces namespaces = null;
    int size = list.getLength();
    for (int i = 0; i < size; i++) {
        Node child = list.item(i);
        if (child instanceof Element) {
            Element childElement = (Element) child;
            Object object = binder.getJAXBNode(child);
            if (object instanceof NamespaceAware) {
                NamespaceAware namespaceAware = (NamespaceAware) object;
                if (namespaces == null) {
                    namespaces = new Namespaces(element);
                }
                namespaces.configure(namespaceAware);
            }
            injectNamespaces(childElement, binder);
        }
    }
}
Also used : Namespaces(org.apache.camel.builder.xml.Namespaces) NamespaceAware(org.apache.camel.spi.NamespaceAware) NodeList(org.w3c.dom.NodeList) ExpressionNode(org.apache.camel.model.ExpressionNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Endpoint(org.apache.camel.Endpoint)

Example 12 with NodeList

use of org.w3c.dom.NodeList 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 13 with NodeList

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

the class BacklogTracerTest method testBacklogTracerEventMessageAsXml.

public void testBacklogTracerEventMessageAsXml() throws Exception {
    MBeanServer mbeanServer = getMBeanServer();
    ObjectName on = new ObjectName("org.apache.camel:context=camel-1,type=tracer,name=BacklogTracer");
    assertNotNull(on);
    mbeanServer.isRegistered(on);
    Boolean enabled = (Boolean) mbeanServer.getAttribute(on, "Enabled");
    assertEquals("Should not be enabled", Boolean.FALSE, enabled);
    Integer size = (Integer) mbeanServer.getAttribute(on, "BacklogSize");
    assertEquals("Should be 1000", 1000, size.intValue());
    // enable it
    mbeanServer.setAttribute(on, new Attribute("Enabled", Boolean.TRUE));
    getMockEndpoint("mock:foo").expectedMessageCount(2);
    getMockEndpoint("mock:bar").expectedMessageCount(2);
    template.sendBody("direct:start", "Hello World");
    template.sendBody("direct:start", "Bye World");
    assertMockEndpointsSatisfied();
    String events = (String) mbeanServer.invoke(on, "dumpTracedMessagesAsXml", new Object[] { "foo" }, new String[] { "java.lang.String" });
    assertNotNull(events);
    log.info(events);
    // should be valid XML
    Document dom = context.getTypeConverter().convertTo(Document.class, events);
    assertNotNull(dom);
    NodeList list = dom.getElementsByTagName("backlogTracerEventMessage");
    assertEquals(2, list.getLength());
}
Also used : Attribute(javax.management.Attribute) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 14 with NodeList

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

the class BacklogTracerTest method testBacklogTracerEventMessageDumpAllAsXml.

public void testBacklogTracerEventMessageDumpAllAsXml() throws Exception {
    MBeanServer mbeanServer = getMBeanServer();
    ObjectName on = new ObjectName("org.apache.camel:context=camel-1,type=tracer,name=BacklogTracer");
    assertNotNull(on);
    mbeanServer.isRegistered(on);
    Boolean enabled = (Boolean) mbeanServer.getAttribute(on, "Enabled");
    assertEquals("Should not be enabled", Boolean.FALSE, enabled);
    // enable it
    mbeanServer.setAttribute(on, new Attribute("Enabled", Boolean.TRUE));
    getMockEndpoint("mock:foo").expectedMessageCount(2);
    getMockEndpoint("mock:bar").expectedMessageCount(2);
    template.sendBody("direct:start", "Hello World");
    template.sendBody("direct:start", "Bye World");
    assertMockEndpointsSatisfied();
    String events = (String) mbeanServer.invoke(on, "dumpAllTracedMessagesAsXml", null, null);
    assertNotNull(events);
    log.info(events);
    // should be valid XML
    Document dom = context.getTypeConverter().convertTo(Document.class, events);
    assertNotNull(dom);
    NodeList list = dom.getElementsByTagName("backlogTracerEventMessage");
    assertEquals(6, list.getLength());
}
Also used : Attribute(javax.management.Attribute) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 15 with NodeList

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

the class TransformXpathTest method testTransformWithXpath.

public void testTransformWithXpath() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.message(0).body().isInstanceOf(NodeList.class);
    String xml = context.getTypeConverter().convertTo(String.class, new File("src/test/resources/org/apache/camel/processor/students.xml"));
    template.sendBody("direct:start", xml);
    assertMockEndpointsSatisfied();
    NodeList list = mock.getReceivedExchanges().get(0).getIn().getBody(NodeList.class);
    assertEquals(2, list.getLength());
    assertEquals("Claus", context.getTypeConverter().convertTo(String.class, list.item(0).getTextContent().trim()));
    assertEquals("Hadrian", context.getTypeConverter().convertTo(String.class, list.item(1).getTextContent().trim()));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) NodeList(org.w3c.dom.NodeList) File(java.io.File)

Aggregations

NodeList (org.w3c.dom.NodeList)1806 Node (org.w3c.dom.Node)1059 Element (org.w3c.dom.Element)902 Document (org.w3c.dom.Document)636 ArrayList (java.util.ArrayList)314 DocumentBuilder (javax.xml.parsers.DocumentBuilder)268 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)208 IOException (java.io.IOException)183 NamedNodeMap (org.w3c.dom.NamedNodeMap)144 InputSource (org.xml.sax.InputSource)131 HashMap (java.util.HashMap)121 Test (org.junit.Test)117 SAXException (org.xml.sax.SAXException)117 StringReader (java.io.StringReader)106 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)100 XPath (javax.xml.xpath.XPath)99 Attr (org.w3c.dom.Attr)80 XPathExpressionException (javax.xml.xpath.XPathExpressionException)76 File (java.io.File)64 HashSet (java.util.HashSet)59