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