This project has retired. For details please refer to its Attic page.
GetAsStringTag xref
View Javadoc

1   /*
2    * $Id: GetAsStringTag.java 929598 2010-03-31 15:53:02Z 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 java.io.IOException;
24  
25  import javax.servlet.jsp.JspContext;
26  import javax.servlet.jsp.JspException;
27  import javax.servlet.jsp.JspWriter;
28  import javax.servlet.jsp.tagext.SimpleTagSupport;
29  
30  import org.apache.tiles.Attribute;
31  import org.apache.tiles.jsp.context.JspUtil;
32  import org.apache.tiles.template.DefaultAttributeResolver;
33  import org.apache.tiles.template.GetAsStringModel;
34  
35  /***
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   */
42  public class GetAsStringTag extends SimpleTagSupport {
43  
44      /***
45       * The template model.
46       */
47      private GetAsStringModel model = new GetAsStringModel(
48              new DefaultAttributeResolver());
49  
50      /***
51       * Name to insert.
52       */
53      private String name;
54  
55      /***
56       * The value of the attribute.
57       */
58      private Object value = null;
59  
60      /***
61       * This value is evaluated only if <code>value</code> is null and the
62       * attribute with the associated <code>name</code> is null.
63       *
64       * @since 2.1.2
65       */
66      private Object defaultValue;
67  
68      /***
69       * The type of the {@link #defaultValue}, if it is a string.
70       *
71       * @since 2.1.2
72       */
73      private String defaultValueType;
74  
75      /***
76       * The role to check for the default value. If the user is in the specified
77       * role, the default value is taken into account; otherwise, it is ignored
78       * (skipped).
79       *
80       * @since 2.1.2
81       */
82      private String defaultValueRole;
83  
84      /***
85       * The role to check. If the user is in the specified role, the tag is taken
86       * into account; otherwise, the tag is ignored (skipped).
87       *
88       * @since 2.1.1
89       */
90      private String role;
91  
92      /***
93       * The view preparer to use before the rendering.
94       *
95       * @since 2.1.1
96       */
97      private String preparer;
98  
99      /***
100      * This flag, if <code>true</code>, flushes the content after rendering.
101      *
102      * @since 2.1.1
103      */
104     private boolean flush;
105 
106     /***
107      * This flag, if <code>true</code>, ignores exception thrown by preparers
108      * and those caused by problems with definitions.
109      *
110      * @since 2.1.1
111      */
112     private boolean ignore;
113 
114     /***
115      * Sets the name of the attribute.
116      *
117      * @param value The name of the attribute.
118      */
119     public void setName(String value) {
120         this.name = value;
121     }
122 
123     /***
124      * Returns  the name of the attribute.
125      *
126      * @return The name of the attribute.
127      */
128     public String getName() {
129         return name;
130     }
131 
132     /***
133      * Get the value.
134      *
135      * @return The value.
136      */
137     public Object getValue() {
138         return value;
139     }
140 
141     /***
142      * Set the value.
143      *
144      * @param value The new value
145      */
146     public void setValue(Object value) {
147         this.value = value;
148     }
149 
150     /***
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      */
156     public Object getDefaultValue() {
157         return defaultValue;
158     }
159 
160     /***
161      * Sets the default value, that is evaluated only if <code>value</code> is
162      * null and the attribute with the associated <code>name</code> is null.
163      *
164      * @param defaultValue The default value to set.
165      */
166     public void setDefaultValue(Object defaultValue) {
167         this.defaultValue = defaultValue;
168     }
169 
170     /***
171      * Returns the default value type. It will be used only if
172      * {@link #getDefaultValue()} is a string.
173      *
174      * @return The default value type.
175      */
176     public String getDefaultValueType() {
177         return defaultValueType;
178     }
179 
180     /***
181      * Sets the default value type. To be used in conjunction with
182      * {@link #setDefaultValue(Object)} when passing a string.
183      *
184      * @param defaultValueType The default value type.
185      */
186     public void setDefaultValueType(String defaultValueType) {
187         this.defaultValueType = defaultValueType;
188     }
189 
190     /***
191      * Returns the role to check for the default value. If the user is in the specified
192      * role, the default value is taken into account; otherwise, it is ignored
193      * (skipped).
194      *
195      * @return The default value role.
196      */
197     public String getDefaultValueRole() {
198         return defaultValueRole;
199     }
200 
201     /***
202      * Sets the role to check for the default value. If the user is in the specified
203      * role, the default value is taken into account; otherwise, it is ignored
204      * (skipped).
205      *
206      * @param defaultValueRole The default value role.
207      */
208     public void setDefaultValueRole(String defaultValueRole) {
209         this.defaultValueRole = defaultValueRole;
210     }
211 
212     /***
213      * Returns the role to check. If the user is in the specified role, the tag is
214      * taken into account; otherwise, the tag is ignored (skipped).
215      *
216      * @return The role to check.
217      * @since 2.1.1
218      */
219     public String getRole() {
220         return role;
221     }
222 
223     /***
224      * Sets the role to check. If the user is in the specified role, the tag is
225      * taken into account; otherwise, the tag is ignored (skipped).
226      *
227      * @param role The role to check.
228      * @since 2.1.1
229      */
230     public void setRole(String role) {
231         this.role = role;
232     }
233 
234     /***
235      * Returns the preparer name.
236      *
237      * @return The preparer name.
238      * @since 2.1.1
239      */
240     public String getPreparer() {
241         return preparer;
242     }
243 
244     /***
245      * Sets the preparer name.
246      *
247      * @param preparer The preparer name.
248      * @since 2.1.1
249      */
250     public void setPreparer(String preparer) {
251         this.preparer = preparer;
252     }
253 
254     /***
255      * Returns the flush flag. If <code>true</code>, current page out stream
256      * is flushed after insertion.
257      *
258      * @return The flush flag.
259      * @since 2.1.1
260      */
261     public boolean isFlush() {
262         return flush;
263     }
264 
265     /***
266      * Sets the flush flag. If <code>true</code>, current page out stream
267      * is flushed after insertion.
268      *
269      * @param flush The flush flag.
270      * @since 2.1.1
271      */
272     public void setFlush(boolean flush) {
273         this.flush = flush;
274     }
275 
276     /***
277      * Returns the ignore flag. If it is set to true, and the attribute
278      * specified by the name does not exist, simply return without writing
279      * anything. The default value is false, which will cause a runtime
280      * exception to be thrown.
281      *
282      * @return The ignore flag.
283      * @since 2.1.1
284      */
285     public boolean isIgnore() {
286         return ignore;
287     }
288 
289     /***
290      * Sets the ignore flag. If this attribute is set to true, and the attribute
291      * specified by the name does not exist, simply return without writing
292      * anything. The default value is false, which will cause a runtime
293      * exception to be thrown.
294      *
295      * @param ignore The ignore flag.
296      * @since 2.1.1
297      */
298     public void setIgnore(boolean ignore) {
299         this.ignore = ignore;
300     }
301 
302     /*** {@inheritDoc} */
303     @Override
304     public void doTag() throws JspException, IOException {
305         JspContext jspContext = getJspContext();
306         model.start(JspUtil.getComposeStack(jspContext), JspUtil
307                 .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), JspUtil
313                 .getCurrentContainer(jspContext), writer, ignore,
314                 jspContext);
315         if(isFlush()){
316             writer.flush();
317         }
318     }
319 }