use of org.w3c.dom.traversal.TreeWalker in project myrobotlab by MyRobotLab.
the class DomUtils method isLastOfItsKindIn.
public static boolean isLastOfItsKindIn(Node node, Node ancestor) {
if (node == null || ancestor == null)
return false;
Document doc = node.getOwnerDocument();
if (doc == null)
return false;
TreeWalker tw = ((DocumentTraversal) doc).createTreeWalker(doc, NodeFilter.SHOW_ALL, new NameNodeFilter(node.getNodeName()), true);
tw.setCurrentNode(node);
Node next = tw.nextNode();
return // no node with same name after this one
next == null || // next is not in the same ancestor
!isAncestor(ancestor, next);
}
use of org.w3c.dom.traversal.TreeWalker in project myrobotlab by MyRobotLab.
the class DomUtils method getFirstElementByTagName.
/**
* Get the first child element with the given tag name, or <code>null</code> if there is no such element.
*
* @param n
* n
* @param name
* name
* @return tx.nextNode
*/
public static Element getFirstElementByTagName(Node n, String name) {
Document doc = (n instanceof Document) ? (Document) n : n.getOwnerDocument();
TreeWalker tw = ((DocumentTraversal) doc).createTreeWalker(n, NodeFilter.SHOW_ELEMENT, new NameNodeFilter(name), true);
return (Element) tw.nextNode();
}
use of org.w3c.dom.traversal.TreeWalker in project OpenUnison by TremoloSecurity.
the class ForRemoval method loadUnisonConfiguration.
@Override
public JAXBElement<TremoloType> loadUnisonConfiguration(Unmarshaller unmarshaller) throws Exception {
InputStream in;
if (configXML.startsWith("WEB-INF")) {
in = new ByteArrayInputStream(OpenUnisonConfigLoader.generateOpenUnisonConfig(ctx.getRealPath("/" + configXML)).getBytes("UTF-8"));
} else {
in = new ByteArrayInputStream(OpenUnisonConfigLoader.generateOpenUnisonConfig(configXML).getBytes("UTF-8"));
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder loader = factory.newDocumentBuilder();
Document document = loader.parse(in);
DocumentTraversal traversal = (DocumentTraversal) document;
TreeWalker walker = traversal.createTreeWalker(document.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
traverseLevel(walker, "");
StringWriter writer = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
transformer = tf.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(writer));
String xmlString = writer.getBuffer().toString();
ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
Object obj = unmarshaller.unmarshal(bais);
JAXBElement<TremoloType> cfg = (JAXBElement<TremoloType>) obj;
this.unisonConfig = cfg.getValue();
QueueConfigType qct = (QueueConfigType) GlobalEntries.getGlobalEntries().get("openunison.queueconfig");
if (qct != null) {
logger.info("Overriding Queue Configuration");
if (cfg.getValue().getProvisioning() == null) {
cfg.getValue().setProvisioning(new ProvisioningType());
}
cfg.getValue().getProvisioning().setQueueConfig(qct);
}
return cfg;
}
Aggregations