1 24 package javax.jcr.util; 25 26 import javax.jcr.*; 27 import java.util.LinkedList ; 28 29 46 public abstract class TraversingItemVisitor implements ItemVisitor { 47 48 52 final protected boolean breadthFirst; 53 54 59 final protected int maxLevel; 60 61 64 private LinkedList currentQueue; 65 private LinkedList nextQueue; 66 67 70 private int currentLevel; 71 72 78 public TraversingItemVisitor() { 79 this(false, -1); 80 } 81 82 89 public TraversingItemVisitor(boolean breadthFirst) { 90 this(breadthFirst, -1); 91 } 92 93 104 public TraversingItemVisitor(boolean breadthFirst, int maxLevel) { 105 this.breadthFirst = breadthFirst; 106 if (breadthFirst) { 107 currentQueue = new LinkedList (); 108 nextQueue = new LinkedList (); 109 } 110 currentLevel = 0; 111 this.maxLevel = maxLevel; 112 } 113 114 122 protected abstract void entering(Property property, int level) 123 throws RepositoryException; 124 125 133 protected abstract void entering(Node node, int level) 134 throws RepositoryException; 135 136 144 protected abstract void leaving(Property property, int level) 145 throws RepositoryException; 146 147 155 protected abstract void leaving(Node node, int level) 156 throws RepositoryException; 157 158 171 public void visit(Property property) throws RepositoryException { 172 entering(property, currentLevel); 173 leaving(property, currentLevel); 174 } 175 176 188 public void visit(Node node) 189 throws RepositoryException { 190 try { 191 if (!breadthFirst) { 192 entering(node, currentLevel); 194 if (maxLevel == -1 || currentLevel < maxLevel) { 195 currentLevel++; 196 NodeIterator nodeIter = node.getNodes(); 197 while (nodeIter.hasNext()) { 198 nodeIter.nextNode().accept(this); 199 } 200 PropertyIterator propIter = node.getProperties(); 201 while (propIter.hasNext()) { 202 propIter.nextProperty().accept(this); 203 } 204 currentLevel--; 205 } 206 leaving(node, currentLevel); 207 } else { 208 entering(node, currentLevel); 210 leaving(node, currentLevel); 211 212 if (maxLevel == -1 || currentLevel < maxLevel) { 213 NodeIterator nodeIter = node.getNodes(); 214 while (nodeIter.hasNext()) { 215 nextQueue.addLast(nodeIter.nextNode()); 216 } 217 PropertyIterator propIter = node.getProperties(); 218 while (propIter.hasNext()) { 219 nextQueue.addLast(propIter.nextProperty()); 220 } 221 } 222 223 while (!currentQueue.isEmpty() || !nextQueue.isEmpty()) { 224 if (currentQueue.isEmpty()) { 225 currentLevel++; 226 currentQueue = nextQueue; 227 nextQueue = new LinkedList (); 228 } 229 Item e = (Item) currentQueue.removeFirst(); 230 e.accept(this); 231 } 232 currentLevel = 0; 233 } 234 } catch (RepositoryException re) { 235 currentLevel = 0; 236 throw re; 237 } 238 } 239 240 244 public static class Default extends TraversingItemVisitor { 245 246 249 public Default() { 250 } 251 252 255 public Default(boolean breadthFirst) { 256 super(breadthFirst); 257 } 258 259 262 public Default(boolean breadthFirst, int maxLevel) { 263 super(breadthFirst, maxLevel); 264 } 265 266 269 protected void entering(Node node, int level) 270 throws RepositoryException { 271 } 272 273 276 protected void entering(Property property, int level) 277 throws RepositoryException { 278 } 279 280 283 protected void leaving(Node node, int level) 284 throws RepositoryException { 285 } 286 287 290 protected void leaving(Property property, int level) 291 throws RepositoryException { 292 } 293 } 294 } 295 | Popular Tags |