1 32 33 package com.jeantessier.dependency; 34 35 import java.util.*; 36 37 import org.apache.log4j.*; 38 39 import com.jeantessier.classreader.*; 40 41 public class DeletingVisitor implements Visitor, RemoveVisitor { 42 private NodeFactory factory; 43 44 public DeletingVisitor(NodeFactory factory) { 45 this.factory = factory; 46 } 47 48 public NodeFactory getFactory() { 49 return factory; 50 } 51 52 public void traverseNodes(Collection nodes) { 53 } 58 59 62 63 public void visitPackageNode(PackageNode node) { 64 Logger.getLogger(getClass()).debug("visitPackageNode(" + node + ")"); 65 66 Iterator i = new ArrayList(node.getClasses()).iterator(); 67 while (i.hasNext()) { 68 ((Node) i.next()).accept(this); 69 } 70 71 visitNode(node); 72 } 73 74 public void visitClassNode(ClassNode node) { 75 Logger.getLogger(getClass()).debug("visitClassNode(" + node + ")"); 76 77 Iterator i = new ArrayList(node.getFeatures()).iterator(); 78 while (i.hasNext()) { 79 ((Node) i.next()).accept(this); 80 } 81 82 visitNode(node); 83 } 84 85 public void visitFeatureNode(FeatureNode node) { 86 Logger.getLogger(getClass()).debug("visitFeatureNode(" + node + ")"); 87 88 visitNode(node); 89 } 90 91 private void visitNode(Node node) { 92 node.setConfirmed(false); 93 94 Iterator i = new ArrayList(node.getOutboundDependencies()).iterator(); 95 while (i.hasNext()) { 96 Node outbound = (Node) i.next(); 97 node.removeDependency(outbound); 98 outbound.acceptOutbound(this); 99 } 100 101 node.acceptOutbound(this); 102 } 103 104 107 108 public void visitOutboundPackageNode(PackageNode node) { 109 Logger.getLogger(getClass()).debug("visitOutboundPackageNode(" + node + ")"); 110 111 if (canDeletePackage(node)) { 112 factory.deletePackage(node); 113 } 114 } 115 116 public void visitOutboundClassNode(ClassNode node) { 117 Logger.getLogger(getClass()).debug("visitOutboundClassNode(" + node + ")"); 118 119 if (canDeleteClass(node)) { 120 factory.deleteClass(node); 121 } 122 123 node.getPackageNode().acceptOutbound(this); 124 } 125 126 public void visitOutboundFeatureNode(FeatureNode node) { 127 Logger.getLogger(getClass()).debug("visitOutboundFeatureNode(" + node + ")"); 128 129 if (canDeleteFeature(node)) { 130 factory.deleteFeature(node); 131 } 132 133 node.getClassNode().acceptOutbound(this); 134 } 135 136 private boolean canDelete(Node node) { 137 return !node.isConfirmed() && node.getInboundDependencies().isEmpty(); 138 } 139 140 private boolean canDeletePackage(PackageNode node) { 141 return canDelete(node) && node.getClasses().isEmpty(); 142 } 143 144 private boolean canDeleteClass(ClassNode node) { 145 return canDelete(node) && node.getFeatures().isEmpty(); 146 } 147 148 private boolean canDeleteFeature(FeatureNode node) { 149 return canDelete(node); 150 } 151 152 155 156 public void visitInboundPackageNode(PackageNode node) { 157 } 159 160 public void visitInboundClassNode(ClassNode node) { 161 } 163 164 public void visitInboundFeatureNode(FeatureNode node) { 165 } 167 168 171 172 public void removeClass(String classname) { 173 Node node = (Node) factory.getClasses().get(classname); 174 if (node != null) { 175 node.accept(this); 176 } 177 } 178 } 179 | Popular Tags |