adds support for fields as well as local variables.
[cfb.git] / prod / net / jaekl / cfb / xml / Field.java
1 package net.jaekl.cfb.xml;
2
3 import java.io.PrintWriter;
4
5 import org.xml.sax.Attributes;
6
7 import net.jaekl.cfb.util.Util;
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 Field extends ParseResult implements Variable {
13         static final String TAG = "Field";
14         static final String[] INTERNAL = { };
15         static final Object[][] EXTERNAL = { };
16
17         static final String CLASSNAME = "classname";
18         static final String FIELD = "FIELD";
19         static final String IS_STATIC = "isStatic";
20         static final String NAME = "name";
21         static final String SIGNATURE = "signature";
22         
23         String m_className;
24         String m_name;
25         String m_signature;
26         boolean m_isStatic;
27         
28         public Field() {
29                 super(TAG, INTERNAL, EXTERNAL);
30                 
31                 m_className = m_name = m_signature = null;
32                 m_isStatic = false;
33         }
34         
35         public String getClassName() { return m_className; }
36         public String getSignature() { return m_signature; }
37         public boolean isStatic() { return m_isStatic; }
38         
39         @Override
40         public String getDescription() 
41         {
42                 String result = "";
43                 if (isStatic()) {
44                         result += "static ";
45                 }
46                 if (null != getClassName()) {
47                         result += getClassName() + ".";
48                 }
49                 result += getName();
50                 return result;
51         }
52         
53         @Override
54         public String getName() { return m_name; }
55         
56         @Override
57         public String getRole() { return FIELD; }
58
59         @Override
60         public void handleMainAttributes(Attributes attr)
61                 throws MissingAttributeException
62         {
63                 m_className = getRequiredAttr(TAG, attr, CLASSNAME);
64                 
65                 String isStaticStr = getRequiredAttr(TAG, attr, CLASSNAME);
66                 m_isStatic = "true".equals(isStaticStr);
67                 
68                 m_name = getRequiredAttr(TAG, attr, NAME);
69                 
70                 m_signature = getRequiredAttr(TAG, attr, SIGNATURE);
71         }
72         
73         @Override
74         public void endContents(String uri, String localName, String qName, String chars) 
75                 throws XmlParseException 
76         {
77         }
78
79         @Override
80         public void endExternal(String uri, String localName, String qName)
81                 throws XmlParseException 
82         {
83                 // no-op
84         }
85         
86         @Override
87         public void dump(PrintWriter pw, int indent) 
88         {
89                 super.dump(pw, indent);
90                 String tab = String.format("%" + (indent + 2) + "s", "");
91
92                 pw.println(tab + CLASSNAME + "=" + m_className);
93                 pw.println(tab + IS_STATIC + "=" + (m_isStatic ? "true" : "false"));
94                 pw.println(tab + NAME + "=" + m_name);
95                 pw.println(tab + SIGNATURE + "=" + m_signature);
96         }
97         
98         @Override
99         public boolean equals(Object obj) 
100         {
101                 if (null == obj) {
102                         return false;
103                 }
104                 if (obj instanceof Field) {
105                         Field that = (Field)obj;
106                         return (   Util.objsAreEqual(this.m_className, that.m_className)
107                                         && (this.m_isStatic == that.m_isStatic)
108                                     && Util.objsAreEqual(this.m_name, that.m_name)
109                                     && Util.objsAreEqual(this.m_signature, that.m_signature) );
110                 }
111                 return false;
112         }
113         
114         @Override
115         public int hashCode()
116         {
117                 return (  (Util.objHashCode(m_className))
118                                 ^ (Boolean.valueOf(m_isStatic).hashCode())
119                                 ^ (Util.objHashCode(m_name)) 
120                                 ^ (Util.objHashCode(m_signature)) );
121         }
122 }