Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.taglib.base; | |
34 | ||
35 | import java.io.IOException; | |
36 | import java.util.HashMap; | |
37 | import java.util.Iterator; | |
38 | import java.util.Map; | |
39 | ||
40 | import javax.servlet.ServletException; | |
41 | import javax.servlet.jsp.JspException; | |
42 | import javax.servlet.jsp.JspWriter; | |
43 | import javax.servlet.jsp.tagext.TagSupport; | |
44 | ||
45 | /** | |
46 | * <p>Superclass to address a shortcoming in the JSP custom tag specifications, | |
47 | * namely the inadequate life-cycle definitions with regard to tag pooling. | |
48 | * The specifications include a <code>release()</code> method, but it is the | |
49 | * equivalent to the servlet <code>destroy()</code> method; it is only | |
50 | * called during application shutdown.</p> | |
51 | * | |
52 | * <p>More frequently, a tag will need a lifecycle method to cleanup information | |
53 | * when the tag is returned to the tag pool. For example, tag instance variables | |
54 | * may need to revert to default values for the next tag use. This class defines | |
55 | * the following extended lifecycle methods:</p> | |
56 | * | |
57 | * <ul> | |
58 | * <li><code>init()</code>: Called before doStart().</li> | |
59 | * <li><code>doStart()</code>: Analogous to doStartTag().</li> | |
60 | * <li><code>doEnd()</code>: Analogous to doEndTag().</li> | |
61 | * <li><code>cleanup()</code>: Called after doEnd().</li> | |
62 | * </ul> | |
63 | * | |
64 | * <p>In addition, the <code>doStart()</code> and <code>doEnd()</code> methods | |
65 | * may throw an <code>IOException</code> or a <code>ServletException</code> | |
66 | * as well as a <code>JspException</code>. Note that if an exception is thrown | |
67 | * by the tag handler, this tag does <i>not</i> guarantee that the | |
68 | * <code>cleanup()</code> method will be called. This should be safe, though, | |
69 | * because it is likely the tag pool will discard tag handler in that case.</p> | |
70 | * | |
71 | * <p>Finally, this class implements the <code>Parameterized</code> interface | |
72 | * to support generic manipulation of tag parameters.</p> | |
73 | * | |
74 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
75 | */ | |
76 | 400 | public class LifeCycleTagSupport extends TagSupport implements Parameterized { |
77 | ||
78 | /** Map for accumulating parameters. */ | |
79 | private Map parameterMap; | |
80 | ||
81 | /** Map for accumulating pass-through attributes. */ | |
82 | private Map attributeMap; | |
83 | ||
84 | /** | |
85 | * Calls init() then doStart(). | |
86 | * | |
87 | * @return The return value of doStart(). | |
88 | * @throws JspException For Jsp, IO and Servlet exceptions. | |
89 | */ | |
90 | public final int doStartTag() throws JspException { | |
91 | 37 | int result = 0; |
92 | try { | |
93 | 37 | init(); |
94 | 37 | result = doStart(); |
95 | 35 | } catch (IOException ex) { |
96 | 1 | throw new TagException(ex); |
97 | } catch (ServletException ex) { | |
98 | 1 | throw new TagException(ex); |
99 | } | |
100 | 35 | return result; |
101 | } | |
102 | ||
103 | /** | |
104 | * Calls doEnd() then cleanup(). | |
105 | * | |
106 | * @return The return value of doEnd(). | |
107 | * @throws JspException For Jsp, IO and Servlet exceptions. | |
108 | */ | |
109 | public final int doEndTag() throws JspException { | |
110 | 12 | int result = 0; |
111 | try { | |
112 | 12 | result = doEnd(); |
113 | 10 | cleanup(); |
114 | 10 | } catch (IOException ex) { |
115 | 1 | throw new TagException(ex); |
116 | } catch (ServletException ex) { | |
117 | 1 | throw new TagException(ex); |
118 | } | |
119 | 10 | return result; |
120 | } | |
121 | ||
122 | /** | |
123 | * Method for tag initialization. Should be overridden in tag handler. | |
124 | * | |
125 | * @throws JspException For errors. | |
126 | */ | |
127 | public void init() throws JspException { | |
128 | // Do nothing in base class | |
129 | 39 | } |
130 | ||
131 | /** | |
132 | * Method for tag do-start logic. Should be overridden in tag handler. | |
133 | * | |
134 | * @return EVAL_BODY_INCLUDE by default. | |
135 | * @throws JspException For errors. | |
136 | * @throws IOException For errors. | |
137 | * @throws ServletException For errors. | |
138 | */ | |
139 | public int doStart() throws JspException, IOException, ServletException { | |
140 | 2 | return EVAL_BODY_INCLUDE; |
141 | } | |
142 | ||
143 | /** | |
144 | * Method for tag do-end logic. Should be overridden in tag handler. | |
145 | * | |
146 | * @return EVAL_PAGE by default. | |
147 | * @throws JspException For errors. | |
148 | * @throws IOException For errors. | |
149 | * @throws ServletException For errors. | |
150 | */ | |
151 | public int doEnd() throws JspException, IOException, ServletException { | |
152 | 3 | return EVAL_PAGE; |
153 | } | |
154 | ||
155 | /** | |
156 | * Clean up data for tag handler reuse and pooling. Should be overridden in | |
157 | * tag handler. Subclasses should always invoke their superclass's cleanup | |
158 | * method: <code>super.cleanup()</code> | |
159 | */ | |
160 | public void cleanup() { | |
161 | 27 | if (this.parameterMap != null) { |
162 | 1 | this.parameterMap.clear(); |
163 | } | |
164 | 27 | if (this.attributeMap != null) { |
165 | 2 | this.attributeMap.clear(); |
166 | } | |
167 | 27 | } |
168 | ||
169 | /** | |
170 | * Add a tag parameter. | |
171 | * | |
172 | * @param name The parameter name. | |
173 | * @param value The parameter value. | |
174 | */ | |
175 | public void addParameter(String name, String value) { | |
176 | 9 | getParameterMap().put(name, value); |
177 | 9 | } |
178 | ||
179 | /** | |
180 | * Returns parameter value. | |
181 | * | |
182 | * @param name The parameter name. | |
183 | * @return The parameter value. | |
184 | */ | |
185 | public String getParameter(String name) { | |
186 | 5 | return (String) getParameterMap().get(name); |
187 | } | |
188 | ||
189 | /** | |
190 | * Returns iterator for all parameter names. | |
191 | * | |
192 | * @return An iterator for parameter names. | |
193 | */ | |
194 | public Iterator getParameterNames() { | |
195 | 3 | return getParameterMap().keySet().iterator(); |
196 | } | |
197 | ||
198 | /** | |
199 | * Lazy loads the parameter map. | |
200 | * | |
201 | * @return The parameter map. | |
202 | */ | |
203 | private Map getParameterMap() { | |
204 | 17 | if (this.parameterMap == null) { |
205 | 8 | this.parameterMap = new HashMap(); |
206 | } | |
207 | 17 | return parameterMap; |
208 | } | |
209 | ||
210 | /** | |
211 | * Add a tag attribute. This method is used to manipulate generic or | |
212 | * pass-through attributes. | |
213 | * | |
214 | * @param name The attribute name. | |
215 | * @param value The attribute value. | |
216 | */ | |
217 | public void addAttribute(String name, String value) { | |
218 | 52 | getAttributeMap().put(name, value); |
219 | 52 | } |
220 | ||
221 | /** | |
222 | * Returns attribute value. | |
223 | * | |
224 | * @param name The attribute name. | |
225 | * @return The attribute value. | |
226 | */ | |
227 | public String getAttribute(String name) { | |
228 | 69 | return (String) getAttributeMap().get(name); |
229 | } | |
230 | ||
231 | /** | |
232 | * Returns iterator for all attribute names. | |
233 | * | |
234 | * @return An iterator for attribute names. | |
235 | */ | |
236 | public Iterator getAttributeNames() { | |
237 | 22 | return getAttributeMap().keySet().iterator(); |
238 | } | |
239 | ||
240 | /** | |
241 | * Lazy loads the attribute map. | |
242 | * | |
243 | * @return The attribute map. | |
244 | */ | |
245 | private Map getAttributeMap() { | |
246 | 143 | if (this.attributeMap == null) { |
247 | 66 | this.attributeMap = new HashMap(); |
248 | } | |
249 | 143 | return attributeMap; |
250 | } | |
251 | ||
252 | /** | |
253 | * Print attributes as [name='value'] pairs. | |
254 | * | |
255 | * @throws IOException For write errors. | |
256 | */ | |
257 | public void printAttributes() throws IOException { | |
258 | // Print attributes: | |
259 | 19 | JspWriter out = pageContext.getOut(); |
260 | 19 | Iterator attributes = getAttributeNames(); |
261 | 40 | while (attributes.hasNext()) { |
262 | 2 | String attribute = (String) attributes.next(); |
263 | 2 | String value = getAttribute(attribute); |
264 | 2 | out.print(" "); |
265 | 2 | out.print(attribute); |
266 | 2 | out.print("='"); |
267 | 2 | out.print(value); |
268 | 2 | out.print("'"); |
269 | } | |
270 | 19 | } |
271 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |