1 32 33 package com.jeantessier.dependencyfinder.ant; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.tools.ant.*; 39 import org.apache.tools.ant.types.*; 40 41 import com.jeantessier.classreader.*; 42 import com.jeantessier.dependency.*; 43 44 public class DependencyExtractor extends Task { 45 private boolean xml = false; 46 private boolean minimize = false; 47 private boolean maximize = false; 48 private String encoding = com.jeantessier.dependency.XMLPrinter.DEFAULT_ENCODING; 49 private String dtdPrefix = com.jeantessier.dependency.XMLPrinter.DEFAULT_DTD_PREFIX; 50 private String indentText; 51 private File destfile; 52 private Path path; 53 54 public boolean getXml() { 55 return xml; 56 } 57 58 public void setXml(boolean xml) { 59 this.xml = xml; 60 } 61 62 public boolean getMinimize() { 63 return minimize; 64 } 65 66 public void setMinimize(boolean minimize) { 67 this.minimize = minimize; 68 } 69 70 public boolean getMaximize() { 71 return maximize; 72 } 73 74 public void setMaximize(boolean maximize) { 75 this.maximize = maximize; 76 } 77 78 public String getEncoding() { 79 return encoding; 80 } 81 82 public void setEncoding(String encoding) { 83 this.encoding = encoding; 84 } 85 86 public String getDtdprefix() { 87 return dtdPrefix; 88 } 89 90 public void setDtdprefix(String dtdPrefix) { 91 this.dtdPrefix = dtdPrefix; 92 } 93 94 public String getIndenttext() { 95 return indentText; 96 } 97 98 public void setIntenttext(String indentText) { 99 this.indentText = indentText; 100 } 101 102 public File getDestfile() { 103 return destfile; 104 } 105 106 public void setDestfile(File destfile) { 107 this.destfile = destfile; 108 } 109 110 public Path createPath() { 111 if (path == null) { 112 path = new Path(getProject()); 113 } 114 115 return path; 116 } 117 118 public Path getPath() { 119 return path; 120 } 121 122 public void execute() throws BuildException { 123 125 if (getPath() == null) { 126 throw new BuildException("path must be set!"); 127 } 128 129 if (getDestfile() == null) { 130 throw new BuildException("destfile must be set!"); 131 } 132 133 log("Reading classes from path " + getPath()); 134 135 VerboseListener verboseListener = new VerboseListener(this); 136 137 NodeFactory factory = new NodeFactory(); 138 CodeDependencyCollector collector = new CodeDependencyCollector(factory); 139 140 ClassfileLoader loader = new TransientClassfileLoader(); 141 loader.addLoadListener(new LoadListenerVisitorAdapter(collector)); 142 loader.addLoadListener(verboseListener); 143 loader.load(Arrays.asList(getPath().list())); 144 145 if (getMinimize()) { 146 LinkMinimizer minimizer = new LinkMinimizer(); 147 minimizer.traverseNodes(factory.getPackages().values()); 148 } else if (getMaximize()) { 149 LinkMaximizer maximizer = new LinkMaximizer(); 150 maximizer.traverseNodes(factory.getPackages().values()); 151 } 152 153 log("Saving dependency graph to " + getDestfile().getAbsolutePath()); 154 155 try { 156 PrintWriter out = new PrintWriter(new FileWriter(getDestfile())); 157 158 com.jeantessier.dependency.Printer printer; 159 if (getXml()) { 160 printer = new com.jeantessier.dependency.XMLPrinter(out, getEncoding(), getDtdprefix()); 161 } else { 162 printer = new com.jeantessier.dependency.TextPrinter(out); 163 } 164 165 if (getIndenttext() != null) { 166 printer.setIndentText(getIndenttext()); 167 } 168 169 printer.traverseNodes(factory.getPackages().values()); 170 171 out.close(); 172 } catch (IOException ex) { 173 throw new BuildException(ex); 174 } 175 } 176 } 177 | Popular Tags |