Improve XML parsing to handle attributes as well.
[cfb.git] / prod / net / jaekl / cfb / CFB.java
1 package net.jaekl.cfb;
2
3 // Comparative FindBugs
4 // 
5 // Tool to compare successive runs of FindBugs, 
6 // flagging the change from one run to the next.
7 // 
8 // Copyright (C) 2015 Christian Jaekl
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.io.PrintWriter;
13 import java.sql.Connection;
14 import java.sql.SQLException;
15 import java.text.MessageFormat;
16 import java.util.Locale;
17
18 import net.jaekl.cfb.analyze.Analysis;
19 import net.jaekl.cfb.analyze.Analyzer;
20 import net.jaekl.cfb.db.CfbSchema;
21 import net.jaekl.cfb.db.driver.DbDriver;
22 import net.jaekl.cfb.db.driver.PostgresqlDriver;
23 import net.jaekl.qd.xml.XmlParseException;
24
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.cli.GnuParser;
27 import org.apache.commons.cli.HelpFormatter;
28 import org.apache.commons.cli.Options;
29 import org.apache.commons.cli.ParseException;
30
31 public class CFB {
32         DbDriver m_driver;
33         CfbSchema m_schema;
34         CfbBundle m_bundle;     
35         Locale m_locale;
36         
37         // Command-line parameters
38         String m_dbName; // db name
39         File m_fbp;             // FindBugsProject file
40         File m_fbDir;   // Directory where FindBugs is installed
41         String m_host;  // db host
42         int m_port;             // db port
43         String m_user;  // db user
44         String m_pass;  // db password
45         
46         CFB(Locale locale) {
47                 m_driver = new PostgresqlDriver();
48                 m_schema = new CfbSchema(m_driver);
49                 m_locale = locale;
50                 m_bundle = CfbBundle.getInst(m_locale);
51                 
52                 m_dbName = "CFB";
53                 m_fbp    = null;
54                 m_fbDir  = null;
55                 m_host = "localhost";
56                 m_port = 5432;
57                 m_pass = "";
58                 m_user = "user";
59         }
60         
61         Options createOptions() {
62                 Options opt = new Options();
63                 
64                 opt.addOption("d", "dbname", true, "DB name");
65                 opt.addOption("f", "fbp",    true, "FindBugsProject file");
66                 opt.addOption("h", "host",   true, "DB hostname");
67                 opt.addOption("p", "pass",   true, "DB password");
68                 opt.addOption("t", "port",   true, "DB port");
69                 opt.addOption("u", "user",   true, "DB username");
70                 
71                 return opt;
72         }
73         
74         boolean parseArgs(PrintWriter pw, String[] args) {
75                 Options opt = createOptions();
76                 
77                 try {
78                         CommandLine line = new GnuParser().parse(opt, args);
79                         if (line.hasOption("d")) {
80                                 m_dbName = line.getOptionValue("d");
81                         }
82                         if (line.hasOption("f")) {
83                                 m_fbp = new File(line.getOptionValue("f"));
84                         }
85                         if (line.hasOption("h")) {
86                                 m_host = line.getOptionValue("h");
87                         }
88                         if (line.hasOption("p")) {
89                                 m_pass = line.getOptionValue("p");
90                         }
91                         if (line.hasOption("t")) {
92                                 m_port = Integer.parseInt(line.getOptionValue("t"));
93                         }
94                         if (line.hasOption("u")) {
95                                 m_user = line.getOptionValue("u");
96                         }
97                 } 
98                 catch (ParseException exc) {
99                         usage(pw, opt);
100                         return false;
101                 }
102                 
103                 return true;
104         }
105         
106         void usage(PrintWriter pw, Options opt) {
107                 HelpFormatter help = new HelpFormatter();
108                 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
109         }
110         
111         String trans(String key) {
112                 return m_bundle.get(key);
113         }
114         
115         String getenv(String varName) {
116                 // This is a separate function so that we can override it at unit test time
117                 return System.getenv(varName);
118         }
119         
120         String getProperty(String propName) {
121                 // This is a separate function so that we can override it at unit test time
122                 return System.getProperty(propName);
123         }
124         
125         void initArgs() {
126                 String findBugsDir = getenv("FINDBUGS_HOME");
127                 if (null != findBugsDir) {
128                         m_fbDir = new File(findBugsDir);
129                 }
130                 findBugsDir = getProperty("findbugs.home");
131                 if (null != findBugsDir) {
132                         m_fbDir = new File(findBugsDir);
133                 }
134         } 
135         
136         void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException {
137                 initArgs();     // read environment and system properties
138                 if ( ! parseArgs(pw, args) ) {
139                         return;
140                 }
141                 
142                 try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
143                         m_schema.ensureDbInitialized(con);                      
144                 }
145                 catch (SQLException exc) {
146                         String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
147                         String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
148                         exc.printStackTrace(pw);
149                         pw.println(cannotConnect);
150                         return;
151                 }
152                 
153                 File findBugsDir = (null != m_fbDir) ? m_fbDir : new File(".");
154                 File workDir = new File(".");
155                 Analyzer analyzer = new Analyzer(findBugsDir);
156                 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp);
157                 if (null != analysis) {
158                         // TODO
159                 }
160         }
161         
162         public static void main(String[] args) {
163                 CFB cfb = new CFB(Locale.getDefault());
164                 
165                 try (PrintWriter pw = new PrintWriter(System.out)){
166                         cfb.doMain(pw, args);
167                         pw.flush();
168                 } catch (SQLException | IOException | XmlParseException exc) {
169                         exc.printStackTrace();
170                 }
171         }
172
173 }