Search in sources :

Example 31 with Document

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

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

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

the class XmlConverter method toDOMSource.

@Converter
public DOMSource toDOMSource(InputStream is, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
    InputSource source = new InputSource(is);
    String systemId = source.getSystemId();
    DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
    Document document = builder.parse(source);
    return new DOMSource(document, systemId);
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) Converter(org.apache.camel.Converter)

Example 34 with Document

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

the class XmlConverter method toDOMSourceFromStream.

@Converter
public DOMSource toDOMSourceFromStream(StreamSource source, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
    Document document;
    String systemId = source.getSystemId();
    DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
    Reader reader = source.getReader();
    if (reader != null) {
        document = builder.parse(new InputSource(reader));
    } else {
        InputStream inputStream = source.getInputStream();
        if (inputStream != null) {
            InputSource inputsource = new InputSource(inputStream);
            inputsource.setSystemId(systemId);
            document = builder.parse(inputsource);
        } else {
            throw new IOException("No input stream or reader available on StreamSource: " + source);
        }
    }
    return new DOMSource(document, systemId);
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) Converter(org.apache.camel.Converter)

Example 35 with Document

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

the class ManagedCamelContext method dumpRoutesAsXml.

@Override
public String dumpRoutesAsXml(boolean resolvePlaceholders) throws Exception {
    List<RouteDefinition> routes = context.getRouteDefinitions();
    if (routes.isEmpty()) {
        return null;
    }
    // use a routes definition to dump the routes
    RoutesDefinition def = new RoutesDefinition();
    def.setRoutes(routes);
    String xml = ModelHelper.dumpModelAsXml(context, def);
    // if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
    if (resolvePlaceholders) {
        final AtomicBoolean changed = new AtomicBoolean();
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {

            @Override
            public String transform(String text) {
                try {
                    String after = getContext().resolvePropertyPlaceholders(text);
                    if (!changed.get()) {
                        changed.set(!text.equals(after));
                    }
                    return after;
                } catch (Exception e) {
                    // ignore
                    return text;
                }
            }
        });
        // okay there were some property placeholder replaced so re-create the model
        if (changed.get()) {
            xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
            RoutesDefinition copy = ModelHelper.createModelFromXml(context, xml, RoutesDefinition.class);
            xml = ModelHelper.dumpModelAsXml(context, copy);
        }
    }
    return xml;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RouteDefinition(org.apache.camel.model.RouteDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RoutesDefinition(org.apache.camel.model.RoutesDefinition) Document(org.w3c.dom.Document) XmlLineNumberParser(org.apache.camel.util.XmlLineNumberParser) IOException(java.io.IOException)

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