1 11 package org.eclipse.core.internal.watson; 12 13 import org.eclipse.core.internal.dtree.AbstractDataTreeNode; 14 import org.eclipse.core.internal.dtree.DataTreeNode; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.Path; 17 18 41 public class ElementTreeIterator implements IPathRequestor { 42 private String [] segments = new String [10]; 44 private int nextFreeSegment; 45 46 47 private ElementTree tree; 48 49 50 private IPath path; 51 52 53 private DataTreeNode treeRoot; 54 55 59 public ElementTreeIterator(ElementTree tree, IPath path) { 60 this.tree = tree; 61 this.path = path; 62 synchronized (tree) { 65 treeRoot = (DataTreeNode) tree.getDataTree().safeCopyCompleteSubtree(path); 66 } 67 } 68 69 73 private void doIteration(DataTreeNode node, IElementContentVisitor visitor) { 74 if (nextFreeSegment >= segments.length) { 76 grow(); 77 } 78 segments[nextFreeSegment++] = node.getName(); 79 80 if (visitor.visitElement(tree, this, node.getData())) { 82 AbstractDataTreeNode[] children = node.getChildren(); 84 for (int i = children.length; --i >= 0;) { 85 doIteration((DataTreeNode) children[i], visitor); 86 } 87 } 88 89 nextFreeSegment--; 91 if (nextFreeSegment < 0) 92 nextFreeSegment = 0; 93 } 94 95 98 private void grow() { 99 int oldLen = segments.length; 101 String [] newPaths = new String [oldLen * 2]; 102 System.arraycopy(segments, 0, newPaths, 0, oldLen); 103 segments = newPaths; 104 } 105 106 111 public void iterate(IElementContentVisitor visitor) { 112 if (path.isRoot()) { 113 if (visitor.visitElement(tree, this, tree.getTreeData())) { 115 if (treeRoot == null) 116 return; 117 AbstractDataTreeNode[] children = treeRoot.getChildren(); 118 for (int i = children.length; --i >= 0;) { 119 doIteration((DataTreeNode) children[i], visitor); 120 } 121 } 122 } else { 123 if (treeRoot == null) 124 return; 125 push(path, path.segmentCount() - 1); 126 doIteration(treeRoot, visitor); 127 } 128 } 129 130 133 private void push(IPath pathToPush, int toPush) { 134 if (toPush <= 0) 135 return; 136 for (int i = 0; i < toPush; i++) { 137 if (nextFreeSegment >= segments.length) { 138 grow(); 139 } 140 segments[nextFreeSegment++] = pathToPush.segment(i); 141 } 142 } 143 144 public String requestName() { 145 if (nextFreeSegment == 0) 146 return ""; return segments[nextFreeSegment - 1]; 148 } 149 150 public IPath requestPath() { 151 if (nextFreeSegment == 0) 152 return Path.ROOT; 153 int length = nextFreeSegment; 154 for (int i = 0; i < nextFreeSegment; i++) { 155 length += segments[i].length(); 156 } 157 StringBuffer pathBuf = new StringBuffer (length); 158 for (int i = 0; i < nextFreeSegment; i++) { 159 pathBuf.append('/'); 160 pathBuf.append(segments[i]); 161 } 162 return new Path(null, pathBuf.toString()); 163 } 164 } 165 | Popular Tags |