4c78f8d5606d62ed6ab991e3248236801ac94ab5
[cfb.git] / prod / net / jaekl / cfb / xml / BugInstance.java
1 package net.jaekl.cfb.xml;
2
3 import java.io.PrintWriter;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.List;
7
8 import org.xml.sax.Attributes;
9
10 import net.jaekl.qd.xml.MissingAttributeException;
11 import net.jaekl.qd.xml.ParseResult;
12 import net.jaekl.qd.xml.XmlParseException;
13
14 public class BugInstance extends ParseResult {
15
16         static final String TAG = "BugInstance";
17         static final String[] INTERNAL = {  };
18         static final Object[][] EXTERNAL = { { BugClass.TAG, BugClass.class},
19                                                  { BugMethod.TAG, BugMethod.class},
20                                                  { LocalVariable.TAG, LocalVariable.class},
21                                                  { SourceLine.TAG, SourceLine.class} };
22         static final String CATEGORY = "category";
23         static final String TYPE = "type";
24
25         String m_category;
26         String m_type;
27         ArrayList<BugClass> m_classes;
28         ArrayList<BugMethod> m_methods;
29         ArrayList<LocalVariable> m_locals;
30         ArrayList<SourceLine> m_lines;
31         
32         public BugInstance() {
33                 super(TAG, INTERNAL, EXTERNAL);
34                 
35                 m_category = m_type = null;
36                 m_classes = new ArrayList<BugClass>();
37                 m_methods = new ArrayList<BugMethod>();
38                 m_locals = new ArrayList<LocalVariable>();
39                 m_lines = new ArrayList<SourceLine>();
40         }
41         
42         public String getCategory() { return m_category; }
43         public String getType() { return m_type; }
44         public List<BugClass> getClasses() { return Collections.unmodifiableList(m_classes); }
45         public List<BugMethod> getMethods() { return Collections.unmodifiableList(m_methods); }
46         public List<SourceLine> getLines() { return Collections.unmodifiableList(m_lines); }
47         
48         @Override
49         public void endContents(String uri, String localName, String qName, String chars) 
50                 throws XmlParseException 
51         {
52                 // no-op
53         }
54
55         @Override
56         public void endExternal(String uri, String localName, String qName)
57                 throws XmlParseException 
58         {
59                 if (BugClass.TAG.equals(localName)) {
60                         ParseResult[] collected = collectParsedChildren(BugClass.class);
61                         for (ParseResult pr : collected) {
62                                 assert(pr instanceof BugClass);
63                                 m_classes.add((BugClass) pr);
64                         }
65                 }
66                 else if (BugMethod.TAG.equals(localName)) {
67                         ParseResult[] collected = collectParsedChildren(BugMethod.class);
68                         for (ParseResult pr : collected) {
69                                 assert(pr instanceof BugMethod);
70                                 m_methods.add((BugMethod)pr);
71                         }                       
72                 }
73                 else if (LocalVariable.TAG.equals(localName)) {
74                         ParseResult[] collected = collectParsedChildren(LocalVariable.class);
75                         for (ParseResult pr : collected) {
76                                 assert(pr instanceof LocalVariable);
77                                 m_locals.add((LocalVariable)pr);
78                         }                       
79                 }
80                 else if (SourceLine.TAG.equals(localName)) {
81                         ParseResult[] collected = collectParsedChildren(SourceLine.class);
82                         for (ParseResult pr : collected) {
83                                 assert(pr instanceof SourceLine);
84                                 m_lines.add((SourceLine)pr);
85                         }
86                 }
87         }
88         
89         @Override
90         public void handleMainAttributes(Attributes attr) throws MissingAttributeException
91         {
92                 m_type = this.getRequiredAttr(TAG, attr, TYPE);
93                 m_category = this.getRequiredAttr(TAG, attr, CATEGORY);
94         }
95
96         @Override 
97         public void dump(PrintWriter pw, int indent)
98         {
99                 int childIndent = indent + 2;
100                 String margin = String.format("%" + indent + "s", "");
101                 
102                 pw.println(margin + TAG + " (" + m_type + ")");
103                 pw.println(margin + CATEGORY + " (" + m_category + ")");
104                 for (BugClass bc : m_classes) {
105                         bc.dump(pw, childIndent);
106                 }
107                 for (BugMethod bm : m_methods) {
108                         bm.dump(pw, childIndent);
109                 }
110                 for (LocalVariable lv : m_locals) {
111                         lv.dump(pw, childIndent);
112                 }
113                 for (SourceLine sl : m_lines) {
114                         sl.dump(pw, childIndent);
115                 }
116         }
117 }