3f3c4cfa1395c548f2066486d737f06f9637f9fd
[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.Date;
10 import java.util.Locale;
11 import java.util.Locale.Category;
12
13 import org.xml.sax.InputSource;
14 import org.xml.sax.SAXException;
15
16 import net.jaekl.cfb.CfbBundle;
17 import net.jaekl.cfb.util.Command;
18 import net.jaekl.qd.xml.XmlParseException;
19
20 public class Analyzer {
21         MessageMap m_msgMap;
22         
23         public Analyzer(MessageMap msgMap) {
24                 m_msgMap = msgMap;
25         }
26         
27         public Analysis analyze(PrintWriter pw, File workDir, File fbp, String buildNumber) throws IOException, XmlParseException, SAXException 
28         {
29                 Analysis result = new Analysis(buildNumber);
30         
31                 File fbOutput = outputWorkFile(workDir, fbp);
32                 
33                 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
34                 pw.println(cmdLine);
35                 pw.flush();
36                 Command.Result fbResult = new Command().exec(cmdLine);
37                 pw.println("==> " + fbResult.getRetCode());
38                 pw.flush();
39                 if (0 != fbResult.getRetCode()) {
40                         // Our attempt to execute FindBugs failed.
41                         // Report the error and return null.
42                         String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
43                         String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
44                         pw.println(cannotExecMsg);
45                         pw.println(trans(CfbBundle.STDOUT_WAS));
46                         pw.println(fbResult.getStdout());
47                         pw.println(trans(CfbBundle.STDERR_WAS));
48                         pw.println(fbResult.getStderr());
49                         return null;
50                 }
51                 
52                 result.setEnd(new Date());
53                 result.parse(new InputSource(fbOutput.getAbsolutePath()));
54                 // result.dump(pw);                     
55                 return result;
56         }
57         
58         String trans(String key) {
59                 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
60         }
61         
62         String buildCommandLine(File workDir, File fbp, File fbOutput) 
63         {
64                 assert(null != workDir);
65                 assert(null != fbp);
66                 assert(null != fbOutput);
67                 
68                 StringBuilder sb = new StringBuilder();
69                 
70                 sb.append(m_msgMap.getFindBugsDir().getAbsolutePath())
71                   .append(File.separator)
72                   .append("bin")
73                   .append(File.separator)
74                   .append("findbugs -textui -xml -output ")
75                   .append(fbOutput.getAbsolutePath())
76                   .append(" -project ")
77                   .append(fbp.getAbsolutePath());
78                 
79                 return sb.toString();
80         }       
81         
82         // Come up with an appropriate name for the XML output file.
83         //   workDir:  place where the file should be created
84         //       fbp:  FindBugsProject file
85         File outputWorkFile(File workDir, File fbp) 
86         {
87                 assert(null != workDir);
88                 assert(null != fbp);
89                 
90                 String workPath = workDir.getAbsolutePath();
91                 
92                 String projName = fbp.getName();
93                 int len = projName.length();
94                 if (len > 4) {
95                         String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
96                         if (extension.equals(".fbp")) {
97                                 projName = projName.substring(0, len - 4);
98                         }
99                 }
100                 
101                 return new File(workPath + File.separator + projName + ".xml");
102         }
103 }