ac5288faf7e673b48fbf9f6c51fb4264f8f7a057
[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                 File msgXml = new File(findBugsDir.getAbsolutePath() + File.separator + MESSAGES + "_" + langName + "." + XML);
40                 if (! msgXml.canRead()) {
41                         msgXml = new File(findBugsDir.getAbsolutePath() + File.separator + MESSAGES + "." + XML);
42                 }
43                 
44                 if (! msgXml.canRead()) {
45                         throw new FileNotFoundException(msgXml.getAbsolutePath());
46                 }
47                 
48                 parse(new InputSource(new FileInputStream(msgXml)));
49                 
50         }
51         
52         void parse(InputSource xml) throws FileNotFoundException, IOException, SAXException 
53         {
54                 m_msgColl = new MessageCollection();
55                 
56             XMLReader reader = XMLReaderFactory.createXMLReader();
57             ParseHandler ph = new ParseHandler(m_msgColl);
58             ParseErrorHandler peh = new ParseErrorHandler();
59             reader.setContentHandler(ph);
60             reader.setErrorHandler(peh);
61             reader.parse(xml);
62         }
63 }