Partial implementation of XML parse for FindBugs output
[cfb.git] / prod / net / jaekl / cfb / analyze / Analyzer.java
diff --git a/prod/net/jaekl/cfb/analyze/Analyzer.java b/prod/net/jaekl/cfb/analyze/Analyzer.java
new file mode 100644 (file)
index 0000000..715cae9
--- /dev/null
@@ -0,0 +1,99 @@
+package net.jaekl.cfb.analyze;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.Locale.Category;
+
+import net.jaekl.cfb.CfbBundle;
+import net.jaekl.cfb.util.Command;
+
+public class Analyzer {
+       File m_findbugsDir;
+       
+       public Analyzer(File findbugsDir) {
+               m_findbugsDir = findbugsDir;
+       }
+       
+       public Analysis analyze(PrintWriter pw, File workDir, File fbp) throws IOException {
+               Analysis result = new Analysis();
+       
+               File fbOutput = outputWorkFile(workDir, fbp);
+               
+               String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
+               pw.println(cmdLine);
+               pw.flush();
+               Command.Result fbResult = new Command().exec(cmdLine);
+               if (0 != fbResult.getRetCode()) {
+                       // Our attempt to execute FindBugs failed.
+                       // Report the error and return null.
+                       String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
+                       String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
+                       pw.println(cannotExecMsg);
+                       pw.println(trans(CfbBundle.STDOUT_WAS));
+                       pw.println(fbResult.getStdout());
+                       pw.println(trans(CfbBundle.STDERR_WAS));
+                       pw.println(fbResult.getStderr());
+                       return null;
+               }
+               
+               result = parseFbOutput(fbOutput);
+               
+               return result;
+       }
+       
+       String trans(String key) {
+               return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
+       }
+       
+       String buildCommandLine(File workDir, File fbp, File fbOutput) 
+       {
+               assert(null != workDir);
+               assert(null != fbp);
+               assert(null != fbOutput);
+               
+               StringBuilder sb = new StringBuilder();
+               
+               sb.append(m_findbugsDir.getAbsolutePath())
+                 .append(File.separator)
+                 .append("bin")
+                 .append(File.separator)
+                 .append("findbugs -textui -xml -output ")
+                 .append(fbOutput.getAbsolutePath())
+                 .append(" -project ")
+                 .append(fbp.getAbsolutePath());
+               
+               return sb.toString();
+       }       
+       
+       // Come up with an appropriate name for the XML output file.
+       //   workDir:  place where the file should be created
+       //       fbp:  FindBugsProject file
+       File outputWorkFile(File workDir, File fbp) 
+       {
+               assert(null != workDir);
+               assert(null != fbp);
+               
+               String workPath = workDir.getAbsolutePath();
+               
+               String projName = fbp.getName();
+               int len = projName.length();
+               if (len > 4) {
+                       String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
+                       if (extension.equals(".fbp")) {
+                               projName = projName.substring(0, len - 4);
+                       }
+               }
+               
+               return new File(workPath + File.separator + projName + ".xml");
+       }
+       
+       // Parse the output.xml that resulted from a FindBugs run,
+       // and store its findings into an Analysis object.
+       Analysis parseFbOutput(File fbOutput) 
+       {
+               return null;
+       }
+}