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

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.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  */
76400public 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 {
9137    int result = 0;
92     try {
9337      init();
9437      result = doStart();
9535    } catch (IOException ex) {
961      throw new TagException(ex);
97     } catch (ServletException ex) {
981      throw new TagException(ex);
99     }
10035    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 {
11012    int result = 0;
111     try {
11212      result = doEnd();
11310      cleanup();
11410    } catch (IOException ex) {
1151      throw new TagException(ex);
116     } catch (ServletException ex) {
1171      throw new TagException(ex);
118     }
11910    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
12939  }
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 {
1402    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 {
1523    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() {
16127    if (this.parameterMap != null) {
1621      this.parameterMap.clear();
163     }
16427    if (this.attributeMap != null) {
1652      this.attributeMap.clear();
166     }
16727  }
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) {
1769    getParameterMap().put(name, value);
1779  }
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) {
1865    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() {
1953    return getParameterMap().keySet().iterator();
196   }
197  
198   /**
199    * Lazy loads the parameter map.
200    *
201    * @return The parameter map.
202    */
203   private Map getParameterMap() {
20417    if (this.parameterMap == null) {
2058      this.parameterMap = new HashMap();
206     }
20717    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) {
21852    getAttributeMap().put(name, value);
21952  }
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) {
22869    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() {
23722    return getAttributeMap().keySet().iterator();
238   }
239  
240   /**
241    * Lazy loads the attribute map.
242    *
243    * @return The attribute map.
244    */
245   private Map getAttributeMap() {
246143    if (this.attributeMap == null) {
24766      this.attributeMap = new HashMap();
248     }
249143    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:
25919    JspWriter out = pageContext.getOut();
26019    Iterator attributes = getAttributeNames();
26140    while (attributes.hasNext()) {
2622      String attribute = (String) attributes.next();
2632      String value = getAttribute(attribute);
2642      out.print(" ");
2652      out.print(attribute);
2662      out.print("='");
2672      out.print(value);
2682      out.print("'");
269     }
27019  }
271 }

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.