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.InputSource;
13 import org.xml.sax.SAXException;
15 import net.jaekl.cfb.CfbBundle;
16 import net.jaekl.cfb.util.Command;
17 import net.jaekl.qd.xml.XmlParseException;
19 public class Analyzer {
22 public Analyzer(File findbugsDir) {
23 m_findbugsDir = findbugsDir;
26 public Analysis analyze(PrintWriter pw, File workDir, File fbp) throws IOException, XmlParseException {
27 Analysis result = new Analysis();
29 File fbOutput = outputWorkFile(workDir, fbp);
31 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
34 Command.Result fbResult = new Command().exec(cmdLine);
35 if (0 != fbResult.getRetCode()) {
36 // Our attempt to execute FindBugs failed.
37 // Report the error and return null.
38 String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
39 String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
40 pw.println(cannotExecMsg);
41 pw.println(trans(CfbBundle.STDOUT_WAS));
42 pw.println(fbResult.getStdout());
43 pw.println(trans(CfbBundle.STDERR_WAS));
44 pw.println(fbResult.getStderr());
48 result = parseFbOutput(new InputSource(fbOutput.getAbsolutePath()));
53 String trans(String key) {
54 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
57 String buildCommandLine(File workDir, File fbp, File fbOutput)
59 assert(null != workDir);
61 assert(null != fbOutput);
63 StringBuilder sb = new StringBuilder();
65 sb.append(m_findbugsDir.getAbsolutePath())
66 .append(File.separator)
68 .append(File.separator)
69 .append("findbugs -textui -xml -output ")
70 .append(fbOutput.getAbsolutePath())
72 .append(fbp.getAbsolutePath());
77 // Come up with an appropriate name for the XML output file.
78 // workDir: place where the file should be created
79 // fbp: FindBugsProject file
80 File outputWorkFile(File workDir, File fbp)
82 assert(null != workDir);
85 String workPath = workDir.getAbsolutePath();
87 String projName = fbp.getName();
88 int len = projName.length();
90 String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
91 if (extension.equals(".fbp")) {
92 projName = projName.substring(0, len - 4);
96 return new File(workPath + File.separator + projName + ".xml");
99 // Parse the output.xml that resulted from a FindBugs run,
100 // and store its findings into an Analysis object.
101 Analysis parseFbOutput(InputSource fbOutput) throws XmlParseException
103 Analysis result = new Analysis();
105 result.parse(fbOutput);
106 } catch (IOException | SAXException exc) {
107 throw new XmlParseException(exc);