1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import org.w3c.dom.DOMException ; 61 import org.w3c.dom.Node ; 62 import org.w3c.dom.traversal.NodeFilter; 63 import org.w3c.dom.traversal.TreeWalker; 64 65 66 69 70 public class TreeWalkerImpl implements TreeWalker { 71 72 76 77 private boolean fEntityReferenceExpansion = false; 78 79 int fWhatToShow = NodeFilter.SHOW_ALL; 80 81 NodeFilter fNodeFilter; 82 83 Node fCurrentNode; 84 85 Node fRoot; 86 87 93 94 98 99 public TreeWalkerImpl(Node root, 100 int whatToShow, 101 NodeFilter nodeFilter, 102 boolean entityReferenceExpansion) { 103 fCurrentNode = root; 104 fRoot = root; 105 fWhatToShow = whatToShow; 106 fNodeFilter = nodeFilter; 107 fEntityReferenceExpansion = entityReferenceExpansion; 108 } 109 110 public Node getRoot() { 111 return fRoot; 112 } 113 114 115 public int getWhatToShow() { 116 return fWhatToShow; 117 } 118 119 public void setWhatShow(int whatToShow){ 120 fWhatToShow = whatToShow; 121 } 122 123 public NodeFilter getFilter() { 124 return fNodeFilter; 125 } 126 127 128 public boolean getExpandEntityReferences() { 129 return fEntityReferenceExpansion; 130 } 131 132 133 public Node getCurrentNode() { 134 return fCurrentNode; 135 } 136 137 public void setCurrentNode(Node node) { 138 if (node == null) { 139 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 140 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 141 } 142 143 fCurrentNode = node; 144 } 145 146 150 public Node parentNode() { 151 152 if (fCurrentNode == null) return null; 153 154 Node node = getParentNode(fCurrentNode); 155 if (node !=null) { 156 fCurrentNode = node; 157 } 158 return node; 159 160 } 161 162 166 public Node firstChild() { 167 168 if (fCurrentNode == null) return null; 169 170 Node node = getFirstChild(fCurrentNode); 171 if (node !=null) { 172 fCurrentNode = node; 173 } 174 return node; 175 } 176 180 public Node lastChild() { 181 182 if (fCurrentNode == null) return null; 183 184 Node node = getLastChild(fCurrentNode); 185 if (node !=null) { 186 fCurrentNode = node; 187 } 188 return node; 189 } 190 191 195 public Node previousSibling() { 196 197 if (fCurrentNode == null) return null; 198 199 Node node = getPreviousSibling(fCurrentNode); 200 if (node !=null) { 201 fCurrentNode = node; 202 } 203 return node; 204 } 205 206 210 public Node nextSibling(){ 211 if (fCurrentNode == null) return null; 212 213 Node node = getNextSibling(fCurrentNode); 214 if (node !=null) { 215 fCurrentNode = node; 216 } 217 return node; 218 } 219 220 224 public Node previousNode() { 225 Node result; 226 227 if (fCurrentNode == null) return null; 228 229 result = getPreviousSibling(fCurrentNode); 231 if (result == null) { 232 result = getParentNode(fCurrentNode); 233 if (result != null) { 234 fCurrentNode = result; 235 return fCurrentNode; 236 } 237 return null; 238 } 239 240 Node lastChild = getLastChild(result); 242 243 Node prev = lastChild ; 244 while (lastChild != null) { 245 prev = lastChild ; 246 lastChild = getLastChild(prev) ; 247 } 248 249 lastChild = prev ; 250 251 if (lastChild != null) { 253 fCurrentNode = lastChild; 254 return fCurrentNode; 255 } 256 257 if (result != null) { 259 fCurrentNode = result; 260 return fCurrentNode; 261 } 262 263 return null; 265 } 266 267 271 public Node nextNode() { 272 273 if (fCurrentNode == null) return null; 274 275 Node result = getFirstChild(fCurrentNode); 276 277 if (result != null) { 278 fCurrentNode = result; 279 return result; 280 } 281 282 result = getNextSibling(fCurrentNode); 283 284 if (result != null) { 285 fCurrentNode = result; 286 return result; 287 } 288 289 Node parent = getParentNode(fCurrentNode); 291 while (parent != null) { 292 result = getNextSibling(parent); 293 if (result != null) { 294 fCurrentNode = result; 295 return result; 296 } else { 297 parent = getParentNode(parent); 298 } 299 } 300 301 return null; 303 } 304 305 310 Node getParentNode(Node node) { 311 312 if (node == null || node == fRoot) return null; 313 314 Node newNode = node.getParentNode(); 315 if (newNode == null) return null; 316 317 int accept = acceptNode(newNode); 318 319 if (accept == NodeFilter.FILTER_ACCEPT) 320 return newNode; 321 else 322 { 324 return getParentNode(newNode); 325 } 326 327 328 } 329 330 335 Node getNextSibling(Node node) { 336 return getNextSibling(node, fRoot); 337 } 338 339 345 Node getNextSibling(Node node, Node root) { 346 347 if (node == null || node == root) return null; 348 349 Node newNode = node.getNextSibling(); 350 if (newNode == null) { 351 352 newNode = node.getParentNode(); 353 354 if (newNode == null || newNode == root) return null; 355 356 int parentAccept = acceptNode(newNode); 357 358 if (parentAccept==NodeFilter.FILTER_SKIP) { 359 return getNextSibling(newNode, root); 360 } 361 362 return null; 363 } 364 365 int accept = acceptNode(newNode); 366 367 if (accept == NodeFilter.FILTER_ACCEPT) 368 return newNode; 369 else 370 if (accept == NodeFilter.FILTER_SKIP) { 371 Node fChild = getFirstChild(newNode); 372 if (fChild == null) { 373 return getNextSibling(newNode, root); 374 } 375 return fChild; 376 } 377 else 378 { 380 return getNextSibling(newNode, root); 381 } 382 383 } 385 390 Node getPreviousSibling(Node node) { 391 return getPreviousSibling(node, fRoot); 392 } 393 394 400 Node getPreviousSibling(Node node, Node root) { 401 402 if (node == null || node == root) return null; 403 404 Node newNode = node.getPreviousSibling(); 405 if (newNode == null) { 406 407 newNode = node.getParentNode(); 408 if (newNode == null || newNode == root) return null; 409 410 int parentAccept = acceptNode(newNode); 411 412 if (parentAccept==NodeFilter.FILTER_SKIP) { 413 return getPreviousSibling(newNode, root); 414 } 415 416 return null; 417 } 418 419 int accept = acceptNode(newNode); 420 421 if (accept == NodeFilter.FILTER_ACCEPT) 422 return newNode; 423 else 424 if (accept == NodeFilter.FILTER_SKIP) { 425 Node fChild = getLastChild(newNode); 426 if (fChild == null) { 427 return getPreviousSibling(newNode, root); 428 } 429 return fChild; 430 } 431 else 432 { 434 return getPreviousSibling(newNode, root); 435 } 436 437 } 439 444 Node getFirstChild(Node node) { 445 if (node == null) return null; 446 447 if ( !fEntityReferenceExpansion 448 && node.getNodeType() == Node.ENTITY_REFERENCE_NODE) 449 return null; 450 Node newNode = node.getFirstChild(); 451 if (newNode == null) return null; 452 int accept = acceptNode(newNode); 453 454 if (accept == NodeFilter.FILTER_ACCEPT) 455 return newNode; 456 else 457 if (accept == NodeFilter.FILTER_SKIP 458 && newNode.hasChildNodes()) 459 { 460 Node fChild = getFirstChild(newNode); 461 462 if (fChild == null) { 463 return getNextSibling(newNode, node); 464 } 465 return fChild; 466 } 467 else 468 { 470 return getNextSibling(newNode, node); 471 } 472 473 474 } 475 476 481 Node getLastChild(Node node) { 482 483 if (node == null) return null; 484 485 if ( !fEntityReferenceExpansion 486 && node.getNodeType() == Node.ENTITY_REFERENCE_NODE) 487 return null; 488 489 Node newNode = node.getLastChild(); 490 if (newNode == null) return null; 491 492 int accept = acceptNode(newNode); 493 494 if (accept == NodeFilter.FILTER_ACCEPT) 495 return newNode; 496 else 497 if (accept == NodeFilter.FILTER_SKIP 498 && newNode.hasChildNodes()) 499 { 500 Node lChild = getLastChild(newNode); 501 if (lChild == null) { 502 return getPreviousSibling(newNode, node); 503 } 504 return lChild; 505 } 506 else 507 { 509 return getPreviousSibling(newNode, node); 510 } 511 512 513 } 514 515 517 short acceptNode(Node node) { 518 526 527 if (fNodeFilter == null) { 528 if ( ( fWhatToShow & (1 << node.getNodeType()-1)) != 0) { 529 return NodeFilter.FILTER_ACCEPT; 530 } else { 531 return NodeFilter.FILTER_SKIP; 532 } 533 } else { 534 if ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) { 535 return fNodeFilter.acceptNode(node); 536 } else { 537 return NodeFilter.FILTER_SKIP; 540 } 541 } 542 } 543 } 544 | Popular Tags |