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.CATEGORYID,
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.BUGID, CfbSchema.TYPE };
78 Table[] tables = { CfbSchema.BUGS };
79 Condition[] conditions = { };
80 List<Row> rows = driver.select(con, columns, tables, conditions);
83 Long bugId = row.getLong(0);
84 String type = row.getString(1);
86 BugPattern bug = getColl().getPattern(type);
88 throw new SQLException("Database result (" + bugId + ", \"" + type + "\") not found in messages.xml. "
89 + "Perhaps your database and findbugs versions are out of sync?");
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
99 m_findBugsDir = findBugsDir;
101 String langName = locale.getLanguage();
103 String basePath = findBugsDir.getAbsolutePath() + File.separator + "etc" + File.separator;
105 File msgXml = new File(basePath + MESSAGES + "_" + langName + "." + XML);
106 if (! msgXml.canRead()) {
107 msgXml = new File(basePath + MESSAGES + "." + XML);
110 if (! msgXml.canRead()) {
111 throw new FileNotFoundException(msgXml.getAbsolutePath());
114 parse(new InputSource(new FileInputStream(msgXml)));
118 // Parse the FindBugs messages.xml file
119 public void parse(InputSource xml) throws FileNotFoundException, IOException, SAXException
121 m_msgColl = new MessageCollection();
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);