1/*2 * $Id: GetAsStringTag.java 929598 2010-03-31 15:53:02Z 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 */21package org.apache.tiles.jsp.taglib;
2223import java.io.IOException;
2425import javax.servlet.jsp.JspContext;
26import javax.servlet.jsp.JspException;
27import javax.servlet.jsp.JspWriter;
28import javax.servlet.jsp.tagext.SimpleTagSupport;
2930import org.apache.tiles.Attribute;
31import org.apache.tiles.jsp.context.JspUtil;
32import org.apache.tiles.template.DefaultAttributeResolver;
33import org.apache.tiles.template.GetAsStringModel;
3435/***36 * Retrieve the value of the specified definition/template attribute property,37 * and render it to the current JspWriter as a String.38 * The usual toString() conversion is applied on the found value.39 *40 * @version $Rev: 929598 $ $Date: 2010-03-31 17:53:02 +0200 (mer, 31 mar 2010) $41 */42publicclassGetAsStringTagextends SimpleTagSupport {
4344/***45 * The template model.46 */47private GetAsStringModel model = new GetAsStringModel(
48new DefaultAttributeResolver());
4950/***51 * Name to insert.52 */53private String name;
5455/***56 * The value of the attribute.57 */58private Object value = null;
5960/***61 * This value is evaluated only if <code>value</code> is null and the62 * attribute with the associated <code>name</code> is null.63 *64 * @since 2.1.265 */66private Object defaultValue;
6768/***69 * The type of the {@link #defaultValue}, if it is a string.70 *71 * @since 2.1.272 */73private String defaultValueType;
7475/***76 * The role to check for the default value. If the user is in the specified77 * role, the default value is taken into account; otherwise, it is ignored78 * (skipped).79 *80 * @since 2.1.281 */82private String defaultValueRole;
8384/***85 * The role to check. If the user is in the specified role, the tag is taken86 * into account; otherwise, the tag is ignored (skipped).87 *88 * @since 2.1.189 */90private String role;
9192/***93 * The view preparer to use before the rendering.94 *95 * @since 2.1.196 */97private String preparer;
9899/***100 * This flag, if <code>true</code>, flushes the content after rendering.101 *102 * @since 2.1.1103 */104privateboolean flush;
105106/***107 * This flag, if <code>true</code>, ignores exception thrown by preparers108 * and those caused by problems with definitions.109 *110 * @since 2.1.1111 */112privateboolean ignore;
113114/***115 * Sets the name of the attribute.116 *117 * @param value The name of the attribute.118 */119publicvoid setName(String value) {
120this.name = value;
121 }
122123/***124 * Returns the name of the attribute.125 *126 * @return The name of the attribute.127 */128public String getName() {
129return name;
130 }
131132/***133 * Get the value.134 *135 * @return The value.136 */137public Object getValue() {
138return value;
139 }
140141/***142 * Set the value.143 *144 * @param value The new value145 */146publicvoid setValue(Object value) {
147this.value = value;
148 }
149150/***151 * Returns the default value, that is evaluated only if <code>value</code>152 * is null and the attribute with the associated <code>name</code> is null.153 *154 * @return The default value.155 */156public Object getDefaultValue() {
157return defaultValue;
158 }
159160/***161 * Sets the default value, that is evaluated only if <code>value</code> is162 * null and the attribute with the associated <code>name</code> is null.163 *164 * @param defaultValue The default value to set.165 */166publicvoid setDefaultValue(Object defaultValue) {
167this.defaultValue = defaultValue;
168 }
169170/***171 * Returns the default value type. It will be used only if172 * {@link #getDefaultValue()} is a string.173 *174 * @return The default value type.175 */176public String getDefaultValueType() {
177return defaultValueType;
178 }
179180/***181 * Sets the default value type. To be used in conjunction with182 * {@link #setDefaultValue(Object)} when passing a string.183 *184 * @param defaultValueType The default value type.185 */186publicvoid setDefaultValueType(String defaultValueType) {
187this.defaultValueType = defaultValueType;
188 }
189190/***191 * Returns the role to check for the default value. If the user is in the specified192 * role, the default value is taken into account; otherwise, it is ignored193 * (skipped).194 *195 * @return The default value role.196 */197public String getDefaultValueRole() {
198return defaultValueRole;
199 }
200201/***202 * Sets the role to check for the default value. If the user is in the specified203 * role, the default value is taken into account; otherwise, it is ignored204 * (skipped).205 *206 * @param defaultValueRole The default value role.207 */208publicvoid setDefaultValueRole(String defaultValueRole) {
209this.defaultValueRole = defaultValueRole;
210 }
211212/***213 * Returns the role to check. If the user is in the specified role, the tag is214 * taken into account; otherwise, the tag is ignored (skipped).215 *216 * @return The role to check.217 * @since 2.1.1218 */219public String getRole() {
220return role;
221 }
222223/***224 * Sets the role to check. If the user is in the specified role, the tag is225 * taken into account; otherwise, the tag is ignored (skipped).226 *227 * @param role The role to check.228 * @since 2.1.1229 */230publicvoid setRole(String role) {
231this.role = role;
232 }
233234/***235 * Returns the preparer name.236 *237 * @return The preparer name.238 * @since 2.1.1239 */240public String getPreparer() {
241return preparer;
242 }
243244/***245 * Sets the preparer name.246 *247 * @param preparer The preparer name.248 * @since 2.1.1249 */250publicvoid setPreparer(String preparer) {
251this.preparer = preparer;
252 }
253254/***255 * Returns the flush flag. If <code>true</code>, current page out stream256 * is flushed after insertion.257 *258 * @return The flush flag.259 * @since 2.1.1260 */261publicboolean isFlush() {
262return flush;
263 }
264265/***266 * Sets the flush flag. If <code>true</code>, current page out stream267 * is flushed after insertion.268 *269 * @param flush The flush flag.270 * @since 2.1.1271 */272publicvoid setFlush(boolean flush) {
273this.flush = flush;
274 }
275276/***277 * Returns the ignore flag. If it is set to true, and the attribute278 * specified by the name does not exist, simply return without writing279 * anything. The default value is false, which will cause a runtime280 * exception to be thrown.281 *282 * @return The ignore flag.283 * @since 2.1.1284 */285publicboolean isIgnore() {
286return ignore;
287 }
288289/***290 * Sets the ignore flag. If this attribute is set to true, and the attribute291 * specified by the name does not exist, simply return without writing292 * anything. The default value is false, which will cause a runtime293 * exception to be thrown.294 *295 * @param ignore The ignore flag.296 * @since 2.1.1297 */298publicvoid setIgnore(boolean ignore) {
299this.ignore = ignore;
300 }
301302/*** {@inheritDoc} */303 @Override
304publicvoid doTag() throws JspException, IOException {
305 JspContext jspContext = getJspContext();
306 model.start(JspUtil.getComposeStack(jspContext), JspUtil307 .getCurrentContainer(jspContext), ignore, preparer, role,
308 defaultValue, defaultValueRole, defaultValueType, name,
309 (Attribute) value, jspContext);
310 JspWriter writer = jspContext.getOut();
311 JspUtil.evaluateFragment(getJspBody());
312 model.end(JspUtil.getComposeStack(jspContext), JspUtil313 .getCurrentContainer(jspContext), writer, ignore,
314 jspContext);
315if(isFlush()){
316 writer.flush();
317 }
318 }
319 }