下面列出了怎么用org.w3c.dom.traversal.TreeWalker的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @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));
}
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
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);
}
}
public Traversal(TreeWalker walker, Object source) {
this.source = source;
this.walker = walker;
}
public Traversal<T> reset(TreeWalker walker, Object source) {
this.walker = walker;
this.source = source;
return this;
}
public Traversal(TreeWalker walker, Object source) {
this.source = source;
this.walker = walker;
}
public Traversal<T> reset(TreeWalker walker, Object source) {
this.walker = walker;
this.source = source;
return this;
}
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;
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
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;
}
/**
* 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);
}
/**
* 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);
}