1/*2 * $Id: Definition.java 619574 2008-02-07 19:09:33Z 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;
222324import java.util.HashSet;
25import java.util.Iterator;
26import java.util.Map;
27import java.util.HashMap;
28import java.util.Set;
2930import org.apache.tiles.Attribute.AttributeType;
3132/***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 new36 * definitions with the Container.37 *38 * @since Tiles 2.039 * @version $Rev: 619574 $ $Date: 2008-02-07 20:09:33 +0100 (Thu, 07 Feb 2008) $40 */41publicclassDefinition {
42/***43 * Extends attribute value.44 */45protected String inherit;
46/***47 * Definition name.48 */49protected String name = null;
50/***51 * Template path.52 */53protected String template = null;
54/***55 * Attributes defined for the definition.56 */57protected Map<String, Attribute> attributes = null;
58/***59 * The roles that can render this definition.60 * @since 2.0.661 */62protected Set<String> roles = null;
63/***64 * Associated ViewPreparer URL or classname, if defined.65 */66protected String preparer = null;
676869/***70 * Constructor.71 */72publicDefinition() {
73 attributes = new HashMap<String, Attribute>();
74 }
7576/***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 Map80 * containing attributes.81 *82 * @param definition The definition to copy.83 */84publicDefinition(Definition definition) {
85 attributes = new HashMap<String, Attribute>(
86 definition.getAttributes());
87this.name = definition.name;
88this.template = definition.template;
89this.roles = definition.roles;
90this.preparer = definition.preparer;
91this.inherit = definition.inherit;
92 }
9394/***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 */100publicDefinition(String name, String template,
101 Map<String, Attribute> attributes) {
102this.name = name;
103this.template = template;
104this.attributes = attributes;
105 }
106107/***108 * Access method for the name property.109 *110 * @return the current value of the name property111 */112public String getName() {
113return name;
114 }
115116/***117 * Sets the value of the name property.118 *119 * @param aName the new value of the name property120 */121publicvoid setName(String aName) {
122 name = aName;
123 }
124125/***126 * Access method for the template property.127 *128 * @return the current value of the template property129 */130public String getTemplate() {
131return template;
132 }
133134/***135 * Sets the value of the template property.136 *137 * @param template the new value of the path property138 */139publicvoid setTemplate(String template) {
140this.template = template;
141 }
142143/***144 * Access method for the role property.145 *146 * @return the current value of the role property147 */148public String getRole() {
149 String retValue = null;
150151if (roles != null && !roles.isEmpty()) {
152 StringBuilder builder = new StringBuilder();
153 Iterator<String> roleIt = roles.iterator();
154if (roleIt.hasNext()) {
155 builder.append(roleIt.next());
156while (roleIt.hasNext()) {
157 builder.append(",");
158 builder.append(roleIt.next());
159 }
160 retValue = builder.toString();
161 }
162 }
163164return retValue;
165 }
166167/***168 * Returns the roles that can render this attribute.169 *170 * @return The enabled roles.171 * @since 2.0.6172 */173public Set<String> getRoles() {
174return roles;
175 }
176177/***178 * Sets the value of the role property.179 *180 * @param role the new value of the role property181 */182publicvoid setRole(String role) {
183if (role != null && role.trim().length() > 0) {
184 String[] rolesStrings = role.split("//s*,//s*");
185 roles = new HashSet<String>();
186for (int i = 0; i < rolesStrings.length; i++) {
187 roles.add(rolesStrings[i]);
188 }
189 } else {
190 roles = null;
191 }
192 }
193194/***195 * Sets the roles that can render this attribute.196 *197 * @param roles The enabled roles.198 * @since 2.0.6199 */200publicvoid setRoles(Set<String> roles) {
201this.roles = roles;
202 }
203204/***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 property209 */210public Map<String, Attribute> getAttributes() {
211return attributes;
212 }
213214/***215 * Returns the attribute for the given name, or null if no attribute of the216 * given name exists.217 *218 * @param key name of the attribute219 * @return requested attribute or null if not found220 */221publicAttribute getAttribute(String key) {
222return attributes.get(key);
223 }
224225/***226 * Put a new attribute in this definition.227 *228 * @param key String key for attribute229 * @param value Attibute value.230 */231publicvoid putAttribute(String key, Attribute value) {
232 attributes.put(key, value);
233 }
234235/***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
244publicvoid addAttribute(Attribute attribute) {
245 putAttribute(attribute.getName(), attribute);
246 }
247248/***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 */254publicboolean hasAttributeValue(String key) {
255return attributes.containsKey(key);
256 }
257258/***259 * Put an attribute in template definition.260 * Attribute can be used as content for tag get.261 *262 * @param name Attribute name263 * @param content Attribute value264 */265publicvoid put(String name, Object content) {
266 put(name, content, null);
267 }
268269/***270 * Put an attribute in template definition.271 * Attribute can be used as content for tag get.272 *273 * @param name Attribute name274 * @param content Attribute value275 * @param role Determine if content is used by get tag. If user is in role, content is used.276 */277publicvoid put(String name, Object content, String role) {
278 put(name, content, null, role);
279 }
280281/***282 * Put an attribute in template definition.283 * Attribute can be used as content for tag get.284 *285 * @param name Attribute name286 * @param content Attribute value287 * @param type attribute type: template, string, definition288 * @param role Determine if content is used by get tag. If user is in role, content is used.289 */290publicvoid 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.294Attribute attribute = newAttribute(content, role, type);
295 putAttribute(name, attribute);
296 }
297298/***299 * Get associated preparerInstance.300 *301 * @return The preparer name.302 */303public String getPreparer() {
304return preparer;
305 }
306307/***308 * Set associated preparerInstance URL.309 *310 * @param url Url called locally311 */312publicvoid setPreparer(String url) {
313this.preparer = url;
314 }
315316/***317 * Set extends.318 *319 * @param name Name of the extended definition.320 */321publicvoid setExtends(String name) {
322 inherit = name;
323 }
324325/***326 * Get extends.327 *328 * @return Name of the extended definition.329 */330public String getExtends() {
331return inherit;
332 }
333334/*** {@inheritDoc} */335 @Override
336publicint hashCode() {
337return name != null ? name.hashCode() : 0;
338 }
339340/***341 * Get extends flag.342 *343 * @return <code>true</code> if this definition extends another.344 */345publicboolean isExtending() {
346return inherit != null;
347 }
348349/***350 * Returns a description of the attributes.351 *352 * @return A string representation of the content of this definition.353 */354public String toString() {
355return"{name="356 + name
357 + ", template="358 + template
359 + ", role="360 + getRoles()
361 + ", preparerInstance="362 + preparer
363 + ", attributes="364 + attributes
365 + "}\n";
366 }
367 }