1 32 33 package com.jeantessier.dependency; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.log4j.*; 39 import org.apache.oro.text.perl.*; 40 41 public class TextPrinter extends Printer { 42 private static final Perl5Util perl = new Perl5Util(); 43 44 protected static Perl5Util perl() { 45 return perl; 46 } 47 48 private boolean showInferred = true; 49 private Map dependencies = new TreeMap(); 50 51 public TextPrinter(PrintWriter out) { 52 super(out); 53 } 54 55 public TextPrinter(TraversalStrategy strategy, PrintWriter out) { 56 super(strategy, out); 57 } 58 59 public boolean isShowInferred() { 60 return showInferred; 61 } 62 63 public void setShowInferred(boolean showInferred) { 64 this.showInferred = showInferred; 65 } 66 67 protected void preprocessPackageNode(PackageNode node) { 68 Logger.getLogger(getClass()).debug("Printing package \"" + node + "\" and its " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds"); 69 70 super.preprocessPackageNode(node); 71 72 raiseIndent(); 73 74 dependencies.clear(); 75 } 76 77 protected void preprocessAfterDependenciesPackageNode(PackageNode node) { 78 Logger.getLogger(getClass()).debug("Package \"" + node + "\" with " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds had " + dependencies.size() + " dependencies."); 79 80 if (shouldShowPackageNode(node) || !dependencies.isEmpty()) { 81 lowerIndent(); 82 indent().printNodeName(node).eol(); 83 raiseIndent(); 84 } 85 86 printDependencies(dependencies); 87 } 88 89 protected void postprocessPackageNode(PackageNode node) { 90 lowerIndent(); 91 92 super.postprocessPackageNode(node); 93 } 94 95 public void visitInboundPackageNode(PackageNode node) { 96 if (isShowInbounds()) { 97 Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" <-- \"" + node + "\""); 98 99 Integer i = (Integer ) dependencies.get(node); 100 if (i != null) { 101 dependencies.put(node, new Integer (i.intValue() - 1)); 102 } else { 103 dependencies.put(node, new Integer (-1)); 104 } 105 } else { 106 Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" <-- \"" + node + "\""); 107 } 108 } 109 110 public void visitOutboundPackageNode(PackageNode node) { 111 if (isShowOutbounds()) { 112 Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" --> \"" + node + "\""); 113 114 Integer i = (Integer ) dependencies.get(node); 115 if (i != null) { 116 dependencies.put(node, new Integer (i.intValue() + 1)); 117 } else { 118 dependencies.put(node, new Integer (1)); 119 } 120 } else { 121 Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" --> \"" + node + "\""); 122 } 123 } 124 125 protected void preprocessClassNode(ClassNode node) { 126 Logger.getLogger(getClass()).debug("Printing class \"" + node + "\" and its " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds"); 127 128 super.preprocessClassNode(node); 129 130 raiseIndent(); 131 132 dependencies.clear(); 133 } 134 135 protected void preprocessAfterDependenciesClassNode(ClassNode node) { 136 Logger.getLogger(getClass()).debug("Class \"" + node + "\" with " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds had " + dependencies.size() + " dependencies."); 137 138 if (shouldShowClassNode(node) || !dependencies.isEmpty()) { 139 lowerIndent(); 140 indent().printNodeName(node, node.getName().substring(node.getName().lastIndexOf('.') + 1)).eol(); 141 raiseIndent(); 142 } 143 144 printDependencies(dependencies); 145 } 146 147 protected void postprocessClassNode(ClassNode node) { 148 lowerIndent(); 149 150 super.postprocessClassNode(node); 151 } 152 153 public void visitInboundClassNode(ClassNode node) { 154 if (isShowInbounds()) { 155 Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" <-- \"" + node + "\""); 156 157 Integer i = (Integer ) dependencies.get(node); 158 159 if (i != null) { 160 dependencies.put(node, new Integer (i.intValue() - 1)); 161 } else { 162 dependencies.put(node, new Integer (-1)); 163 } 164 } else { 165 Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" <-- \"" + node + "\""); 166 } 167 } 168 169 public void visitOutboundClassNode(ClassNode node) { 170 if (isShowOutbounds()) { 171 Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" --> \"" + node + "\""); 172 173 Integer i = (Integer ) dependencies.get(node); 174 175 if (i != null) { 176 dependencies.put(node, new Integer (i.intValue() + 1)); 177 } else { 178 dependencies.put(node, new Integer (1)); 179 } 180 } else { 181 Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" --> \"" + node + "\""); 182 } 183 } 184 185 protected void preprocessFeatureNode(FeatureNode node) { 186 Logger.getLogger(getClass()).debug("Printing feature \"" + node + "\" and its " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds"); 187 188 super.preprocessFeatureNode(node); 189 190 raiseIndent(); 191 192 dependencies.clear(); 193 } 194 195 protected void postprocessFeatureNode(FeatureNode node) { 196 Logger.getLogger(getClass()).debug("Feature \"" + node + "\" with " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds had " + dependencies.size() + " dependencies."); 197 198 if (shouldShowFeatureNode(node) || !dependencies.isEmpty()) { 199 lowerIndent(); 200 if (perl().match("/([^\\.]*\\(.*\\))$/", node.getName())) { 201 indent().printNodeName(node, perl().group(1)).eol(); 202 } else if (perl().match("/([^\\.]*)$/", node.getName())) { 203 indent().printNodeName(node, perl().group(1)).eol(); 204 } else { 205 indent().printNodeName(node, node.getName().substring(node.getName().lastIndexOf('.') + 1)).eol(); 206 } 207 raiseIndent(); 208 } 209 210 printDependencies(dependencies); 211 212 lowerIndent(); 213 214 super.postprocessFeatureNode(node); 215 } 216 217 public void visitInboundFeatureNode(FeatureNode node) { 218 if (isShowInbounds()) { 219 Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" <-- \"" + node + "\""); 220 221 Integer i = (Integer ) dependencies.get(node); 222 if (i != null) { 223 dependencies.put(node, new Integer (i.intValue() - 1)); 224 } else { 225 dependencies.put(node, new Integer (-1)); 226 } 227 } else { 228 Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" <-- \"" + node + "\""); 229 } 230 } 231 232 public void visitOutboundFeatureNode(FeatureNode node) { 233 if (isShowOutbounds()) { 234 Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" --> \"" + node + "\""); 235 236 Integer i = (Integer ) dependencies.get(node); 237 if (i != null) { 238 dependencies.put(node, new Integer (i.intValue() + 1)); 239 } else { 240 dependencies.put(node, new Integer (1)); 241 } 242 } else { 243 Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" --> \"" + node + "\""); 244 } 245 } 246 247 protected Printer printNodeName(Node node, String name) { 248 super.printNodeName(node, name); 249 250 if (isShowInferred() && !node.isConfirmed()) { 251 append(" *"); 252 } 253 254 return this; 255 } 256 257 private void printDependencies(Map dependencies) { 258 Iterator i = dependencies.entrySet().iterator(); 259 while (i.hasNext()) { 260 Map.Entry entry = (Map.Entry) i.next(); 261 if (((Integer ) entry.getValue()).intValue() < 0) { 262 indent().append("<-- ").printNodeName((Node) entry.getKey()).eol(); 263 } else if (((Integer ) entry.getValue()).intValue() > 0) { 264 indent().append("--> ").printNodeName((Node) entry.getKey()).eol(); 265 } else { 266 indent().append("<-> ").printNodeName((Node) entry.getKey()).eol(); 267 } 268 } 269 } 270 } 271 | Popular Tags |