This project has retired. For details please refer to its Attic page.
Attribute xref
View Javadoc

1   /*
2    * $Id: Attribute.java 788344 2009-06-25 12:47:40Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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: 788344 $ $Date: 2009-06-25 14:47:40 +0200 (gio, 25 giu 2009) $
35   */
36  public class Attribute implements Serializable, Cloneable {
37  
38      /***
39       * The name of the template renderer.
40       */
41      private static final String TEMPLATE_RENDERER = "template";
42  
43      /***
44       * Attribute types.
45       *
46       * @deprecated Use {@link Attribute#setRenderer(String)} and
47       * {@link Attribute#getRenderer()}.
48       */
49      public static enum AttributeType {
50          /***
51           * Attribute of type string.
52           */
53          STRING("string"),
54  
55          /***
56           * Attribute of type definition.
57           */
58          DEFINITION("definition"),
59  
60          /***
61           * Attribute of type template.
62           */
63          TEMPLATE("template"),
64  
65          /***
66           * Attribute of type object.
67           */
68          OBJECT("object");
69  
70          /***
71           * The string representation of the enum element.
72           */
73          private String stringRepresentation;
74  
75          /***
76           * Maps the string representation to the attribute type.
77           */
78          private static Map<String, AttributeType> representation2type;
79  
80          static {
81              representation2type = new HashMap<String, AttributeType>();
82              representation2type.put("string", AttributeType.STRING);
83              representation2type.put("definition", AttributeType.DEFINITION);
84              representation2type.put("template", AttributeType.TEMPLATE);
85              representation2type.put("object", AttributeType.OBJECT);
86          }
87  
88          /***
89           * Returns the type for the given string representation.
90           *
91           * @param stringRepresentation The string representation of the needed
92           * type.
93           * @return The corresponding attribute type, if found, or
94           * <code>null</code> if not.
95           */
96          public static AttributeType getType(String stringRepresentation) {
97              return representation2type.get(stringRepresentation);
98          }
99  
100         /***
101          * Constructor.
102          *
103          * @param stringRepresentation The string representation of the enum
104          * element.
105          */
106         private AttributeType(String stringRepresentation) {
107             this.stringRepresentation = stringRepresentation;
108         }
109 
110         /***
111          * Returns the string representation of the element.
112          *
113          * @return The string representation.
114          */
115         @Override
116         public String toString() {
117             return stringRepresentation;
118         }
119     };
120 
121     /***
122      * The roles that can render this attribute.
123      * @since 2.0.6
124      */
125     protected Set<String> roles = null;
126 
127     /***
128      * The value of the attribute.
129      */
130     protected Object value = null;
131 
132     /***
133      * The expression to evaluate. Ignored if {@link #value} is not
134      * <code>null</code>.
135      *
136      * @since 2.1.2
137      */
138     protected String expression = null;
139 
140     /***
141      * The renderer name of the attribute. Default names are <code>string</code>,
142      * <code>template</code>, <code>definition</code>, <code>object</code>.
143      */
144     private String renderer = null;
145 
146     /***
147      * The name of the attribute. If it is <code>null</code>, it should be used
148      * as an element of a list attribute.
149      * @deprecated It is not used.
150      */
151     @Deprecated
152     private String name = null;
153 
154     /***
155      * Constructor.
156      *
157      */
158     public Attribute() {
159     }
160 
161     /***
162      * Constructor.
163      *
164      * @param value Object to store.
165      */
166     public Attribute(Object value) {
167         this.value = value;
168     }
169 
170     /***
171      * Copy constructor.
172      *
173      * @param attribute The attribute to copy from.
174      */
175     public Attribute(Attribute attribute) {
176         this.name = attribute.name;
177         this.roles = attribute.roles;
178         this.value = attribute.getValue();
179         this.expression = attribute.expression;
180         this.renderer = attribute.renderer;
181     }
182 
183     /***
184      * Constructor.
185      *
186      * @param name name of the attribute
187      * @param value Object to store.
188      */
189     public Attribute(String name, Object value) {
190         this.name = name;
191         this.value = value;
192     }
193 
194     /***
195      * Constructor.
196      *
197      * @param value Object to store.
198      * @param role  Asociated role.
199      */
200     public Attribute(Object value, String role) {
201         this.value = value;
202         setRole(role);
203     }
204 
205     /***
206      * Constructor.
207      *
208      * @param value Object to store.
209      * @param role Associated role.
210      * @param type Attribute type.
211      * @deprecated Use {@link Attribute#Attribute(Object, String, String, String)}.
212      */
213     @Deprecated
214     public Attribute(Object value, String role, AttributeType type) {
215         this.value = value;
216         setType(type);
217         setRole(role);
218     }
219 
220     /***
221      * Constructor.
222      *
223      * @param value Object to store. If specified, the <code>expression</code>
224      * parameter will be ignored.
225      * @param expression The expression to be evaluated. Ignored if the
226      * <code>value</code> is not null.
227      * @param role Associated role.
228      * @param rendererName The renderer name.
229      * @since 2.1.2
230      */
231     public Attribute(Object value, String expression, String role, String rendererName) {
232         this.value = value;
233         this.expression = expression;
234         this.renderer = rendererName;
235         setRole(role);
236     }
237 
238     /***
239      * Constructor.
240      *
241      * @param name name of the attribute
242      * @param value Object to store.
243      * @param role Asociated role.
244      * @param type Attribute type.
245      * @deprecated Use
246      * {@link Attribute#Attribute(Object, String, String)))}.
247      */
248     public Attribute(String name, Object value, String role,
249             AttributeType type) {
250         this.name = name;
251         this.value = value;
252         setType(type);
253         setRole(role);
254     }
255 
256     /***
257      * Creates a template attribute, starting from the name of the template.
258      *
259      * @param template The template that will be rendered.
260      * @return The template attribute.
261      * @since 2.1.2
262      */
263     public static Attribute createTemplateAttribute(String template) {
264         Attribute attribute = new Attribute();
265         attribute.setValue(template);
266         attribute.setRenderer(TEMPLATE_RENDERER);
267         return attribute;
268     }
269 
270     /***
271      * Creates a template attribute, starting from the expression to evaluate to
272      * obtain the template.
273      *
274      * @param templateExpression The expression to evaluate.
275      * @return The template attribute.
276      * @since 2.1.2
277      */
278     public static Attribute createTemplateAttributeWithExpression(
279             String templateExpression) {
280         Attribute attribute = new Attribute();
281         attribute.setExpression(templateExpression);
282         attribute.setRenderer(TEMPLATE_RENDERER);
283         return attribute;
284     }
285 
286     /***
287      * Get role.
288      * @return the name of the required role(s)
289      */
290     public String getRole() {
291         String retValue = null;
292 
293         if (roles != null && !roles.isEmpty()) {
294             StringBuilder builder = new StringBuilder();
295             Iterator<String> roleIt = roles.iterator();
296             if (roleIt.hasNext()) {
297                 builder.append(roleIt.next());
298                 while (roleIt.hasNext()) {
299                     builder.append(",");
300                     builder.append(roleIt.next());
301                 }
302                 retValue = builder.toString();
303             }
304         }
305 
306         return retValue;
307     }
308 
309     /***
310      * Returns the roles that can render this attribute.
311      *
312      * @return The enabled roles.
313      * @since 2.0.6
314      */
315     public Set<String> getRoles() {
316         return roles;
317     }
318 
319     /***
320      * Set role.
321      *
322      * @param role Associated role.
323      */
324     public void setRole(String role) {
325         if (role != null && role.trim().length() > 0) {
326             String[] rolesStrings = role.split("//s*,//s*");
327             roles = new HashSet<String>();
328             for (int i = 0; i < rolesStrings.length; i++) {
329                 roles.add(rolesStrings[i]);
330             }
331         } else {
332             roles = null;
333         }
334     }
335 
336     /***
337      * Sets the roles that can render this attribute.
338      *
339      * @param roles The enabled roles.
340      * @since 2.0.6
341      */
342     public void setRoles(Set<String> roles) {
343         this.roles = roles;
344     }
345 
346     /***
347      * Get value.
348      * @return the value
349      */
350     public Object getValue() {
351         return value;
352     }
353 
354     /***
355      * Set value.
356      *
357      * @param value New value.
358      */
359     public void setValue(Object value) {
360         this.value = value;
361     }
362 
363     /***
364      * Returns The expression to evaluate. Ignored if {@link #value} is not
365      * <code>null</code>.
366      *
367      * @return The expression to be evaluated.
368      * @since 2.1.2
369      */
370     public String getExpression() {
371         return expression;
372     }
373 
374     /***
375      * Sets The expression to evaluate. Ignored if {@link #value} is not
376      * <code>null</code>.
377      *
378      * @param expression The expression to be evaluated.
379      * @since 2.1.2
380      */
381     public void setExpression(String expression) {
382         this.expression = expression;
383     }
384 
385     /*** {@inheritDoc} */
386     public String toString() {
387         if (value != null) {
388             return value.toString();
389         }
390         return null;
391     }
392 
393     /***
394      * Returns the type of this attribute.
395      *
396      * @return The attribute type. It can be <code>string</code>,
397      * <code>template</code>, <code>definition</code>, <code>object</code>.
398      * @deprecated Use {@link Attribute#getRenderer()}.
399      */
400     public AttributeType getType() {
401         return AttributeType.getType(renderer);
402     }
403 
404     /***
405      * Sets the type of this attribute.
406      *
407      * @param type The attribute type.
408      * @deprecated Use {@link Attribute#setRenderer(String))}.
409      */
410     public void setType(AttributeType type) {
411         this.renderer = type.toString();
412     }
413 
414     /***
415      * Returns the renderer name to use.
416      *
417      * @return The renderer name.
418      * @since 2.1.0
419      */
420     public String getRenderer() {
421         return renderer;
422     }
423 
424     /***
425      * Sets the renderer name to use.
426      *
427      * @param rendererName The renderer.
428      * @since 2.1.0
429      */
430     public void setRenderer(String rendererName) {
431         this.renderer = rendererName;
432     }
433 
434     /***
435      * Returns the name of the attribute.
436      *
437      * @return The name of the attribute. It can be <code>null</code>, but in
438      * this case it should be used as an element of <code>ListAttribute</code>
439      * @deprecated Use the <code>getName</code> methods in object that contain
440      * attributes.
441      */
442     @Deprecated
443     public String getName() {
444         return name;
445     }
446 
447     /***
448      * Sets the name of the attribute.
449      *
450      * @param name The name of the attribute. It can be <code>null</code>,
451      * but in this case it should be used as an element of
452      * <code>ListAttribute</code>
453      * @deprecated Use the <code>setName</code> methods in object that contain
454      * attributes.
455      */
456     @Deprecated
457     public void setName(String name) {
458         this.name = name;
459     }
460 
461     /***
462      * Sets the body of this attribute.
463      *
464      * @param body The attribute body.
465      */
466     // FIXME Is it necessary?
467     public void setBody(String body) {
468         if (body != null && body.length() != 0) {
469             setValue(body);
470         }
471     }
472 
473     /***
474      * Inherits an attribute, i.e. overwrites null properties with the ones
475      * provided by the attribute.
476      *
477      * @param attribute The attribute to inherit.
478      * @since 2.1.2
479      */
480     public void inherit(Attribute attribute) {
481         if (value == null) {
482             value = attribute.getValue();
483         }
484         if (expression == null) {
485             expression = attribute.getExpression();
486         }
487         if (roles == null || roles.isEmpty()) {
488             roles = attribute.getRoles();
489         }
490         if (renderer == null) {
491             renderer = attribute.getRenderer();
492         }
493     }
494 
495     /*** {@inheritDoc} */
496     @Override
497     public Attribute clone() {
498         return new Attribute(this);
499     }
500 }