Search in sources :

Example 16 with Document

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

the class XPathTest method testXPathWithDocumentTypeDOMSourceNoResultQName.

public void testXPathWithDocumentTypeDOMSourceNoResultQName() throws Exception {
    Document doc = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>");
    XPathBuilder builder = xpath("/foo");
    builder.setDocumentType(DOMSource.class);
    builder.setResultQName(null);
    Object result = builder.evaluate(createExchange(doc));
    assertNotNull(result);
    String s = context.getTypeConverter().convertTo(String.class, result);
    assertEquals("bar", s);
}
Also used : Document(org.w3c.dom.Document)

Example 17 with Document

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

the class XPathTest method testXPathSplitConcurrent.

public void testXPathSplitConcurrent() throws Exception {
    int size = 100;
    final Object node = XPathBuilder.xpath("foo/bar").nodeResult().evaluate(createExchange("<foo><bar>cheese</bar><bar>cake</bar><bar>beer</bar></foo>"));
    assertNotNull(node);
    // convert the node concurrently to test that XML Parser is not thread safe when
    // importing nodes to a new Document, so try a test for that
    final List<Document> result = new ArrayList<Document>();
    ExecutorService executor = Executors.newFixedThreadPool(size);
    final CountDownLatch latch = new CountDownLatch(size);
    for (int i = 0; i < size; i++) {
        executor.submit(new Callable<Document>() {

            public Document call() throws Exception {
                try {
                    Document doc = context.getTypeConverter().convertTo(Document.class, node);
                    result.add(doc);
                    return doc;
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    // give time to convert concurrently
    assertTrue(latch.await(20, TimeUnit.SECONDS));
    Iterator<Document> it = result.iterator();
    int count = 0;
    while (it.hasNext()) {
        count++;
        Document doc = it.next();
        assertNotNull(doc);
    }
    assertEquals(size, count);
    executor.shutdownNow();
}
Also used : ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Document(org.w3c.dom.Document) CountDownLatch(java.util.concurrent.CountDownLatch) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

Example 18 with Document

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

the class XPathTest method testXPathWithDocumentTypeDOMSource.

public void testXPathWithDocumentTypeDOMSource() throws Exception {
    Document doc = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>");
    XPathBuilder builder = xpath("/foo");
    builder.setDocumentType(DOMSource.class);
    Object result = builder.evaluate(createExchange(doc));
    assertNotNull(result);
    String s = context.getTypeConverter().convertTo(String.class, result);
    assertEquals("<foo>bar</foo>", s);
}
Also used : Document(org.w3c.dom.Document)

Example 19 with Document

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

the class XPathTest method testXPathNodeListSimpleTest.

public void testXPathNodeListSimpleTest() throws Exception {
    String xml = "<foo><person>Claus</person></foo>";
    Document doc = context.getTypeConverter().convertTo(Document.class, xml);
    Object result = xpath("/foo/person").nodeSetResult().evaluate(createExchange(doc));
    assertNotNull(result);
    String s = context.getTypeConverter().convertTo(String.class, result);
    assertEquals("<person>Claus</person>", s);
}
Also used : Document(org.w3c.dom.Document)

Example 20 with Document

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

the class DumpModelAsXmlSplitBodyRouteTest 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("body", node.getTextContent());
    nodes = doc.getElementsByTagName("split");
    assertEquals(1, nodes.getLength());
    nodes = doc.getElementsByTagName("to");
    assertEquals(1, nodes.getLength());
    node = (Element) nodes.item(0);
    assertNotNull("Node <to> expected to be instanceof Element", node);
    assertEquals("mock:sub", 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)

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