1 package net.jaekl.cfb.analyze;
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;
10 import java.util.Locale;
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;
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;
30 public class MessageMap {
31 static final String MESSAGES = "messages";
32 static final String XML = "xml";
34 MessageCollection m_msgColl;
42 public MessageCollection getColl() { return m_msgColl; }
43 public File getFindBugsDir() { return m_findBugsDir; }
45 // Load the list of primary keys (sequence numbers) from the database
46 public void loadIds(Connection con, DbDriver driver) throws SQLException, TypeMismatchException
48 loadCategoryIds(con, driver);
49 loadBugPatternIds(con, driver);
53 void loadCategoryIds(Connection con, DbDriver driver) throws SQLException, TypeMismatchException
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);
61 for (Row row : rows) {
62 long catId = row.getLong(0);
63 String catName = row.getString(1);
65 BugCategory cat = getColl().getCategory(catName);
67 throw new SQLException("Database result (" + catId + ", \"" + catName + "\") not found in messages.xml. "
68 + "Perhaps your database and findbugs versions are out of sync?");
75 void loadBugPatternIds(Connection con, DbDriver driver) throws SQLException, TypeMismatchException
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);
84 long bugId = row.getLong(0);
85 String type = row.getString(1);
87 BugPattern bug = getColl().getPattern(type);
89 throw new SQLException("Database result (" + bugId + ", \"" + type + "\") not found in messages.xml. "
90 + "Perhaps your database and findbugs versions are out of sync?");
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
100 m_findBugsDir = findBugsDir;
102 String langName = locale.getLanguage();
104 String basePath = findBugsDir.getAbsolutePath() + File.separator + "etc" + File.separator;
106 File msgXml = new File(basePath + MESSAGES + "_" + langName + "." + XML);
107 if (! msgXml.canRead()) {
108 msgXml = new File(basePath + MESSAGES + "." + XML);
111 if (! msgXml.canRead()) {
112 throw new FileNotFoundException(msgXml.getAbsolutePath());
115 parse(new InputSource(new FileInputStream(msgXml)));
119 // Parse the FindBugs messages.xml file
120 void parse(InputSource xml) throws FileNotFoundException, IOException, SAXException
122 m_msgColl = new MessageCollection();
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);