Partial implementation of XML parse for FindBugs output
[cfb.git] / prod / net / jaekl / cfb / analyze / Analyzer.java
1 package net.jaekl.cfb.analyze;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6 import java.text.MessageFormat;
7 import java.util.Locale;
8 import java.util.Locale.Category;
9
10 import net.jaekl.cfb.CfbBundle;
11 import net.jaekl.cfb.util.Command;
12
13 public class Analyzer {
14         File m_findbugsDir;
15         
16         public Analyzer(File findbugsDir) {
17                 m_findbugsDir = findbugsDir;
18         }
19         
20         public Analysis analyze(PrintWriter pw, File workDir, File fbp) throws IOException {
21                 Analysis result = new Analysis();
22         
23                 File fbOutput = outputWorkFile(workDir, fbp);
24                 
25                 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
26                 pw.println(cmdLine);
27                 pw.flush();
28                 Command.Result fbResult = new Command().exec(cmdLine);
29                 if (0 != fbResult.getRetCode()) {
30                         // Our attempt to execute FindBugs failed.
31                         // Report the error and return null.
32                         String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
33                         String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
34                         pw.println(cannotExecMsg);
35                         pw.println(trans(CfbBundle.STDOUT_WAS));
36                         pw.println(fbResult.getStdout());
37                         pw.println(trans(CfbBundle.STDERR_WAS));
38                         pw.println(fbResult.getStderr());
39                         return null;
40                 }
41                 
42                 result = parseFbOutput(fbOutput);
43                 
44                 return result;
45         }
46         
47         String trans(String key) {
48                 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
49         }
50         
51         String buildCommandLine(File workDir, File fbp, File fbOutput) 
52         {
53                 assert(null != workDir);
54                 assert(null != fbp);
55                 assert(null != fbOutput);
56                 
57                 StringBuilder sb = new StringBuilder();
58                 
59                 sb.append(m_findbugsDir.getAbsolutePath())
60                   .append(File.separator)
61                   .append("bin")
62                   .append(File.separator)
63                   .append("findbugs -textui -xml -output ")
64                   .append(fbOutput.getAbsolutePath())
65                   .append(" -project ")
66                   .append(fbp.getAbsolutePath());
67                 
68                 return sb.toString();
69         }       
70         
71         // Come up with an appropriate name for the XML output file.
72         //   workDir:  place where the file should be created
73         //       fbp:  FindBugsProject file
74         File outputWorkFile(File workDir, File fbp) 
75         {
76                 assert(null != workDir);
77                 assert(null != fbp);
78                 
79                 String workPath = workDir.getAbsolutePath();
80                 
81                 String projName = fbp.getName();
82                 int len = projName.length();
83                 if (len > 4) {
84                         String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
85                         if (extension.equals(".fbp")) {
86                                 projName = projName.substring(0, len - 4);
87                         }
88                 }
89                 
90                 return new File(workPath + File.separator + projName + ".xml");
91         }
92         
93         // Parse the output.xml that resulted from a FindBugs run,
94         // and store its findings into an Analysis object.
95         Analysis parseFbOutput(File fbOutput) 
96         {
97                 return null;
98         }
99 }