Add code to load bug categories and patterns from the FindBugs messages.xml file.
[cfb.git] / prod / net / jaekl / cfb / CFB.java
index d587c1878e882b0a0dfab3152669240149da5dd1..28d2b1c698dc437113d58e1899596f4882fc32b6 100644 (file)
@@ -14,12 +14,15 @@ import java.sql.Connection;
 import java.sql.SQLException;
 import java.text.MessageFormat;
 import java.util.Locale;
+import java.util.Locale.Category;
 
 import net.jaekl.cfb.analyze.Analysis;
 import net.jaekl.cfb.analyze.Analyzer;
+import net.jaekl.cfb.analyze.MessageMap;
 import net.jaekl.cfb.db.CfbSchema;
 import net.jaekl.cfb.db.driver.DbDriver;
 import net.jaekl.cfb.db.driver.PostgresqlDriver;
+import net.jaekl.cfb.store.DbStore;
 import net.jaekl.qd.xml.XmlParseException;
 
 import org.apache.commons.cli.CommandLine;
@@ -27,6 +30,7 @@ import org.apache.commons.cli.GnuParser;
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
+import org.xml.sax.SAXException;
 
 public class CFB {
        DbDriver m_driver;
@@ -42,6 +46,7 @@ public class CFB {
        int m_port;             // db port
        String m_user;  // db user
        String m_pass;  // db password
+       String m_buildNum; // build number (version)
        
        CFB(Locale locale) {
                m_driver = new PostgresqlDriver();
@@ -56,6 +61,7 @@ public class CFB {
                m_port = 5432;
                m_pass = "";
                m_user = "user";
+               m_buildNum = null;
        }
        
        Options createOptions() {
@@ -64,6 +70,7 @@ public class CFB {
                opt.addOption("d", "dbname", true, "DB name");
                opt.addOption("f", "fbp",    true, "FindBugsProject file");
                opt.addOption("h", "host",   true, "DB hostname");
+               opt.addOption("n", "number", true, "Build number (version)");
                opt.addOption("p", "pass",   true, "DB password");
                opt.addOption("t", "port",   true, "DB port");
                opt.addOption("u", "user",   true, "DB username");
@@ -85,6 +92,9 @@ public class CFB {
                        if (line.hasOption("h")) {
                                m_host = line.getOptionValue("h");
                        }
+                       if (line.hasOption("n")) {
+                               m_buildNum = line.getOptionValue("n");
+                       }
                        if (line.hasOption("p")) {
                                m_pass = line.getOptionValue("p");
                        }
@@ -122,6 +132,10 @@ public class CFB {
                return System.getProperty(propName);
        }
        
+       File getFindBugsDir() {
+               return (null != m_fbDir) ? m_fbDir : new File(".");
+       }
+       
        void initArgs() {
                String findBugsDir = getenv("FINDBUGS_HOME");
                if (null != findBugsDir) {
@@ -133,7 +147,7 @@ public class CFB {
                }
        } 
        
-       void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException {
+       void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException {
                initArgs();     // read environment and system properties
                if ( ! parseArgs(pw, args) ) {
                        return;
@@ -143,21 +157,38 @@ public class CFB {
                        m_schema.ensureDbInitialized(con);                      
                }
                catch (SQLException exc) {
-                       String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
-                       String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
-                       exc.printStackTrace(pw);
-                       pw.println(cannotConnect);
+                       reportUnableToConnect(pw, exc);
                        return;
                }
                
-               File findBugsDir = (null != m_fbDir) ? m_fbDir : new File(".");
+               File findBugsDir = getFindBugsDir();
                File workDir = new File(".");
-               Analyzer analyzer = new Analyzer(findBugsDir);
-               Analysis analysis = analyzer.analyze(pw, workDir, m_fbp);
-               if (null != analysis) {
-                       // TODO
+               MessageMap messageMap = new MessageMap();
+               messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
+               Analyzer analyzer = new Analyzer(messageMap);
+               Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_buildNum);
+               if (null == analysis) {
+                       pw.println(trans(CfbBundle.ANALYSIS_FAILED));
+                       return;
+               }
+               
+               try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
+                       DbStore store = new DbStore(con);
+                       
+                       store.put(analysis);
+               }
+               catch (SQLException exc) {
+                       reportUnableToConnect(pw, exc);
+                       return;
                }
        }
+
+       private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
+               String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
+               String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
+               exc.printStackTrace(pw);
+               pw.println(cannotConnect);
+       }
        
        public static void main(String[] args) {
                CFB cfb = new CFB(Locale.getDefault());
@@ -165,7 +196,7 @@ public class CFB {
                try (PrintWriter pw = new PrintWriter(System.out)){
                        cfb.doMain(pw, args);
                        pw.flush();
-               } catch (SQLException | IOException | XmlParseException exc) {
+               } catch (SQLException | IOException | XmlParseException | SAXException exc) {
                        exc.printStackTrace();
                }
        }