1 32 33 package com.jeantessier.dependency; 34 35 import java.util.*; 36 37 import org.apache.log4j.*; 38 39 45 public abstract class VisitorBase implements Visitor { 46 private TraversalStrategy strategy; 47 48 private LinkedList currentNodes = new LinkedList(); 49 private SortedSet scope = new TreeSet(); 50 51 public VisitorBase() { 52 this(new SelectiveTraversalStrategy()); 53 } 54 55 public VisitorBase(TraversalStrategy strategy) { 56 this.strategy = strategy; 57 } 58 59 protected TraversalStrategy getStrategy() { 60 return strategy; 61 } 62 63 public void traverseNodes(Collection nodes) { 64 Logger.getLogger(getClass()).debug("nodes = " + nodes); 65 66 Iterator i = getStrategy().order(nodes).iterator(); 67 while (i.hasNext()) { 68 ((Node) i.next()).accept(this); 69 } 70 } 71 72 protected void traverseInbound(Collection nodes) { 73 Iterator i = getStrategy().order(nodes).iterator(); 74 while (i.hasNext()) { 75 ((Node) i.next()).acceptInbound(this); 76 } 77 } 78 79 protected void traverseOutbound(Collection nodes) { 80 Iterator i = getStrategy().order(nodes).iterator(); 81 while (i.hasNext()) { 82 ((Node) i.next()).acceptOutbound(this); 83 } 84 } 85 86 protected Node getCurrentNode() { 87 Node result = null; 88 89 if (!currentNodes.isEmpty()) { 90 result = (Node) currentNodes.getLast(); 91 } 92 93 if (Logger.getLogger(getClass()).isDebugEnabled()) { 94 Logger.getLogger(getClass()).debug(currentNodes + ": " + result); 95 } 96 97 return result; 98 } 99 100 protected void pushNode(Node currentNode) { 101 if (Logger.getLogger(getClass()).isDebugEnabled()) { 102 Logger.getLogger(getClass()).debug(currentNodes + " + " + currentNode); 103 } 104 105 currentNodes.addLast(currentNode); 106 } 107 108 protected Node popNode() { 109 Node result = (Node) currentNodes.removeLast(); 110 111 if (Logger.getLogger(getClass()).isDebugEnabled()) { 112 Logger.getLogger(getClass()).debug(currentNodes + " -> " + result); 113 } 114 115 return result; 116 } 117 118 public void visitPackageNode(PackageNode node) { 119 boolean inScope = getStrategy().isInScope(node); 120 121 if (inScope) { 122 preprocessPackageNode(node); 123 124 if (getStrategy().doPreOutboundTraversal()) { 125 traverseOutbound(node.getOutboundDependencies()); 126 } 127 128 if (getStrategy().doPreInboundTraversal()) { 129 traverseInbound(node.getInboundDependencies()); 130 } 131 132 preprocessAfterDependenciesPackageNode(node); 133 } 134 135 traverseNodes(node.getClasses()); 136 137 if (inScope) { 138 postprocessBeforeDependenciesPackageNode(node); 139 140 if (getStrategy().doPostOutboundTraversal()) { 141 traverseOutbound(node.getOutboundDependencies()); 142 } 143 144 if (getStrategy().doPostInboundTraversal()) { 145 traverseInbound(node.getInboundDependencies()); 146 } 147 148 postprocessPackageNode(node); 149 } 150 } 151 152 protected void preprocessPackageNode(PackageNode node) { 153 pushNode(node); 154 } 155 156 protected void preprocessAfterDependenciesPackageNode(PackageNode node) { 157 } 159 160 protected void postprocessBeforeDependenciesPackageNode(PackageNode node) { 161 } 163 164 protected void postprocessPackageNode(PackageNode node) { 165 if (node.equals(getCurrentNode())) { 166 popNode(); 167 } 168 } 169 170 public void visitInboundPackageNode(PackageNode node) { 171 } 173 174 public void visitOutboundPackageNode(PackageNode node) { 175 } 177 178 public void visitClassNode(ClassNode node) { 179 boolean inScope = getStrategy().isInScope(node); 180 181 if (inScope) { 182 preprocessClassNode(node); 183 184 if (getStrategy().doPreOutboundTraversal()) { 185 traverseOutbound(node.getOutboundDependencies()); 186 } 187 188 if (getStrategy().doPreInboundTraversal()) { 189 traverseInbound(node.getInboundDependencies()); 190 } 191 192 preprocessAfterDependenciesClassNode(node); 193 } 194 195 traverseNodes(node.getFeatures()); 196 197 if (inScope) { 198 postprocessBeforeDependenciesClassNode(node); 199 200 if (getStrategy().doPostOutboundTraversal()) { 201 traverseOutbound(node.getOutboundDependencies()); 202 } 203 204 if (getStrategy().doPostInboundTraversal()) { 205 traverseInbound(node.getInboundDependencies()); 206 } 207 208 postprocessClassNode(node); 209 } 210 } 211 212 protected void preprocessClassNode(ClassNode node) { 213 pushNode(node); 214 } 215 216 protected void preprocessAfterDependenciesClassNode(ClassNode node) { 217 } 219 220 protected void postprocessBeforeDependenciesClassNode(ClassNode node) { 221 } 223 224 protected void postprocessClassNode(ClassNode node) { 225 if (node.equals(getCurrentNode())) { 226 popNode(); 227 } 228 } 229 230 public void visitInboundClassNode(ClassNode node) { 231 } 233 234 public void visitOutboundClassNode(ClassNode node) { 235 } 237 238 public void visitFeatureNode(FeatureNode node) { 239 if (getStrategy().isInScope(node)) { 240 preprocessFeatureNode(node); 241 242 if (getStrategy().doPreOutboundTraversal()) { 243 traverseOutbound(node.getOutboundDependencies()); 244 } 245 246 if (getStrategy().doPreInboundTraversal()) { 247 traverseInbound(node.getInboundDependencies()); 248 } 249 250 if (getStrategy().doPostOutboundTraversal()) { 251 traverseOutbound(node.getOutboundDependencies()); 252 } 253 254 if (getStrategy().doPostInboundTraversal()) { 255 traverseInbound(node.getInboundDependencies()); 256 } 257 258 postprocessFeatureNode(node); 259 } 260 } 261 262 protected void preprocessFeatureNode(FeatureNode node) { 263 pushNode(node); 264 } 265 266 protected void postprocessFeatureNode(FeatureNode node) { 267 if (node.equals(getCurrentNode())) { 268 popNode(); 269 } 270 } 271 272 public void visitInboundFeatureNode(FeatureNode node) { 273 } 275 276 public void visitOutboundFeatureNode(FeatureNode node) { 277 } 279 } 280 | Popular Tags |