1 18 package org.apache.batik.dom.traversal; 19 20 import org.apache.batik.dom.AbstractNode; 21 import org.w3c.dom.DOMException ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.traversal.NodeFilter; 24 import org.w3c.dom.traversal.TreeWalker; 25 26 33 public class DOMTreeWalker implements TreeWalker { 34 35 38 protected Node root; 39 40 43 protected int whatToShow; 44 45 48 protected NodeFilter filter; 49 50 54 protected boolean expandEntityReferences; 55 56 59 protected Node currentNode; 60 61 69 public DOMTreeWalker(Node n, int what, NodeFilter nf, boolean exp) { 70 root = n; 71 whatToShow = what; 72 filter = nf; 73 expandEntityReferences = exp; 74 75 currentNode = root; 76 } 77 78 81 public Node getRoot() { 82 return root; 83 } 84 85 88 public int getWhatToShow() { 89 return whatToShow; 90 } 91 92 95 public NodeFilter getFilter() { 96 return filter; 97 } 98 99 102 public boolean getExpandEntityReferences() { 103 return expandEntityReferences; 104 } 105 106 109 public Node getCurrentNode() { 110 return currentNode; 111 } 112 113 116 public void setCurrentNode(Node n) { 117 if (n == null) { 118 throw ((AbstractNode)root).createDOMException 119 (DOMException.NOT_SUPPORTED_ERR, 120 "null.current.node", null); 121 } 122 currentNode = n; 123 } 124 125 128 public Node parentNode() { 129 Node result = parentNode(currentNode); 130 if (result != null) { 131 currentNode = result; 132 } 133 return result; 134 } 135 136 139 public Node firstChild() { 140 Node result = firstChild(currentNode); 141 if (result != null) { 142 currentNode = result; 143 } 144 return result; 145 } 146 147 150 public Node lastChild() { 151 Node result = lastChild(currentNode); 152 if (result != null) { 153 currentNode = result; 154 } 155 return result; 156 } 157 158 161 public Node previousSibling() { 162 Node result = previousSibling(currentNode, root); 163 if (result != null) { 164 currentNode = result; 165 } 166 return result; 167 } 168 169 172 public Node nextSibling() { 173 Node result = nextSibling(currentNode, root); 174 if (result != null) { 175 currentNode = result; 176 } 177 return result; 178 } 179 180 183 public Node previousNode() { 184 Node result = previousSibling(currentNode, root); 185 if (result == null) { 186 result = parentNode(currentNode); 187 if (result != null) { 188 currentNode = result; 189 } 190 return result; 191 } 192 Node n = lastChild(result); 193 Node last = n; 194 while (n != null) { 195 last = n; 196 n = lastChild(last); 197 } 198 return currentNode = (last != null) ? last : result; 199 } 200 201 204 public Node nextNode() { 205 Node result; 206 if ((result = firstChild(currentNode)) != null) { 207 return currentNode = result; 208 } 209 if ((result = nextSibling(currentNode, root)) != null) { 210 return currentNode = result; 211 } 212 Node parent = currentNode; 213 for (;;) { 214 parent = parentNode(parent); 215 if (parent == null) { 216 return null; 217 } 218 if ((result = nextSibling(parent, root)) != null) { 219 return currentNode = result; 220 } 221 } 222 } 223 224 227 protected Node parentNode(Node n) { 228 if (n == root) { 229 return null; 230 } 231 Node result = n; 232 for (;;) { 233 result = result.getParentNode(); 234 if (result == null) { 235 return null; 236 } 237 if ((whatToShow & (1 << result.getNodeType() - 1)) != 0) { 238 if (filter == null || 239 filter.acceptNode(result) == NodeFilter.FILTER_ACCEPT) { 240 return result; 241 } 242 } 243 } 244 } 245 246 249 protected Node firstChild(Node n) { 250 if (n.getNodeType() == Node.ENTITY_REFERENCE_NODE && 251 !expandEntityReferences) { 252 return null; 253 } 254 Node result = n.getFirstChild(); 255 if (result == null) { 256 return null; 257 } 258 switch (acceptNode(result)) { 259 case NodeFilter.FILTER_ACCEPT: 260 return result; 261 case NodeFilter.FILTER_SKIP: 262 Node t = firstChild(result); 263 if (t != null) { 264 return t; 265 } 266 default: return nextSibling(result, n); 269 } 270 } 271 272 275 protected Node lastChild(Node n) { 276 if (n.getNodeType() == Node.ENTITY_REFERENCE_NODE && 277 !expandEntityReferences) { 278 return null; 279 } 280 Node result = n.getLastChild(); 281 if (result == null) { 282 return null; 283 } 284 switch (acceptNode(result)) { 285 case NodeFilter.FILTER_ACCEPT: 286 return result; 287 case NodeFilter.FILTER_SKIP: 288 Node t = lastChild(result); 289 if (t != null) { 290 return t; 291 } 292 default: return previousSibling(result, n); 295 } 296 } 297 298 301 protected Node previousSibling(Node n, Node root) { 302 while (true) { 303 if (n == root) { 304 return null; 305 } 306 Node result = n.getPreviousSibling(); 307 if (result == null) { 308 result = n.getParentNode(); 309 if (result == null || result == root) { 310 return null; 311 } 312 if (acceptNode(result) == NodeFilter.FILTER_SKIP) { 313 n = result; 314 continue; 315 } 316 return null; 317 } 318 switch (acceptNode(result)) { 319 case NodeFilter.FILTER_ACCEPT: 320 return result; 321 case NodeFilter.FILTER_SKIP: 322 Node t = lastChild(result); 323 if (t != null) { 324 return t; 325 } 326 default: n = result; 329 continue; 330 } 331 } 332 } 333 334 337 protected Node nextSibling(Node n, Node root) { 338 while (true) { 339 if (n == root) { 340 return null; 341 } 342 Node result = n.getNextSibling(); 343 if (result == null) { 344 result = n.getParentNode(); 345 if (result == null || result == root) { 346 return null; 347 } 348 if (acceptNode(result) == NodeFilter.FILTER_SKIP) { 349 n = result; 350 continue; 351 } 352 return null; 353 } 354 355 switch (acceptNode(result)) { 356 case NodeFilter.FILTER_ACCEPT: 357 return result; 358 case NodeFilter.FILTER_SKIP: 359 Node t = firstChild(result); 360 if (t != null) { 361 return t; 362 } 363 default: n = result; 366 continue; 367 } 368 } 369 } 370 371 374 protected short acceptNode(Node n) { 375 if ((whatToShow & (1 << n.getNodeType() - 1)) != 0) { 376 if (filter == null) { 377 return NodeFilter.FILTER_ACCEPT; 378 } else { 379 return filter.acceptNode(n); 380 } 381 } else { 382 return NodeFilter.FILTER_SKIP; 383 } 384 } 385 } 386 | Popular Tags |