use of org.w3c.dom.Node in project camel by apache.
the class XPathBuilder method logDiscoveredNamespaces.
private void logDiscoveredNamespaces(NodeList namespaces) {
Map<String, HashSet<String>> map = new LinkedHashMap<String, HashSet<String>>();
for (int i = 0; i < namespaces.getLength(); i++) {
Node n = namespaces.item(i);
if (n.getNodeName().equals("xmlns:xml")) {
// skip the implicit XML namespace as it provides no value
continue;
}
String prefix = namespaces.item(i).getNodeName();
if (prefix.equals("xmlns")) {
prefix = "DEFAULT";
}
// add to map
if (!map.containsKey(prefix)) {
map.put(prefix, new HashSet<String>());
}
map.get(prefix).add(namespaces.item(i).getNodeValue());
}
LOG.info("Namespaces discovered in message: {}.", map);
}
use of org.w3c.dom.Node in project camel by apache.
the class Namespaces method add.
public Namespaces add(Element element) {
// let's set the parent first in case we overload a prefix here
Node parentNode = element.getParentNode();
if (parentNode instanceof org.w3c.dom.Element) {
add((Element) parentNode);
}
NamedNodeMap attributes = element.getAttributes();
int size = attributes.getLength();
for (int i = 0; i < size; i++) {
Attr node = (Attr) attributes.item(i);
String name = node.getName();
if (name.startsWith("xmlns:")) {
String prefix = name.substring("xmlns:".length());
String uri = node.getValue();
add(prefix, uri);
}
}
return this;
}
use of org.w3c.dom.Node 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.Node in project camel by apache.
the class DomConverter method toString.
@Converter
public String toString(NodeList nodeList, Exchange exchange) throws TransformerException {
// converting NodeList to String is more tricky
// sometimes the NodeList is a Node which we can then leverage
// the XML converter to turn into XML incl. tags
StringBuilder buffer = new StringBuilder();
// use XML converter at first since it preserves tag names
boolean found = false;
if (nodeList instanceof Node) {
Node node = (Node) nodeList;
String s = toString(node, exchange);
if (ObjectHelper.isNotEmpty(s)) {
found = true;
buffer.append(s);
}
} else {
// use XML converter at first since it preserves tag names
int size = nodeList.getLength();
for (int i = 0; i < size; i++) {
Node node = nodeList.item(i);
String s = toString(node, exchange);
if (ObjectHelper.isNotEmpty(s)) {
found = true;
buffer.append(s);
}
}
}
// used an xpath to select an attribute or text() or something
if (!found) {
append(buffer, nodeList);
}
return buffer.toString();
}
use of org.w3c.dom.Node 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);
}
Aggregations