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;
}
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);
}
}
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));
}
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);
}
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);
}
Aggregations