1/*2 * $Id: Definition.java 790234 2009-07-01 15:54:28Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles;
2223import java.util.Map;
2425/***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 new29 * definitions with the Container.30 *31 * @since Tiles 2.032 * @version $Rev: 790234 $ $Date: 2009-07-01 17:54:28 +0200 (mer, 01 lug 2009) $33 */34publicclassDefinitionextendsBasicAttributeContext {
35/***36 * Extends attribute value.37 */38protected String inherit;
39/***40 * Definition name.41 */42protected String name = null;
4344/***45 * Constructor.46 */47publicDefinition() {
48super();
49 }
5051/***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 Map55 * containing attributes.56 *57 * @param definition The definition to copy.58 */59publicDefinition(Definition definition) {
60super(definition);
61this.name = definition.name;
62this.inherit = definition.inherit;
63 }
6465/***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 */72publicDefinition(String name, String template,
73 Map<String, Attribute> attributes) {
74this(name, Attribute.createTemplateAttribute(template), attributes);
75 }
7677/***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.284 */85publicDefinition(String name, Attribute templateAttribute,
86 Map<String, Attribute> attributes) {
87super(attributes);
88this.name = name;
89this.templateAttribute = templateAttribute;
90 }
9192/***93 * Access method for the name property.94 *95 * @return the current value of the name property96 */97public String getName() {
98return name;
99 }
100101/***102 * Sets the value of the name property.103 *104 * @param aName the new value of the name property105 */106publicvoid setName(String aName) {
107 name = aName;
108 }
109110/***111 * Access method for the template property.112 *113 * @return the current value of the template property114 * @deprecated Use {@link #getTemplateAttribute()}.115 */116 @Deprecated
117public String getTemplate() {
118if (templateAttribute == null) {
119 templateAttribute = Attribute.createTemplateAttribute(null);
120 }
121return (String) templateAttribute.getValue();
122 }
123124/***125 * Sets the value of the template property.126 *127 * @param template the new value of the path property128 * @deprecated Use {@link #getTemplateAttribute()}.129 */130 @Deprecated
131publicvoid setTemplate(String template) {
132if (templateAttribute == null) {
133 templateAttribute = Attribute.createTemplateAttribute(template);
134 } else {
135 templateAttribute.setValue(template);
136 }
137 }
138139/***140 * Access method for the role property.141 *142 * @return the current value of the role property143 * @deprecated Use {@link #getTemplateAttribute()}.144 */145 @Deprecated
146public String getRole() {
147if (templateAttribute == null) {
148 templateAttribute = Attribute.createTemplateAttribute(null);
149 }
150151return templateAttribute.getRole();
152 }
153154/***155 * Sets the value of the role property.156 *157 * @param role the new value of the role property158 * @deprecated Use {@link #getTemplateAttribute()}.159 */160 @Deprecated
161publicvoid setRole(String role) {
162if (templateAttribute == null) {
163 templateAttribute = Attribute.createTemplateAttribute(null);
164 }
165166 templateAttribute.setRole(role);
167 }
168169/***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 property174 * @deprecated Use {@link AttributeContext#getLocalAttributeNames()} and175 * {@link AttributeContext#getCascadedAttributeNames()}.176 */177 @Deprecated
178public Map<String, Attribute> getAttributes() {
179return attributes;
180 }
181182/***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
191publicvoid addAttribute(Attribute attribute) {
192 putAttribute(attribute.getName(), attribute);
193 }
194195/***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
204publicboolean hasAttributeValue(String key) {
205return getAttribute(key) != null;
206 }
207208/***209 * Put an attribute in template definition. Attribute can be used as content210 * for tag get.211 *212 * @param name Attribute name213 * @param content Attribute value214 * @deprecated Use {@link AttributeContext#putAttribute(String, Attribute)}215 * or {@link AttributeContext#putAttribute(String, Attribute, boolean)}.216 */217 @Deprecated
218publicvoid put(String name, Object content) {
219 put(name, content, null);
220 }
221222/***223 * Put an attribute in template definition.224 * Attribute can be used as content for tag get.225 *226 * @param name Attribute name227 * @param content Attribute value228 * @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
233publicvoid put(String name, Object content, String role) {
234Attribute attribute = newAttribute(content, null, role, (String) null);
235 putAttribute(name, attribute);
236 }
237238/***239 * Put an attribute in template definition.240 * Attribute can be used as content for tag get.241 *242 * @param name Attribute name243 * @param content Attribute value244 * @param type attribute type: template, string, definition245 * @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
250publicvoid 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.255Attribute attribute = newAttribute(content, role, type);
256 putAttribute(name, attribute);
257 }
258259/***260 * Set extends.261 *262 * @param name Name of the extended definition.263 */264publicvoid setExtends(String name) {
265 inherit = name;
266 }
267268/***269 * Get extends.270 *271 * @return Name of the extended definition.272 */273public String getExtends() {
274return inherit;
275 }
276277/*** {@inheritDoc} */278 @Override
279publicint hashCode() {
280return name != null ? name.hashCode() : 0;
281 }
282283/***284 * Get extends flag.285 *286 * @return <code>true</code> if this definition extends another.287 */288publicboolean isExtending() {
289return inherit != null;
290 }
291292/***293 * Returns a description of the attributes.294 *295 * @return A string representation of the content of this definition.296 */297public String toString() {
298return"{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 }