Package org.w3c.dom.traversal

Examples of org.w3c.dom.traversal.TreeWalker


    public void setUp() throws Exception {
        dom = DOMParser.toDocument(XML).getDocumentElement();
    }

    public void testRootNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);
        assertEquals("a", tw.getRoot().getLocalName());
    }
View Full Code Here


        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);
        assertEquals("a", tw.getRoot().getLocalName());
    }

    public void testWhatToShow() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_COMMENT, null, false);
        assertEquals(NodeFilter.SHOW_COMMENT, tw.getWhatToShow());
        assertEquals(null, tw.nextNode());
    }
View Full Code Here

                }
                return NodeFilter.FILTER_SKIP;
            }
        };

        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, nf, false);
        assertEquals(nf, tw.getFilter());
        assertEquals("b", tw.nextNode().getNodeName());
        assertEquals("f", tw.nextNode().getNodeName());
        assertEquals(null, tw.nextNode());
    }
View Full Code Here

        assertEquals("f", tw.nextNode().getNodeName());
        assertEquals(null, tw.nextNode());
    }

    public void testNextNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);
        assertEquals("a", tw.getCurrentNode().getLocalName());

        assertEquals("b", tw.nextNode().getLocalName());
        assertEquals("c", tw.nextNode().getLocalName());
        assertEquals("d", tw.nextNode().getLocalName());
        assertEquals("e", tw.nextNode().getLocalName());
        assertEquals("f", tw.nextNode().getLocalName());
        assertEquals("g", tw.nextNode().getLocalName());
        assertEquals("h", tw.nextNode().getLocalName());
        assertEquals("i", tw.nextNode().getLocalName());
        assertEquals(null, tw.nextNode());
    }
View Full Code Here

        assertEquals("i", tw.nextNode().getLocalName());
        assertEquals(null, tw.nextNode());
    }

    public void testPreviousNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);
        tw.setCurrentNode(dom.getLastChild());
        assertEquals("i", tw.getCurrentNode().getLocalName());

        assertEquals("h", tw.previousNode().getLocalName());
        assertEquals("g", tw.previousNode().getLocalName());
        assertEquals("f", tw.previousNode().getLocalName());
        assertEquals("e", tw.previousNode().getLocalName());
        assertEquals("d", tw.previousNode().getLocalName());
        assertEquals("c", tw.previousNode().getLocalName());
        assertEquals("b", tw.previousNode().getLocalName());
        assertEquals("a", tw.previousNode().getLocalName());
        assertEquals(null, tw.previousNode());
    }
View Full Code Here

        assertEquals("a", tw.previousNode().getLocalName());
        assertEquals(null, tw.previousNode());
    }

    public void testCurrentNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);

        assertEquals("a", tw.getCurrentNode().getLocalName());
        assertEquals(null, tw.previousNode());
        assertEquals("a", tw.getCurrentNode().getLocalName());

        tw.setCurrentNode(dom.getLastChild());
        assertEquals("i", tw.getCurrentNode().getLocalName());
        assertEquals(null, tw.nextNode());
        assertEquals("i", tw.getCurrentNode().getLocalName());

        try {
            tw.setCurrentNode(null);
            fail("Exception expected");
        } catch (DOMException e) {
        }
        assertEquals("i", tw.getCurrentNode().getLocalName());
    }
View Full Code Here

        }
        assertEquals("i", tw.getCurrentNode().getLocalName());
    }

    public void testParentNode() throws Exception {
        TreeWalker tw = new TreeWalkerImpl(dom, NodeFilter.SHOW_ALL, null, false);
        // Go to F node
        tw.nextNode();
        tw.nextNode();
        tw.nextNode();
        tw.nextNode();
        tw.nextNode();
        assertEquals("f", tw.getCurrentNode().getLocalName());

        assertEquals("d", tw.parentNode().getLocalName());
        assertEquals("c", tw.parentNode().getLocalName());
        assertEquals("a", tw.parentNode().getLocalName());
        assertEquals(null, tw.parentNode());
    }
View Full Code Here

                accepted.add(scopeNode);
            }
        }

        // TreeWalker to walk DOM tree starting with the scopeNode. Nodes maybe visited multiple times.
        TreeWalker treeWalker = ((DocumentTraversal) scopeNode.getOwnerDocument())
            .createTreeWalker(scopeNode, NodeFilter.SHOW_ELEMENT, new NodeFilter() {
            @Override
            public short acceptNode(Node node) {
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    NamedNodeMap attributes = node.getAttributes();
                    if (attributes.getNamedItem(ITEMPROP_ATTRIBUTE) != null && !scopeNode.equals(node)) {
                        accepted.add(node);
                    }
                    if (attributes.getNamedItem(ITEMSCOPE_ATTRIBUTE) != null) {
                        // Don't visit descendants of nodes that define a new scope
                        return FILTER_REJECT;
                    }
                }
                return FILTER_ACCEPT;
            }
        }, false);

        // To populate accepted we only need to walk the tree.
      while (treeWalker.nextNode() != null);

        final List<ItemProp> result = new ArrayList<ItemProp>();
        for(Node itemPropNode :  accepted) {
            final String itemProp = DomUtils.readAttribute(itemPropNode, ITEMPROP_ATTRIBUTE, null);
            final String[] propertyNames = itemProp.split(" ");
View Full Code Here

                accepted.add(scopeNode);
            }
        }

        // TreeWalker to walk DOM tree starting with the scopeNode. Nodes maybe visited multiple times.
        TreeWalker treeWalker = ((DocumentTraversal) scopeNode.getOwnerDocument())
            .createTreeWalker(scopeNode, NodeFilter.SHOW_ELEMENT, new NodeFilter() {
            @Override
            public short acceptNode(Node node) {
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    NamedNodeMap attributes = node.getAttributes();
                    if (attributes.getNamedItem(ITEMPROP_ATTRIBUTE) != null && !scopeNode.equals(node)) {
                        accepted.add(node);
                    }
                    if (attributes.getNamedItem(ITEMSCOPE_ATTRIBUTE) != null) {
                        // Don't visit descendants of nodes that define a new scope
                        return FILTER_REJECT;
                    }
                }
                return FILTER_ACCEPT;
            }
        }, false);

        // To populate accepted we only need to walk the tree.
      while (treeWalker.nextNode() != null);

        final List<ItemProp> result = new ArrayList<ItemProp>();
        for(Node itemPropNode :  accepted) {
            final String itemProp = DomUtils.readAttribute(itemPropNode, ITEMPROP_ATTRIBUTE, null);
            final String[] propertyNames = itemProp.split(" ");
View Full Code Here

        private void parseDocument(Document dasDoc, String chr, List<Feature> features) {

            try {
                DocumentTraversal traversal = (DocumentTraversal) dasDoc;
                TreeWalker treewalker = traversal.createTreeWalker(
                        dasDoc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
                parseTree(treewalker, "FEATURE", chr, features);

            } catch (Exception ex) {
                log.error(ex);
View Full Code Here

TOP

Related Classes of org.w3c.dom.traversal.TreeWalker

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.