22d65edfcf5d83d1880e011a36fc1f5eb18806eb
[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                 if (0 != fbResult.getRetCode()) {
38                         // Our attempt to execute FindBugs failed.
39                         // Report the error and return null.
40                         String cannotExecFormat = trans(CfbBundle.CANNOT_EXEC);
41                         String cannotExecMsg = MessageFormat.format(cannotExecFormat, cmdLine, fbResult.getRetCode());
42                         pw.println(cannotExecMsg);
43                         pw.println(trans(CfbBundle.STDOUT_WAS));
44                         pw.println(fbResult.getStdout());
45                         pw.println(trans(CfbBundle.STDERR_WAS));
46                         pw.println(fbResult.getStderr());
47                         return null;
48                 }
49                 
50                 result.setEnd(new Date());
51                 result.parse(new InputSource(fbOutput.getAbsolutePath()));
52                 result.dump(pw);                        
53                 return result;
54         }
55         
56         String trans(String key) {
57                 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
58         }
59         
60         String buildCommandLine(File workDir, File fbp, File fbOutput) 
61         {
62                 assert(null != workDir);
63                 assert(null != fbp);
64                 assert(null != fbOutput);
65                 
66                 StringBuilder sb = new StringBuilder();
67                 
68                 sb.append(m_msgMap.getFindBugsDir().getAbsolutePath())
69                   .append(File.separator)
70                   .append("bin")
71                   .append(File.separator)
72                   .append("findbugs -textui -xml -output ")
73                   .append(fbOutput.getAbsolutePath())
74                   .append(" -project ")
75                   .append(fbp.getAbsolutePath());
76                 
77                 return sb.toString();
78         }       
79         
80         // Come up with an appropriate name for the XML output file.
81         //   workDir:  place where the file should be created
82         //       fbp:  FindBugsProject file
83         File outputWorkFile(File workDir, File fbp) 
84         {
85                 assert(null != workDir);
86                 assert(null != fbp);
87                 
88                 String workPath = workDir.getAbsolutePath();
89                 
90                 String projName = fbp.getName();
91                 int len = projName.length();
92                 if (len > 4) {
93                         String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
94                         if (extension.equals(".fbp")) {
95                                 projName = projName.substring(0, len - 4);
96                         }
97                 }
98                 
99                 return new File(workPath + File.separator + projName + ".xml");
100         }
101 }