97180030af8a9b80e109278532fe81f4afc29436
[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.util.Locale;
8
9 import net.jaekl.cfb.xml.messages.MessageCollection;
10 import net.jaekl.qd.xml.ParseErrorHandler;
11 import net.jaekl.qd.xml.ParseHandler;
12
13 import org.xml.sax.InputSource;
14 import org.xml.sax.SAXException;
15 import org.xml.sax.XMLReader;
16 import org.xml.sax.helpers.XMLReaderFactory;
17
18 public class MessageMap {
19         static final String MESSAGES = "messages";
20         static final String XML = "xml";
21         
22         MessageCollection m_msgColl;
23         File m_findBugsDir;
24         
25         public MessageMap() {
26                 m_msgColl = null;
27                 m_findBugsDir = null;
28         }
29         
30         public MessageCollection getColl() { return m_msgColl; }
31         public File getFindBugsDir() { return m_findBugsDir; }
32
33         public void load(File findBugsDir, Locale locale) throws FileNotFoundException, IOException, SAXException
34         {
35                 m_findBugsDir = findBugsDir;
36                 
37                 String langName = locale.getLanguage();
38                 
39                 String basePath = findBugsDir.getAbsolutePath() + File.separator + "etc" + File.separator;
40                 
41                 File msgXml = new File(basePath + MESSAGES + "_" + langName + "." + XML);
42                 if (! msgXml.canRead()) {
43                         msgXml = new File(basePath + MESSAGES + "." + XML);
44                 }
45                 
46                 if (! msgXml.canRead()) {
47                         throw new FileNotFoundException(msgXml.getAbsolutePath());
48                 }
49                 
50                 parse(new InputSource(new FileInputStream(msgXml)));
51                 
52         }
53         
54         void parse(InputSource xml) throws FileNotFoundException, IOException, SAXException 
55         {
56                 m_msgColl = new MessageCollection();
57                 
58             XMLReader reader = XMLReaderFactory.createXMLReader();
59             ParseHandler ph = new ParseHandler(m_msgColl);
60             ParseErrorHandler peh = new ParseErrorHandler();
61             reader.setContentHandler(ph);
62             reader.setErrorHandler(peh);
63             reader.parse(xml);
64         }
65 }