1 /*
2 * $Id: RenderTagSupport.java 647249 2008-04-11 18:02:32Z apetrelli $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.tiles.jsp.taglib;
22
23 import org.apache.tiles.Attribute;
24 import org.apache.tiles.TilesException;
25 import org.apache.tiles.Attribute.AttributeType;
26
27 import javax.servlet.jsp.JspException;
28 import java.io.IOException;
29
30 /**
31 * <p>
32 * Support for all tags which render (a template, or definition).
33 * </p>
34 * <p>
35 * Properly invokes the defined preparer and invokes the abstract render method
36 * upon completion.
37 * </p>
38 * This tag takes special care to ensure that the attribute context is reset to
39 * it's original state after the execution of the tag is complete. This ensures
40 * that all all included attributes in subsequent tiles are scoped properly and
41 * do not bleed outside their intended scope.
42 *
43 * @since Tiles 2.0
44 * @version $Rev: 647249 $ $Date: 2008-04-11 20:02:32 +0200 (Fri, 11 Apr 2008) $
45 */
46 public abstract class RenderTagSupport extends ContainerTagSupport
47 implements PutAttributeTagParent {
48
49 /**
50 * The view preparer to use before the rendering.
51 */
52 protected String preparer;
53
54 /**
55 * This flag, if <code>true</code>, flushes the content before rendering.
56 */
57 protected boolean flush;
58
59 /**
60 * This flag, if <code>true</code>, ignores exception thrown by preparers
61 * and those caused by problems with definitions.
62 */
63 protected boolean ignore;
64
65 /**
66 * Returns the preparer name.
67 *
68 * @return The preparer name.
69 */
70 public String getPreparer() {
71 return preparer;
72 }
73
74 /**
75 * Sets the preparer name.
76 *
77 * @param preparer The preparer name.
78 */
79 public void setPreparer(String preparer) {
80 this.preparer = preparer;
81 }
82
83 /**
84 * Returns the flush flag. If <code>true</code>, current page out stream
85 * is flushed before insertion.
86 *
87 * @return The flush flag.
88 */
89 public boolean isFlush() {
90 return flush;
91 }
92
93 /**
94 * Sets the flush flag. If <code>true</code>, current page out stream
95 * is flushed before insertion.
96 *
97 * @param flush The flush flag.
98 */
99 public void setFlush(boolean flush) {
100 this.flush = flush;
101 }
102
103 /**
104 * Returns the ignore flag. If it is set to true, and the attribute
105 * specified by the name does not exist, simply return without writing
106 * anything. The default value is false, which will cause a runtime
107 * exception to be thrown.
108 *
109 * @return The ignore flag.
110 */
111 public boolean isIgnore() {
112 return ignore;
113 }
114
115 /**
116 * Sets the ignore flag. If this attribute is set to true, and the attribute
117 * specified by the name does not exist, simply return without writing
118 * anything. The default value is false, which will cause a runtime
119 * exception to be thrown.
120 *
121 * @param ignore The ignore flag.
122 */
123 public void setIgnore(boolean ignore) {
124 this.ignore = ignore;
125 }
126
127
128 /** {@inheritDoc} */
129 public void release() {
130 preparer = null;
131 flush = false;
132 ignore = false;
133 super.release();
134 }
135
136 /** {@inheritDoc} */
137 public int doStartTag() throws JspException {
138 super.doStartTag();
139 return isAccessAllowed() ? EVAL_BODY_BUFFERED : SKIP_BODY;
140 }
141
142 /**
143 * Execute the tag by invoking the preparer, if defined, and then
144 * rendering.
145 *
146 * @throws TilesException if a prepare or render exception occurs.
147 * @throws JspException if a jsp exception occurs.
148 * @throws IOException if an io exception occurs.
149 */
150 protected void execute() throws TilesException, JspException, IOException {
151 if (preparer != null) {
152 container.prepare(preparer, pageContext);
153 }
154 render();
155 if (flush) {
156 pageContext.getOut().flush();
157 }
158 }
159
160 /**
161 * Render the specified content.
162 *
163 * @throws TilesException if a prepare or render exception occurs.
164 * @throws JspException if a jsp exception occurs.
165 * @throws IOException if an io exception occurs.
166 */
167 protected abstract void render() throws JspException, TilesException, IOException;
168
169 /**
170 * <p>
171 * Process nested ≶put> tag.
172 * <p/>
173 * <p>
174 * Places the value of the nested tag within the
175 * {@link org.apache.tiles.AttributeContext}.It is the responsibility
176 * of the descendent to check security. Tags extending
177 * the {@link ContainerTagSupport} will automatically provide
178 * the appropriate security.
179 * </p>
180 *
181 * @param nestedTag the put tag desciendent.
182 */
183 public void processNestedTag(PutAttributeTag nestedTag) {
184 Attribute attribute = new Attribute(
185 nestedTag.getValue(), nestedTag.getRole(),
186 AttributeType.getType(nestedTag.getType()));
187
188 attributeContext.putAttribute(
189 nestedTag.getName(),
190 attribute
191 );
192 }
193 }