Coverage details for org.chwf.taglib.base.LifeCycleBodyTagSupport

LineHitsSource
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.BodyTagSupport;
44  
45 import org.chwf.servlet.ServletUtils;
46  
47 /**
48  * <p>As {@link LifeCycleTagSupport} but for body tags.</p>
49  *
50  * <p>Superclass to address a shortcoming in the JSP custom tag specifications,
51  * namely the inadequate life-cycle definitions with regard to tag pooling.
52  * The specifications include a <code>release()</code> method, but it is the
53  * equivalent to the servlet <code>destroy()</code> method; it is only
54  * called during application shutdown.</p>
55  *
56  * <p>More frequently, a tag will need a lifecycle method to cleanup information
57  * when the tag is returned to the tag pool. For example, tag instance variables
58  * may need to revert to default values for the next tag use. This class defines
59  * the following extended lifecycle methods:</p>
60  *
61  * <ul>
62  * <li><code>init()</code>: Called before doStart().</li>
63  * <li><code>doStart()</code>: Analogous to doStartTag().</li>
64  * <li><code>doEnd()</code>: Analogous to doEndTag().</li>
65  * <li><code>cleanup()</code>: Called after doEnd().</li>
66  * </ul>
67  *
68  * <p>In addition, the <code>doStart()</code> and <code>doEnd()</code> methods
69  * may throw an <code>IOException</code> or a <code>ServletException</code>
70  * as well as a <code>JspException</code>. Note that if an exception is thrown
71  * by the tag handler, this tag does <i>not</i> guarantee that the
72  * <code>cleanup()</code> method will be called. This should be safe, though,
73  * because it is likely the tag pool will discard tag handler in that case.</p>
74  *
75  * <p>Finally, this class implements the <code>Parameterized</code> interface
76  * to support generic manipulation of tag parameters and attributes.</p>
77  *
78  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
79  */
80102public class LifeCycleBodyTagSupport
81   extends BodyTagSupport
82   implements Parameterized {
83  
84   /** Map for accumulating parameters. */
85   private Map parameterMap;
86     
87   /** Map for accumulating pass-through attributes. */
88   private Map attributeMap;
89  
90   /**
91    * Calls init() then doStart().
92    *
93    * @return The return value of doStart().
94    * @throws JspException For Jsp, IO and Servlet exceptions.
95    */
96   public final int doStartTag() throws JspException {
973    int result = 0;
98     try {
993      init();
1003      result = doStart();
1011    } catch (IOException ex) {
1021      throw new TagException(ex);
103     } catch (ServletException ex) {
1041      throw new TagException(ex);
105     }
1061    return result;
107   }
108  
109   /**
110    * Calls doEnd() then cleanup().
111    *
112    * @return The return value of doEnd().
113    * @throws JspException For Jsp, IO and Servlet exceptions.
114    */
115   public final int doEndTag() throws JspException {
1163    int result = 0;
117     try {
1183      result = doEnd();
1191      cleanup();
1201    } catch (IOException ex) {
1211      throw new TagException(ex);
122     } catch (ServletException ex) {
1231      throw new TagException(ex);
124     }
1251    return result;
126   }
127  
128   /**
129    * Method for tag initialization. Should be overridden in tag handler.
130    *
131    * @throws JspException For errors.
132    */
133   public void init() throws JspException {
134     // Do nothing in base class
1353  }
136  
137   /**
138    * Method for tag do-start logic. Should be overridden in tag handler.
139    *
140    * @return EVAL_BODY_TAG by default.
141    * @throws JspException For errors.
142    * @throws IOException For errors.
143    * @throws ServletException For errors.
144    */
145   public int doStart() throws JspException, IOException, ServletException {
1462    return ServletUtils.EVAL_BODY_TAG;
147   }
148  
149   /**
150    * Method for tag do-end logic. Should be overridden in tag handler.
151    *
152    * @return EVAL_PAGE by default.
153    * @throws JspException For errors.
154    * @throws IOException For errors.
155    * @throws ServletException For errors.
156    */
157   public int doEnd() throws JspException, IOException, ServletException {
1582    return EVAL_PAGE;
159   }
160  
161   /**
162    * Clean up data for tag handler reuse and pooling. Should be overridden in
163    * tag handler. Subclasses should always invoke their superclass's cleanup
164    * method: <code>super.cleanup()</code>
165    */
166   public void cleanup() {
1677    if (this.parameterMap != null) {
1681      this.parameterMap.clear();
169     }
1707    if (this.attributeMap != null) {
1711      this.attributeMap.clear();
172     }
1737  }
174  
175   /**
176    * Add a tag parameter.
177    *
178    * @param name The parameter name.
179    * @param value The parameter value.
180    */
181   public void addParameter(String name, String value) {
18212    getParameterMap().put(name, value);
18312  }
184  
185   /**
186    * Returns parameter value.
187    *
188    * @param name The parameter name.
189    * @return The parameter value.
190    */
191   public String getParameter(String name) {
19212    return getParameterMap().get(name).toString();
193   }
194  
195   /**
196    * Returns iterator for all parameter names.
197    *
198    * @return An iterator for parameter names.
199    */
200   public Iterator getParameterNames() {
20113    return getParameterMap().keySet().iterator();
202   }
203  
204   /**
205    * Lazy loads the parameter map.
206    *
207    * @return The parameter map.
208    */
209   private Map getParameterMap() {
21037    if (this.parameterMap == null) {
21116      this.parameterMap = new HashMap();
212     }
21337    return parameterMap;
214   }
215  
216   /**
217    * Add a tag attribute. This method is used to manipulate generic or
218    * pass-through attributes.
219    *
220    * @param name The attribute name.
221    * @param value The attribute value.
222    */
223   public void addAttribute(String name, String value) {
22412    getAttributeMap().put(name, value);
22512  }
226  
227   /**
228    * Returns attribute value.
229    *
230    * @param name The attribute name.
231    * @return The attribute value.
232    */
233   public String getAttribute(String name) {
23410    return (String) getAttributeMap().get(name);
235   }
236  
237   /**
238    * Returns iterator for all attribute names.
239    *
240    * @return An iterator for attribute names.
241    */
242   public Iterator getAttributeNames() {
2439    return getAttributeMap().keySet().iterator();
244   }
245  
246   /**
247    * Lazy loads the attribute map.
248    *
249    * @return The attribute map.
250    */
251   private Map getAttributeMap() {
25231    if (this.attributeMap == null) {
25317      this.attributeMap = new HashMap();
254     }
25531    return attributeMap;
256   }
257  
258   /**
259    * Print attributes as [name='value'] pairs.
260    *
261    * @throws IOException For write errors.
262    */
263   public void printAttributes() throws IOException {
264     // Print attributes:
2656    JspWriter out = pageContext.getOut();
2666    Iterator attributes = getAttributeNames();
26713    while (attributes.hasNext()) {
2681      String attribute = (String) attributes.next();
2691      String value = getAttribute(attribute);
2701      out.print(" ");
2711      out.print(attribute);
2721      out.print("='");
2731      out.print(value);
2741      out.print("'");
275     }
2766  }
277 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.