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(MessageMap msgMap) {
26 public Analysis analyze(PrintWriter pw, File workDir, File fbp, String buildNumber) throws IOException, XmlParseException, SAXException
28 Analysis result = new Analysis(buildNumber);
30 File fbOutput = outputWorkFile(workDir, fbp);
32 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
35 Command.Result fbResult = new Command().exec(cmdLine);
36 if (0 != fbResult.getRetCode()) {
37 // Our attempt to execute FindBugs failed.
38 // Report the error and return null.
39 String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
40 String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
41 pw.println(cannotExecMsg);
42 pw.println(trans(CfbBundle.STDOUT_WAS));
43 pw.println(fbResult.getStdout());
44 pw.println(trans(CfbBundle.STDERR_WAS));
45 pw.println(fbResult.getStderr());
49 result.parse(new InputSource(fbOutput.getAbsolutePath()));
54 String trans(String key) {
55 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
58 String buildCommandLine(File workDir, File fbp, File fbOutput)
60 assert(null != workDir);
62 assert(null != fbOutput);
64 StringBuilder sb = new StringBuilder();
66 sb.append(m_msgMap.getFindBugsDir().getAbsolutePath())
67 .append(File.separator)
69 .append(File.separator)
70 .append("findbugs -textui -xml -output ")
71 .append(fbOutput.getAbsolutePath())
73 .append(fbp.getAbsolutePath());
78 // Come up with an appropriate name for the XML output file.
79 // workDir: place where the file should be created
80 // fbp: FindBugsProject file
81 File outputWorkFile(File workDir, File fbp)
83 assert(null != workDir);
86 String workPath = workDir.getAbsolutePath();
88 String projName = fbp.getName();
89 int len = projName.length();
91 String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
92 if (extension.equals(".fbp")) {
93 projName = projName.substring(0, len - 4);
97 return new File(workPath + File.separator + projName + ".xml");