1/*2 * $Id: PutAttributeModel.java 797765 2009-07-25 13:20:26Z 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.template;
2324import org.apache.tiles.ArrayStack;
25import org.apache.tiles.Attribute;
26import org.apache.tiles.AttributeContext;
27import org.apache.tiles.Expression;
28import org.apache.tiles.TilesContainer;
2930/***31 * <p>32 * <strong>Put an attribute in enclosing attribute container tag.</strong>33 * </p>34 * <p>35 * Enclosing attribute container tag can be :36 * <ul>37 * <li><initContainer></li>38 * <li><definition></li>39 * <li><insertAttribute></li>40 * <li><insertDefinition></li>41 * <li><putListAttribute></li>42 * </ul>43 * (or any other tag which implements the <code>PutAttributeTagParent</code>44 * interface. Exception is thrown if no appropriate tag can be found.45 * </p>46 * <p>47 * Put tag can have following atributes :48 * <ul>49 * <li>name : Name of the attribute</li>50 * <li>value : value to put as attribute</li>51 * <li>type : value type. Possible type are : string (value is used as direct52 * string), template (value is used as a page url to insert), definition (value53 * is used as a definition name to insert), object (value is used as it is)</li>54 * <li>role : Role to check when 'insertAttribute' will be called.</li>55 * </ul>56 * </p>57 * <p>58 * Value can also come from tag body. Tag body is taken into account only if59 * value is not set by one of the tag attributes. In this case Attribute type is60 * "string", unless tag body define another type.61 * </p>62 *63 * @version $Rev: 797765 $ $Date: 2009-07-25 15:20:26 +0200 (sab, 25 lug 2009) $64 * @since 2.2.065 */66publicclassPutAttributeModel {
6768/***69 * Starts the operation.70 *71 * @param composeStack The compose stack.72 * @since 2.2.073 */74publicvoid start(ArrayStack<Object> composeStack) {
75 Attribute attribute = new Attribute();
76 composeStack.push(attribute);
77 }
7879/***80 * Ends the operation.81 *82 * @param container The Tiles container to use.83 * @param composeStack The composing stack.84 * @param name The name of the attribute to put.85 * @param value The value of the attribute. Use this parameter, or86 * expression, or body.87 * @param expression The expression to calculate the value from. Use this88 * parameter, or value, or body.89 * @param body The body of the tag. Use this parameter, or value, or90 * expression.91 * @param role A comma-separated list of roles. If present, the attribute92 * will be rendered only if the current user belongs to one of the roles.93 * @param type The type (renderer) of the attribute.94 * @param cascade If <code>true</code> the attribute will be cascaded to all nested attributes.95 * @param requestItems The request objects.96 * @since 2.2.097 */98publicvoid end(TilesContainer container, ArrayStack<Object> composeStack,
99 String name, Object value, String expression, String body,
100 String role, String type, boolean cascade, Object... requestItems) {
101 Attribute attribute = (Attribute) composeStack.pop();
102 putAttributeInParent(attribute, container, composeStack, name, value,
103 expression, body, role, type, cascade, requestItems);
104 }
105106/***107 * Executes the operation.108 *109 * @param container The Tiles container to use.110 * @param composeStack The composing stack.111 * @param name The name of the attribute to put.112 * @param value The value of the attribute. Use this parameter, or113 * expression, or body.114 * @param expression The expression to calculate the value from. Use this115 * parameter, or value, or body.116 * @param body The body of the tag. Use this parameter, or value, or117 * expression.118 * @param role A comma-separated list of roles. If present, the attribute119 * will be rendered only if the current user belongs to one of the roles.120 * @param type The type (renderer) of the attribute.121 * @param cascade If <code>true</code> the attribute will be cascaded to all nested attributes.122 * @param requestItems The request objects.123 * @since 2.2.0124 */125publicvoid execute(TilesContainer container, ArrayStack<Object> composeStack,
126 String name, Object value, String expression, String body,
127 String role, String type, boolean cascade, Object... requestItems) {
128 putAttributeInParent(new Attribute(), container, composeStack, name,
129 value, expression, body, role, type, cascade, requestItems);
130 }
131132/***133 * Determines the parent and puts the attribute inside it.134 *135 * @param attribute The attribute to put;136 * @param container The Tiles container to use.137 * @param composeStack The composing stack.138 * @param name The name of the attribute to put.139 * @param value The value of the attribute. Use this parameter, or140 * expression, or body.141 * @param expression The expression to calculate the value from. Use this142 * parameter, or value, or body.143 * @param body The body of the tag. Use this parameter, or value, or144 * expression.145 * @param role A comma-separated list of roles. If present, the attribute146 * will be rendered only if the current user belongs to one of the roles.147 * @param type The type (renderer) of the attribute.148 * @param cascade If <code>true</code> the attribute will be cascaded to all nested attributes.149 * @param requestItems The request objects.150 */151privatevoid putAttributeInParent(Attribute attribute,
152 TilesContainer container, ArrayStack<Object> composeStack, String name,
153 Object value, String expression, String body, String role,
154 String type, boolean cascade, Object... requestItems) {
155 AttributeContext attributeContext = null;
156if (!composeStack.isEmpty()) {
157 Object obj = composeStack.peek();
158if (obj instanceof AttributeContext) {
159 attributeContext = (AttributeContext) obj;
160 }
161 }
162if (attributeContext == null) {
163 attributeContext = container.getAttributeContext(requestItems);
164 }
165if (value != null) {
166 attribute.setValue(value);
167 } elseif (attribute.getValue() == null && body != null) {
168 attribute.setValue(body);
169 }
170if (expression != null) {
171 attribute.setExpressionObject(Expression
172 .createExpressionFromDescribedExpression(expression));
173 }
174if (role != null) {
175 attribute.setRole(role);
176 }
177if (type != null) {
178 attribute.setRenderer(type);
179 }
180181 attributeContext.putAttribute(name, attribute, cascade);
182 }
183 }