1 57 58 package org.xquark.xpath.datamodel.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 115 public NodeFilter getFilter() { 116 return fNodeFilter; 117 } 118 119 120 public boolean getExpandEntityReferences() { 121 return fEntityReferenceExpansion; 122 } 123 124 125 public Node getCurrentNode() { 126 return fCurrentNode; 127 } 128 129 public void setCurrentNode(Node node) { 130 fCurrentNode = node; 131 } 132 133 137 public Node parentNode() { 138 139 if (fCurrentNode == null) return null; 140 141 Node node = getParentNode(fCurrentNode); 142 if (node !=null) { 143 fCurrentNode = node; 144 } 145 return node; 146 147 } 148 149 153 public Node firstChild() { 154 155 if (fCurrentNode == null) return null; 156 157 Node node = getFirstChild(fCurrentNode); 158 if (node !=null) { 159 fCurrentNode = node; 160 } 161 return node; 162 } 163 167 public Node lastChild() { 168 169 if (fCurrentNode == null) return null; 170 171 Node node = getLastChild(fCurrentNode); 172 if (node !=null) { 173 fCurrentNode = node; 174 } 175 return node; 176 } 177 178 182 public Node previousSibling() { 183 184 if (fCurrentNode == null) return null; 185 186 Node node = getPreviousSibling(fCurrentNode); 187 if (node !=null) { 188 fCurrentNode = node; 189 } 190 return node; 191 } 192 193 197 public Node nextSibling(){ 198 if (fCurrentNode == null) return null; 199 200 Node node = getNextSibling(fCurrentNode); 201 if (node !=null) { 202 fCurrentNode = node; 203 } 204 return node; 205 } 206 207 211 public Node previousNode() { 212 Node result; 213 214 if (fCurrentNode == null) return null; 215 216 result = getPreviousSibling(fCurrentNode); 218 if (result == null) { 219 result = getParentNode(fCurrentNode); 220 if (result != null) { 221 fCurrentNode = result; 222 return fCurrentNode; 223 } 224 return null; 225 } 226 227 Node lastChild = getLastChild(result); 229 230 Node prev = lastChild ; 231 while (lastChild != null) { 232 prev = lastChild ; 233 lastChild = getLastChild(prev) ; 234 } 235 236 lastChild = prev ; 237 238 if (lastChild != null) { 240 fCurrentNode = lastChild; 241 return fCurrentNode; 242 } 243 244 if (result != null) { 246 fCurrentNode = result; 247 return fCurrentNode; 248 } 249 250 return null; 252 } 253 254 258 public Node nextNode() { 259 260 if (fCurrentNode == null) return null; 261 262 Node result = getFirstChild(fCurrentNode); 263 264 if (result != null) { 265 fCurrentNode = result; 266 return result; 267 } 268 269 result = getNextSibling(fCurrentNode); 270 271 if (result != null) { 272 fCurrentNode = result; 273 return result; 274 } 275 276 Node parent = getParentNode(fCurrentNode); 278 while (parent != null) { 279 result = getNextSibling(parent); 280 if (result != null) { 281 fCurrentNode = result; 282 return result; 283 } else { 284 parent = getParentNode(parent); 285 } 286 } 287 288 return null; 290 } 291 292 297 Node getParentNode(Node node) { 298 299 if (node == null || node == fRoot) return null; 300 301 Node newNode = node.getParentNode(); 302 if (newNode == null) return null; 303 304 int accept = acceptNode(newNode); 305 306 if (accept == NodeFilter.FILTER_ACCEPT) 307 return newNode; 308 else 309 { 311 return getParentNode(newNode); 312 } 313 314 315 } 316 317 322 Node getNextSibling(Node node) { 323 324 if (node == null || node == fRoot) return null; 325 326 Node newNode = node.getNextSibling(); 327 if (newNode == null) { 328 329 newNode = node.getParentNode(); 330 331 if (newNode == null || node == fRoot) return null; 332 333 int parentAccept = acceptNode(newNode); 334 335 if (parentAccept==NodeFilter.FILTER_SKIP) { 336 return getNextSibling(newNode); 337 } 338 339 return null; 340 } 341 342 int accept = acceptNode(newNode); 343 344 if (accept == NodeFilter.FILTER_ACCEPT) 345 return newNode; 346 else 347 if (accept == NodeFilter.FILTER_SKIP) { 348 Node fChild = getFirstChild(newNode); 349 if (fChild == null) { 350 return getNextSibling(newNode); 351 } 352 return fChild; 353 } 354 else 355 { 357 return getNextSibling(newNode); 358 } 359 360 } 362 367 Node getPreviousSibling(Node node) { 368 369 if (node == null || node == fRoot) return null; 370 371 Node newNode = node.getPreviousSibling(); 372 if (newNode == null) { 373 374 newNode = node.getParentNode(); 375 if (newNode == null || node == fRoot) return null; 376 377 int parentAccept = acceptNode(newNode); 378 379 if (parentAccept==NodeFilter.FILTER_SKIP) { 380 return getPreviousSibling(newNode); 381 } 382 383 return null; 384 } 385 386 int accept = acceptNode(newNode); 387 388 if (accept == NodeFilter.FILTER_ACCEPT) 389 return newNode; 390 else 391 if (accept == NodeFilter.FILTER_SKIP) { 392 Node fChild = getLastChild(newNode); 393 if (fChild == null) { 394 return getPreviousSibling(newNode); 395 } 396 return fChild; 397 } 398 else 399 { 401 return getPreviousSibling(newNode); 402 } 403 404 } 406 411 Node getFirstChild(Node node) { 412 413 if (node == null) return null; 414 415 if ( !fEntityReferenceExpansion 416 && node.getNodeType() == Node.ENTITY_REFERENCE_NODE) 417 return null; 418 419 Node newNode = node.getFirstChild(); 420 if (newNode == null) return null; 421 422 int accept = acceptNode(newNode); 423 424 if (accept == NodeFilter.FILTER_ACCEPT) 425 return newNode; 426 else 427 if (accept == NodeFilter.FILTER_SKIP 428 && newNode.hasChildNodes()) 429 { 430 return getFirstChild(newNode); 431 } 432 else 433 { 435 return getNextSibling(newNode); 436 } 437 438 439 } 440 441 446 Node getLastChild(Node node) { 447 448 if (node == null) return null; 449 450 if ( !fEntityReferenceExpansion 451 && node.getNodeType() == Node.ENTITY_REFERENCE_NODE) 452 return null; 453 454 Node newNode = node.getLastChild(); 455 if (newNode == null) return null; 456 457 int accept = acceptNode(newNode); 458 459 if (accept == NodeFilter.FILTER_ACCEPT) 460 return newNode; 461 else 462 if (accept == NodeFilter.FILTER_SKIP 463 && newNode.hasChildNodes()) 464 { 465 return getLastChild(newNode); 466 } 467 else 468 { 470 return getPreviousSibling(newNode); 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 |