Search in sources :

Example 16 with TreeWalker

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);
}
Also used : Node(org.w3c.dom.Node) TreeWalker(org.w3c.dom.traversal.TreeWalker) DocumentTraversal(org.w3c.dom.traversal.DocumentTraversal) Document(org.w3c.dom.Document)

Example 17 with TreeWalker

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();
}
Also used : Element(org.w3c.dom.Element) TreeWalker(org.w3c.dom.traversal.TreeWalker) DocumentTraversal(org.w3c.dom.traversal.DocumentTraversal) Document(org.w3c.dom.Document)

Example 18 with TreeWalker

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TremoloType(com.tremolosecurity.config.xml.TremoloType) QueueConfigType(com.tremolosecurity.config.xml.QueueConfigType) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TreeWalker(org.w3c.dom.traversal.TreeWalker) JAXBElement(javax.xml.bind.JAXBElement) Document(org.w3c.dom.Document) ProvisioningType(com.tremolosecurity.config.xml.ProvisioningType) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DocumentTraversal(org.w3c.dom.traversal.DocumentTraversal)

Aggregations

TreeWalker (org.w3c.dom.traversal.TreeWalker)18 DocumentTraversal (org.w3c.dom.traversal.DocumentTraversal)16 Node (org.w3c.dom.Node)14 Document (org.w3c.dom.Document)13 Test (org.junit.jupiter.api.Test)8 Element (org.w3c.dom.Element)4 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 ProvisioningType (com.tremolosecurity.config.xml.ProvisioningType)1 QueueConfigType (com.tremolosecurity.config.xml.QueueConfigType)1 TremoloType (com.tremolosecurity.config.xml.TremoloType)1 TwsTreeWalker (com.twinsoft.convertigo.engine.util.TwsTreeWalker)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 LinkedList (java.util.LinkedList)1 JAXBElement (javax.xml.bind.JAXBElement)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Transformer (javax.xml.transform.Transformer)1