1 38 package com.gargoylesoftware.htmlunit.html.xpath; 39 40 import java.util.Iterator ; 41 import java.util.Map ; 42 import java.util.Collections ; 43 44 import org.jaxen.DefaultNavigator; 45 import org.jaxen.XPath; 46 import org.jaxen.JaxenException; 47 import com.gargoylesoftware.htmlunit.html.DomNode; 48 import com.gargoylesoftware.htmlunit.html.HtmlElement; 49 import com.gargoylesoftware.htmlunit.html.DomText; 50 import com.gargoylesoftware.htmlunit.html.HtmlPage; 51 import com.gargoylesoftware.htmlunit.html.Util; 52 53 67 public class DocumentNavigator extends DefaultNavigator { 68 private static final long serialVersionUID = -5323715453687261210L; 69 70 73 public static final DocumentNavigator instance = new DocumentNavigator(); 74 75 81 public Iterator getChildAxisIterator (final Object contextNode) { 82 return ((DomNode)contextNode).getChildIterator(); 83 } 84 85 91 public Iterator getParentAxisIterator (final Object contextNode) { 92 return new Iterator () { 93 private DomNode parent_ = ((DomNode)contextNode).getParentNode(); 94 95 public boolean hasNext() { 96 return parent_ != null; 97 } 98 public Object next() { 99 final DomNode next = parent_; 100 parent_ = null; 101 return next; 102 } 103 public void remove() { 104 throw new UnsupportedOperationException (); 105 } 106 }; 107 } 108 109 115 public Iterator getFollowingSiblingAxisIterator (final Object contextNode) { 116 return Util.getFollowingSiblingAxisIterator((DomNode) contextNode); 117 } 118 119 125 public Iterator getPrecedingSiblingAxisIterator (final Object contextNode) { 126 return Util.getPrecedingSiblingAxisIterator((DomNode)contextNode); 127 } 128 129 135 public Iterator getFollowingAxisIterator (final Object contextNode) { 136 return Util.getFollowingAxisIterator((DomNode)contextNode); 137 } 138 139 145 public Iterator getPrecedingAxisIterator (final Object contextNode) { 146 return Util.getPrecedingAxisIterator((DomNode)contextNode); 147 } 148 149 155 public Iterator getAttributeAxisIterator (final Object contextNode) { 156 if(contextNode instanceof HtmlElement) { 157 return ((HtmlElement)contextNode).getAttributeEntriesIterator(); 158 } 159 else { 160 return Collections.EMPTY_LIST.iterator(); 161 } 162 } 163 164 170 public XPath parseXPath (final String xpath) throws JaxenException { 171 return new HtmlUnitXPath(xpath); 172 } 173 174 180 public Object getDocumentNode (final Object contextNode) { 181 return ((DomNode)contextNode).getPage(); 182 } 183 184 191 public String getElementNamespaceUri (final Object object) { 192 193 if(object instanceof HtmlElement) { 195 return ""; 196 } 197 else { 198 return null; 199 } 200 } 201 202 209 public String getElementName (final Object object) { 210 return ((DomNode)object).getNodeName(); 211 } 212 213 220 public String getElementQName (final Object object) { 221 return ((DomNode)object).getNodeName(); 222 } 223 224 228 public String getAttributeNamespaceUri (final Object object) { 229 return ""; 230 } 231 232 239 public String getAttributeName (final Object object) { 240 return (String )((Map.Entry )object).getKey(); 241 } 242 243 250 public String getAttributeQName (final Object object) { 251 return (String )((Map.Entry )object).getKey(); 252 } 253 254 260 public boolean isDocument (final Object object) { 261 return (object instanceof HtmlPage); 262 } 263 264 270 public boolean isNamespace (final Object object) { 271 return false; 272 } 273 274 280 public boolean isElement (final Object object) { 281 return (object instanceof HtmlElement); 282 } 283 284 290 public boolean isAttribute (final Object object) { 291 return (object instanceof Map.Entry ); 292 } 293 294 300 public boolean isComment (final Object object) { 301 return false; 302 } 303 304 310 public boolean isText (final Object object) { 311 return (object instanceof DomText); 312 } 313 314 320 public boolean isProcessingInstruction (final Object object) { 321 return false; 322 } 323 324 331 public String getElementStringValue (final Object object) { 332 return ((DomNode)object).asText(); 333 } 334 335 342 public String getAttributeStringValue (final Object object) { 343 return (String )((Map.Entry )object).getValue(); 344 } 345 346 352 public String getTextStringValue (final Object object) { 353 return ((DomText)object).asText(); 354 } 355 356 363 public String getCommentStringValue (final Object object) { 364 return null; 365 } 366 367 374 public String getNamespaceStringValue (final Object object) { 375 return null; 376 } 377 378 385 public String getNamespacePrefix (final Object object) { 386 return null; 387 } 388 389 407 public Object getElementById(final Object contextNode, final String elementId) { 408 final HtmlPage page = ((DomNode)contextNode).getPage(); 409 return page.getHtmlElementById(elementId); 410 } 411 412 } 413 | Popular Tags |