1 package org.incava.doctorj; 2 3 import java.io.*; 4 import java.util.*; 5 import net.sourceforge.pmd.ast.*; 6 import org.incava.analysis.*; 7 import org.incava.io.Find; 8 import org.incava.java.SimpleNodeUtil; 9 import org.incava.util.TimedEvent; 10 import org.incava.util.TimedEventSet; 11 12 13 public class DoctorJ 14 { 15 private TimedEventSet _totalInit = new TimedEventSet(); 16 17 private TimedEventSet _totalParse = new TimedEventSet(); 18 19 private TimedEventSet _totalAnalysis = new TimedEventSet(); 20 21 private JavaParser _parser = null; 22 23 private Report _report = null; 24 25 private JavaParserVisitor _analyzer; 26 27 private int _exitValue; 28 29 private int _nFiles; 30 31 public DoctorJ(String [] args) 32 { 33 tr.Ace.set(true, 25, 4, 20, 25); 34 tr.Ace.setOutput(tr.Ace.VERBOSE, tr.Ace.LEVEL4); 35 tr.Ace.setOutput(tr.Ace.QUIET, tr.Ace.LEVEL2); 36 37 _exitValue = 0; 38 _nFiles = 0; 39 40 Options opts = Options.get(); 41 String [] names = opts.process(args); 42 43 tr.Ace.log("args", args); 44 tr.Ace.log("names", names); 45 tr.Ace.log("properties", System.getProperties()); 46 47 if (opts.emacsOutput) { 48 _report = new TerseReport(System.out); 49 } 50 else { 51 _report = new ContextReport(System.out); 52 } 53 54 _analyzer = new JavadocAnalyzer(_report); 55 56 for (int ni = 0; ni < names.length; ++ni) { 57 String name = names[ni]; 58 process(name); 59 } 60 61 if (_nFiles > 1) { 62 long total = _totalInit.duration + _totalParse.duration + _totalAnalysis.duration; 63 tr.Ace.log("total time: " + total); 64 tr.Ace.log("init : " + _totalInit.duration); 65 tr.Ace.log("parse : " + _totalParse.duration); 66 tr.Ace.log("analysis : " + _totalAnalysis.duration); 67 tr.Ace.log("#files : " + _nFiles); 68 } 69 } 70 71 public int getExitValue() 72 { 73 return _exitValue; 74 } 75 76 protected void process(String name) 77 { 78 File fd = new File(name); 79 if (fd.exists()) { 80 if (fd.isDirectory()) { 81 tr.Ace.log("processing directory"); 82 String [] contents = fd.list(); 83 Arrays.sort(contents); 84 for (int ci = 0; ci < contents.length; ++ci) { 85 String nm = contents[ci]; 86 String fullName = name + File.separator + nm; 87 File f = new File(fullName); 88 if (f.isDirectory()) { 89 process(fullName); 90 } 91 else if (f.isFile() && nm.endsWith(".java")) { 92 processFile(fullName); 93 } 94 else { 95 tr.Ace.log("not a match", f); 96 } 97 } 98 } 99 else if (fd.isFile()) { 100 processFile(name); 101 } 102 } 103 else { 104 System.err.println("File " + name + " not found."); 105 } 106 } 107 108 protected void processFile(String fileName) 109 { 110 ++_nFiles; 111 112 if (initParser(fileName)) { 113 ASTCompilationUnit cu = parse(fileName); 114 if (cu != null) { 115 analyze(cu); 116 } 117 } 118 } 119 120 protected boolean initParser(String fileName) 121 { 122 tr.Ace.log("fileName", fileName); 123 124 TimedEvent init = new TimedEvent(_totalInit); 125 try { 126 _report.reset(new File(fileName)); 127 128 FileReader rdr = new FileReader(fileName); 130 JavaCharStream jcs = new JavaCharStream(rdr); 131 132 _parser = new JavaParser(jcs); 133 134 String src = Options.get().source; 135 if (src.equals("1.3")) { 136 tr.Ace.log("setting as 1.3"); 137 _parser.setJDK13(); 138 } 139 else if (src.equals("1.4")) { 140 tr.Ace.log("leaving as 1.4"); 141 } 142 else if (src.equals("1.5")) { 143 tr.Ace.log("setting as 1.5"); 144 _parser.setJDK15(); 145 } 146 else { 147 System.err.println("ERROR: source version '" + src + "' not recognized"); 148 System.exit(-1); 149 } 150 init.end(); 151 152 tr.Ace.log("init: " + init.duration); 153 154 return true; 155 } 156 catch (FileNotFoundException e) { 157 System.out.println("File " + fileName + " not found."); 158 _exitValue = 1; 159 160 return false; 161 } 162 catch (IOException e) { 163 System.out.println("Error opening " + fileName + ": " + e); 164 _exitValue = 1; 165 166 return false; 167 } 168 catch (TokenMgrError tme) { 169 System.out.println("Error parsing (tokenizing) " + fileName + ": " + tme.getMessage()); 170 _exitValue = 1; 171 172 return false; 173 } 174 } 175 176 protected ASTCompilationUnit parse(String fileName) 177 { 178 tr.Ace.log("running parser"); 179 180 try { 181 TimedEvent parse = new TimedEvent(_totalParse); 182 ASTCompilationUnit cu = _parser.CompilationUnit(); 183 parse.end(); 184 185 tr.Ace.log("parse: " + parse.duration); 186 187 return cu; 188 } 189 catch (ParseException e) { 190 System.out.println("Parse error in " + fileName + ": " + e.getMessage()); 191 _exitValue = 1; 192 193 return null; 194 } 195 } 196 197 protected void analyze(ASTCompilationUnit cu) 198 { 199 TimedEvent analysis = new TimedEvent(_totalAnalysis); 200 cu.jjtAccept(_analyzer, null); 201 202 _report.flush(); 203 analysis.end(); 204 205 tr.Ace.log("analysis: " + analysis.duration); 206 } 207 208 public static void main(String [] args) 209 { 210 tr.Ace.setVerbose(false); 211 212 DoctorJ drj = new DoctorJ(args); 213 System.exit(drj.getExitValue()); 214 } 215 216 } 217 | Popular Tags |