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;
10 import java.util.Locale;
11 import java.util.Locale.Category;
13 import net.jaekl.cfb.CfbBundle;
14 import net.jaekl.cfb.util.Command;
15 import net.jaekl.qd.xml.XmlParseException;
17 import org.xml.sax.InputSource;
18 import org.xml.sax.SAXException;
20 public class Analyzer {
23 public Analyzer(MessageMap msgMap) {
27 public Analysis analyze(PrintWriter pw, File workDir, File fbp, String buildNumber) throws IOException, XmlParseException, SAXException
29 Analysis result = new Analysis(buildNumber);
31 File fbOutput = outputWorkFile(workDir, fbp);
33 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
36 Command.Result fbResult = new Command().exec(cmdLine);
37 pw.println("==> " + fbResult.getRetCode());
39 if (0 != fbResult.getRetCode()) {
40 // Our attempt to execute FindBugs failed.
41 // Report the error and return null.
42 String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
43 String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
44 pw.println(cannotExecMsg);
45 pw.println(trans(CfbBundle.STDOUT_WAS));
46 pw.println(fbResult.getStdout());
47 pw.println(trans(CfbBundle.STDERR_WAS));
48 pw.println(fbResult.getStderr());
52 result.setEnd(new Date());
53 result.parse(new InputSource(fbOutput.getAbsolutePath()));
58 String trans(String key) {
59 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
62 String buildCommandLine(File workDir, File fbp, File fbOutput)
64 assert(null != workDir);
66 assert(null != fbOutput);
68 StringBuilder sb = new StringBuilder();
70 sb.append(m_msgMap.getFindBugsDir().getAbsolutePath())
71 .append(File.separator)
73 .append(File.separator)
74 .append("findbugs -textui -xml -output ")
75 .append(fbOutput.getAbsolutePath())
77 .append(fbp.getAbsolutePath());
82 // Come up with an appropriate name for the XML output file.
83 // workDir: place where the file should be created
84 // fbp: FindBugsProject file
85 File outputWorkFile(File workDir, File fbp)
87 assert(null != workDir);
90 String workPath = workDir.getAbsolutePath();
92 String projName = fbp.getName();
93 int len = projName.length();
95 String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
96 if (extension.equals(".fbp")) {
97 projName = projName.substring(0, len - 4);
101 return new File(workPath + File.separator + projName + ".xml");