Add the concept of "Project Name" to the RUNS table in the database.
[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 net.jaekl.cfb.CfbBundle;
14 import net.jaekl.cfb.util.Command;
15 import net.jaekl.qd.xml.XmlParseException;
16
17 import org.xml.sax.InputSource;
18 import org.xml.sax.SAXException;
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 projName, String buildNumber) 
28                 throws IOException, XmlParseException, SAXException 
29         {
30                 Analysis result = new Analysis(projName, buildNumber);
31         
32                 File fbOutput = outputWorkFile(workDir, fbp);
33                 
34                 String cmdLine = buildCommandLine(workDir, fbp, fbOutput);
35                 pw.println(cmdLine);
36                 pw.flush();
37                 Command.Result fbResult = new Command().exec(cmdLine);
38                 pw.println("==> " + fbResult.getRetCode());
39                 pw.flush();
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());
50                         return null;
51                 }
52                 
53                 result.setEnd(new Date());
54                 result.parse(new InputSource(fbOutput.getAbsolutePath()));
55                 // result.dump(pw);                     
56                 return result;
57         }
58         
59         String trans(String key) {
60                 return CfbBundle.getInst(Locale.getDefault(Category.DISPLAY)).get(key);
61         }
62         
63         String buildCommandLine(File workDir, File fbp, File fbOutput) 
64         {
65                 assert(null != workDir);
66                 assert(null != fbp);
67                 assert(null != fbOutput);
68                 
69                 StringBuilder sb = new StringBuilder();
70                 
71                 sb.append(m_msgMap.getFindBugsDir().getAbsolutePath())
72                   .append(File.separator)
73                   .append("bin")
74                   .append(File.separator)
75                   .append("findbugs -textui -xml -output ")
76                   .append(fbOutput.getAbsolutePath())
77                   .append(" -project ")
78                   .append(fbp.getAbsolutePath());
79                 
80                 return sb.toString();
81         }       
82         
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) 
87         {
88                 assert(null != workDir);
89                 assert(null != fbp);
90                 
91                 String workPath = workDir.getAbsolutePath();
92                 
93                 String projName = fbp.getName();
94                 int len = projName.length();
95                 if (len > 4) {
96                         String extension = projName.substring(len - 4, len).toLowerCase(Locale.CANADA);
97                         if (extension.equals(".fbp")) {
98                                 projName = projName.substring(0, len - 4);
99                         }
100                 }
101                 
102                 return new File(workPath + File.separator + projName + ".xml");
103         }
104 }