1 package net.jaekl.cfb.analyze;
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;
10 import net.jaekl.cfb.CfbBundle;
11 import net.jaekl.cfb.util.Command;
13 public class Analyzer {
16 public Analyzer(File findbugsDir) {
17 m_findbugsDir = findbugsDir;
20 public Analysis analyze(PrintWriter pw, File workDir, File fbp) throws IOException {
21 Analysis result = new Analysis();
23 File fbOutput = outputWorkFile(workDir, fbp);
25 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
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());
42 result = parseFbOutput(fbOutput);
47 String trans(String key) {
48 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
51 String buildCommandLine(File workDir, File fbp, File fbOutput)
53 assert(null != workDir);
55 assert(null != fbOutput);
57 StringBuilder sb = new StringBuilder();
59 sb.append(m_findbugsDir.getAbsolutePath())
60 .append(File.separator)
62 .append(File.separator)
63 .append("findbugs -textui -xml -output ")
64 .append(fbOutput.getAbsolutePath())
66 .append(fbp.getAbsolutePath());
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)
76 assert(null != workDir);
79 String workPath = workDir.getAbsolutePath();
81 String projName = fbp.getName();
82 int len = projName.length();
84 String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
85 if (extension.equals(".fbp")) {
86 projName = projName.substring(0, len - 4);
90 return new File(workPath + File.separator + projName + ".xml");
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)