1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.jameleon.bean;
18
19 import net.sf.jameleon.XMLable;
20
21 import org.apache.commons.jelly.expression.Expression;
22
23 public class Attribute implements XMLable, Cloneable{
24 private static final long serialVersionUID = 1L;
25 /***
26 * The attribute's name
27 */
28 protected String name;
29 /***
30 * A description about how the attribute is used
31 */
32 protected String description;
33 /***
34 * The type of the attribute
35 */
36 protected String type;
37 /***
38 * Whether this attribute is required or not
39 */
40 protected boolean required;
41 /***
42 * The value of the attribute
43 */
44 protected Object value;
45 /***
46 * The default value of the attribute
47 */
48 protected String defaultValue;
49 /***
50 * The name of this attribute's instance variable in the function tag. Used with reflection to magically set the instance variable
51 */
52 protected String contextName;
53 /***
54 * Describes whether this attribute represents an instance variable or set method. If both a set method and instance variable exist for
55 * this attribute, then the set method should be called, and this should be set to <code>false</code>
56 */
57 protected boolean instanceVariable;
58 /***
59 * Used to keep track if whether the value of this attribute was set or not
60 */
61 protected boolean valueSet = false;
62
63 /***
64 * Default constructor only used to initialize variables
65 */
66 public Attribute() {
67 name = new String();
68 description = new String();
69 type = new String();
70 required = false;
71 contextName = new String();
72 }
73
74 /***
75 * @return The attribute's name
76 */
77 public String getName(){
78 return name;
79 }
80
81 /***
82 * Sets the attribute's name
83 * @param name - The attribute's name
84 */
85 public void setName(String name){
86 this.name = name;
87 }
88
89 /***
90 * @return A description about how the attribute is used
91 */
92 public String getDescription(){
93 return description;
94 }
95
96 /***
97 * Sets the description about how the attribute is used
98 * @param description - The description about how the attribute is used
99 */
100 public void setDescription(String description){
101 this.description = description;
102 }
103
104 /***
105 * @return The type of the attribute
106 */
107 public String getType(){
108 return type;
109 }
110
111 /***
112 * Sets the type of the attribute
113 * @param type - The type of the attribute
114 */
115 public void setType(String type){
116 this.type = type;
117 }
118
119 /***
120 * @return <code>true</code> if this attribute is required
121 */
122 public boolean isRequired(){
123 return required;
124 }
125
126 /***
127 * @return <code>true</code> if the value of the attribute was set
128 */
129 public boolean isValueSet(){
130 return valueSet;
131 }
132
133 /***
134 * Sets the attribute to a required or non-required attribute
135 * @param required - <code>true</code> if this attribute is required
136 */
137 public void setRequired(boolean required){
138 this.required = required;
139 }
140
141 /***
142 * @return The attribute's value
143 */
144 public Object getValue(){
145 return value;
146 }
147
148 /***
149 * Sets the attribute's value
150 * @param value - The attribute's value
151 */
152 public void setValue(Object value){
153 this.value = value;
154 if (value != null) {
155 valueSet = true;
156 }else{
157 valueSet = false;
158 }
159 }
160
161 /***
162 * @return The attribute's default value
163 */
164 public String getDefaultValue(){
165 return defaultValue;
166 }
167
168 /***
169 * Sets the attribute's default value
170 * @param defaultValue - The attribute's default value
171 */
172 public void setDefaultValue(String defaultValue){
173 this.defaultValue = defaultValue;
174 }
175
176 /***
177 * @return The displayable value of the attribute
178 */
179 public String getDisplayedValue(){
180 String returnValue = value.toString();
181 if (value instanceof Expression && value != null){
182 returnValue = ((Expression)value).getExpressionText();
183 }
184 return returnValue;
185 }
186
187 /***
188 * Gets the name of the context variable that the value of the this attribute will be bound to
189 * @return The name of context variable that the value of this attribute will be bound to
190 */
191 public String getContextName(){
192 return contextName;
193 }
194
195 /***
196 * Sets the name of the context variable that the value of the this attribute will be bound to
197 * @param contextName - The name of context variable that the value of this attribute will be bound to
198 */
199 public void setContextName(String contextName){
200 this.contextName = contextName;
201 }
202
203 /***
204 * Tells if this attribute represents an instance variable or set method
205 * @return <code>true</code> if this attribute represents an instance variable and <code>false</code>
206 * if it represents a set method. If both a set method and instance variable exist for
207 * this attribute, then the set method should be called, and this should return <code>false</code>
208 */
209 public boolean isInstanceVariable(){
210 return instanceVariable;
211 }
212
213 /***
214 * Tells if this attribute represents something that is bound to a context variable
215 * @return true if the contextName is set
216 */
217 public boolean isContextVariable(){
218 boolean isContextVariable = false;
219 if (contextName != null && contextName.length() > 0 ) {
220 isContextVariable = true;
221 }
222 return isContextVariable;
223 }
224
225 /***
226 * Sets this attribute to an instance variable <code>true</code> or a set method <code>false</code>
227 * @param instanceVariable - Set to <code>true</code> if an instance variable or <code>false</code>
228 * if this attribute represents a set method. If both a set method and instance variable exist for
229 * this attribute, then the set method should be called, and this should be set to <code>false</code>
230 */
231 public void setInstanceVariable(boolean instanceVariable){
232 this.instanceVariable = instanceVariable;
233 }
234
235 public String toXML(){
236 StringBuffer str = new StringBuffer();
237 str.append("\t\t\t\t<attribute>\n");
238 str.append("\t\t\t\t\t<attribute-name>").append(name).append("</attribute-name>\n");
239 str.append("\t\t\t\t\t<attribute-instancevariable>").append(new Boolean(instanceVariable)).append("</attribute-instancevariable>\n");
240 str.append("\t\t\t\t\t<attribute-description>").append(description).append("</attribute-description>\n");
241 str.append("\t\t\t\t\t<attribute-type>").append(type).append("</attribute-type>\n");;
242 str.append("\t\t\t\t\t<attribute-required>").append(new Boolean(required)).append("</attribute-required>\n");
243 str.append("\t\t\t\t\t<attribute-value>").append(value).append("</attribute-value>\n");
244 str.append("\t\t\t\t\t<attribute-defaultvalue>").append(defaultValue).append("</attribute-defaultvalue>\n");
245 str.append("\t\t\t\t\t<attribute-contextname>").append(contextName).append("</attribute-contextname>\n");
246 str.append("\t\t\t\t</attribute>\n");
247 return str.toString();
248 }
249
250 public String toString(){
251 String str = null;
252 if (contextName != null && contextName.length() > 0) {
253 if (name != null && name.length() > 0) {
254 str = name +" or "+contextName+"";
255 }else{
256 str = contextName;
257 }
258 }else if (name != null && name.length() > 0) {
259 str = name;
260 }
261 return str;
262 }
263
264 public Object clone() throws CloneNotSupportedException {
265 Attribute attr = null;
266 try{
267 attr = (Attribute) super.clone();
268 attr.value = value;
269 }catch(CloneNotSupportedException cnse){
270 System.err.println("Could not create a clone of "+toXML());
271 }
272 return attr;
273 }
274 }