This project has retired. For details please refer to its
Attic page.
Attribute xref
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 static org.apache.tiles.CompareUtil.*;
25
26 import java.io.Serializable;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.Set;
30
31 import org.apache.tiles.request.Request;
32
33
34
35
36
37
38 public class Attribute implements Serializable, Cloneable {
39
40
41
42
43 private static final String TEMPLATE_RENDERER = "template";
44
45
46
47
48
49 protected Set<String> roles = null;
50
51
52
53
54 protected Object value = null;
55
56
57
58
59
60
61
62 protected Expression expressionObject = null;
63
64
65
66
67
68 private String renderer = null;
69
70
71
72
73
74 public Attribute() {
75 }
76
77
78
79
80
81
82 public Attribute(Object value) {
83 this.value = value;
84 }
85
86
87
88
89
90
91 public Attribute(Attribute attribute) {
92 this.roles = attribute.roles;
93 this.value = attribute.getValue();
94 if (attribute.expressionObject != null) {
95 this.expressionObject = new Expression(attribute.expressionObject);
96 } else {
97 this.expressionObject = null;
98 }
99 this.renderer = attribute.renderer;
100 }
101
102
103
104
105
106
107
108 public Attribute(Object value, String role) {
109 this.value = value;
110 setRole(role);
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124 public Attribute(Object value, Expression expression, String role, String rendererName) {
125 this.value = value;
126 this.expressionObject = expression;
127 this.renderer = rendererName;
128 setRole(role);
129 }
130
131
132
133
134
135
136
137
138 public static Attribute createTemplateAttribute(String template) {
139 Attribute attribute = new Attribute();
140 attribute.setValue(template);
141 attribute.setRenderer(TEMPLATE_RENDERER);
142 return attribute;
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public static Attribute createTemplateAttribute(String template,
159 String templateExpression, String templateType, String role) {
160 Attribute templateAttribute = createTemplateAttribute(template);
161 templateAttribute.setRole(role);
162 if (templateType != null) {
163 templateAttribute.setRenderer(templateType);
164 }
165 templateAttribute
166 .setExpressionObject(Expression
167 .createExpressionFromDescribedExpression(templateExpression));
168 return templateAttribute;
169 }
170
171
172
173
174
175
176
177
178
179 public static Attribute createTemplateAttributeWithExpression(
180 String templateExpression) {
181 Attribute attribute = new Attribute();
182 attribute.setExpressionObject(new Expression(templateExpression));
183 attribute.setRenderer(TEMPLATE_RENDERER);
184 return attribute;
185 }
186
187
188
189
190
191 public String getRole() {
192 String retValue = null;
193
194 if (roles != null && !roles.isEmpty()) {
195 StringBuilder builder = new StringBuilder();
196 Iterator<String> roleIt = roles.iterator();
197 if (roleIt.hasNext()) {
198 builder.append(roleIt.next());
199 while (roleIt.hasNext()) {
200 builder.append(",");
201 builder.append(roleIt.next());
202 }
203 retValue = builder.toString();
204 }
205 }
206
207 return retValue;
208 }
209
210
211
212
213
214
215
216 public Set<String> getRoles() {
217 return roles;
218 }
219
220
221
222
223
224
225 public void setRole(String role) {
226 if (role != null && role.trim().length() > 0) {
227 String[] rolesStrings = role.split("\\s*,\\s*");
228 roles = new HashSet<String>();
229 for (int i = 0; i < rolesStrings.length; i++) {
230 roles.add(rolesStrings[i]);
231 }
232 } else {
233 roles = null;
234 }
235 }
236
237
238
239
240
241
242
243 public void setRoles(Set<String> roles) {
244 this.roles = roles;
245 }
246
247
248
249
250
251 public Object getValue() {
252 return value;
253 }
254
255
256
257
258
259
260 public void setValue(Object value) {
261 this.value = value;
262 }
263
264
265
266
267
268
269
270
271 public Expression getExpressionObject() {
272 return expressionObject;
273 }
274
275
276
277
278
279
280
281
282 public void setExpressionObject(Expression expressionObject) {
283 this.expressionObject = expressionObject;
284 }
285
286
287 @Override
288 public String toString() {
289 if (value != null) {
290 return value.toString();
291 }
292 return null;
293 }
294
295
296
297
298
299
300
301 public String getRenderer() {
302 return renderer;
303 }
304
305
306
307
308
309
310
311 public void setRenderer(String rendererName) {
312 this.renderer = rendererName;
313 }
314
315
316
317
318
319
320
321
322 public void inherit(Attribute attribute) {
323 if (value == null) {
324 value = attribute.getValue();
325 }
326 Expression targetExpressionObject = attribute.getExpressionObject();
327 if (targetExpressionObject != null
328 && (expressionObject == null || expressionObject
329 .getExpression() == null)) {
330 expressionObject = new Expression(targetExpressionObject);
331 }
332 if (roles == null || roles.isEmpty()) {
333 roles = attribute.getRoles();
334 }
335 if (renderer == null) {
336 renderer = attribute.getRenderer();
337 }
338 }
339
340
341 @Override
342 public boolean equals(Object obj) {
343 Attribute attribute = (Attribute) obj;
344 return nullSafeEquals(value, attribute.value)
345 && nullSafeEquals(renderer, attribute.renderer)
346 && nullSafeEquals(roles, attribute.roles)
347 && nullSafeEquals(expressionObject, attribute.expressionObject);
348 }
349
350
351
352
353
354
355
356
357 public boolean isPermitted(Request request) {
358 if (roles == null || roles.isEmpty()) {
359 return true;
360 }
361
362 boolean retValue = false;
363
364 for (Iterator<String> roleIt = roles.iterator(); roleIt.hasNext()
365 && !retValue;) {
366 retValue = request.isUserInRole(roleIt.next());
367 }
368
369 return retValue;
370 }
371
372
373 @Override
374 public int hashCode() {
375 return nullSafeHashCode(value) + nullSafeHashCode(renderer)
376 + nullSafeHashCode(roles) + nullSafeHashCode(expressionObject);
377 }
378
379
380 @Override
381 public Attribute clone() {
382 return new Attribute(this);
383 }
384 }