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

1   /*
2    * $Id: Definition.java 619574 2008-02-07 19:09:33Z 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  package org.apache.tiles;
22  
23  
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.HashMap;
28  import java.util.Set;
29  
30  import org.apache.tiles.Attribute.AttributeType;
31  
32  /***
33   * A definition, i.e. a template with (completely or not) filled attributes.
34   * Attributes of a template can be defined with the help of this class.<br>
35   * It can be used as a data transfer object used for registering new
36   * definitions with the Container.
37   *
38   * @since Tiles 2.0
39   * @version $Rev: 619574 $ $Date: 2008-02-07 20:09:33 +0100 (Thu, 07 Feb 2008) $
40   */
41  public class Definition {
42      /***
43       * Extends attribute value.
44       */
45      protected String inherit;
46      /***
47       * Definition name.
48       */
49      protected String name = null;
50      /***
51       * Template path.
52       */
53      protected String template = null;
54      /***
55       * Attributes defined for the definition.
56       */
57      protected Map<String, Attribute> attributes = null;
58      /***
59       * The roles that can render this definition.
60       * @since 2.0.6
61       */
62      protected Set<String> roles = null;
63      /***
64       * Associated ViewPreparer URL or classname, if defined.
65       */
66      protected String preparer = null;
67  
68  
69      /***
70       * Constructor.
71       */
72      public Definition() {
73          attributes = new HashMap<String, Attribute>();
74      }
75  
76      /***
77       * Copy Constructor.
78       * Create a new definition initialized with parent definition.
79       * Do a shallow copy : attributes are shared between copies, but not the Map
80       * containing attributes.
81       *
82       * @param definition The definition to copy.
83       */
84      public Definition(Definition definition) {
85          attributes = new HashMap<String, Attribute>(
86              definition.getAttributes());
87          this.name = definition.name;
88          this.template = definition.template;
89          this.roles = definition.roles;
90          this.preparer = definition.preparer;
91          this.inherit = definition.inherit;
92      }
93  
94      /***
95       * Constructor.
96       * @param name The name of the definition.
97       * @param template The template of the definition.
98       * @param attributes The attribute map of the definition.
99       */
100     public Definition(String name, String template,
101                                Map<String, Attribute> attributes) {
102         this.name = name;
103         this.template = template;
104         this.attributes = attributes;
105     }
106 
107     /***
108      * Access method for the name property.
109      *
110      * @return the current value of the name property
111      */
112     public String getName() {
113         return name;
114     }
115 
116     /***
117      * Sets the value of the name property.
118      *
119      * @param aName the new value of the name property
120      */
121     public void setName(String aName) {
122         name = aName;
123     }
124 
125     /***
126      * Access method for the template property.
127      *
128      * @return the current value of the template property
129      */
130     public String getTemplate() {
131         return template;
132     }
133 
134     /***
135      * Sets the value of the template property.
136      *
137      * @param template the new value of the path property
138      */
139     public void setTemplate(String template) {
140         this.template = template;
141     }
142 
143     /***
144      * Access method for the role property.
145      *
146      * @return the current value of the role property
147      */
148     public String getRole() {
149         String retValue = null;
150 
151         if (roles != null && !roles.isEmpty()) {
152             StringBuilder builder = new StringBuilder();
153             Iterator<String> roleIt = roles.iterator();
154             if (roleIt.hasNext()) {
155                 builder.append(roleIt.next());
156                 while (roleIt.hasNext()) {
157                     builder.append(",");
158                     builder.append(roleIt.next());
159                 }
160                 retValue = builder.toString();
161             }
162         }
163 
164         return retValue;
165     }
166 
167     /***
168      * Returns the roles that can render this attribute.
169      *
170      * @return The enabled roles.
171      * @since 2.0.6
172      */
173     public Set<String> getRoles() {
174         return roles;
175     }
176 
177     /***
178      * Sets the value of the role property.
179      *
180      * @param role the new value of the role property
181      */
182     public void setRole(String role) {
183         if (role != null && role.trim().length() > 0) {
184             String[] rolesStrings = role.split("//s*,//s*");
185             roles = new HashSet<String>();
186             for (int i = 0; i < rolesStrings.length; i++) {
187                 roles.add(rolesStrings[i]);
188             }
189         } else {
190             roles = null;
191         }
192     }
193 
194     /***
195      * Sets the roles that can render this attribute.
196      *
197      * @param roles The enabled roles.
198      * @since 2.0.6
199      */
200     public void setRoles(Set<String> roles) {
201         this.roles = roles;
202     }
203 
204     /***
205      * Access method for the attributes property.
206      * If there is no attributes, return an empty map.
207      *
208      * @return the current value of the attributes property
209      */
210     public Map<String, Attribute> getAttributes() {
211         return attributes;
212     }
213 
214     /***
215      * Returns the attribute for the given name, or null if no attribute of the
216      * given name exists.
217      *
218      * @param key name of the attribute
219      * @return requested attribute or null if not found
220      */
221     public Attribute getAttribute(String key) {
222         return attributes.get(key);
223     }
224 
225     /***
226      * Put a new attribute in this definition.
227      *
228      * @param key   String key for attribute
229      * @param value Attibute value.
230      */
231     public void putAttribute(String key, Attribute value) {
232         attributes.put(key, value);
233     }
234 
235     /***
236      * Add an attribute to this definition.
237      * <p/>
238      * This method is used by Digester to load definitions.
239      *
240      * @param attribute Attribute to add.
241      * @deprecated Use {@link Definition#putAttribute(String, Attribute)}.
242      */
243     @Deprecated
244     public void addAttribute(Attribute attribute) {
245         putAttribute(attribute.getName(), attribute);
246     }
247 
248     /***
249      * Checks whether the <code>key</code> attribute has been set.
250      *
251      * @param key The attribute key to check.
252      * @return <code>true</code> if the attribute has a value.
253      */
254     public boolean hasAttributeValue(String key) {
255         return attributes.containsKey(key);
256     }
257 
258     /***
259      * Put an attribute in template definition.
260      * Attribute can be used as content for tag get.
261      *
262      * @param name    Attribute name
263      * @param content Attribute value
264      */
265     public void put(String name, Object content) {
266         put(name, content, null);
267     }
268 
269     /***
270      * Put an attribute in template definition.
271      * Attribute can be used as content for tag get.
272      *
273      * @param name    Attribute name
274      * @param content Attribute value
275      * @param role    Determine if content is used by get tag. If user is in role, content is used.
276      */
277     public void put(String name, Object content, String role) {
278         put(name, content, null, role);
279     }
280 
281     /***
282      * Put an attribute in template definition.
283      * Attribute can be used as content for tag get.
284      *
285      * @param name    Attribute name
286      * @param content Attribute value
287      * @param type    attribute type: template, string, definition
288      * @param role    Determine if content is used by get tag. If user is in role, content is used.
289      */
290     public void put(String name, Object content, AttributeType type, String role) {
291         // Is there a type set ?
292         // First check direct attribute, and translate it to a valueType.
293         // Then, evaluate valueType, and create requested typed attribute.
294         Attribute attribute = new Attribute(content, role, type);
295         putAttribute(name, attribute);
296     }
297 
298     /***
299      * Get associated preparerInstance.
300      *
301      * @return The preparer name.
302      */
303     public String getPreparer() {
304         return preparer;
305     }
306 
307     /***
308      * Set associated preparerInstance URL.
309      *
310      * @param url Url called locally
311      */
312     public void setPreparer(String url) {
313         this.preparer = url;
314     }
315 
316     /***
317      * Set extends.
318      *
319      * @param name Name of the extended definition.
320      */
321     public void setExtends(String name) {
322         inherit = name;
323     }
324 
325     /***
326      * Get extends.
327      *
328      * @return Name of the extended definition.
329      */
330     public String getExtends() {
331         return inherit;
332     }
333 
334     /*** {@inheritDoc} */
335     @Override
336     public int hashCode() {
337         return name != null ? name.hashCode() : 0;
338     }
339 
340     /***
341      * Get extends flag.
342      *
343      * @return <code>true</code> if this definition extends another.
344      */
345     public boolean isExtending() {
346         return inherit != null;
347     }
348 
349     /***
350      * Returns a description of the attributes.
351      *
352      * @return A string representation of the content of this definition.
353      */
354     public String toString() {
355         return "{name="
356             + name
357             + ", template="
358             + template
359             + ", role="
360             + getRoles()
361             + ", preparerInstance="
362             + preparer
363             + ", attributes="
364             + attributes
365             + "}\n";
366     }
367 }