1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles.template;
23
24 import org.apache.tiles.ArrayStack;
25 import org.apache.tiles.Attribute;
26 import org.apache.tiles.AttributeContext;
27 import org.apache.tiles.Expression;
28 import org.apache.tiles.TilesContainer;
29
30 /***
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 direct
52 * string), template (value is used as a page url to insert), definition (value
53 * 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 if
59 * value is not set by one of the tag attributes. In this case Attribute type is
60 * "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.0
65 */
66 public class PutAttributeModel {
67
68 /***
69 * Starts the operation.
70 *
71 * @param composeStack The compose stack.
72 * @since 2.2.0
73 */
74 public void start(ArrayStack<Object> composeStack) {
75 Attribute attribute = new Attribute();
76 composeStack.push(attribute);
77 }
78
79 /***
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, or
86 * expression, or body.
87 * @param expression The expression to calculate the value from. Use this
88 * parameter, or value, or body.
89 * @param body The body of the tag. Use this parameter, or value, or
90 * expression.
91 * @param role A comma-separated list of roles. If present, the attribute
92 * 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.0
97 */
98 public void 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 }
105
106 /***
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, or
113 * expression, or body.
114 * @param expression The expression to calculate the value from. Use this
115 * parameter, or value, or body.
116 * @param body The body of the tag. Use this parameter, or value, or
117 * expression.
118 * @param role A comma-separated list of roles. If present, the attribute
119 * 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.0
124 */
125 public void 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 }
131
132 /***
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, or
140 * expression, or body.
141 * @param expression The expression to calculate the value from. Use this
142 * parameter, or value, or body.
143 * @param body The body of the tag. Use this parameter, or value, or
144 * expression.
145 * @param role A comma-separated list of roles. If present, the attribute
146 * 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 */
151 private void 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;
156 if (!composeStack.isEmpty()) {
157 Object obj = composeStack.peek();
158 if (obj instanceof AttributeContext) {
159 attributeContext = (AttributeContext) obj;
160 }
161 }
162 if (attributeContext == null) {
163 attributeContext = container.getAttributeContext(requestItems);
164 }
165 if (value != null) {
166 attribute.setValue(value);
167 } else if (attribute.getValue() == null && body != null) {
168 attribute.setValue(body);
169 }
170 if (expression != null) {
171 attribute.setExpressionObject(Expression
172 .createExpressionFromDescribedExpression(expression));
173 }
174 if (role != null) {
175 attribute.setRole(role);
176 }
177 if (type != null) {
178 attribute.setRenderer(type);
179 }
180
181 attributeContext.putAttribute(name, attribute, cascade);
182 }
183 }