1/*2 * $Id: Attribute.java 788344 2009-06-25 12:47:40Z 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 */2122package org.apache.tiles;
2324import java.io.Serializable;
25import java.util.HashMap;
26import java.util.HashSet;
27import java.util.Iterator;
28import java.util.Map;
29import java.util.Set;
3031/***32 * Common implementation of attribute definition.33 *34 * @version $Rev: 788344 $ $Date: 2009-06-25 14:47:40 +0200 (gio, 25 giu 2009) $35 */36publicclassAttribute implements Serializable, Cloneable {
3738/***39 * The name of the template renderer.40 */41privatestaticfinal String TEMPLATE_RENDERER = "template";
4243/***44 * Attribute types.45 *46 * @deprecated Use {@link Attribute#setRenderer(String)} and47 * {@link Attribute#getRenderer()}.48 */49publicstatic enum AttributeType {
50/***51 * Attribute of type string.52 */53 STRING("string"),
5455/***56 * Attribute of type definition.57 */58 DEFINITION("definition"),
5960/***61 * Attribute of type template.62 */63 TEMPLATE("template"),
6465/***66 * Attribute of type object.67 */68 OBJECT("object");
6970/***71 * The string representation of the enum element.72 */73private String stringRepresentation;
7475/***76 * Maps the string representation to the attribute type.77 */78privatestatic Map<String, AttributeType> representation2type;
7980static {
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 }
8788/***89 * Returns the type for the given string representation.90 *91 * @param stringRepresentation The string representation of the needed92 * type.93 * @return The corresponding attribute type, if found, or94 * <code>null</code> if not.95 */96publicstatic AttributeType getType(String stringRepresentation) {
97return representation2type.get(stringRepresentation);
98 }
99100/***101 * Constructor.102 *103 * @param stringRepresentation The string representation of the enum104 * element.105 */106private AttributeType(String stringRepresentation) {
107this.stringRepresentation = stringRepresentation;
108 }
109110/***111 * Returns the string representation of the element.112 *113 * @return The string representation.114 */115 @Override
116public String toString() {
117return stringRepresentation;
118 }
119 };
120121/***122 * The roles that can render this attribute.123 * @since 2.0.6124 */125protected Set<String> roles = null;
126127/***128 * The value of the attribute.129 */130protected Object value = null;
131132/***133 * The expression to evaluate. Ignored if {@link #value} is not134 * <code>null</code>.135 *136 * @since 2.1.2137 */138protected String expression = null;
139140/***141 * The renderer name of the attribute. Default names are <code>string</code>,142 * <code>template</code>, <code>definition</code>, <code>object</code>.143 */144private String renderer = null;
145146/***147 * The name of the attribute. If it is <code>null</code>, it should be used148 * as an element of a list attribute.149 * @deprecated It is not used.150 */151 @Deprecated
152private String name = null;
153154/***155 * Constructor.156 *157 */158publicAttribute() {
159 }
160161/***162 * Constructor.163 *164 * @param value Object to store.165 */166publicAttribute(Object value) {
167this.value = value;
168 }
169170/***171 * Copy constructor.172 *173 * @param attribute The attribute to copy from.174 */175publicAttribute(Attribute attribute) {
176this.name = attribute.name;
177this.roles = attribute.roles;
178this.value = attribute.getValue();
179this.expression = attribute.expression;
180this.renderer = attribute.renderer;
181 }
182183/***184 * Constructor.185 *186 * @param name name of the attribute187 * @param value Object to store.188 */189publicAttribute(String name, Object value) {
190this.name = name;
191this.value = value;
192 }
193194/***195 * Constructor.196 *197 * @param value Object to store.198 * @param role Asociated role.199 */200publicAttribute(Object value, String role) {
201this.value = value;
202 setRole(role);
203 }
204205/***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
214publicAttribute(Object value, String role, AttributeType type) {
215this.value = value;
216 setType(type);
217 setRole(role);
218 }
219220/***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 the226 * <code>value</code> is not null.227 * @param role Associated role.228 * @param rendererName The renderer name.229 * @since 2.1.2230 */231publicAttribute(Object value, String expression, String role, String rendererName) {
232this.value = value;
233this.expression = expression;
234this.renderer = rendererName;
235 setRole(role);
236 }
237238/***239 * Constructor.240 *241 * @param name name of the attribute242 * @param value Object to store.243 * @param role Asociated role.244 * @param type Attribute type.245 * @deprecated Use246 * {@link Attribute#Attribute(Object, String, String)))}.247 */248publicAttribute(String name, Object value, String role,
249 AttributeType type) {
250this.name = name;
251this.value = value;
252 setType(type);
253 setRole(role);
254 }
255256/***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.2262 */263publicstaticAttribute createTemplateAttribute(String template) {
264Attribute attribute = newAttribute();
265 attribute.setValue(template);
266 attribute.setRenderer(TEMPLATE_RENDERER);
267return attribute;
268 }
269270/***271 * Creates a template attribute, starting from the expression to evaluate to272 * obtain the template.273 *274 * @param templateExpression The expression to evaluate.275 * @return The template attribute.276 * @since 2.1.2277 */278publicstaticAttribute createTemplateAttributeWithExpression(
279 String templateExpression) {
280Attribute attribute = newAttribute();
281 attribute.setExpression(templateExpression);
282 attribute.setRenderer(TEMPLATE_RENDERER);
283return attribute;
284 }
285286/***287 * Get role.288 * @return the name of the required role(s)289 */290public String getRole() {
291 String retValue = null;
292293if (roles != null && !roles.isEmpty()) {
294 StringBuilder builder = new StringBuilder();
295 Iterator<String> roleIt = roles.iterator();
296if (roleIt.hasNext()) {
297 builder.append(roleIt.next());
298while (roleIt.hasNext()) {
299 builder.append(",");
300 builder.append(roleIt.next());
301 }
302 retValue = builder.toString();
303 }
304 }
305306return retValue;
307 }
308309/***310 * Returns the roles that can render this attribute.311 *312 * @return The enabled roles.313 * @since 2.0.6314 */315public Set<String> getRoles() {
316return roles;
317 }
318319/***320 * Set role.321 *322 * @param role Associated role.323 */324publicvoid setRole(String role) {
325if (role != null && role.trim().length() > 0) {
326 String[] rolesStrings = role.split("//s*,//s*");
327 roles = new HashSet<String>();
328for (int i = 0; i < rolesStrings.length; i++) {
329 roles.add(rolesStrings[i]);
330 }
331 } else {
332 roles = null;
333 }
334 }
335336/***337 * Sets the roles that can render this attribute.338 *339 * @param roles The enabled roles.340 * @since 2.0.6341 */342publicvoid setRoles(Set<String> roles) {
343this.roles = roles;
344 }
345346/***347 * Get value.348 * @return the value349 */350public Object getValue() {
351return value;
352 }
353354/***355 * Set value.356 *357 * @param value New value.358 */359publicvoid setValue(Object value) {
360this.value = value;
361 }
362363/***364 * Returns The expression to evaluate. Ignored if {@link #value} is not365 * <code>null</code>.366 *367 * @return The expression to be evaluated.368 * @since 2.1.2369 */370public String getExpression() {
371return expression;
372 }
373374/***375 * Sets The expression to evaluate. Ignored if {@link #value} is not376 * <code>null</code>.377 *378 * @param expression The expression to be evaluated.379 * @since 2.1.2380 */381publicvoid setExpression(String expression) {
382this.expression = expression;
383 }
384385/*** {@inheritDoc} */386public String toString() {
387if (value != null) {
388return value.toString();
389 }
390returnnull;
391 }
392393/***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 */400public AttributeType getType() {
401return AttributeType.getType(renderer);
402 }
403404/***405 * Sets the type of this attribute.406 *407 * @param type The attribute type.408 * @deprecated Use {@link Attribute#setRenderer(String))}.409 */410publicvoid setType(AttributeType type) {
411this.renderer = type.toString();
412 }
413414/***415 * Returns the renderer name to use.416 *417 * @return The renderer name.418 * @since 2.1.0419 */420public String getRenderer() {
421return renderer;
422 }
423424/***425 * Sets the renderer name to use.426 *427 * @param rendererName The renderer.428 * @since 2.1.0429 */430publicvoid setRenderer(String rendererName) {
431this.renderer = rendererName;
432 }
433434/***435 * Returns the name of the attribute.436 *437 * @return The name of the attribute. It can be <code>null</code>, but in438 * this case it should be used as an element of <code>ListAttribute</code>439 * @deprecated Use the <code>getName</code> methods in object that contain440 * attributes.441 */442 @Deprecated
443public String getName() {
444return name;
445 }
446447/***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 of452 * <code>ListAttribute</code>453 * @deprecated Use the <code>setName</code> methods in object that contain454 * attributes.455 */456 @Deprecated
457publicvoid setName(String name) {
458this.name = name;
459 }
460461/***462 * Sets the body of this attribute.463 *464 * @param body The attribute body.465 */466// FIXME Is it necessary?467publicvoid setBody(String body) {
468if (body != null && body.length() != 0) {
469 setValue(body);
470 }
471 }
472473/***474 * Inherits an attribute, i.e. overwrites null properties with the ones475 * provided by the attribute.476 *477 * @param attribute The attribute to inherit.478 * @since 2.1.2479 */480publicvoid inherit(Attribute attribute) {
481if (value == null) {
482 value = attribute.getValue();
483 }
484if (expression == null) {
485 expression = attribute.getExpression();
486 }
487if (roles == null || roles.isEmpty()) {
488 roles = attribute.getRoles();
489 }
490if (renderer == null) {
491 renderer = attribute.getRenderer();
492 }
493 }
494495/*** {@inheritDoc} */496 @Override
497publicAttribute clone() {
498returnnewAttribute(this);
499 }
500 }