1 package net.jaekl.cfb.analyze;
3 // Copyright (C) 2015 Christian Jaekl
6 import java.io.IOException;
7 import java.io.PrintWriter;
8 import java.text.MessageFormat;
9 import java.util.Locale;
10 import java.util.Locale.Category;
12 import org.xml.sax.SAXException;
14 import net.jaekl.cfb.CfbBundle;
15 import net.jaekl.cfb.util.Command;
16 import net.jaekl.qd.xml.XmlParseException;
18 public class Analyzer {
21 public Analyzer(File findbugsDir) {
22 m_findbugsDir = findbugsDir;
25 public Analysis analyze(PrintWriter pw, File workDir, File fbp) throws IOException, XmlParseException {
26 Analysis result = new Analysis();
28 File fbOutput = outputWorkFile(workDir, fbp);
30 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
33 Command.Result fbResult = new Command().exec(cmdLine);
34 if (0 != fbResult.getRetCode()) {
35 // Our attempt to execute FindBugs failed.
36 // Report the error and return null.
37 String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
38 String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
39 pw.println(cannotExecMsg);
40 pw.println(trans(CfbBundle.STDOUT_WAS));
41 pw.println(fbResult.getStdout());
42 pw.println(trans(CfbBundle.STDERR_WAS));
43 pw.println(fbResult.getStderr());
47 result = parseFbOutput(fbOutput);
52 String trans(String key) {
53 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
56 String buildCommandLine(File workDir, File fbp, File fbOutput)
58 assert(null != workDir);
60 assert(null != fbOutput);
62 StringBuilder sb = new StringBuilder();
64 sb.append(m_findbugsDir.getAbsolutePath())
65 .append(File.separator)
67 .append(File.separator)
68 .append("findbugs -textui -xml -output ")
69 .append(fbOutput.getAbsolutePath())
71 .append(fbp.getAbsolutePath());
76 // Come up with an appropriate name for the XML output file.
77 // workDir: place where the file should be created
78 // fbp: FindBugsProject file
79 File outputWorkFile(File workDir, File fbp)
81 assert(null != workDir);
84 String workPath = workDir.getAbsolutePath();
86 String projName = fbp.getName();
87 int len = projName.length();
89 String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
90 if (extension.equals(".fbp")) {
91 projName = projName.substring(0, len - 4);
95 return new File(workPath + File.separator + projName + ".xml");
98 // Parse the output.xml that resulted from a FindBugs run,
99 // and store its findings into an Analysis object.
100 Analysis parseFbOutput(File fbOutput) throws XmlParseException
102 Analysis result = new Analysis();
104 result.parse(fbOutput);
105 } catch (IOException | SAXException exc) {
106 throw new XmlParseException(exc);