Add ability to load previously found bugs back out of the database.
[cfb.git] / prod / net / jaekl / cfb / xml / BugMethod.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 BugMethod extends ParseResult {
13         
14         static final String TAG = "Method";
15         static final String[] INTERNAL = { };
16         static final Object[][] EXTERNAL = { { SourceLine.TAG, SourceLine.class} };
17
18         static final String CLASS_NAME = "classname";
19         static final String METHOD_NAME = "name";
20         static final String SIGNATURE = "signature";
21         static final String IS_STATIC = "isStatic";
22         static final String ROLE = "role";
23         
24         static final String TRUE = "true";      // yes value for isStatic
25         
26         String m_className;
27         String m_methodName;
28         String m_signature;
29         boolean m_isStatic;
30         String m_role;          // optional
31         ArrayList<SourceLine> m_sourceLines;
32         
33         public BugMethod() {
34                 super(TAG, INTERNAL, EXTERNAL);
35                 m_className = m_methodName = m_signature = m_role = null;
36                 m_isStatic = false;
37                 m_sourceLines = new ArrayList<SourceLine>();
38         }
39         
40         public BugMethod(Long id, String methodName, String methodRole) {
41                 super(TAG, INTERNAL, EXTERNAL);
42                 m_className = null;
43                 m_methodName = methodName;
44                 m_role = methodRole;
45                 m_isStatic = false;
46                 m_sourceLines = new ArrayList<SourceLine>();
47         }
48         
49         public String getClassName() { return m_className; }
50         public String getMethodName() { return m_methodName; }
51         public String getRole() { return m_role; }
52         public SourceLine[] getSourceLines() { return m_sourceLines.toArray(new SourceLine[m_sourceLines.size()]); }
53         
54         @Override
55         public void handleMainAttributes(Attributes attr) throws MissingAttributeException 
56         {
57                 m_className = getRequiredAttr(TAG, attr, CLASS_NAME);
58                 m_methodName = getRequiredAttr(TAG, attr, METHOD_NAME);
59                 m_signature = getRequiredAttr(TAG, attr, SIGNATURE);
60                 m_isStatic = getRequiredAttr(TAG, attr, IS_STATIC).equals(TRUE);
61                 m_role = getOptionalAttr(attr, ROLE, null);             
62         }
63
64         @Override
65         public void endContents(String uri, String localName, String qName, String chars) 
66                 throws XmlParseException 
67         {
68                 // no-op
69         }
70
71         @Override
72         public void endExternal(String uri, String localName, String qName)
73                 throws XmlParseException 
74         {
75                 if (SourceLine.TAG.equals(localName)) {
76                         ParseResult[] collected = collectParsedChildren(SourceLine.class);
77                         for (ParseResult pr : collected) {
78                                 assert(pr instanceof SourceLine);
79                                 m_sourceLines.add((SourceLine)pr);
80                         }
81                 }
82         }
83         
84         @Override
85         public void dump(PrintWriter pw, int indent) 
86         {
87                 super.dump(pw, indent);
88                 String tab = String.format("%" + (indent + 2) + "s", "");
89                 
90                 pw.println(tab 
91                                 + (m_isStatic ? "static" : "")
92                                 + m_className 
93                                 + "."
94                                 + m_methodName 
95                                 + m_signature);
96                 if (null != m_role) {
97                         pw.println(tab + ROLE + "=" + m_role);
98                 }
99                 if (null != m_sourceLines) {
100                         for (SourceLine sl : m_sourceLines) {
101                                 sl.dump(pw, indent + 2);
102                         }
103                 }
104         }
105
106 }