Search in sources :

Example 36 with Document

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

the class ManagedCamelContext method dumpRestsAsXml.

@Override
public String dumpRestsAsXml(boolean resolvePlaceholders) throws Exception {
    List<RestDefinition> rests = context.getRestDefinitions();
    if (rests.isEmpty()) {
        return null;
    }
    // use a routes definition to dump the rests
    RestsDefinition def = new RestsDefinition();
    def.setRests(rests);
    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);
            RestsDefinition copy = ModelHelper.createModelFromXml(context, xml, RestsDefinition.class);
            xml = ModelHelper.dumpModelAsXml(context, copy);
        }
    }
    return xml;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestDefinition(org.apache.camel.model.rest.RestDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RestsDefinition(org.apache.camel.model.rest.RestsDefinition) Document(org.w3c.dom.Document) XmlLineNumberParser(org.apache.camel.util.XmlLineNumberParser) IOException(java.io.IOException)

Example 37 with Document

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

the class BodyAndHeaderConvertTest method testConversionOfBody.

public void testConversionOfBody() throws Exception {
    Document document = exchange.getIn().getBody(Document.class);
    assertNotNull(document);
    Element element = document.getDocumentElement();
    assertEquals("Root element name", "hello", element.getLocalName());
}
Also used : Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 38 with Document

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

the class BeanProxyTest method testBeanProxyDocumentReturnDocument.

public void testBeanProxyDocumentReturnDocument() throws Exception {
    // START SNIPPET: e3
    Endpoint endpoint = context.getEndpoint("direct:start");
    OrderService service = ProxyHelper.createProxy(endpoint, OrderService.class);
    Document doc = context.getTypeConverter().convertTo(Document.class, "<order type=\"book\">Camel in action</order>");
    Document reply = service.submitOrderDocumentReturnDocument(doc);
    assertNotNull(reply);
    String s = context.getTypeConverter().convertTo(String.class, reply);
    assertEquals("<order id=\"123\">OK</order>", s);
// END SNIPPET: e3
}
Also used : Endpoint(org.apache.camel.Endpoint) Document(org.w3c.dom.Document)

Example 39 with Document

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

the class BeanProxyTest method testBeanProxyStringReturnDocument.

public void testBeanProxyStringReturnDocument() throws Exception {
    Endpoint endpoint = context.getEndpoint("direct:start");
    OrderService service = ProxyHelper.createProxy(endpoint, OrderService.class);
    Document reply = service.submitOrderStringReturnDocument("<order type=\"book\">Camel in action</order>");
    assertNotNull(reply);
    String s = context.getTypeConverter().convertTo(String.class, reply);
    assertEquals("<order id=\"123\">OK</order>", s);
}
Also used : Endpoint(org.apache.camel.Endpoint) Document(org.w3c.dom.Document)

Example 40 with Document

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

the class BeanProxyTest method testBeanProxyDocumentReturnString.

public void testBeanProxyDocumentReturnString() throws Exception {
    Endpoint endpoint = context.getEndpoint("direct:start");
    OrderService service = ProxyHelper.createProxy(endpoint, OrderService.class);
    Document doc = context.getTypeConverter().convertTo(Document.class, "<order type=\"book\">Camel in action</order>");
    String reply = service.submitOrderDocumentReturnString(doc);
    assertEquals("<order id=\"123\">OK</order>", reply);
}
Also used : Endpoint(org.apache.camel.Endpoint) Document(org.w3c.dom.Document)

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