1 57 58 package org.enhydra.apache.xerces.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.NodeIterator; 64 65 66 76 public class NodeIteratorImpl implements NodeIterator { 77 78 82 83 private DocumentImpl fDocument; 84 85 private Node fRoot; 86 87 private int fWhatToShow = NodeFilter.SHOW_ALL; 88 89 private NodeFilter fNodeFilter; 90 91 private boolean fDetach = false; 92 93 106 107 private Node fCurrentNode; 108 109 115 private boolean fForward = true; 116 117 118 private boolean fEntityReferenceExpansion; 119 120 124 125 public NodeIteratorImpl( DocumentImpl document, 126 Node root, 127 int whatToShow, 128 NodeFilter nodeFilter, 129 boolean entityReferenceExpansion) { 130 fDocument = document; 131 fRoot = root; 132 fCurrentNode = null; 133 fWhatToShow = whatToShow; 134 fNodeFilter = nodeFilter; 135 fEntityReferenceExpansion = entityReferenceExpansion; 136 } 137 138 public Node getRoot() { 139 return fRoot; 140 } 141 142 146 147 public int getWhatToShow() { 148 return fWhatToShow; 149 } 150 151 152 public NodeFilter getFilter() { 153 return fNodeFilter; 154 } 155 156 157 public boolean getExpandEntityReferences() { 158 return fEntityReferenceExpansion; 159 } 160 161 165 public Node nextNode() { 166 167 if( fDetach) { 168 throw new DOMException ( 169 DOMException.INVALID_STATE_ERR, 170 "DOM011 Invalid state"); 171 } 172 173 if (fRoot == null) return null; 175 176 Node nextNode = fCurrentNode; 177 boolean accepted = false; 179 accepted_loop: 180 while (!accepted) { 181 182 if (!fForward && nextNode!=null) { 184 nextNode = fCurrentNode; 186 } else { 187 if (!fEntityReferenceExpansion 189 && nextNode != null 190 && nextNode.getNodeType() == Node.ENTITY_REFERENCE_NODE) { 191 nextNode = nextNode(nextNode, false); 192 } else { 193 nextNode = nextNode(nextNode, true); 194 } 195 } 196 197 fForward = true; 199 if (nextNode == null) return null; 201 202 accepted = acceptNode(nextNode); 204 if (accepted) { 205 fCurrentNode = nextNode; 207 return fCurrentNode; 208 } else 209 continue accepted_loop; 210 211 } 213 return null; 215 216 } 217 218 221 public Node previousNode() { 222 223 if( fDetach) { 224 throw new DOMException ( 225 DOMException.INVALID_STATE_ERR, 226 "DOM011 Invalid state"); 227 } 228 229 if (fRoot == null || fCurrentNode == null) return null; 231 232 Node previousNode = fCurrentNode; 233 boolean accepted = false; 234 235 accepted_loop: 236 while (!accepted) { 237 238 if (fForward && previousNode != null) { 239 previousNode = fCurrentNode; 241 } else { 242 previousNode = previousNode(previousNode); 244 } 245 246 fForward = false; 248 249 if (previousNode == null) return null; 252 253 accepted = acceptNode(previousNode); 255 if (accepted) { 256 fCurrentNode = previousNode; 258 return fCurrentNode; 259 } else 260 continue accepted_loop; 261 } 262 return null; 264 } 265 266 267 boolean acceptNode(Node node) { 268 269 if (fNodeFilter == null) { 270 return ( fWhatToShow & (1 << node.getNodeType()-1)) != 0 ; 271 } else { 272 return ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) 273 && fNodeFilter.acceptNode(node) == NodeFilter.FILTER_ACCEPT; 274 } 275 } 276 277 278 Node matchNodeOrParent(Node node) { 279 for (Node n = node; n != fRoot; n = n.getParentNode()) { 280 if (node == n) return n; 281 } 282 return null; 283 } 284 285 291 Node nextNode(Node node, boolean visitChildren) { 292 293 if (node == null) return fRoot; 294 295 Node result; 296 if (visitChildren) { 298 if (node.hasChildNodes()) { 300 result = node.getFirstChild(); 301 return result; 302 } 303 } 304 305 if (node == fRoot) { return null; 307 } 308 309 result = node.getNextSibling(); 311 if (result != null) return result; 312 313 314 Node parent = node.getParentNode(); 316 while (parent != null && parent != fRoot) { 317 result = parent.getNextSibling(); 318 if (result != null) { 319 return result; 320 } else { 321 parent = parent.getParentNode(); 322 } 323 324 } 326 return null; 328 } 329 330 333 Node previousNode(Node node) { 334 335 Node result; 336 337 if (node == fRoot) return null; 339 340 result = node.getPreviousSibling(); 342 if (result == null) { 343 result = node.getParentNode(); 345 return result; 346 } 347 348 if (result.hasChildNodes() 350 && !(!fEntityReferenceExpansion 351 && result != null 352 && result.getNodeType() == Node.ENTITY_REFERENCE_NODE)) 353 354 { 355 while (result.hasChildNodes()) { 356 result = result.getLastChild(); 357 } 358 } 359 360 return result; 361 } 362 363 366 public void removeNode(Node node) { 367 368 371 if (node == null) return; 372 373 Node deleted = matchNodeOrParent(node); 374 375 if (deleted == null) return; 376 377 if (fForward) { 378 fCurrentNode = previousNode(deleted); 379 } else 380 { 382 Node next = nextNode(deleted, false); 383 if (next!=null) { 384 fCurrentNode = next; 386 } else { 387 fCurrentNode = previousNode(deleted); 390 fForward = true; 391 } 392 393 } 394 395 } 396 397 public void detach() { 398 fDetach = true; 399 fDocument.removeNodeIterator(this); 400 } 401 402 } 403 | Popular Tags |