1 /*
2 * $Id: AddAttributeModel.java 1305937 2012-03-27 18:15:15Z nlebas $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.tiles.template;
23
24 import java.io.IOException;
25 import java.util.Deque;
26
27 import org.apache.tiles.Attribute;
28 import org.apache.tiles.Expression;
29 import org.apache.tiles.ListAttribute;
30 import org.apache.tiles.autotag.core.runtime.ModelBody;
31 import org.apache.tiles.request.Request;
32
33 /**
34 * <p>
35 * <strong>Add an element to the surrounding list. Equivalent to 'putAttribute',
36 * but for list element.</strong>
37 * </p>
38 *
39 * <p>
40 * Add an element to the surrounding list. This tag can only be used inside
41 * 'putListAttribute' or 'addListAttribute' tags. Value can come from a direct
42 * assignment (value="aValue")
43 * </p>
44 *
45 * @version $Rev: 1305937 $ $Date: 2012-03-28 05:15:15 +1100 (Wed, 28 Mar 2012) $
46 * @since 2.2.0
47 */
48 public class AddAttributeModel {
49
50 /**
51 * Executes the operation.
52 * @param value The value of the attribute. Use this parameter, or
53 * expression, or body.
54 * @param expression The expression to calculate the value from. Use this
55 * parameter, or value, or body.
56 * @param role A comma-separated list of roles. If present, the attribute
57 * will be rendered only if the current user belongs to one of the roles.
58 * @param type The type (renderer) of the attribute.
59 * @param request The request.
60 * @param modelBody The body.
61 * @throws IOException If the body cannot be correctly evaluated.
62 * @since 2.2.0
63 */
64 public void execute(Object value, String expression, String role,
65 String type, Request request, ModelBody modelBody)
66 throws IOException {
67 Attribute attribute = new Attribute();
68 Deque<Object> composeStack = ComposeStackUtil.getComposeStack(request);
69 composeStack.push(attribute);
70 String body = modelBody.evaluateAsString();
71 attribute = (Attribute) composeStack.pop();
72 addAttributeToList(attribute, composeStack, value, expression, body,
73 role, type);
74 }
75
76 /**
77 * Adds the attribute to the containing list attribute.
78 *
79 * @param attribute The attribute to add to the list attribute.
80 * @param composeStack The composing stack.
81 * @param value The value of the attribute. Use this parameter, or
82 * expression, or body.
83 * @param expression The expression to calculate the value from. Use this
84 * parameter, or value, or body.
85 * @param body The body of the tag. Use this parameter, or value, or
86 * expression.
87 * @param role A comma-separated list of roles. If present, the attribute
88 * will be rendered only if the current user belongs to one of the roles.
89 * @param type The type (renderer) of the attribute.
90 * @since 2.2.0
91 */
92 private void addAttributeToList(Attribute attribute,
93 Deque<Object> composeStack, Object value, String expression,
94 String body, String role, String type) {
95 ListAttribute listAttribute = (ListAttribute) ComposeStackUtil
96 .findAncestorWithClass(composeStack, ListAttribute.class);
97
98 if (listAttribute == null) {
99 throw new NullPointerException("There is no ListAttribute in the stack");
100 }
101 if (value != null) {
102 attribute.setValue(value);
103 } else if (attribute.getValue() == null && body != null) {
104 attribute.setValue(body);
105 }
106 if (expression != null) {
107 attribute.setExpressionObject(Expression
108 .createExpressionFromDescribedExpression(expression));
109 }
110 if (role != null) {
111 attribute.setRole(role);
112 }
113 if (type != null) {
114 attribute.setRenderer(type);
115 }
116 listAttribute.add(attribute);
117 }
118 }