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.jsp.taglib;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.jsp.tagext.TagSupport;
26
27 import org.apache.tiles.jsp.taglib.definition.DefinitionTagParent;
28
29 /***
30 * <p><strong>Adds an attribute in enclosing attribute container tag.</strong></p>
31 * <p>Enclosing attribute container tag can be :
32 * <ul>
33 * <li><putListAttribute></li>
34 * <li><putAttribute></li>
35 * </ul>
36 * (or any other tag which implements the <code>{@link AddAttributeTagParent}</code> interface.
37 * Exception is thrown if no appropriate tag can be found.</p>
38 * <p>Put tag can have following atributes :
39 * <ul>
40 * <li>name : Name of the attribute</li>
41 * <li>value : value to put as attribute</li>
42 * <li>type : value type. Only valid if value is a String and is set by
43 * value="something" or by a bean.
44 * Possible type are : string (value is used as direct string),
45 * template (value is used as a page url to insert),
46 * definition (value is used as a definition name to insert)</li>
47 * <li>role : Role to check when 'insert' will be called. If enclosing tag is
48 * <insert>, role is checked immediately. If enclosing tag is
49 * <definition>, role will be checked when this definition will be
50 * inserted.</li>
51 * </ul></p>
52 * <p>Value can also come from tag body. Tag body is taken into account only if
53 * value is not set by one of the tag attributes. In this case Attribute type is
54 * "string", unless tag body define another type.</p>
55 *
56 * @version $Rev: 739526 $ $Date: 2009-01-31 09:50:58 +0100 (sab, 31 gen 2009) $
57 */
58 public class AddAttributeTag extends TilesBodyTag implements DefinitionTagParent {
59
60 /***
61 * The role to check. If the user is in the specified role, the tag is taken
62 * into account; otherwise, the tag is ignored (skipped).
63 */
64 protected String role;
65
66 /***
67 * Associated attribute value.
68 */
69 private Object value = null;
70
71 /***
72 * Requested type for the value.
73 */
74 private String type = null;
75
76 /***
77 * Returns the role to check. If the user is in the specified role, the tag is
78 * taken into account; otherwise, the tag is ignored (skipped).
79 *
80 * @return The role to check.
81 */
82 public String getRole() {
83 return role;
84 }
85
86 /***
87 * Sets the role to check. If the user is in the specified role, the tag is
88 * taken into account; otherwise, the tag is ignored (skipped).
89 *
90 * @param role The role to check.
91 */
92 public void setRole(String role) {
93 this.role = role;
94 }
95
96 /***
97 * Returns the attribute value.
98 *
99 * @return Attribute value. Can be a String or Object.
100 */
101 public Object getValue() {
102 return value;
103 }
104
105 /***
106 * Sets the attribute value.
107 *
108 * @param value Attribute value. Can be a String or Object.
109 */
110 public void setValue(Object value) {
111 this.value = value;
112 }
113
114 /***
115 * <p>
116 * Returns content type: string, template or definition.
117 * </p>
118 * <ul>
119 * <li>String : Content is printed directly.</li>
120 * <li>template : Content is included from specified URL. Value is used as
121 * an URL.</li>
122 * <li>definition : Value denote a definition defined in factory (xml
123 * file). Definition will be searched in the inserted tile, in a
124 * <code><insert attribute="attributeName"></code> tag, where
125 * 'attributeName' is the name used for this tag.</li>
126 * </ul>
127 *
128 * @return The attribute type.
129 */
130 public String getType() {
131 return type;
132 }
133
134 /***
135 * <p>
136 * Sets content type: string, template or definition.
137 * </p>
138 * <ul>
139 * <li>String : Content is printed directly.</li>
140 * <li>template : Content is included from specified URL. Value is used as
141 * an URL.</li>
142 * <li>definition : Value denote a definition defined in factory (xml
143 * file). Definition will be searched in the inserted tile, in a
144 * <code><insert attribute="attributeName"></code> tag, where
145 * 'attributeName' is the name used for this tag.</li>
146 * </ul>
147 *
148 * @param type The attribute type.
149 */
150 public void setType(String type) {
151 this.type = type;
152 }
153
154 /*** {@inheritDoc} */
155 @Override
156 protected void reset() {
157 super.reset();
158 role = null;
159 value = null;
160 type = null;
161 }
162
163 /***
164 * Save the body content of this tag (if any).
165 *
166 * @return It returns <code>SKIP_BODY</code>.
167 */
168 public int doAfterBody() {
169 if (value == null && bodyContent != null) {
170 value = bodyContent.getString();
171 type = "string";
172 }
173 return (SKIP_BODY);
174 }
175
176 /*** {@inheritDoc} */
177 public int doEndTag() throws TilesJspException {
178 if (isAccessAllowed()) {
179 execute();
180 }
181
182 return EVAL_PAGE;
183 }
184
185 /*** {@inheritDoc} */
186 public void processNestedDefinitionName(String definitionName) {
187 value = definitionName;
188 if (type == null) {
189 type = "definition";
190 }
191 }
192
193 /***
194 * Executes the processing of this tag, calling its parent tag.
195 *
196 * @throws TilesJspException If something goes wrong during execution.
197 */
198 protected void execute() throws TilesJspException {
199 AddAttributeTagParent parent = (AddAttributeTagParent)
200 TagSupport.findAncestorWithClass(this, AddAttributeTagParent.class);
201
202 if (parent == null) {
203 throw new TilesJspException(
204 "Error: no enclosing tag accepts 'addAttribute' tag.");
205 }
206
207 parent.processNestedTag(this);
208 }
209
210 /***
211 * Checks if the user is inside the specified role.
212 *
213 * @return <code>true</code> if the user is allowed to have the tag
214 * rendered.
215 */
216 protected boolean isAccessAllowed() {
217 HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
218 return (role == null || req.isUserInRole(role));
219 }
220 }