Search in sources :

Example 1 with TreeWalker

use of org.w3c.dom.traversal.TreeWalker in project cobigen by devonfw.

the class XmlInputReader method getInputObjects.

/**
 * {@inheritDoc}<br>
 * Splits an XMI Document into multiple sub-documents, one per each found class. Returns a {@link List} of
 * DocumentImpl.
 */
@Override
public List<Object> getInputObjects(Object input, Charset inputCharset) {
    LOG.debug("Retrieve xml input objects...");
    long start = System.currentTimeMillis();
    if (input instanceof Document) {
        Document doc = (Document) input;
        DocumentTraversal traversal = (DocumentTraversal) doc;
        TreeWalker treeWalker = traversal.createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, false);
        Node nextNode = treeWalker.nextNode();
        List<Object> docsList = new LinkedList<>();
        while (nextNode != null) {
            docsList.add(new Node[] { doc, nextNode });
            nextNode = treeWalker.nextNode();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} sub trees extracted in {}s", docsList.size(), (System.currentTimeMillis() - start) / 1000d);
        }
        return docsList;
    }
    throw new IllegalArgumentException("XmlInputReader::getInputObjects(Object,Charset) called with a wrong input parameter. This is a bug!");
}
Also used : Node(org.w3c.dom.Node) TreeWalker(org.w3c.dom.traversal.TreeWalker) DocumentTraversal(org.w3c.dom.traversal.DocumentTraversal) Document(org.w3c.dom.Document) LinkedList(java.util.LinkedList)

Example 2 with TreeWalker

use of org.w3c.dom.traversal.TreeWalker in project myrobotlab by MyRobotLab.

the class DomUtils method getLastElementByTagName.

/**
 * Get the last descendant element with the given tag name, or <code>null</code> if there is no such element.
 *
 * @param e
 *            e
 * @param name
 *            name
 * @return previous
 */
public static Element getLastElementByTagName(Element e, String name) {
    // This implementation is certainly inefficient, but I have
    // no better idea at the moment.
    TreeWalker tw = ((DocumentTraversal) e.getOwnerDocument()).createTreeWalker(e, NodeFilter.SHOW_ELEMENT, new NameNodeFilter(name), true);
    Node current = null;
    Node previous = null;
    while ((current = tw.nextNode()) != null) previous = current;
    return (Element) previous;
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) TreeWalker(org.w3c.dom.traversal.TreeWalker) DocumentTraversal(org.w3c.dom.traversal.DocumentTraversal)

Example 3 with TreeWalker

use of org.w3c.dom.traversal.TreeWalker in project myrobotlab by MyRobotLab.

the class DomUtils method getPreviousOfItsKindIn.

/**
 * Find the Element with the same tag name as <code>element</code> preceding <code>element</code> within the same subtree
 * under <code>root</code>. Precondition: <code>root</code> must be an ancestor of <code>element</code>.
 *
 * @param element
 *            element
 * @param root
 *            root
 * @return that Element, or <code>null</code> if there is no such Element.
 */
public static Element getPreviousOfItsKindIn(Element element, Element root) {
    if (element == null || root == null)
        return null;
    String tagname = element.getTagName();
    TreeWalker tw = ((DocumentTraversal) element.getOwnerDocument()).createTreeWalker(root, NodeFilter.SHOW_ELEMENT, new NameNodeFilter(tagname), true);
    tw.setCurrentNode(element);
    for (Node previous = tw.previousNode(); previous != null; previous = tw.previousNode()) {
        if (previous.getNodeName().equals(tagname)) {
            return (Element) previous;
        }
    }
    return null;
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) TreeWalker(org.w3c.dom.traversal.TreeWalker) DocumentTraversal(org.w3c.dom.traversal.DocumentTraversal)

Example 4 with TreeWalker

use of org.w3c.dom.traversal.TreeWalker in project myrobotlab by MyRobotLab.

the class DomUtils method isFirstOfItsKindIn.

public static boolean isFirstOfItsKindIn(Node node, Node ancestor) {
    if (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 prev = tw.previousNode();
    return // no node with same name before this one
    prev == null || // prev is not in the same ancestor
    !isAncestor(ancestor, prev);
}
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 5 with TreeWalker

use of org.w3c.dom.traversal.TreeWalker in project santuario-java by apache.

the class DOMNamespaceContextTest method testBoundDefaultNamespace.

@Test
public void testBoundDefaultNamespace() throws Exception {
    Document document = createDocument("<root xmlns='urn:ns'/>");
    TreeWalker walker = ((DocumentTraversal) document).createTreeWalker(document, SHOW_ELEMENT, null, true);
    Node root = walker.nextNode();
    DOMNamespaceContext namespaceContext = new DOMNamespaceContext(root);
    assertThrows(IllegalArgumentException.class, () -> namespaceContext.getNamespaceURI(null));
    assertThat(namespaceContext.getNamespaceURI(DEFAULT_NS_PREFIX), is(equalTo("urn:ns")));
    assertThat(namespaceContext.getNamespaceURI(XML_NS_PREFIX), is(equalTo(XML_NS_URI)));
    assertThat(namespaceContext.getNamespaceURI(XMLNS_ATTRIBUTE), is(equalTo(XMLNS_ATTRIBUTE_NS_URI)));
    assertThat(namespaceContext.getNamespaceURI("unbound-ns"), is(equalTo(NULL_NS_URI)));
    assertThrows(IllegalArgumentException.class, () -> namespaceContext.getPrefix(null));
    assertThat(namespaceContext.getPrefix(NULL_NS_URI), is(nullValue()));
    assertThat(namespaceContext.getPrefix("urn:ns"), is(equalTo(DEFAULT_NS_PREFIX)));
    assertThat(namespaceContext.getPrefix(XML_NS_URI), is(equalTo(XML_NS_PREFIX)));
    assertThat(namespaceContext.getPrefix(XMLNS_ATTRIBUTE_NS_URI), is(equalTo(XMLNS_ATTRIBUTE)));
    assertThat(namespaceContext.getPrefix("urn:unbound-ns"), is(nullValue()));
}
Also used : Node(org.w3c.dom.Node) TreeWalker(org.w3c.dom.traversal.TreeWalker) DocumentTraversal(org.w3c.dom.traversal.DocumentTraversal) Document(org.w3c.dom.Document) Test(org.junit.jupiter.api.Test)

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