adds support for fields as well as local variables.
[cfb.git] / prod / net / jaekl / cfb / xml / LocalVariable.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 LocalVariable extends ParseResult implements Variable {
13
14         static final String TAG = "LocalVariable";
15         static final String[] INTERNAL = { };
16         static final Object[][] EXTERNAL = { };
17
18         static final String NAME = "name";
19         static final String ROLE = "role";
20         
21         Long m_id;
22         String m_name;
23         String m_role;
24         
25         public LocalVariable() {
26                 super(TAG, INTERNAL, EXTERNAL);
27                 
28                 m_id = null;
29                 m_name = m_role = null;
30         }
31         
32         public LocalVariable(Long id, String name, String role) {
33                 super(TAG, INTERNAL, EXTERNAL);
34
35                 m_id = id;
36                 m_name = name;
37                 m_role = role;
38         }
39         
40         public Long getId() { return m_id; }
41         
42         @Override
43         public String getDescription()
44         {
45                 String result = getName();
46                 if (null != getRole()) {
47                         result += " (" + getRole() + ")";
48                 }
49                 return result;
50         }
51         
52         @Override
53         public String getName() { return m_name; }
54         
55         @Override
56         public String getRole() { return m_role; }
57         
58         @Override
59         public void handleMainAttributes(Attributes attr)
60                 throws MissingAttributeException
61         {
62                 m_name = getRequiredAttr(TAG, attr, NAME);
63                 m_role = getRequiredAttr(TAG, attr, ROLE);
64         }
65         
66         @Override
67         public void endContents(String uri, String localName, String qName, String chars) 
68                 throws XmlParseException 
69         {
70         }
71
72         @Override
73         public void endExternal(String uri, String localName, String qName)
74                 throws XmlParseException 
75         {
76                 // no-op
77         }
78         
79         @Override
80         public void dump(PrintWriter pw, int indent) 
81         {
82                 super.dump(pw, indent);
83                 String tab = String.format("%" + (indent + 2) + "s", "");
84                 
85                 pw.println(tab + NAME + "=" + m_name);
86                 pw.println(tab + ROLE + "=" + m_role);
87         }
88         
89         @Override
90         public boolean equals(Object obj) 
91         {
92                 if (null == obj) {
93                         return false;
94                 }
95                 if (obj instanceof LocalVariable) {
96                         LocalVariable that = (LocalVariable)obj;
97                         return (   Util.objsAreEqual(this.m_name, that.m_name) 
98                                     && Util.objsAreEqual(this.m_role, that.m_role) );
99                 }
100                 return false;
101         }
102         
103         @Override
104         public int hashCode()
105         {
106                 return ( (Util.objHashCode(m_name)) ^ (Util.objHashCode(m_role)) );
107         }
108 }