1/*2 * $Id: AddAttributeTag.java 739526 2009-01-31 08:50:58Z 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.jsp.taglib;
2324import javax.servlet.http.HttpServletRequest;
25import javax.servlet.jsp.tagext.TagSupport;
2627import org.apache.tiles.jsp.taglib.definition.DefinitionTagParent;
2829/***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 by43 * 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 is48 * <insert>, role is checked immediately. If enclosing tag is49 * <definition>, role will be checked when this definition will be50 * inserted.</li>51 * </ul></p>52 * <p>Value can also come from tag body. Tag body is taken into account only if53 * value is not set by one of the tag attributes. In this case Attribute type is54 * "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 */58publicclassAddAttributeTagextendsTilesBodyTag implements DefinitionTagParent {
5960/***61 * The role to check. If the user is in the specified role, the tag is taken62 * into account; otherwise, the tag is ignored (skipped).63 */64protected String role;
6566/***67 * Associated attribute value.68 */69private Object value = null;
7071/***72 * Requested type for the value.73 */74private String type = null;
7576/***77 * Returns the role to check. If the user is in the specified role, the tag is78 * taken into account; otherwise, the tag is ignored (skipped).79 *80 * @return The role to check.81 */82public String getRole() {
83return role;
84 }
8586/***87 * Sets the role to check. If the user is in the specified role, the tag is88 * taken into account; otherwise, the tag is ignored (skipped).89 *90 * @param role The role to check.91 */92publicvoid setRole(String role) {
93this.role = role;
94 }
9596/***97 * Returns the attribute value.98 *99 * @return Attribute value. Can be a String or Object.100 */101public Object getValue() {
102return value;
103 }
104105/***106 * Sets the attribute value.107 *108 * @param value Attribute value. Can be a String or Object.109 */110publicvoid setValue(Object value) {
111this.value = value;
112 }
113114/***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 as121 * an URL.</li>122 * <li>definition : Value denote a definition defined in factory (xml123 * file). Definition will be searched in the inserted tile, in a124 * <code><insert attribute="attributeName"></code> tag, where125 * 'attributeName' is the name used for this tag.</li>126 * </ul>127 *128 * @return The attribute type.129 */130public String getType() {
131return type;
132 }
133134/***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 as141 * an URL.</li>142 * <li>definition : Value denote a definition defined in factory (xml143 * file). Definition will be searched in the inserted tile, in a144 * <code><insert attribute="attributeName"></code> tag, where145 * 'attributeName' is the name used for this tag.</li>146 * </ul>147 *148 * @param type The attribute type.149 */150publicvoid setType(String type) {
151this.type = type;
152 }
153154/*** {@inheritDoc} */155 @Override
156protectedvoid reset() {
157super.reset();
158 role = null;
159 value = null;
160 type = null;
161 }
162163/***164 * Save the body content of this tag (if any).165 *166 * @return It returns <code>SKIP_BODY</code>.167 */168publicint doAfterBody() {
169if (value == null && bodyContent != null) {
170 value = bodyContent.getString();
171 type = "string";
172 }
173return (SKIP_BODY);
174 }
175176/*** {@inheritDoc} */177publicint doEndTag() throws TilesJspException {
178if (isAccessAllowed()) {
179 execute();
180 }
181182return EVAL_PAGE;
183 }
184185/*** {@inheritDoc} */186publicvoid processNestedDefinitionName(String definitionName) {
187 value = definitionName;
188if (type == null) {
189 type = "definition";
190 }
191 }
192193/***194 * Executes the processing of this tag, calling its parent tag.195 *196 * @throws TilesJspException If something goes wrong during execution.197 */198protectedvoid execute() throws TilesJspException {
199AddAttributeTagParent parent = (AddAttributeTagParent)
200 TagSupport.findAncestorWithClass(this, AddAttributeTagParent.class);
201202if (parent == null) {
203thrownewTilesJspException(
204"Error: no enclosing tag accepts 'addAttribute' tag.");
205 }
206207 parent.processNestedTag(this);
208 }
209210/***211 * Checks if the user is inside the specified role.212 *213 * @return <code>true</code> if the user is allowed to have the tag214 * rendered.215 */216protectedboolean isAccessAllowed() {
217 HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
218return (role == null || req.isUserInRole(role));
219 }
220 }