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 projName, String buildNumber)
28 throws IOException, XmlParseException, SAXException
30 Analysis result = new Analysis(projName, buildNumber);
32 File fbOutput = outputWorkFile(workDir, fbp);
34 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
37 Command.Result fbResult = new Command().exec(cmdLine);
38 pw.println("==> " + fbResult.getRetCode());
40 if (0 != fbResult.getRetCode()) {
41 // Our attempt to execute FindBugs failed.
42 // Report the error and return null.
43 String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
44 String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
45 pw.println(cannotExecMsg);
46 pw.println(trans(CfbBundle.STDOUT_WAS));
47 pw.println(fbResult.getStdout());
48 pw.println(trans(CfbBundle.STDERR_WAS));
49 pw.println(fbResult.getStderr());
53 result.setEnd(new Date());
54 result.parse(new InputSource(fbOutput.getAbsolutePath()));
59 String trans(String key) {
60 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
63 String buildCommandLine(File workDir, File fbp, File fbOutput)
65 assert(null != workDir);
67 assert(null != fbOutput);
69 StringBuilder sb = new StringBuilder();
71 sb.append(m_msgMap.getFindBugsDir().getAbsolutePath())
72 .append(File.separator)
74 .append(File.separator)
75 .append("findbugs -textui -xml -output ")
76 .append(fbOutput.getAbsolutePath())
78 .append(fbp.getAbsolutePath());
83 // Come up with an appropriate name for the XML output file.
84 // workDir: place where the file should be created
85 // fbp: FindBugsProject file
86 File outputWorkFile(File workDir, File fbp)
88 assert(null != workDir);
91 String workPath = workDir.getAbsolutePath();
93 String projName = fbp.getName();
94 int len = projName.length();
96 String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
97 if (extension.equals(".fbp")) {
98 projName = projName.substring(0, len - 4);
102 return new File(workPath + File.separator + projName + ".xml");