Some progress toward implementing store(Analysis).
[cfb.git] / prod / net / jaekl / cfb / analyze / MessageMap.java
1 package net.jaekl.cfb.analyze;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.sql.Connection;
8 import java.sql.SQLException;
9 import java.util.List;
10 import java.util.Locale;
11
12 import net.jaekl.cfb.db.CfbSchema;
13 import net.jaekl.cfb.db.Column;
14 import net.jaekl.cfb.db.Condition;
15 import net.jaekl.cfb.db.Row;
16 import net.jaekl.cfb.db.Table;
17 import net.jaekl.cfb.db.TypeMismatchException;
18 import net.jaekl.cfb.db.driver.DbDriver;
19 import net.jaekl.cfb.xml.messages.BugCategory;
20 import net.jaekl.cfb.xml.messages.BugPattern;
21 import net.jaekl.cfb.xml.messages.MessageCollection;
22 import net.jaekl.qd.xml.ParseErrorHandler;
23 import net.jaekl.qd.xml.ParseHandler;
24
25 import org.xml.sax.InputSource;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.XMLReader;
28 import org.xml.sax.helpers.XMLReaderFactory;
29
30 public class MessageMap {
31         static final String MESSAGES = "messages";
32         static final String XML = "xml";
33         
34         MessageCollection m_msgColl;
35         File m_findBugsDir;
36         
37         public MessageMap() {
38                 m_msgColl = null;
39                 m_findBugsDir = null;
40         }
41         
42         public MessageCollection getColl() { return m_msgColl; }
43         public File getFindBugsDir() { return m_findBugsDir; }
44
45         // Load the list of primary keys (sequence numbers) from the database
46         public void loadIds(Connection con, DbDriver driver) throws SQLException, TypeMismatchException
47         {
48                 loadCategoryIds(con, driver);
49                 loadBugPatternIds(con, driver);
50                 
51         }
52         
53         void loadCategoryIds(Connection con, DbDriver driver) throws SQLException, TypeMismatchException
54         {
55                 Column[] columns = { CfbSchema.CATEGORIES.getColumn(CfbSchema.CATEGORYID),
56                                              CfbSchema.CATEGORIES.getColumn(CfbSchema.CATEGORY)   };
57                 Table[] tables = { CfbSchema.CATEGORIES };
58                 Condition[] conditions = { };
59                 List<Row> rows = driver.select(con, columns, tables, conditions);
60                 
61                 for (Row row : rows) {
62                         long catId = row.getLong(0);
63                         String catName = row.getString(1);
64                         
65                         BugCategory cat = getColl().getCategory(catName);
66                         if (null == cat) {
67                                 throw new SQLException("Database result (" + catId + ", \"" + catName + "\") not found in messages.xml.  "
68                                                                + "Perhaps your database and findbugs versions are out of sync?");
69                         }
70                         
71                         cat.setId(catId);
72                 }
73         }
74         
75         void loadBugPatternIds(Connection con, DbDriver driver) throws SQLException, TypeMismatchException
76         {
77                 Column[] columns = { CfbSchema.BUGS.getColumn(CfbSchema.BUGID),
78                                      CfbSchema.BUGS.getColumn(CfbSchema.TYPE)   }; 
79                 Table[] tables = { CfbSchema.BUGS };
80                 Condition[] conditions = { };
81                 List<Row> rows = driver.select(con, columns, tables, conditions);
82                 
83                 for (Row row: rows) {
84                         long bugId = row.getLong(0);
85                         String type = row.getString(1);
86                         
87                         BugPattern bug = getColl().getPattern(type);
88                         if (null == bug) {
89                                 throw new SQLException("Database result (" + bugId + ", \"" + type + "\") not found in messages.xml.  "
90                                        + "Perhaps your database and findbugs versions are out of sync?");
91                         }
92                         
93                         bug.setId(bugId);
94                 }
95         }
96
97         // Load the list of bug patterns and categories from the FindBugs messages.xml file.
98         public void load(File findBugsDir, Locale locale) throws FileNotFoundException, IOException, SAXException
99         {
100                 m_findBugsDir = findBugsDir;
101                 
102                 String langName = locale.getLanguage();
103                 
104                 String basePath = findBugsDir.getAbsolutePath() + File.separator + "etc" + File.separator;
105                 
106                 File msgXml = new File(basePath + MESSAGES + "_" + langName + "." + XML);
107                 if (! msgXml.canRead()) {
108                         msgXml = new File(basePath + MESSAGES + "." + XML);
109                 }
110                 
111                 if (! msgXml.canRead()) {
112                         throw new FileNotFoundException(msgXml.getAbsolutePath());
113                 }
114                 
115                 parse(new InputSource(new FileInputStream(msgXml)));
116                 
117         }
118         
119         // Parse the FindBugs messages.xml file
120         void parse(InputSource xml) throws FileNotFoundException, IOException, SAXException 
121         {
122                 m_msgColl = new MessageCollection();
123                 
124             XMLReader reader = XMLReaderFactory.createXMLReader();
125             ParseHandler ph = new ParseHandler(m_msgColl);
126             ParseErrorHandler peh = new ParseErrorHandler();
127             reader.setContentHandler(ph);
128             reader.setErrorHandler(peh);
129             reader.parse(xml);
130         }
131 }