1 package com.puppycrawl.tools.checkstyle.checks.xpath; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 24 import org.jaxen.DefaultNavigator; 25 import org.jaxen.XPath; 26 import org.jaxen.util.SingleObjectIterator; 27 import org.saxpath.SAXPathException; 28 29 import com.puppycrawl.tools.checkstyle.api.DetailAST; 30 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 31 32 39 public class DocumentNavigator 40 extends DefaultNavigator 41 { 42 43 private static final Iterator EMPTY_ITERATOR = new ArrayList ().iterator(); 44 45 48 public String getAttributeName(Object aObject) 49 { 50 return ((Attribute) aObject).getName(); 51 } 52 53 56 public String getAttributeNamespaceUri(Object aObject) 57 { 58 return null; 59 } 60 61 64 public String getAttributeQName(Object aObject) 65 { 66 return ((Attribute) aObject).getName(); 67 } 68 69 72 public String getAttributeStringValue(Object aObject) 73 { 74 return ((Attribute) aObject).getValue(); 75 } 76 77 80 public String getCommentStringValue(Object aObject) 81 { 82 return null; 83 } 84 85 88 public String getElementName(Object aObject) 89 { 90 final int type = ((DetailAST) aObject).getType(); 91 return TokenTypes.getTokenName(type); 92 } 93 94 97 public String getElementNamespaceUri(Object aObject) 98 { 99 return null; 100 } 101 102 105 public String getElementQName(Object aObject) 106 { 107 return getElementName(aObject); 108 } 109 110 113 public String getElementStringValue(Object aObject) 114 { 115 return null; 116 } 117 118 121 public String getNamespacePrefix(Object aObject) 122 { 123 return null; 124 } 125 126 129 public String getNamespaceStringValue(Object aObject) 130 { 131 return null; 132 } 133 134 137 public String getTextStringValue(Object aObject) 138 { 139 return null; 140 } 141 142 145 public boolean isAttribute(Object aObject) 146 { 147 return aObject instanceof Attribute; 148 } 149 150 153 public boolean isComment(Object aObject) 154 { 155 return false; 156 } 157 158 161 public boolean isDocument(Object aObject) 162 { 163 if (aObject instanceof DetailAST) { 164 final DetailAST node = (DetailAST) aObject; 165 return (node.getType() == TokenTypes.EOF); 166 } 167 else { 168 return false; 169 } 170 } 171 172 175 public boolean isElement(Object aObject) 176 { 177 return aObject instanceof DetailAST; 178 } 179 180 183 public boolean isNamespace(Object aObject) 184 { 185 return false; 186 } 187 188 191 public boolean isProcessingInstruction(Object aObject) 192 { 193 return false; 194 } 195 196 199 public boolean isText(Object aObject) 200 { 201 return false; 202 } 203 204 207 public XPath parseXPath(String aObject) 208 throws SAXPathException 209 { 210 return null; 211 } 212 213 216 public Object getParentNode(Object aObject) 217 { 218 if (aObject instanceof DetailAST) { 219 return ((DetailAST) aObject).getParent(); 220 } 221 else { 222 return ((Attribute) aObject).getParent(); 223 } 224 } 225 226 229 public Iterator getAttributeAxisIterator(Object aObject) 230 { 231 final DetailAST contextNode = (DetailAST) aObject; 232 return new AttributeAxisIterator(contextNode); 233 } 234 235 241 public Iterator getChildAxisIterator(Object aObject) 242 { 243 return new NodeIterator((DetailAST) aObject) 244 { 245 246 protected DetailAST getFirstNode(DetailAST aAST) 247 { 248 return getFirstChild(aAST); 249 } 250 251 252 protected DetailAST getNextNode(DetailAST aAST) 253 { 254 return getNextSibling(aAST); 255 } 256 }; 257 } 258 259 265 public Iterator getParentAxisIterator(Object aObject) 266 { 267 if (isAttribute(aObject)) { 268 return new SingleObjectIterator(((Attribute) aObject).getParent()); 269 } 270 else { 271 DetailAST parent = ((DetailAST) aObject).getParent(); 272 if (parent != null) { 273 return new SingleObjectIterator(parent); 274 } 275 else { 276 return EMPTY_ITERATOR; 277 } 278 } 279 } 280 281 287 public Iterator getFollowingSiblingAxisIterator(Object aObject) 288 { 289 return new NodeIterator((DetailAST) aObject) 290 { 291 292 protected DetailAST getFirstNode(DetailAST aAST) 293 { 294 return getNextNode(aAST); 295 } 296 297 298 protected DetailAST getNextNode(DetailAST aAST) 299 { 300 return getNextSibling(aAST); 301 } 302 }; 303 } 304 305 311 public Iterator getPrecedingSiblingAxisIterator(Object aObject) 312 { 313 return new NodeIterator((DetailAST) aObject) 314 { 315 316 protected DetailAST getFirstNode(DetailAST aAST) 317 { 318 return getNextNode(aAST); 319 } 320 321 322 protected DetailAST getNextNode(DetailAST aAST) 323 { 324 return getPreviousSibling(aAST); 325 } 326 }; 327 } 328 329 335 public Iterator getFollowingAxisIterator(Object aObject) 336 { 337 return new NodeIterator((DetailAST) aObject) 338 { 339 340 protected DetailAST getFirstNode(DetailAST aAST) 341 { 342 if (aAST == null) { 343 return null; 344 } 345 else { 346 final DetailAST sibling = getNextSibling(aAST); 347 if (sibling == null) { 348 return getFirstNode(aAST.getParent()); 349 } 350 else { 351 return sibling; 352 } 353 } 354 } 355 356 357 protected DetailAST getNextNode(DetailAST aAST) 358 { 359 if (aAST == null) { 360 return null; 361 } 362 else { 363 DetailAST n = getFirstChild(aAST); 364 if (n == null) { 365 n = getNextSibling(aAST); 366 } 367 if (n == null) { 368 return getFirstNode(aAST.getParent()); 369 } 370 else { 371 return n; 372 } 373 } 374 } 375 }; 376 } 377 378 384 public Iterator getPrecedingAxisIterator(Object aObject) 385 { 386 return new NodeIterator((DetailAST) aObject) 387 { 388 389 protected DetailAST getFirstNode(DetailAST aAST) 390 { 391 if (aAST == null) { 392 return null; 393 } 394 else { 395 final DetailAST sibling = getPreviousSibling(aAST); 396 if (sibling == null) { 397 return getFirstNode(aAST.getParent()); 398 } 399 else { 400 return sibling; 401 } 402 } 403 } 404 405 406 protected DetailAST getNextNode(DetailAST aAST) 407 { 408 if (aAST == null) { 409 return null; 410 } 411 else { 412 DetailAST n = getLastChild(aAST); 413 if (n == null) { 414 n = getPreviousSibling(aAST); 415 } 416 if (n == null) { 417 return getFirstNode(aAST.getParent()); 418 } 419 else { 420 return n; 421 } 422 } 423 } 424 }; 425 } 426 427 428 public Object getDocumentNode(Object aObject) 429 { 430 if (isDocument(aObject)) { 431 return aObject; 432 } 433 else { 434 return getDocumentNode(getParentNode(aObject)); 435 } 436 } 437 } 438 | Popular Tags |