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

1   /*
2    * $Id: Definition.java 790234 2009-07-01 15:54:28Z 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  import java.util.Map;
24  
25  /***
26   * A definition, i.e. a template with (completely or not) filled attributes.
27   * Attributes of a template can be defined with the help of this class.<br>
28   * It can be used as a data transfer object used for registering new
29   * definitions with the Container.
30   *
31   * @since Tiles 2.0
32   * @version $Rev: 790234 $ $Date: 2009-07-01 17:54:28 +0200 (mer, 01 lug 2009) $
33   */
34  public class Definition extends BasicAttributeContext {
35      /***
36       * Extends attribute value.
37       */
38      protected String inherit;
39      /***
40       * Definition name.
41       */
42      protected String name = null;
43  
44      /***
45       * Constructor.
46       */
47      public Definition() {
48          super();
49      }
50  
51      /***
52       * Copy Constructor.
53       * Create a new definition initialized with parent definition.
54       * Do a shallow copy : attributes are shared between copies, but not the Map
55       * containing attributes.
56       *
57       * @param definition The definition to copy.
58       */
59      public Definition(Definition definition) {
60          super(definition);
61          this.name = definition.name;
62          this.inherit = definition.inherit;
63      }
64  
65      /***
66       * Constructor.
67       * @param name The name of the definition.
68       * @param template The template of the definition.
69       * @param attributes The attribute map of the definition.
70       * @deprecated Use {@link #Definition(String, Attribute, Map)}.
71       */
72      public Definition(String name, String template,
73                                 Map<String, Attribute> attributes) {
74          this(name, Attribute.createTemplateAttribute(template), attributes);
75      }
76  
77      /***
78       * Constructor.
79       * @param name The name of the definition.
80       * @param templateAttribute The template attribute of the definition.
81       * @param attributes The attribute map of the definition.
82       *
83       * @since 2.1.2
84       */
85      public Definition(String name, Attribute templateAttribute,
86                                 Map<String, Attribute> attributes) {
87          super(attributes);
88          this.name = name;
89          this.templateAttribute = templateAttribute;
90      }
91  
92      /***
93       * Access method for the name property.
94       *
95       * @return the current value of the name property
96       */
97      public String getName() {
98          return name;
99      }
100 
101     /***
102      * Sets the value of the name property.
103      *
104      * @param aName the new value of the name property
105      */
106     public void setName(String aName) {
107         name = aName;
108     }
109 
110     /***
111      * Access method for the template property.
112      *
113      * @return the current value of the template property
114      * @deprecated Use {@link #getTemplateAttribute()}.
115      */
116     @Deprecated
117     public String getTemplate() {
118         if (templateAttribute == null) {
119             templateAttribute = Attribute.createTemplateAttribute(null);
120         }
121         return (String) templateAttribute.getValue();
122     }
123 
124     /***
125      * Sets the value of the template property.
126      *
127      * @param template the new value of the path property
128      * @deprecated Use {@link #getTemplateAttribute()}.
129      */
130     @Deprecated
131     public void setTemplate(String template) {
132         if (templateAttribute == null) {
133             templateAttribute = Attribute.createTemplateAttribute(template);
134         } else {
135             templateAttribute.setValue(template);
136         }
137     }
138 
139     /***
140      * Access method for the role property.
141      *
142      * @return the current value of the role property
143      * @deprecated Use {@link #getTemplateAttribute()}.
144      */
145     @Deprecated
146     public String getRole() {
147         if (templateAttribute == null) {
148             templateAttribute = Attribute.createTemplateAttribute(null);
149         }
150 
151         return templateAttribute.getRole();
152     }
153 
154     /***
155      * Sets the value of the role property.
156      *
157      * @param role the new value of the role property
158      * @deprecated Use {@link #getTemplateAttribute()}.
159      */
160     @Deprecated
161     public void setRole(String role) {
162         if (templateAttribute == null) {
163             templateAttribute = Attribute.createTemplateAttribute(null);
164         }
165 
166         templateAttribute.setRole(role);
167     }
168 
169     /***
170      * Access method for the attributes property. If there is no attributes,
171      * return an empty map.
172      *
173      * @return the current value of the attributes property
174      * @deprecated Use {@link AttributeContext#getLocalAttributeNames()} and
175      * {@link AttributeContext#getCascadedAttributeNames()}.
176      */
177     @Deprecated
178     public Map<String, Attribute> getAttributes() {
179         return attributes;
180     }
181 
182     /***
183      * Add an attribute to this definition.
184      * <p/>
185      * This method is used by Digester to load definitions.
186      *
187      * @param attribute Attribute to add.
188      * @deprecated Use {@link Definition#putAttribute(String, Attribute)}.
189      */
190     @Deprecated
191     public void addAttribute(Attribute attribute) {
192         putAttribute(attribute.getName(), attribute);
193     }
194 
195     /***
196      * Checks whether the <code>key</code> attribute has been set.
197      *
198      * @param key The attribute key to check.
199      * @return <code>true</code> if the attribute has a value.
200      * @deprecated Check if the {@link AttributeContext#getAttribute(String)}
201      * returns null.
202      */
203     @Deprecated
204     public boolean hasAttributeValue(String key) {
205         return getAttribute(key) != null;
206     }
207 
208     /***
209      * Put an attribute in template definition. Attribute can be used as content
210      * for tag get.
211      *
212      * @param name Attribute name
213      * @param content Attribute value
214      * @deprecated Use {@link AttributeContext#putAttribute(String, Attribute)}
215      * or {@link AttributeContext#putAttribute(String, Attribute, boolean)}.
216      */
217     @Deprecated
218     public void put(String name, Object content) {
219         put(name, content, null);
220     }
221 
222     /***
223      * Put an attribute in template definition.
224      * Attribute can be used as content for tag get.
225      *
226      * @param name    Attribute name
227      * @param content Attribute value
228      * @param role    Determine if content is used by get tag. If user is in role, content is used.
229      * @deprecated Use {@link AttributeContext#putAttribute(String, Attribute)}
230      * or {@link AttributeContext#putAttribute(String, Attribute, boolean)}.
231      */
232     @Deprecated
233     public void put(String name, Object content, String role) {
234         Attribute attribute = new Attribute(content, null, role, (String) null);
235         putAttribute(name, attribute);
236     }
237 
238     /***
239      * Put an attribute in template definition.
240      * Attribute can be used as content for tag get.
241      *
242      * @param name    Attribute name
243      * @param content Attribute value
244      * @param type    attribute type: template, string, definition
245      * @param role    Determine if content is used by get tag. If user is in role, content is used.
246      * @deprecated Use {@link AttributeContext#putAttribute(String, Attribute)}
247      * or {@link AttributeContext#putAttribute(String, Attribute, boolean)}.
248      */
249     @Deprecated
250     public void put(String name, Object content,
251             org.apache.tiles.Attribute.AttributeType type, String role) {
252         // Is there a type set ?
253         // First check direct attribute, and translate it to a valueType.
254         // Then, evaluate valueType, and create requested typed attribute.
255         Attribute attribute = new Attribute(content, role, type);
256         putAttribute(name, attribute);
257     }
258 
259     /***
260      * Set extends.
261      *
262      * @param name Name of the extended definition.
263      */
264     public void setExtends(String name) {
265         inherit = name;
266     }
267 
268     /***
269      * Get extends.
270      *
271      * @return Name of the extended definition.
272      */
273     public String getExtends() {
274         return inherit;
275     }
276 
277     /*** {@inheritDoc} */
278     @Override
279     public int hashCode() {
280         return name != null ? name.hashCode() : 0;
281     }
282 
283     /***
284      * Get extends flag.
285      *
286      * @return <code>true</code> if this definition extends another.
287      */
288     public boolean isExtending() {
289         return inherit != null;
290     }
291 
292     /***
293      * Returns a description of the attributes.
294      *
295      * @return A string representation of the content of this definition.
296      */
297     public String toString() {
298         return "{name="
299             + name
300             + ", template="
301             + (templateAttribute != null ? templateAttribute.getValue() : "<null>")
302             + ", role="
303             + (templateAttribute != null ? templateAttribute.getRoles() : "<null>")
304             + ", preparerInstance="
305             + preparer
306             + ", attributes="
307             + attributes
308             + "}";
309     }
310 }