(Finally) reach the point where we have some useful, if basic, functionality.
[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.CATEGORYID,
56                                              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.BUGID, CfbSchema.TYPE }; 
78                 Table[] tables = { CfbSchema.BUGS };
79                 Condition[] conditions = { };
80                 List<Row> rows = driver.select(con, columns, tables, conditions);
81                 
82                 for (Row row: rows) {
83                         Long bugId = row.getLong(0);
84                         String type = row.getString(1);
85                         
86                         BugPattern bug = getColl().getPattern(type);
87                         if (null == bug) {
88                                 throw new SQLException("Database result (" + bugId + ", \"" + type + "\") not found in messages.xml.  "
89                                        + "Perhaps your database and findbugs versions are out of sync?");
90                         }
91                         
92                         bug.setId(bugId);
93                 }
94         }
95
96         // Load the list of bug patterns and categories from the FindBugs messages.xml file.
97         public void load(File findBugsDir, Locale locale) throws FileNotFoundException, IOException, SAXException
98         {
99                 m_findBugsDir = findBugsDir;
100                 
101                 String langName = locale.getLanguage();
102                 
103                 String basePath = findBugsDir.getAbsolutePath() + File.separator + "etc" + File.separator;
104                 
105                 File msgXml = new File(basePath + MESSAGES + "_" + langName + "." + XML);
106                 if (! msgXml.canRead()) {
107                         msgXml = new File(basePath + MESSAGES + "." + XML);
108                 }
109                 
110                 if (! msgXml.canRead()) {
111                         throw new FileNotFoundException(msgXml.getAbsolutePath());
112                 }
113                 
114                 parse(new InputSource(new FileInputStream(msgXml)));
115                 
116         }
117         
118         // Parse the FindBugs messages.xml file
119         public void parse(InputSource xml) throws FileNotFoundException, IOException, SAXException 
120         {
121                 m_msgColl = new MessageCollection();
122                 
123             XMLReader reader = XMLReaderFactory.createXMLReader();
124             ParseHandler ph = new ParseHandler(m_msgColl);
125             ParseErrorHandler peh = new ParseErrorHandler();
126             reader.setContentHandler(ph);
127             reader.setErrorHandler(peh);
128             reader.parse(xml);
129         }
130 }