03aeb1c6a4db718198e2eca9da9b9538829eab39
[cfb.git] / prod / net / jaekl / cfb / analyze / Analyzer.java
1 package net.jaekl.cfb.analyze;
2
3 // Copyright (C) 2015 Christian Jaekl
4
5 import java.io.File;
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;
11
12 import org.xml.sax.InputSource;
13 import org.xml.sax.SAXException;
14
15 import net.jaekl.cfb.CfbBundle;
16 import net.jaekl.cfb.util.Command;
17 import net.jaekl.qd.xml.XmlParseException;
18
19 public class Analyzer {
20         MessageMap m_msgMap;
21         
22         public Analyzer(MessageMap msgMap) {
23                 m_msgMap = msgMap;
24         }
25         
26         public Analysis analyze(PrintWriter pw, File workDir, File fbp, String buildNumber) throws IOException, XmlParseException, SAXException 
27         {
28                 Analysis result = new Analysis(buildNumber);
29         
30                 File fbOutput = outputWorkFile(workDir, fbp);
31                 
32                 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
33                 pw.println(cmdLine);
34                 pw.flush();
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());
46                         return null;
47                 }
48                 
49                 result.parse(new InputSource(fbOutput.getAbsolutePath()));
50                 result.dump(pw);                        
51                 return result;
52         }
53         
54         String trans(String key) {
55                 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
56         }
57         
58         String buildCommandLine(File workDir, File fbp, File fbOutput) 
59         {
60                 assert(null != workDir);
61                 assert(null != fbp);
62                 assert(null != fbOutput);
63                 
64                 StringBuilder sb = new StringBuilder();
65                 
66                 sb.append(m_msgMap.getFindBugsDir().getAbsolutePath())
67                   .append(File.separator)
68                   .append("bin")
69                   .append(File.separator)
70                   .append("findbugs -textui -xml -output ")
71                   .append(fbOutput.getAbsolutePath())
72                   .append(" -project ")
73                   .append(fbp.getAbsolutePath());
74                 
75                 return sb.toString();
76         }       
77         
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) 
82         {
83                 assert(null != workDir);
84                 assert(null != fbp);
85                 
86                 String workPath = workDir.getAbsolutePath();
87                 
88                 String projName = fbp.getName();
89                 int len = projName.length();
90                 if (len > 4) {
91                         String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
92                         if (extension.equals(".fbp")) {
93                                 projName = projName.substring(0, len - 4);
94                         }
95                 }
96                 
97                 return new File(workPath + File.separator + projName + ".xml");
98         }
99 }