Search in sources :

Example 26 with Document

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

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

the class ModelHelper method dumpModelAsXml.

/**
     * Dumps the definition as XML
     *
     * @param context    the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use
     * @param definition the definition, such as a {@link org.apache.camel.NamedNode}
     * @return the output in XML (is formatted)
     * @throws JAXBException is throw if error marshalling to XML
     */
public static String dumpModelAsXml(CamelContext context, NamedNode definition) throws JAXBException {
    JAXBContext jaxbContext = getJAXBContext(context);
    final Map<String, String> namespaces = new LinkedHashMap<>();
    // gather all namespaces from the routes or route which is stored on the expression nodes
    if (definition instanceof RoutesDefinition) {
        List<RouteDefinition> routes = ((RoutesDefinition) definition).getRoutes();
        for (RouteDefinition route : routes) {
            extractNamespaces(route, namespaces);
        }
    } else if (definition instanceof RouteDefinition) {
        RouteDefinition route = (RouteDefinition) definition;
        extractNamespaces(route, namespaces);
    }
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter buffer = new StringWriter();
    marshaller.marshal(definition, buffer);
    XmlConverter xmlConverter = newXmlConverter(context);
    String xml = buffer.toString();
    Document dom;
    try {
        dom = xmlConverter.toDOMDocument(xml, null);
    } catch (Exception e) {
        throw new TypeConversionException(xml, Document.class, e);
    }
    // Add additional namespaces to the document root element
    Element documentElement = dom.getDocumentElement();
    for (String nsPrefix : namespaces.keySet()) {
        String prefix = nsPrefix.equals("xmlns") ? nsPrefix : "xmlns:" + nsPrefix;
        documentElement.setAttribute(prefix, namespaces.get(nsPrefix));
    }
    // We invoke the type converter directly because we need to pass some custom XML output options
    Properties outputProperties = new Properties();
    outputProperties.put(OutputKeys.INDENT, "yes");
    outputProperties.put(OutputKeys.STANDALONE, "yes");
    try {
        return xmlConverter.toStringFromDocument(dom, outputProperties);
    } catch (TransformerException e) {
        throw new IllegalStateException("Failed converting document object to string", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) TypeConversionException(org.apache.camel.TypeConversionException) Element(org.w3c.dom.Element) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException) JAXBException(javax.xml.bind.JAXBException) TypeConversionException(org.apache.camel.TypeConversionException) LinkedHashMap(java.util.LinkedHashMap) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) StringWriter(java.io.StringWriter) TransformerException(javax.xml.transform.TransformerException)

Example 28 with Document

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

the class XPathToFileTest method testXPathToFile.

public void testXPathToFile() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(2);
    String xml = "<foo><person id=\"1\">Claus<country>SE</country></person>" + "<person id=\"2\">Jonathan<country>CA</country></person></foo>";
    Document doc = context.getTypeConverter().convertTo(Document.class, xml);
    template.sendBody("direct:start", doc);
    assertMockEndpointsSatisfied();
    File first = new File("target/xpath/xpath-0.xml");
    assertTrue("File xpath-0.xml should exists", first.exists());
    assertEquals("<person id=\"1\">Claus<country>SE</country></person>", context.getTypeConverter().convertTo(String.class, first));
    File second = new File("target/xpath/xpath-1.xml");
    assertTrue("File xpath-1.xml should exists", second.exists());
    assertEquals("<person id=\"2\">Jonathan<country>CA</country></person>", context.getTypeConverter().convertTo(String.class, second));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Document(org.w3c.dom.Document) File(java.io.File)

Example 29 with Document

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

the class XsltRouteTest method testSendDomMessage.

public void testSendDomMessage() throws Exception {
    XmlConverter converter = new XmlConverter();
    Document body = converter.toDOMDocument("<mail><subject>Hey</subject><body>Hello world!</body></mail>");
    sendMessageAndHaveItTransformed(body);
}
Also used : Document(org.w3c.dom.Document) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 30 with Document

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

the class ManagedCamelContextDumpRoutesCoverageAsXml method testRouteCoverageStats.

public void testRouteCoverageStats() 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=context,name=\"camel-1\"");
    getMockEndpoint("mock:foo").expectedMessageCount(1);
    getMockEndpoint("mock:bar").expectedMessageCount(2);
    template.asyncSendBody("direct:start", "Hello World");
    template.asyncSendBody("direct:bar", "Hi World");
    template.asyncSendBody("direct:bar", "Bye World");
    assertMockEndpointsSatisfied();
    String xml = (String) mbeanServer.invoke(on, "dumpRoutesCoverageAsXml", null, null);
    log.info(xml);
    assertTrue(xml.contains("exchangesTotal=\"3\""));
    assertTrue(xml.contains("exchangesTotal=\"2\""));
    // should be valid XML
    Document doc = context.getTypeConverter().convertTo(Document.class, xml);
    assertNotNull(doc);
}
Also used : Document(org.w3c.dom.Document) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

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