Search in sources :

Example 16 with Node

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

the class XPathBuilder method logDiscoveredNamespaces.

private void logDiscoveredNamespaces(NodeList namespaces) {
    Map<String, HashSet<String>> map = new LinkedHashMap<String, HashSet<String>>();
    for (int i = 0; i < namespaces.getLength(); i++) {
        Node n = namespaces.item(i);
        if (n.getNodeName().equals("xmlns:xml")) {
            // skip the implicit XML namespace as it provides no value
            continue;
        }
        String prefix = namespaces.item(i).getNodeName();
        if (prefix.equals("xmlns")) {
            prefix = "DEFAULT";
        }
        // add to map
        if (!map.containsKey(prefix)) {
            map.put(prefix, new HashSet<String>());
        }
        map.get(prefix).add(namespaces.item(i).getNodeValue());
    }
    LOG.info("Namespaces discovered in message: {}.", map);
}
Also used : Node(org.w3c.dom.Node) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Example 17 with Node

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

the class Namespaces method add.

public Namespaces add(Element element) {
    // let's set the parent first in case we overload a prefix here
    Node parentNode = element.getParentNode();
    if (parentNode instanceof org.w3c.dom.Element) {
        add((Element) parentNode);
    }
    NamedNodeMap attributes = element.getAttributes();
    int size = attributes.getLength();
    for (int i = 0; i < size; i++) {
        Attr node = (Attr) attributes.item(i);
        String name = node.getName();
        if (name.startsWith("xmlns:")) {
            String prefix = name.substring("xmlns:".length());
            String uri = node.getValue();
            add(prefix, uri);
        }
    }
    return this;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Example 18 with Node

use of org.w3c.dom.Node 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 node the xml node
     * @throws Exception is thrown if an error is encountered unmarshalling from xml to model
     */
public static RoutesDefinition loadRoutesDefinition(CamelContext context, Node node) throws Exception {
    JAXBContext jaxbContext = getJAXBContext(context);
    Map<String, String> namespaces = new LinkedHashMap<>();
    Document dom = node instanceof Document ? (Document) node : node.getOwnerDocument();
    extractNamespaces(dom, namespaces);
    Binder<Node> binder = jaxbContext.createBinder();
    Object result = binder.unmarshal(node);
    if (result == null) {
        throw new JAXBException("Cannot unmarshal to RoutesDefinition using JAXB");
    }
    // can either be routes or a single route
    RoutesDefinition answer;
    if (result instanceof RouteDefinition) {
        RouteDefinition route = (RouteDefinition) result;
        answer = new RoutesDefinition();
        applyNamespaces(route, namespaces);
        answer.getRoutes().add(route);
    } else if (result instanceof RoutesDefinition) {
        answer = (RoutesDefinition) result;
        for (RouteDefinition route : answer.getRoutes()) {
            applyNamespaces(route, namespaces);
        }
    } else {
        throw new IllegalArgumentException("Unmarshalled object is an unsupported type: " + ObjectHelper.className(result) + " -> " + result);
    }
    return answer;
}
Also used : 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) LinkedHashMap(java.util.LinkedHashMap)

Example 19 with Node

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

the class DomConverter method toString.

@Converter
public String toString(NodeList nodeList, Exchange exchange) throws TransformerException {
    // converting NodeList to String is more tricky
    // sometimes the NodeList is a Node which we can then leverage
    // the XML converter to turn into XML incl. tags
    StringBuilder buffer = new StringBuilder();
    // use XML converter at first since it preserves tag names
    boolean found = false;
    if (nodeList instanceof Node) {
        Node node = (Node) nodeList;
        String s = toString(node, exchange);
        if (ObjectHelper.isNotEmpty(s)) {
            found = true;
            buffer.append(s);
        }
    } else {
        // use XML converter at first since it preserves tag names
        int size = nodeList.getLength();
        for (int i = 0; i < size; i++) {
            Node node = nodeList.item(i);
            String s = toString(node, exchange);
            if (ObjectHelper.isNotEmpty(s)) {
                found = true;
                buffer.append(s);
            }
        }
    }
    // used an xpath to select an attribute or text() or something
    if (!found) {
        append(buffer, nodeList);
    }
    return buffer.toString();
}
Also used : Node(org.w3c.dom.Node) Converter(org.apache.camel.Converter)

Example 20 with Node

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

the class XmlLineNumberParserTest method testParseCamelContextForceNamespace.

public void testParseCamelContextForceNamespace() throws Exception {
    FileInputStream fis = new FileInputStream("src/test/resources/org/apache/camel/util/camel-context.xml");
    Document dom = XmlLineNumberParser.parseXml(fis, null, "camelContext", "http://camel.apache.org/schema/spring");
    assertNotNull(dom);
    NodeList list = dom.getElementsByTagName("camelContext");
    assertEquals(1, list.getLength());
    Node node = list.item(0);
    String lineNumber = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER);
    String lineNumberEnd = (String) node.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
    String ns = node.getNamespaceURI();
    assertEquals("http://camel.apache.org/schema/spring", ns);
    assertEquals("28", lineNumber);
    assertEquals("46", lineNumberEnd);
    // and there are two routes
    list = dom.getElementsByTagName("route");
    assertEquals(2, list.getLength());
    Node node1 = list.item(0);
    Node node2 = list.item(1);
    String lineNumber1 = (String) node1.getUserData(XmlLineNumberParser.LINE_NUMBER);
    String lineNumberEnd1 = (String) node1.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
    assertEquals("30", lineNumber1);
    assertEquals("36", lineNumberEnd1);
    String lineNumber2 = (String) node2.getUserData(XmlLineNumberParser.LINE_NUMBER);
    String lineNumberEnd2 = (String) node2.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
    assertEquals("38", lineNumber2);
    assertEquals("44", lineNumberEnd2);
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream)

Aggregations

Node (org.w3c.dom.Node)2347 NodeList (org.w3c.dom.NodeList)1062 Element (org.w3c.dom.Element)720 Document (org.w3c.dom.Document)545 NamedNodeMap (org.w3c.dom.NamedNodeMap)333 ArrayList (java.util.ArrayList)318 DocumentBuilder (javax.xml.parsers.DocumentBuilder)202 IOException (java.io.IOException)176 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)147 Test (org.junit.Test)132 HashMap (java.util.HashMap)127 Attr (org.w3c.dom.Attr)126 SAXException (org.xml.sax.SAXException)107 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)98 HashSet (java.util.HashSet)86 InputSource (org.xml.sax.InputSource)75 XPath (javax.xml.xpath.XPath)70 List (java.util.List)67 File (java.io.File)62 ByteArrayInputStream (java.io.ByteArrayInputStream)57