org.w3c.dom.traversal.TreeWalker Java Examples

The following examples show how to use org.w3c.dom.traversal.TreeWalker. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DomTreeWalkerTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void firstChild() throws Exception {
    final String html = "<html><head><script>\n"
            + "function test() {\n"
            + "}\n"
            + "</script></head>\n"
            + "<body onload='test()'>\n"
            + "  <form name='f1'>\n"
            + "    <input>\n"
            + "    <INPUT>\n"
            + "  </form>\n"
            + "</body></html>";

    final WebDriver driver = loadPageWithAlerts2(html);
    if (driver instanceof HtmlUnitDriver) {
        final WebWindow webWindow = getWebWindowOf((HtmlUnitDriver) driver);
        final HtmlPage page = (HtmlPage) webWindow.getEnclosedPage();
        final TreeWalker walker = page.createTreeWalker(page.getDocumentElement(),
                org.w3c.dom.traversal.NodeFilter.SHOW_ALL, null, true);
        assertThat(walker.firstChild(), instanceOf(HtmlHead.class));
    }
}
 
Example #2
Source File: DocumentImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #3
Source File: ElementMap.java    From jStyleParser with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ElementMap(Document doc) {
	elementIDs = new HashMap<String, Element>();
	elementNames = new HashMap<String, Element>();
       DocumentTraversal traversal = (DocumentTraversal) doc;
       TreeWalker w = traversal.createTreeWalker(doc, NodeFilter.SHOW_ELEMENT, null, false);
	//TreeWalker w = new TidyTreeWalker(doc, NodeFilter.SHOW_ELEMENT);
	Element current;
	while ((current = (Element) w.nextNode()) != null) {
		elementNames.put(current.getNodeName().toLowerCase(), current);
		String id = current.getAttribute("id");
		if(id!=null)
			elementIDs.put(id, current);
	}
}
 
Example #4
Source File: DocumentImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #5
Source File: DocumentImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #6
Source File: DocumentImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #7
Source File: UtilsClustering.java    From apogen with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively construct a LblTree from DOM tree
 *
 * @param walker
 *            tree walker for DOM tree traversal
 * @return tree represented by DOM tree
 */
public static LblTree createTree(TreeWalker walker) {
	Node parent = walker.getCurrentNode();
	LblTree node = new LblTree(((Element) parent).getNodeName(), -1); // treeID = -1
	for (Node n = walker.firstChild(); n != null; n = walker.nextSibling()) {
		node.add(createTree(walker));
	}
	walker.setCurrentNode(parent);
	return node;
}
 
Example #8
Source File: DocumentImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #9
Source File: DocumentImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #10
Source File: DocumentImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #11
Source File: DocumentImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #12
Source File: DocumentImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #13
Source File: DocumentImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #14
Source File: DocumentImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 * @param entityReferenceExpansion true to expand the contents of
 *                                 EntityReference nodes
 * @since WD-DOM-Level-2-19990923
 */
public TreeWalker createTreeWalker(Node root,
                                   int whatToShow,
                                   NodeFilter filter,
                                   boolean entityReferenceExpansion)
{
    if (root == null) {
        String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
    }
    return new TreeWalkerImpl(root, whatToShow, filter,
                              entityReferenceExpansion);
}
 
Example #15
Source File: Analyzer.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected boolean matchSelector(CombinedSelector sel, Element e, TreeWalker w) {

		// store current walker position
		Node current = w.getCurrentNode();

		boolean retval = false;
		Selector.Combinator combinator = null;
		// traverse simple selector backwards
		for (int i = sel.size() - 1; i >= 0; i--) {
			// last simple selector
			Selector s = sel.get(i);
			//log.trace("Iterating loop with selector {}, combinator {}",	s, combinator);

			// decide according to combinator anti-pattern
			if (combinator == null) {
				retval = this.elementSelectorMatches(s, e);
			} else if (combinator == Selector.Combinator.ADJACENT) {
				Element adjacent = (Element) w.previousSibling();
				retval = false;
				if (adjacent != null)
					retval = this.elementSelectorMatches(s, adjacent);
            } else if (combinator == Selector.Combinator.PRECEDING) {
                Element preceding;
                retval = false;
                while (!retval && (preceding = (Element) w.previousSibling()) != null) {
                    retval = this.elementSelectorMatches(s, preceding);
                }
			} else if (combinator == Selector.Combinator.DESCENDANT) {
                Element ancestor;
                retval = false;
                while (!retval && (ancestor = (Element) w.parentNode()) != null) {
                    retval = this.elementSelectorMatches(s, ancestor);
                }
			} else if (combinator == Selector.Combinator.CHILD) {
                Element parent = (Element) w.parentNode();
                retval = false;
                if (parent != null)
                    retval = this.elementSelectorMatches(s, parent);
			}

			// set combinator for next loop
			combinator = s.getCombinator();

			// leave loop if not matched
			if (!retval)
				break;
		}

		// restore walker position
		w.setCurrentNode(current);
		return retval;
	}
 
Example #16
Source File: GenericTreeWalker.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Traversal<T> reset(TreeWalker walker, Object source) {
	this.walker = walker;
	this.source = source;
	return this;
}
 
Example #17
Source File: GenericTreeWalker.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Traversal(TreeWalker walker, Object source) {
	this.source = source;
	this.walker = walker;
}
 
Example #18
Source File: Traversal.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Traversal<T> reset(TreeWalker walker, Object source) {
    this.walker = walker;
    this.source = source;
    return this;
}
 
Example #19
Source File: Traversal.java    From jStyleParser with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Traversal(TreeWalker walker, Object source) {
    this.source = source;
    this.walker = walker;
}
 
Example #20
Source File: DocumentImpl.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}
 
Example #21
Source File: DocumentImpl.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}
 
Example #22
Source File: DocumentImpl.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}
 
Example #23
Source File: DocumentImpl.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}
 
Example #24
Source File: DocumentImpl.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}
 
Example #25
Source File: DocumentImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}
 
Example #26
Source File: DocumentImpl.java    From hottub with GNU General Public License v2.0 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}
 
Example #27
Source File: UtilsClustering.java    From apogen with Apache License 2.0 3 votes vote down vote up
private static LblTree getDomTree(String dom1) throws IOException {

		org.w3c.dom.Document doc1 = DomUtils.asDocument(dom1);

		LblTree domTree = null;

		DocumentTraversal traversal = (DocumentTraversal) doc1;
		TreeWalker walker = traversal.createTreeWalker(doc1.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
		domTree = createTree(walker);

		return domTree;
	}
 
Example #28
Source File: DocumentImpl.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}
 
Example #29
Source File: DocumentImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}
 
Example #30
Source File: DocumentImpl.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * NON-DOM extension:
 * Create and return a TreeWalker.
 *
 * @param root The root of the iterator.
 * @param whatToShow The whatToShow mask.
 * @param filter The NodeFilter installed. Null means no filter.
 */
public TreeWalker createTreeWalker(Node root,
                                   short whatToShow,
                                   NodeFilter filter)
{
    return createTreeWalker(root, whatToShow, filter, true);
}