1 57 58 package org.enhydra.apache.xerces.dom; 59 60 import org.w3c.dom.Node ; 61 import org.w3c.dom.traversal.NodeFilter; 62 import org.w3c.dom.traversal.TreeWalker; 63 64 65 public class TreeWalkerImpl implements TreeWalker { 66 67 71 72 private boolean fEntityReferenceExpansion = false; 73 74 int fWhatToShow = NodeFilter.SHOW_ALL; 75 76 NodeFilter fNodeFilter; 77 78 Node fCurrentNode; 79 80 Node fRoot; 81 82 88 89 93 94 public TreeWalkerImpl(Node root, 95 int whatToShow, 96 NodeFilter nodeFilter, 97 boolean entityReferenceExpansion) { 98 fCurrentNode = root; 99 fRoot = root; 100 fWhatToShow = whatToShow; 101 fNodeFilter = nodeFilter; 102 fEntityReferenceExpansion = entityReferenceExpansion; 103 } 104 105 public Node getRoot() { 106 return fRoot; 107 } 108 109 110 public int getWhatToShow() { 111 return fWhatToShow; 112 } 113 114 public void setWhatShow(int whatToShow){ 115 fWhatToShow = whatToShow; 116 } 117 118 public NodeFilter getFilter() { 119 return fNodeFilter; 120 } 121 122 123 public boolean getExpandEntityReferences() { 124 return fEntityReferenceExpansion; 125 } 126 127 128 public Node getCurrentNode() { 129 return fCurrentNode; 130 } 131 132 public void setCurrentNode(Node node) { 133 fCurrentNode = node; 134 } 135 136 140 public Node parentNode() { 141 142 if (fCurrentNode == null) return null; 143 144 Node node = getParentNode(fCurrentNode); 145 if (node !=null) { 146 fCurrentNode = node; 147 } 148 return node; 149 150 } 151 152 156 public Node firstChild() { 157 158 if (fCurrentNode == null) return null; 159 160 Node node = getFirstChild(fCurrentNode); 161 if (node !=null) { 162 fCurrentNode = node; 163 } 164 return node; 165 } 166 170 public Node lastChild() { 171 172 if (fCurrentNode == null) return null; 173 174 Node node = getLastChild(fCurrentNode); 175 if (node !=null) { 176 fCurrentNode = node; 177 } 178 return node; 179 } 180 181 185 public Node previousSibling() { 186 187 if (fCurrentNode == null) return null; 188 189 Node node = getPreviousSibling(fCurrentNode); 190 if (node !=null) { 191 fCurrentNode = node; 192 } 193 return node; 194 } 195 196 200 public Node nextSibling(){ 201 if (fCurrentNode == null) return null; 202 203 Node node = getNextSibling(fCurrentNode); 204 if (node !=null) { 205 fCurrentNode = node; 206 } 207 return node; 208 } 209 210 214 public Node previousNode() { 215 Node result; 216 217 if (fCurrentNode == null) return null; 218 219 result = getPreviousSibling(fCurrentNode); 221 if (result == null) { 222 result = getParentNode(fCurrentNode); 223 if (result != null) { 224 fCurrentNode = result; 225 return fCurrentNode; 226 } 227 return null; 228 } 229 230 Node lastChild = getLastChild(result); 232 233 Node prev = lastChild ; 234 while (lastChild != null) { 235 prev = lastChild ; 236 lastChild = getLastChild(prev) ; 237 } 238 239 lastChild = prev ; 240 241 if (lastChild != null) { 243 fCurrentNode = lastChild; 244 return fCurrentNode; 245 } 246 247 if (result != null) { 249 fCurrentNode = result; 250 return fCurrentNode; 251 } 252 253 return null; 255 } 256 257 261 public Node nextNode() { 262 263 if (fCurrentNode == null) return null; 264 265 Node result = getFirstChild(fCurrentNode); 266 267 if (result != null) { 268 fCurrentNode = result; 269 return result; 270 } 271 272 result = getNextSibling(fCurrentNode); 273 274 if (result != null) { 275 fCurrentNode = result; 276 return result; 277 } 278 279 Node parent = getParentNode(fCurrentNode); 281 while (parent != null) { 282 result = getNextSibling(parent); 283 if (result != null) { 284 fCurrentNode = result; 285 return result; 286 } else { 287 parent = getParentNode(parent); 288 } 289 } 290 291 return null; 293 } 294 295 300 Node getParentNode(Node node) { 301 302 if (node == null || node == fRoot) return null; 303 304 Node newNode = node.getParentNode(); 305 if (newNode == null) return null; 306 307 int accept = acceptNode(newNode); 308 309 if (accept == NodeFilter.FILTER_ACCEPT) 310 return newNode; 311 else 312 { 314 return getParentNode(newNode); 315 } 316 317 318 } 319 320 325 Node getNextSibling(Node node) { 326 return getNextSibling(node, fRoot); 327 } 328 329 335 Node getNextSibling(Node node, Node root) { 336 337 if (node == null || node == root) return null; 338 339 Node newNode = node.getNextSibling(); 340 if (newNode == null) { 341 342 newNode = node.getParentNode(); 343 344 if (newNode == null || newNode == root) return null; 345 346 int parentAccept = acceptNode(newNode); 347 348 if (parentAccept==NodeFilter.FILTER_SKIP) { 349 return getNextSibling(newNode, root); 350 } 351 352 return null; 353 } 354 355 int accept = acceptNode(newNode); 356 357 if (accept == NodeFilter.FILTER_ACCEPT) 358 return newNode; 359 else 360 if (accept == NodeFilter.FILTER_SKIP) { 361 Node fChild = getFirstChild(newNode); 362 if (fChild == null) { 363 return getNextSibling(newNode, root); 364 } 365 return fChild; 366 } 367 else 368 { 370 return getNextSibling(newNode, root); 371 } 372 373 } 375 380 Node getPreviousSibling(Node node) { 381 return getPreviousSibling(node, fRoot); 382 } 383 384 390 Node getPreviousSibling(Node node, Node root) { 391 392 if (node == null || node == root) return null; 393 394 Node newNode = node.getPreviousSibling(); 395 if (newNode == null) { 396 397 newNode = node.getParentNode(); 398 if (newNode == null || newNode == root) return null; 399 400 int parentAccept = acceptNode(newNode); 401 402 if (parentAccept==NodeFilter.FILTER_SKIP) { 403 return getPreviousSibling(newNode, root); 404 } 405 406 return null; 407 } 408 409 int accept = acceptNode(newNode); 410 411 if (accept == NodeFilter.FILTER_ACCEPT) 412 return newNode; 413 else 414 if (accept == NodeFilter.FILTER_SKIP) { 415 Node fChild = getLastChild(newNode); 416 if (fChild == null) { 417 return getPreviousSibling(newNode, root); 418 } 419 return fChild; 420 } 421 else 422 { 424 return getPreviousSibling(newNode, root); 425 } 426 427 } 429 434 Node getFirstChild(Node node) { 435 if (node == null) return null; 436 437 if ( !fEntityReferenceExpansion 438 && node.getNodeType() == Node.ENTITY_REFERENCE_NODE) 439 return null; 440 Node newNode = node.getFirstChild(); 441 if (newNode == null) return null; 442 int accept = acceptNode(newNode); 443 444 if (accept == NodeFilter.FILTER_ACCEPT) 445 return newNode; 446 else 447 if (accept == NodeFilter.FILTER_SKIP 448 && newNode.hasChildNodes()) 449 { 450 Node fChild = getFirstChild(newNode); 451 452 if (fChild == null) { 453 return getNextSibling(newNode, node); 454 } 455 return fChild; 456 } 457 else 458 { 460 return getNextSibling(newNode, node); 461 } 462 463 464 } 465 466 471 Node getLastChild(Node node) { 472 473 if (node == null) return null; 474 475 if ( !fEntityReferenceExpansion 476 && node.getNodeType() == Node.ENTITY_REFERENCE_NODE) 477 return null; 478 479 Node newNode = node.getLastChild(); 480 if (newNode == null) return null; 481 482 int accept = acceptNode(newNode); 483 484 if (accept == NodeFilter.FILTER_ACCEPT) 485 return newNode; 486 else 487 if (accept == NodeFilter.FILTER_SKIP 488 && newNode.hasChildNodes()) 489 { 490 Node lChild = getLastChild(newNode); 491 if (lChild == null) { 492 return getPreviousSibling(newNode, node); 493 } 494 return lChild; 495 } 496 else 497 { 499 return getPreviousSibling(newNode, node); 500 } 501 502 503 } 504 505 507 short acceptNode(Node node) { 508 516 517 if (fNodeFilter == null) { 518 if ( ( fWhatToShow & (1 << node.getNodeType()-1)) != 0) { 519 return NodeFilter.FILTER_ACCEPT; 520 } else { 521 return NodeFilter.FILTER_SKIP; 522 } 523 } else { 524 if ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) { 525 return fNodeFilter.acceptNode(node); 526 } else { 527 return NodeFilter.FILTER_SKIP; 530 } 531 } 532 } 533 } 534 | Popular Tags |