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 org.xml.sax.InputSource;
14 import org.xml.sax.SAXException;
16 import net.jaekl.cfb.CfbBundle;
17 import net.jaekl.cfb.util.Command;
18 import net.jaekl.qd.xml.XmlParseException;
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 if (0 != fbResult.getRetCode()) {
38 // Our attempt to execute FindBugs failed.
39 // Report the error and return null.
40 String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
41 String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
42 pw.println(cannotExecMsg);
43 pw.println(trans(CfbBundle.STDOUT_WAS));
44 pw.println(fbResult.getStdout());
45 pw.println(trans(CfbBundle.STDERR_WAS));
46 pw.println(fbResult.getStderr());
50 result.setEnd(new Date());
51 result.parse(new InputSource(fbOutput.getAbsolutePath()));
56 String trans(String key) {
57 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
60 String buildCommandLine(File workDir, File fbp, File fbOutput)
62 assert(null != workDir);
64 assert(null != fbOutput);
66 StringBuilder sb = new StringBuilder();
68 sb.append(m_msgMap.getFindBugsDir().getAbsolutePath())
69 .append(File.separator)
71 .append(File.separator)
72 .append("findbugs -textui -xml -output ")
73 .append(fbOutput.getAbsolutePath())
75 .append(fbp.getAbsolutePath());
80 // Come up with an appropriate name for the XML output file.
81 // workDir: place where the file should be created
82 // fbp: FindBugsProject file
83 File outputWorkFile(File workDir, File fbp)
85 assert(null != workDir);
88 String workPath = workDir.getAbsolutePath();
90 String projName = fbp.getName();
91 int len = projName.length();
93 String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
94 if (extension.equals(".fbp")) {
95 projName = projName.substring(0, len - 4);
99 return new File(workPath + File.separator + projName + ".xml");