1 38 package com.gargoylesoftware.htmlunit.html.xpath; 39 40 import org.jaxen.BaseXPath; 41 import org.jaxen.JaxenException; 42 import org.jaxen.XPath; 43 44 import com.gargoylesoftware.htmlunit.html.DomNode; 45 46 62 class NodeRelativeNavigator extends DocumentNavigator { 63 64 private static final long serialVersionUID = 3833748784969691447L; 65 private final DomNode rootNode_; 66 67 72 public NodeRelativeNavigator(final DomNode node) { 73 rootNode_ = node; 74 } 75 76 82 public XPath parseXPath (final String xpath) throws JaxenException { 83 return new BaseXPath(xpath, this); 84 } 85 86 92 public Object getDocumentNode (final Object contextNode) { 93 return rootNode_; 94 } 95 96 102 public boolean isDocument (final Object object) { 103 return (object == rootNode_); 104 } 105 106 112 public boolean isElement (final Object object) { 113 return super.isElement(object) && (object != rootNode_); 114 } 115 116 134 public Object getElementById(final Object contextNode, final String elementId) { 135 final DomNode node = (DomNode) super.getElementById(contextNode, elementId); 136 if (isChild(node)) { 137 return node; 138 } 139 return null; 140 } 141 142 147 private boolean isChild(final DomNode node) { 148 DomNode parent = node; 149 while (parent != null) { 150 if (parent == rootNode_) { 151 return true; 152 } 153 parent = parent.getParentNode(); 154 } 155 156 return false; 157 } 158 } 159 | Popular Tags |