Improve XML parsing to handle attributes as well.
[cfb.git] / prod / net / jaekl / cfb / xml / BugInstance.java
index 81ebcfcd671f4abdf8afa16fe62bec1175116abd..900cd1cfd37c1f9fa6fa5643b624dbf635d5fb38 100644 (file)
 package net.jaekl.cfb.xml;
 
+import java.io.PrintWriter;
+import java.util.ArrayList;
+
 import org.xml.sax.Attributes;
 
+import net.jaekl.qd.xml.MissingAttributeException;
 import net.jaekl.qd.xml.ParseResult;
 import net.jaekl.qd.xml.XmlParseException;
 
 public class BugInstance extends ParseResult {
 
-       static final String ROOT_TAG = "BugInstance";
+       static final String TAG = "BugInstance";
        static final String[] INTERNAL = {  };
-       static final Object[][] EXTERNAL = { { BugClass.ROOT_TAG, BugClass.class},
-                                                { BugMethod.ROOT_TAG, BugMethod.class},
-                                                { SourceLine.ROOT_TAG, SourceLine.class} };
+       static final Object[][] EXTERNAL = { { BugClass.TAG, BugClass.class},
+                                                { BugMethod.TAG, BugMethod.class},
+                                                { LocalVariable.TAG, LocalVariable.class},
+                                                { SourceLine.TAG, SourceLine.class} };
+       static final String TYPE = "type";
 
+       String m_type;
+       ArrayList<BugClass> m_classes;
+       ArrayList<BugMethod> m_methods;
+       ArrayList<LocalVariable> m_locals;
+       ArrayList<SourceLine> m_lines;
+       
        public BugInstance() {
-               super(ROOT_TAG, INTERNAL, EXTERNAL);
+               super(TAG, INTERNAL, EXTERNAL);
+               
+               m_type = null;
+               m_classes = new ArrayList<BugClass>();
+               m_methods = new ArrayList<BugMethod>();
+               m_locals = new ArrayList<LocalVariable>();
+               m_lines = new ArrayList<SourceLine>();
        }
        
        @Override
-       public void endContents(String uri, String localName, String qName,
-                       String chars, Attributes attr) throws XmlParseException {
-               // TODO Auto-generated method stub
-
+       public void endContents(String uri, String localName, String qName, String chars) 
+               throws XmlParseException 
+       {
+               // no-op
        }
 
        @Override
        public void endExternal(String uri, String localName, String qName)
-                       throws XmlParseException {
-               // TODO Auto-generated method stub
-
+               throws XmlParseException 
+       {
+               if (BugClass.TAG.equals(localName)) {
+                       ParseResult[] collected = collectParsedChildren(BugClass.class);
+                       for (ParseResult pr : collected) {
+                               assert(pr instanceof BugClass);
+                               m_classes.add((BugClass) pr);
+                       }
+               }
+               else if (BugMethod.TAG.equals(localName)) {
+                       ParseResult[] collected = collectParsedChildren(BugMethod.class);
+                       for (ParseResult pr : collected) {
+                               assert(pr instanceof BugMethod);
+                               m_methods.add((BugMethod)pr);
+                       }                       
+               }
+               else if (LocalVariable.TAG.equals(localName)) {
+                       ParseResult[] collected = collectParsedChildren(LocalVariable.class);
+                       for (ParseResult pr : collected) {
+                               assert(pr instanceof LocalVariable);
+                               m_locals.add((LocalVariable)pr);
+                       }                       
+               }
+               else if (SourceLine.TAG.equals(localName)) {
+                       ParseResult[] collected = collectParsedChildren(SourceLine.class);
+                       for (ParseResult pr : collected) {
+                               assert(pr instanceof SourceLine);
+                               m_lines.add((SourceLine)pr);
+                       }
+               }
+       }
+       
+       @Override
+       public void handleMainAttributes(Attributes attr) throws MissingAttributeException
+       {
+               m_type = this.getRequiredAttr(TAG, attr, TYPE);
        }
 
+       @Override 
+       public void dump(PrintWriter pw, int indent)
+       {
+               int childIndent = indent + 2;
+               String margin = String.format("%" + indent + "s", "");
+               
+               pw.println(margin + TAG + " (" + m_type + ")");
+               for (BugClass bc : m_classes) {
+                       bc.dump(pw, childIndent);
+               }
+               for (BugMethod bm : m_methods) {
+                       bm.dump(pw, childIndent);
+               }
+               for (LocalVariable lv : m_locals) {
+                       lv.dump(pw, childIndent);
+               }
+               for (SourceLine sl : m_lines) {
+                       sl.dump(pw, childIndent);
+               }
+       }
 }