1/*2 * $Id: Attribute.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 */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: 619574 $ $Date: 2008-02-07 20:09:33 +0100 (Thu, 07 Feb 2008) $35 */36publicclassAttribute implements Serializable {
3738/***39 * Attribute types.40 */41publicstatic enum AttributeType {
42/***43 * Attribute of type string.44 */45 STRING("string"),
4647/***48 * Attribute of type definition.49 */50 DEFINITION("definition"),
5152/***53 * Attribute of type template.54 */55 TEMPLATE("template"),
5657/***58 * Attribute of type object.59 */60 OBJECT("object");
6162/***63 * The string representation of the enum element.64 */65private String stringRepresentation;
6667/***68 * Maps the string representation to the attribute type.69 */70privatestatic Map<String, AttributeType> representation2type;
7172static {
73 representation2type = new HashMap<String, AttributeType>();
74 representation2type.put("string", AttributeType.STRING);
75 representation2type.put("definition", AttributeType.DEFINITION);
76 representation2type.put("template", AttributeType.TEMPLATE);
77 representation2type.put("object", AttributeType.OBJECT);
78 }
7980/***81 * Returns the type for the given string representation.82 *83 * @param stringRepresentation The string representation of the needed84 * type.85 * @return The corresponding attribute type, if found, or86 * <code>null</code> if not.87 */88publicstatic AttributeType getType(String stringRepresentation) {
89return representation2type.get(stringRepresentation);
90 }
9192/***93 * Constructor.94 *95 * @param stringRepresentation The string representation of the enum96 * element.97 */98private AttributeType(String stringRepresentation) {
99this.stringRepresentation = stringRepresentation;
100 }
101102/***103 * Returns the string representation of the element.104 *105 * @return The string representation.106 */107 @Override
108public String toString() {
109return stringRepresentation;
110 }
111 };
112113/***114 * The roles that can render this attribute.115 * @since 2.0.6116 */117protected Set<String> roles = null;
118119/***120 * The value of the attribute.121 */122protected Object value = null;
123124/***125 * The type of the attribute. It can be <code>string</code>,126 * <code>template</code>, <code>definition</code>.127 */128private AttributeType type = null;
129130/***131 * The name of the attribute. If it is <code>null</code>, it should be used132 * as an element of a list attribute.133 * @deprecated It is not used.134 */135 @Deprecated
136private String name = null;
137138/***139 * Constructor.140 *141 */142publicAttribute() {
143 }
144145/***146 * Constructor.147 *148 * @param value Object to store.149 */150publicAttribute(Object value) {
151this.value = value;
152 }
153154/***155 * Copy constructor.156 *157 * @param attribute The attribute to copy from.158 */159publicAttribute(Attribute attribute) {
160this.name = attribute.name;
161this.roles = attribute.roles;
162this.type = attribute.type;
163this.value = attribute.getValue();
164 }
165166/***167 * Constructor.168 *169 * @param name name of the attribute170 * @param value Object to store.171 */172publicAttribute(String name, Object value) {
173this.name = name;
174this.value = value;
175 }
176177/***178 * Constructor.179 *180 * @param value Object to store.181 * @param role Asociated role.182 */183publicAttribute(Object value, String role) {
184this.value = value;
185 setRole(role);
186 }
187188/***189 * Constructor.190 *191 * @param value Object to store.192 * @param role Asociated role.193 * @param type Attribute type.194 */195publicAttribute(Object value, String role, AttributeType type) {
196this.value = value;
197this.type = type;
198 setRole(role);
199 }
200201/***202 * Constructor.203 *204 * @param name name of the attribute205 * @param value Object to store.206 * @param role Asociated role.207 * @param type Attribute type.208 */209publicAttribute(String name, Object value, String role,
210 AttributeType type) {
211this.name = name;
212this.value = value;
213this.type = type;
214 setRole(role);
215 }
216217/***218 * Get role.219 * @return the name of the required role(s)220 */221public String getRole() {
222 String retValue = null;
223224if (roles != null && !roles.isEmpty()) {
225 StringBuilder builder = new StringBuilder();
226 Iterator<String> roleIt = roles.iterator();
227if (roleIt.hasNext()) {
228 builder.append(roleIt.next());
229while (roleIt.hasNext()) {
230 builder.append(",");
231 builder.append(roleIt.next());
232 }
233 retValue = builder.toString();
234 }
235 }
236237return retValue;
238 }
239240/***241 * Returns the roles that can render this attribute.242 *243 * @return The enabled roles.244 * @since 2.0.6245 */246public Set<String> getRoles() {
247return roles;
248 }
249250/***251 * Set role.252 *253 * @param role Associated role.254 */255publicvoid setRole(String role) {
256if (role != null && role.trim().length() > 0) {
257 String[] rolesStrings = role.split("//s*,//s*");
258 roles = new HashSet<String>();
259for (int i = 0; i < rolesStrings.length; i++) {
260 roles.add(rolesStrings[i]);
261 }
262 } else {
263 roles = null;
264 }
265 }
266267/***268 * Sets the roles that can render this attribute.269 *270 * @param roles The enabled roles.271 * @since 2.0.6272 */273publicvoid setRoles(Set<String> roles) {
274this.roles = roles;
275 }
276277/***278 * Get value.279 * @return the value280 */281public Object getValue() {
282return value;
283 }
284285/***286 * Set value.287 *288 * @param value New value.289 */290publicvoid setValue(Object value) {
291this.value = value;
292 }
293294/*** {@inheritDoc} */295public String toString() {
296if (value != null) {
297return value.toString();
298 }
299returnnull;
300 }
301302/***303 * Returns the type of this attribute.304 *305 * @return The attribute type. It can be <code>string</code>,306 * <code>template</code>, <code>definition</code>, <code>object</code>.307 */308public AttributeType getType() {
309return type;
310 }
311312/***313 * Sets the type of this attribute.314 *315 * @param type The attribute type.316 */317publicvoid setType(AttributeType type) {
318this.type = type;
319 }
320321/***322 * Returns the name of the attribute.323 *324 * @return The name of the attribute. It can be <code>null</code>, but in325 * this case it should be used as an element of <code>ListAttribute</code>326 * @deprecated Use the <code>getName</code> methods in object that contain327 * attributes.328 */329 @Deprecated
330public String getName() {
331return name;
332 }
333334/***335 * Sets the name of the attribute.336 *337 * @param name The name of the attribute. It can be <code>null</code>,338 * but in this case it should be used as an element of339 * <code>ListAttribute</code>340 * @deprecated Use the <code>setName</code> methods in object that contain341 * attributes.342 */343 @Deprecated
344publicvoid setName(String name) {
345this.name = name;
346 }
347348/***349 * Sets the body of this attribute.350 *351 * @param body The attribute body.352 */353// FIXME Is it necessary?354publicvoid setBody(String body) {
355if (body != null && body.length() != 0) {
356 setValue(body);
357 }
358 }
359 }