1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles;
23
24 import java.io.Serializable;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Set;
30
31 /***
32 * Common implementation of attribute definition.
33 *
34 * @version $Rev: 619574 $ $Date: 2008-02-07 20:09:33 +0100 (Thu, 07 Feb 2008) $
35 */
36 public class Attribute implements Serializable {
37
38 /***
39 * Attribute types.
40 */
41 public static enum AttributeType {
42 /***
43 * Attribute of type string.
44 */
45 STRING("string"),
46
47 /***
48 * Attribute of type definition.
49 */
50 DEFINITION("definition"),
51
52 /***
53 * Attribute of type template.
54 */
55 TEMPLATE("template"),
56
57 /***
58 * Attribute of type object.
59 */
60 OBJECT("object");
61
62 /***
63 * The string representation of the enum element.
64 */
65 private String stringRepresentation;
66
67 /***
68 * Maps the string representation to the attribute type.
69 */
70 private static Map<String, AttributeType> representation2type;
71
72 static {
73 representation2type = new HashMap<String, AttributeType>();
74 representation2type.put("string", AttributeType.STRING);
75 representation2type.put("definition", AttributeType.DEFINITION);
76 representation2type.put("template", AttributeType.TEMPLATE);
77 representation2type.put("object", AttributeType.OBJECT);
78 }
79
80 /***
81 * Returns the type for the given string representation.
82 *
83 * @param stringRepresentation The string representation of the needed
84 * type.
85 * @return The corresponding attribute type, if found, or
86 * <code>null</code> if not.
87 */
88 public static AttributeType getType(String stringRepresentation) {
89 return representation2type.get(stringRepresentation);
90 }
91
92 /***
93 * Constructor.
94 *
95 * @param stringRepresentation The string representation of the enum
96 * element.
97 */
98 private AttributeType(String stringRepresentation) {
99 this.stringRepresentation = stringRepresentation;
100 }
101
102 /***
103 * Returns the string representation of the element.
104 *
105 * @return The string representation.
106 */
107 @Override
108 public String toString() {
109 return stringRepresentation;
110 }
111 };
112
113 /***
114 * The roles that can render this attribute.
115 * @since 2.0.6
116 */
117 protected Set<String> roles = null;
118
119 /***
120 * The value of the attribute.
121 */
122 protected Object value = null;
123
124 /***
125 * The type of the attribute. It can be <code>string</code>,
126 * <code>template</code>, <code>definition</code>.
127 */
128 private AttributeType type = null;
129
130 /***
131 * The name of the attribute. If it is <code>null</code>, it should be used
132 * as an element of a list attribute.
133 * @deprecated It is not used.
134 */
135 @Deprecated
136 private String name = null;
137
138 /***
139 * Constructor.
140 *
141 */
142 public Attribute() {
143 }
144
145 /***
146 * Constructor.
147 *
148 * @param value Object to store.
149 */
150 public Attribute(Object value) {
151 this.value = value;
152 }
153
154 /***
155 * Copy constructor.
156 *
157 * @param attribute The attribute to copy from.
158 */
159 public Attribute(Attribute attribute) {
160 this.name = attribute.name;
161 this.roles = attribute.roles;
162 this.type = attribute.type;
163 this.value = attribute.getValue();
164 }
165
166 /***
167 * Constructor.
168 *
169 * @param name name of the attribute
170 * @param value Object to store.
171 */
172 public Attribute(String name, Object value) {
173 this.name = name;
174 this.value = value;
175 }
176
177 /***
178 * Constructor.
179 *
180 * @param value Object to store.
181 * @param role Asociated role.
182 */
183 public Attribute(Object value, String role) {
184 this.value = value;
185 setRole(role);
186 }
187
188 /***
189 * Constructor.
190 *
191 * @param value Object to store.
192 * @param role Asociated role.
193 * @param type Attribute type.
194 */
195 public Attribute(Object value, String role, AttributeType type) {
196 this.value = value;
197 this.type = type;
198 setRole(role);
199 }
200
201 /***
202 * Constructor.
203 *
204 * @param name name of the attribute
205 * @param value Object to store.
206 * @param role Asociated role.
207 * @param type Attribute type.
208 */
209 public Attribute(String name, Object value, String role,
210 AttributeType type) {
211 this.name = name;
212 this.value = value;
213 this.type = type;
214 setRole(role);
215 }
216
217 /***
218 * Get role.
219 * @return the name of the required role(s)
220 */
221 public String getRole() {
222 String retValue = null;
223
224 if (roles != null && !roles.isEmpty()) {
225 StringBuilder builder = new StringBuilder();
226 Iterator<String> roleIt = roles.iterator();
227 if (roleIt.hasNext()) {
228 builder.append(roleIt.next());
229 while (roleIt.hasNext()) {
230 builder.append(",");
231 builder.append(roleIt.next());
232 }
233 retValue = builder.toString();
234 }
235 }
236
237 return retValue;
238 }
239
240 /***
241 * Returns the roles that can render this attribute.
242 *
243 * @return The enabled roles.
244 * @since 2.0.6
245 */
246 public Set<String> getRoles() {
247 return roles;
248 }
249
250 /***
251 * Set role.
252 *
253 * @param role Associated role.
254 */
255 public void setRole(String role) {
256 if (role != null && role.trim().length() > 0) {
257 String[] rolesStrings = role.split("//s*,//s*");
258 roles = new HashSet<String>();
259 for (int i = 0; i < rolesStrings.length; i++) {
260 roles.add(rolesStrings[i]);
261 }
262 } else {
263 roles = null;
264 }
265 }
266
267 /***
268 * Sets the roles that can render this attribute.
269 *
270 * @param roles The enabled roles.
271 * @since 2.0.6
272 */
273 public void setRoles(Set<String> roles) {
274 this.roles = roles;
275 }
276
277 /***
278 * Get value.
279 * @return the value
280 */
281 public Object getValue() {
282 return value;
283 }
284
285 /***
286 * Set value.
287 *
288 * @param value New value.
289 */
290 public void setValue(Object value) {
291 this.value = value;
292 }
293
294 /*** {@inheritDoc} */
295 public String toString() {
296 if (value != null) {
297 return value.toString();
298 }
299 return null;
300 }
301
302 /***
303 * Returns the type of this attribute.
304 *
305 * @return The attribute type. It can be <code>string</code>,
306 * <code>template</code>, <code>definition</code>, <code>object</code>.
307 */
308 public AttributeType getType() {
309 return type;
310 }
311
312 /***
313 * Sets the type of this attribute.
314 *
315 * @param type The attribute type.
316 */
317 public void setType(AttributeType type) {
318 this.type = type;
319 }
320
321 /***
322 * Returns the name of the attribute.
323 *
324 * @return The name of the attribute. It can be <code>null</code>, but in
325 * this case it should be used as an element of <code>ListAttribute</code>
326 * @deprecated Use the <code>getName</code> methods in object that contain
327 * attributes.
328 */
329 @Deprecated
330 public String getName() {
331 return name;
332 }
333
334 /***
335 * Sets the name of the attribute.
336 *
337 * @param name The name of the attribute. It can be <code>null</code>,
338 * but in this case it should be used as an element of
339 * <code>ListAttribute</code>
340 * @deprecated Use the <code>setName</code> methods in object that contain
341 * attributes.
342 */
343 @Deprecated
344 public void setName(String name) {
345 this.name = name;
346 }
347
348 /***
349 * Sets the body of this attribute.
350 *
351 * @param body The attribute body.
352 */
353
354 public void setBody(String body) {
355 if (body != null && body.length() != 0) {
356 setValue(body);
357 }
358 }
359 }