1/*2 * $Id: InsertDefinitionTag.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 */2122package org.apache.tiles.jsp.taglib;
2324import java.io.IOException;
2526import javax.servlet.jsp.JspContext;
27import javax.servlet.jsp.JspException;
28import javax.servlet.jsp.tagext.SimpleTagSupport;
2930import org.apache.tiles.jsp.context.JspUtil;
31import org.apache.tiles.template.InsertDefinitionModel;
3233/***34 * This is the tag handler for <tiles:insertDefinition>, which includes a35 * name, eventually overriding or filling attributes of its template.36 *37 * @version $Rev: 929598 $ $Date: 2010-03-31 17:53:02 +0200 (mer, 31 mar 2010) $38 */39publicclassInsertDefinitionTagextends SimpleTagSupport {
4041/***42 * The template model.43 */44private InsertDefinitionModel model = new InsertDefinitionModel();
4546/***47 * The definition name.48 */49private String name;
5051/***52 * The role to check. If the user is in the specified role, the tag is taken53 * into account; otherwise, the tag is ignored (skipped).54 *55 * @since 2.1.156 */57private String role;
5859/***60 * The view preparer to use before the rendering.61 *62 * @since 2.1.163 */64private String preparer;
6566/***67 * This flag, if <code>true</code>, flushes the content after rendering.68 *69 * @since 2.1.170 */71privateboolean flush;
7273/***74 * This flag, if <code>true</code>, ignores exception thrown by preparers75 * and those caused by problems with definitions.76 *77 * @since 2.1.178 */79privateboolean ignore;
8081/***82 * A string representing the URI of a template (for example, a JSP page).83 *84 * @since 2.1.085 */86private String template;
8788/***89 * The type of the template attribute.90 *91 * @since 2.2.092 */93private String templateType;
9495/***96 * The expression to evaluate to get the value of the template.97 *98 * @since 2.2.099 */100private String templateExpression;
101102/***103 * Returns the name of the definition to insert.104 *105 * @return The name of the definition.106 */107public String getName() {
108return name;
109 }
110111/***112 * Sets the name of the definition to insert.113 *114 * @param name The name of the definition.115 */116publicvoid setName(String name) {
117this.name = name;
118 }
119120/***121 * Returns the role to check. If the user is in the specified role, the tag is122 * taken into account; otherwise, the tag is ignored (skipped).123 *124 * @return The role to check.125 * @since 2.1.1126 */127public String getRole() {
128return role;
129 }
130131/***132 * Sets the role to check. If the user is in the specified role, the tag is133 * taken into account; otherwise, the tag is ignored (skipped).134 *135 * @param role The role to check.136 * @since 2.1.1137 */138publicvoid setRole(String role) {
139this.role = role;
140 }
141142/***143 * Returns the preparer name.144 *145 * @return The preparer name.146 * @since 2.1.1147 */148public String getPreparer() {
149return preparer;
150 }
151152/***153 * Sets the preparer name.154 *155 * @param preparer The preparer name.156 * @since 2.1.1157 */158publicvoid setPreparer(String preparer) {
159this.preparer = preparer;
160 }
161162/***163 * Returns the flush flag. If <code>true</code>, current page out stream164 * is flushed after insertion.165 *166 * @return The flush flag.167 * @since 2.1.1168 */169publicboolean isFlush() {
170return flush;
171 }
172173/***174 * Sets the flush flag. If <code>true</code>, current page out stream175 * is flushed after insertion.176 *177 * @param flush The flush flag.178 * @since 2.1.1179 */180publicvoid setFlush(boolean flush) {
181this.flush = flush;
182 }
183184/***185 * Returns the ignore flag. If it is set to true, and the attribute186 * specified by the name does not exist, simply return without writing187 * anything. The default value is false, which will cause a runtime188 * exception to be thrown.189 *190 * @return The ignore flag.191 * @since 2.1.1192 */193publicboolean isIgnore() {
194return ignore;
195 }
196197/***198 * Sets the ignore flag. If this attribute is set to true, and the attribute199 * specified by the name does not exist, simply return without writing200 * anything. The default value is false, which will cause a runtime201 * exception to be thrown.202 *203 * @param ignore The ignore flag.204 * @since 2.1.1205 */206publicvoid setIgnore(boolean ignore) {
207this.ignore = ignore;
208 }
209210/***211 * Returns a string representing the URI of a template (for example, a JSP212 * page).213 *214 * @return The template URI.215 * @since 2.1.0216 */217public String getTemplate() {
218return template;
219 }
220221/***222 * Sets a string representing the URI of a template (for example, a JSP223 * page).224 *225 * @param template The template URI.226 * @since 2.1.0227 */228publicvoid setTemplate(String template) {
229this.template = template;
230 }
231232/***233 * Returns the type of the template attribute.234 *235 * @return The template type.236 * @since 2.2.0237 */238public String getTemplateType() {
239return templateType;
240 }
241242/***243 * Sets the type of the template attribute.244 *245 * @param templateType The template type.246 * @since 2.2.0247 */248publicvoid setTemplateType(String templateType) {
249this.templateType = templateType;
250 }
251252/***253 * Returns the expression to evaluate to get the value of the template.254 *255 * @return The template expression.256 * @since 2.2.0257 */258public String getTemplateExpression() {
259return templateExpression;
260 }
261262/***263 * Sets the expression to evaluate to get the value of the template.264 *265 * @param templateExpression The template expression.266 * @since 2.2.0267 */268publicvoid setTemplateExpression(String templateExpression) {
269this.templateExpression = templateExpression;
270 }
271272/*** {@inheritDoc} */273 @Override
274publicvoid doTag() throws JspException, IOException {
275 JspContext jspContext = getJspContext();
276 model.start(JspUtil.getCurrentContainer(jspContext), jspContext);
277 JspUtil.evaluateFragment(getJspBody());
278 model.end(JspUtil.getCurrentContainer(jspContext), name, template,
279 templateType, templateExpression, role, preparer, jspContext);
280if(isFlush()){
281 jspContext.getOut().flush();
282 }
283 }
284 }