1 16 17 package org.apache.xerces.dom; 18 19 import org.w3c.dom.DOMException ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.traversal.NodeFilter; 22 import org.w3c.dom.traversal.TreeWalker; 23 24 30 31 public class TreeWalkerImpl implements TreeWalker { 32 33 37 38 private boolean fEntityReferenceExpansion = false; 39 40 int fWhatToShow = NodeFilter.SHOW_ALL; 41 42 NodeFilter fNodeFilter; 43 44 Node fCurrentNode; 45 46 Node fRoot; 47 48 54 55 59 60 public TreeWalkerImpl(Node root, 61 int whatToShow, 62 NodeFilter nodeFilter, 63 boolean entityReferenceExpansion) { 64 fCurrentNode = root; 65 fRoot = root; 66 fWhatToShow = whatToShow; 67 fNodeFilter = nodeFilter; 68 fEntityReferenceExpansion = entityReferenceExpansion; 69 } 70 71 public Node getRoot() { 72 return fRoot; 73 } 74 75 76 public int getWhatToShow() { 77 return fWhatToShow; 78 } 79 80 public void setWhatShow(int whatToShow){ 81 fWhatToShow = whatToShow; 82 } 83 84 public NodeFilter getFilter() { 85 return fNodeFilter; 86 } 87 88 89 public boolean getExpandEntityReferences() { 90 return fEntityReferenceExpansion; 91 } 92 93 94 public Node getCurrentNode() { 95 return fCurrentNode; 96 } 97 98 public void setCurrentNode(Node node) { 99 if (node == null) { 100 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null); 101 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, msg); 102 } 103 104 fCurrentNode = node; 105 } 106 107 111 public Node parentNode() { 112 113 if (fCurrentNode == null) return null; 114 115 Node node = getParentNode(fCurrentNode); 116 if (node !=null) { 117 fCurrentNode = node; 118 } 119 return node; 120 121 } 122 123 127 public Node firstChild() { 128 129 if (fCurrentNode == null) return null; 130 131 Node node = getFirstChild(fCurrentNode); 132 if (node !=null) { 133 fCurrentNode = node; 134 } 135 return node; 136 } 137 141 public Node lastChild() { 142 143 if (fCurrentNode == null) return null; 144 145 Node node = getLastChild(fCurrentNode); 146 if (node !=null) { 147 fCurrentNode = node; 148 } 149 return node; 150 } 151 152 156 public Node previousSibling() { 157 158 if (fCurrentNode == null) return null; 159 160 Node node = getPreviousSibling(fCurrentNode); 161 if (node !=null) { 162 fCurrentNode = node; 163 } 164 return node; 165 } 166 167 171 public Node nextSibling(){ 172 if (fCurrentNode == null) return null; 173 174 Node node = getNextSibling(fCurrentNode); 175 if (node !=null) { 176 fCurrentNode = node; 177 } 178 return node; 179 } 180 181 185 public Node previousNode() { 186 Node result; 187 188 if (fCurrentNode == null) return null; 189 190 result = getPreviousSibling(fCurrentNode); 192 if (result == null) { 193 result = getParentNode(fCurrentNode); 194 if (result != null) { 195 fCurrentNode = result; 196 return fCurrentNode; 197 } 198 return null; 199 } 200 201 Node lastChild = getLastChild(result); 203 204 Node prev = lastChild ; 205 while (lastChild != null) { 206 prev = lastChild ; 207 lastChild = getLastChild(prev) ; 208 } 209 210 lastChild = prev ; 211 212 if (lastChild != null) { 214 fCurrentNode = lastChild; 215 return fCurrentNode; 216 } 217 218 if (result != null) { 220 fCurrentNode = result; 221 return fCurrentNode; 222 } 223 224 return null; 226 } 227 228 232 public Node nextNode() { 233 234 if (fCurrentNode == null) return null; 235 236 Node result = getFirstChild(fCurrentNode); 237 238 if (result != null) { 239 fCurrentNode = result; 240 return result; 241 } 242 243 result = getNextSibling(fCurrentNode); 244 245 if (result != null) { 246 fCurrentNode = result; 247 return result; 248 } 249 250 Node parent = getParentNode(fCurrentNode); 252 while (parent != null) { 253 result = getNextSibling(parent); 254 if (result != null) { 255 fCurrentNode = result; 256 return result; 257 } else { 258 parent = getParentNode(parent); 259 } 260 } 261 262 return null; 264 } 265 266 271 Node getParentNode(Node node) { 272 273 if (node == null || node == fRoot) return null; 274 275 Node newNode = node.getParentNode(); 276 if (newNode == null) return null; 277 278 int accept = acceptNode(newNode); 279 280 if (accept == NodeFilter.FILTER_ACCEPT) 281 return newNode; 282 else 283 { 285 return getParentNode(newNode); 286 } 287 288 289 } 290 291 296 Node getNextSibling(Node node) { 297 return getNextSibling(node, fRoot); 298 } 299 300 306 Node getNextSibling(Node node, Node root) { 307 308 if (node == null || node == root) return null; 309 310 Node newNode = node.getNextSibling(); 311 if (newNode == null) { 312 313 newNode = node.getParentNode(); 314 315 if (newNode == null || newNode == root) return null; 316 317 int parentAccept = acceptNode(newNode); 318 319 if (parentAccept==NodeFilter.FILTER_SKIP) { 320 return getNextSibling(newNode, root); 321 } 322 323 return null; 324 } 325 326 int accept = acceptNode(newNode); 327 328 if (accept == NodeFilter.FILTER_ACCEPT) 329 return newNode; 330 else 331 if (accept == NodeFilter.FILTER_SKIP) { 332 Node fChild = getFirstChild(newNode); 333 if (fChild == null) { 334 return getNextSibling(newNode, root); 335 } 336 return fChild; 337 } 338 else 339 { 341 return getNextSibling(newNode, root); 342 } 343 344 } 346 351 Node getPreviousSibling(Node node) { 352 return getPreviousSibling(node, fRoot); 353 } 354 355 361 Node getPreviousSibling(Node node, Node root) { 362 363 if (node == null || node == root) return null; 364 365 Node newNode = node.getPreviousSibling(); 366 if (newNode == null) { 367 368 newNode = node.getParentNode(); 369 if (newNode == null || newNode == root) return null; 370 371 int parentAccept = acceptNode(newNode); 372 373 if (parentAccept==NodeFilter.FILTER_SKIP) { 374 return getPreviousSibling(newNode, root); 375 } 376 377 return null; 378 } 379 380 int accept = acceptNode(newNode); 381 382 if (accept == NodeFilter.FILTER_ACCEPT) 383 return newNode; 384 else 385 if (accept == NodeFilter.FILTER_SKIP) { 386 Node fChild = getLastChild(newNode); 387 if (fChild == null) { 388 return getPreviousSibling(newNode, root); 389 } 390 return fChild; 391 } 392 else 393 { 395 return getPreviousSibling(newNode, root); 396 } 397 398 } 400 405 Node getFirstChild(Node node) { 406 if (node == null) return null; 407 408 if ( !fEntityReferenceExpansion 409 && node.getNodeType() == Node.ENTITY_REFERENCE_NODE) 410 return null; 411 Node newNode = node.getFirstChild(); 412 if (newNode == null) return null; 413 int accept = acceptNode(newNode); 414 415 if (accept == NodeFilter.FILTER_ACCEPT) 416 return newNode; 417 else 418 if (accept == NodeFilter.FILTER_SKIP 419 && newNode.hasChildNodes()) 420 { 421 Node fChild = getFirstChild(newNode); 422 423 if (fChild == null) { 424 return getNextSibling(newNode, node); 425 } 426 return fChild; 427 } 428 else 429 { 431 return getNextSibling(newNode, node); 432 } 433 434 435 } 436 437 442 Node getLastChild(Node node) { 443 444 if (node == null) return null; 445 446 if ( !fEntityReferenceExpansion 447 && node.getNodeType() == Node.ENTITY_REFERENCE_NODE) 448 return null; 449 450 Node newNode = node.getLastChild(); 451 if (newNode == null) return null; 452 453 int accept = acceptNode(newNode); 454 455 if (accept == NodeFilter.FILTER_ACCEPT) 456 return newNode; 457 else 458 if (accept == NodeFilter.FILTER_SKIP 459 && newNode.hasChildNodes()) 460 { 461 Node lChild = getLastChild(newNode); 462 if (lChild == null) { 463 return getPreviousSibling(newNode, node); 464 } 465 return lChild; 466 } 467 else 468 { 470 return getPreviousSibling(newNode, node); 471 } 472 473 474 } 475 476 478 short acceptNode(Node node) { 479 487 488 if (fNodeFilter == null) { 489 if ( ( fWhatToShow & (1 << node.getNodeType()-1)) != 0) { 490 return NodeFilter.FILTER_ACCEPT; 491 } else { 492 return NodeFilter.FILTER_SKIP; 493 } 494 } else { 495 if ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) { 496 return fNodeFilter.acceptNode(node); 497 } else { 498 return NodeFilter.FILTER_SKIP; 501 } 502 } 503 } 504 } 505 | Popular Tags |