1 57 58 package com.sun.org.apache.xerces.internal.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 78 public class NodeIteratorImpl implements NodeIterator { 79 80 84 85 private DocumentImpl fDocument; 86 87 private Node fRoot; 88 89 private int fWhatToShow = NodeFilter.SHOW_ALL; 90 91 private NodeFilter fNodeFilter; 92 93 private boolean fDetach = false; 94 95 108 109 private Node fCurrentNode; 110 111 117 private boolean fForward = true; 118 119 120 private boolean fEntityReferenceExpansion; 121 122 126 127 public NodeIteratorImpl( DocumentImpl document, 128 Node root, 129 int whatToShow, 130 NodeFilter nodeFilter, 131 boolean entityReferenceExpansion) { 132 fDocument = document; 133 fRoot = root; 134 fCurrentNode = null; 135 fWhatToShow = whatToShow; 136 fNodeFilter = nodeFilter; 137 fEntityReferenceExpansion = entityReferenceExpansion; 138 } 139 140 public Node getRoot() { 141 return fRoot; 142 } 143 144 148 149 public int getWhatToShow() { 150 return fWhatToShow; 151 } 152 153 154 public NodeFilter getFilter() { 155 return fNodeFilter; 156 } 157 158 159 public boolean getExpandEntityReferences() { 160 return fEntityReferenceExpansion; 161 } 162 163 167 public Node nextNode() { 168 169 if( fDetach) { 170 throw new DOMException ( 171 DOMException.INVALID_STATE_ERR, 172 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); 173 } 174 175 if (fRoot == null) return null; 177 178 Node nextNode = fCurrentNode; 179 boolean accepted = false; 181 accepted_loop: 182 while (!accepted) { 183 184 if (!fForward && nextNode!=null) { 186 nextNode = fCurrentNode; 188 } else { 189 if (!fEntityReferenceExpansion 191 && nextNode != null 192 && nextNode.getNodeType() == Node.ENTITY_REFERENCE_NODE) { 193 nextNode = nextNode(nextNode, false); 194 } else { 195 nextNode = nextNode(nextNode, true); 196 } 197 } 198 199 fForward = true; 201 if (nextNode == null) return null; 203 204 accepted = acceptNode(nextNode); 206 if (accepted) { 207 fCurrentNode = nextNode; 209 return fCurrentNode; 210 } else 211 continue accepted_loop; 212 213 } 215 return null; 217 218 } 219 220 223 public Node previousNode() { 224 225 if( fDetach) { 226 throw new DOMException ( 227 DOMException.INVALID_STATE_ERR, 228 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); 229 } 230 231 if (fRoot == null || fCurrentNode == null) return null; 233 234 Node previousNode = fCurrentNode; 235 boolean accepted = false; 236 237 accepted_loop: 238 while (!accepted) { 239 240 if (fForward && previousNode != null) { 241 previousNode = fCurrentNode; 243 } else { 244 previousNode = previousNode(previousNode); 246 } 247 248 fForward = false; 250 251 if (previousNode == null) return null; 254 255 accepted = acceptNode(previousNode); 257 if (accepted) { 258 fCurrentNode = previousNode; 260 return fCurrentNode; 261 } else 262 continue accepted_loop; 263 } 264 return null; 266 } 267 268 269 boolean acceptNode(Node node) { 270 271 if (fNodeFilter == null) { 272 return ( fWhatToShow & (1 << node.getNodeType()-1)) != 0 ; 273 } else { 274 return ((fWhatToShow & (1 << node.getNodeType()-1)) != 0 ) 275 && fNodeFilter.acceptNode(node) == NodeFilter.FILTER_ACCEPT; 276 } 277 } 278 279 280 Node matchNodeOrParent(Node node) { 281 if (fCurrentNode == null) return null; 284 285 for (Node n = fCurrentNode; n != fRoot; n = n.getParentNode()) { 288 if (node == n) return n; 289 } 290 return null; 291 } 292 293 299 Node nextNode(Node node, boolean visitChildren) { 300 301 if (node == null) return fRoot; 302 303 Node result; 304 if (visitChildren) { 306 if (node.hasChildNodes()) { 308 result = node.getFirstChild(); 309 return result; 310 } 311 } 312 313 if (node == fRoot) { return null; 315 } 316 317 result = node.getNextSibling(); 319 if (result != null) return result; 320 321 322 Node parent = node.getParentNode(); 324 while (parent != null && parent != fRoot) { 325 result = parent.getNextSibling(); 326 if (result != null) { 327 return result; 328 } else { 329 parent = parent.getParentNode(); 330 } 331 332 } 334 return null; 336 } 337 338 341 Node previousNode(Node node) { 342 343 Node result; 344 345 if (node == fRoot) return null; 347 348 result = node.getPreviousSibling(); 350 if (result == null) { 351 result = node.getParentNode(); 353 return result; 354 } 355 356 if (result.hasChildNodes() 358 && !(!fEntityReferenceExpansion 359 && result != null 360 && result.getNodeType() == Node.ENTITY_REFERENCE_NODE)) 361 362 { 363 while (result.hasChildNodes()) { 364 result = result.getLastChild(); 365 } 366 } 367 368 return result; 369 } 370 371 374 public void removeNode(Node node) { 375 376 379 if (node == null) return; 380 381 Node deleted = matchNodeOrParent(node); 382 383 if (deleted == null) return; 384 385 if (fForward) { 386 fCurrentNode = previousNode(deleted); 387 } else 388 { 390 Node next = nextNode(deleted, false); 391 if (next!=null) { 392 fCurrentNode = next; 394 } else { 395 fCurrentNode = previousNode(deleted); 398 fForward = true; 399 } 400 401 } 402 403 } 404 405 public void detach() { 406 fDetach = true; 407 fDocument.removeNodeIterator(this); 408 } 409 410 } 411 | Popular Tags |