Add unit tests. Make DbStore handle cases where the bug type or category
[cfb.git] / prod / net / jaekl / cfb / xml / messages / MessageCollection.java
1 package net.jaekl.cfb.xml.messages;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5
6 import net.jaekl.qd.xml.ParseResult;
7 import net.jaekl.qd.xml.XmlParseException;
8
9 public class MessageCollection extends ParseResult {
10         static final String TAG = "MessageCollection";
11         static final String[] INTERNAL = {  };
12         static final Object[][] EXTERNAL = { { BugCategory.TAG, BugCategory.class },
13                                                  { BugPattern.TAG,  BugPattern.class }   };
14
15         HashMap<String, BugCategory> m_categories;
16         HashMap<String, BugPattern> m_patterns;
17         
18         public MessageCollection() 
19         {
20                 super(TAG, INTERNAL, EXTERNAL);
21                 m_categories = new HashMap<String, BugCategory>();
22                 m_patterns   = new HashMap<String, BugPattern>();
23         }
24         
25         public Collection<BugCategory> getCategories() { return m_categories.values(); }
26         public BugCategory getCategory(String category) { 
27                 BugCategory cat = m_categories.get(category); 
28                 if (null == cat) {
29                         cat = BugCategory.UNKNOWN;
30                 }
31                 return cat;
32         }
33         
34         public Collection<BugPattern> getPatterns() { return m_patterns.values(); }
35         public BugPattern getPattern(String type) { 
36                 BugPattern pat = m_patterns.get(type);
37                 if (null == pat) {
38                         pat = BugPattern.UNKNOWN;
39                 }
40                 return pat;
41         }
42
43         @Override
44         public void endContents(String uri, String localName, String qName, String chars) 
45                 throws XmlParseException 
46         {
47                 // Nothing to do
48         }
49
50         @Override
51         public void endExternal(String uri, String localName, String qName) throws XmlParseException 
52         {
53                 ParseResult[] prs;
54                 
55                 prs = collectParsedChildren(BugCategory.class);
56                 if (null != prs && prs.length > 0) {
57                         for (ParseResult pr : prs) {
58                                 assert(pr instanceof BugCategory);
59                                 BugCategory bc = (BugCategory)pr;
60                                 m_categories.put(bc.getCategory(), bc);
61                         }
62                 }
63                 
64                 prs = collectParsedChildren(BugPattern.class);
65                 if (null != prs && prs.length > 0) {
66                         for (ParseResult pr : prs) {
67                                 assert(pr instanceof BugPattern);
68                                 BugPattern bp = (BugPattern) pr;
69                                 m_patterns.put(bp.getType(), bp);
70                         }
71                 }
72         }
73
74 }