1 26 package org.htmlparser.util; 27 28 import org.htmlparser.Node; 29 30 47 public class NodeTreeWalker implements NodeIterator 48 { 49 50 53 protected Node mRootNode; 54 55 58 protected Node mCurrentNode; 59 60 64 protected Node mNextNode; 65 66 70 protected int mMaxDepth; 71 72 75 protected boolean mDepthFirst; 76 77 82 public NodeTreeWalker(Node rootNode) 83 { 84 this(rootNode, true, -1); 85 } 86 87 93 public NodeTreeWalker(Node rootNode, boolean depthFirst) 94 { 95 this(rootNode, depthFirst, -1); 96 } 97 98 106 public NodeTreeWalker(Node rootNode, boolean depthFirst, int maxDepth) 107 { 108 if ( ! ((maxDepth >= 1) || (maxDepth == -1))) throw new IllegalArgumentException ("Paramater maxDepth must be > 0 or equal to -1."); 111 initRootNode(rootNode); this.mDepthFirst = depthFirst; 113 this.mMaxDepth = maxDepth; 114 } 115 116 120 public boolean isDepthFirst() 121 { 122 return (this.mDepthFirst); 123 } 124 125 129 public void setDepthFirst(boolean depthFirst) 130 { 131 if (this.mDepthFirst != depthFirst) this.mNextNode = null; 133 this.mDepthFirst = depthFirst; 134 } 135 136 140 public int getMaxDepth() 141 { 142 return (this.mMaxDepth); 143 } 144 145 148 public void removeMaxDepthRestriction() 149 { 150 this.mMaxDepth = -1; 151 } 152 153 157 public Node getRootNode() 158 { 159 return (this.mRootNode); 160 } 161 162 166 public Node getCurrentNode() 167 { 168 return (this.mCurrentNode); 169 } 170 171 176 public void setCurrentNodeAsRootNode() throws NullPointerException 177 { 178 if (this.mCurrentNode == null) 179 throw new NullPointerException ("Current Node is null, cannot set as root Node."); 180 initRootNode(this.mCurrentNode); 181 } 182 183 189 public void setRootNode(Node rootNode) throws NullPointerException 190 { 191 initRootNode(rootNode); 192 } 193 194 198 public void reset() 199 { 200 this.mCurrentNode = null; 201 this.mNextNode = null; 202 } 203 204 208 public Node nextNode() 209 { 210 if (this.mNextNode != null) { 212 this.mCurrentNode = this.mNextNode; 213 this.mNextNode = null; } 215 else 216 { 217 if (this.mCurrentNode == null) 219 this.mCurrentNode = this.mRootNode.getFirstChild(); 220 else 221 { 222 if (this.mDepthFirst) 223 this.mCurrentNode = getNextNodeDepthFirst(); 224 else 225 this.mCurrentNode = getNextNodeBreadthFirst(); 226 } 227 } 228 return (this.mCurrentNode); 229 } 230 231 237 public int getCurrentNodeDepth() 238 { 239 int depth = 0; 240 if (this.mCurrentNode != null) { 242 Node traverseNode = this.mCurrentNode; 243 while (traverseNode != this.mRootNode) 244 { 245 ++depth; 246 traverseNode = traverseNode.getParent(); 247 } 248 } 249 return (depth); 250 } 251 252 256 public boolean hasMoreNodes() 257 { 258 if (this.mNextNode == null) { 260 if (this.mCurrentNode == null) 261 this.mNextNode = this.mRootNode.getFirstChild(); 262 else 263 { 264 if (this.mDepthFirst) 265 this.mNextNode = getNextNodeDepthFirst(); 266 else 267 this.mNextNode = getNextNodeBreadthFirst(); 268 } 269 } 270 return (this.mNextNode != null); 271 } 272 273 279 protected void initRootNode(Node rootNode) throws NullPointerException 280 { 281 if (rootNode == null) 282 throw new NullPointerException ("Root Node cannot be null."); 283 this.mRootNode = rootNode; 284 this.mCurrentNode = null; 285 this.mNextNode = null; 286 } 287 288 292 protected Node getNextNodeDepthFirst() 293 { 294 int currentDepth = getCurrentNodeDepth(); 296 Node traverseNode = null; 297 if ((this.mMaxDepth == -1) || (currentDepth < this.mMaxDepth)) { 299 traverseNode = this.mCurrentNode.getFirstChild(); 300 if (traverseNode != null) 301 return (traverseNode); 302 } 303 304 traverseNode = this.mCurrentNode; 305 306 Node tempNextSibling = null; while ((traverseNode != this.mRootNode) && (tempNextSibling = traverseNode.getNextSibling()) == null) traverseNode = traverseNode.getParent(); 310 return (tempNextSibling); } 312 313 317 protected Node getNextNodeBreadthFirst() 318 { 319 Node traverseNode; 320 321 traverseNode = this.mCurrentNode.getNextSibling(); 323 if (traverseNode != null) 324 return (traverseNode); 325 326 int depth = getCurrentNodeDepth(); 327 328 330 NodeList traverseNodeList; 331 332 traverseNode = this.mCurrentNode.getParent(); 334 int currentDepth = depth - 1; 335 336 while(currentDepth > 0) { 338 Node tempNextSibling = null; while(((tempNextSibling = traverseNode.getNextSibling()) == null) && (traverseNode != this.mRootNode)) { 342 traverseNode = traverseNode.getParent(); 343 --currentDepth; 344 } 345 346 if (traverseNode == this.mRootNode) 348 break; 349 350 traverseNode = tempNextSibling; 351 352 if (traverseNode != null) 353 { 354 traverseNodeList = traverseNode.getChildren(); 356 while((traverseNodeList != null) && (traverseNodeList.size() != 0)) 357 { 358 traverseNode = traverseNode.getFirstChild(); 359 ++currentDepth; 360 if (currentDepth == depth) 361 return (traverseNode); else 363 traverseNodeList = traverseNode.getChildren(); 364 } } } 368 370 if (this.mMaxDepth != -1) { 373 if (depth >= this.mMaxDepth) 374 return (null); } 376 377 traverseNode = this.mRootNode.getFirstChild(); 378 ++depth; currentDepth = 1; 380 while(currentDepth > 0) 381 { 382 traverseNodeList = traverseNode.getChildren(); 384 while((traverseNodeList != null) && (traverseNodeList.size() != 0)) 385 { 386 traverseNode = traverseNode.getFirstChild(); 387 ++currentDepth; 388 if (currentDepth == depth) 389 return (traverseNode); else 391 traverseNodeList = traverseNode.getChildren(); 392 } 394 while((traverseNode.getNextSibling() == null) && (traverseNode != this.mRootNode)) 396 { 397 traverseNode = traverseNode.getParent(); 398 --currentDepth; 399 } 400 traverseNode = traverseNode.getNextSibling(); 401 if (traverseNode == null) return (null); 403 } 405 return (null); 407 } 408 409 410 412 } | Popular Tags |