Search in sources :

Example 51 with Document

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

the class ManagedRouteDumpStatsAsXmlCustomDomainTest method testPerformanceCounterStats.

public void testPerformanceCounterStats() throws Exception {
    // JMX tests dont work well on AIX CI servers (hangs them)
    if (isPlatform("aix")) {
        return;
    }
    // get the stats for the route
    MBeanServer mbeanServer = getMBeanServer();
    ObjectName on = ObjectName.getInstance(CUSTOM_DOMAIN_NAME + ":context=camel-1,type=routes,name=\"foo\"");
    getMockEndpoint("mock:result").expectedMessageCount(1);
    template.asyncSendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    String xml = (String) mbeanServer.invoke(on, "dumpRouteStatsAsXml", new Object[] { false, true }, new String[] { "boolean", "boolean" });
    log.info(xml);
    // should be valid XML
    Document doc = context.getTypeConverter().convertTo(Document.class, xml);
    assertNotNull(doc);
    int processors = doc.getDocumentElement().getElementsByTagName("processorStat").getLength();
    assertEquals(3, processors);
}
Also used : Document(org.w3c.dom.Document) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 52 with Document

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

the class ManagedRouteDumpStatsAsXmlTest method testPerformanceCounterStats.

public void testPerformanceCounterStats() throws Exception {
    // JMX tests dont work well on AIX CI servers (hangs them)
    if (isPlatform("aix")) {
        return;
    }
    // get the stats for the route
    MBeanServer mbeanServer = getMBeanServer();
    ObjectName on = ObjectName.getInstance("org.apache.camel:context=camel-1,type=routes,name=\"foo\"");
    getMockEndpoint("mock:result").expectedMessageCount(1);
    template.asyncSendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    String xml = (String) mbeanServer.invoke(on, "dumpRouteStatsAsXml", new Object[] { false, true }, new String[] { "boolean", "boolean" });
    log.info(xml);
    // should be valid XML
    Document doc = context.getTypeConverter().convertTo(Document.class, xml);
    assertNotNull(doc);
    int processors = doc.getDocumentElement().getElementsByTagName("processorStat").getLength();
    assertEquals(3, processors);
}
Also used : Document(org.w3c.dom.Document) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 53 with Document

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

Example 54 with Document

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

the class XmlLineNumberParserTest method testParse.

public void testParse() throws Exception {
    FileInputStream fis = new FileInputStream("src/test/resources/org/apache/camel/util/camel-context.xml");
    Document dom = XmlLineNumberParser.parseXml(fis);
    assertNotNull(dom);
    NodeList list = dom.getElementsByTagName("beans");
    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);
    assertEquals("23", lineNumber);
    assertEquals("48", lineNumberEnd);
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream)

Example 55 with Document

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

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