 
| 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.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 | */ | |
| 80 | 102 | public 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 { | |
| 97 | 3 | int result = 0; | 
| 98 | try { | |
| 99 | 3 | init(); | 
| 100 | 3 | result = doStart(); | 
| 101 | 1 | } catch (IOException ex) { | 
| 102 | 1 | throw new TagException(ex); | 
| 103 | } catch (ServletException ex) { | |
| 104 | 1 | throw new TagException(ex); | 
| 105 | } | |
| 106 | 1 | 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 { | |
| 116 | 3 | int result = 0; | 
| 117 | try { | |
| 118 | 3 | result = doEnd(); | 
| 119 | 1 | cleanup(); | 
| 120 | 1 | } catch (IOException ex) { | 
| 121 | 1 | throw new TagException(ex); | 
| 122 | } catch (ServletException ex) { | |
| 123 | 1 | throw new TagException(ex); | 
| 124 | } | |
| 125 | 1 | 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 | |
| 135 | 3 | } | 
| 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 { | |
| 146 | 2 | 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 { | |
| 158 | 2 | 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() { | |
| 167 | 7 | if (this.parameterMap != null) { | 
| 168 | 1 | this.parameterMap.clear(); | 
| 169 | } | |
| 170 | 7 | if (this.attributeMap != null) { | 
| 171 | 1 | this.attributeMap.clear(); | 
| 172 | } | |
| 173 | 7 | } | 
| 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) { | |
| 182 | 12 | getParameterMap().put(name, value); | 
| 183 | 12 | } | 
| 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) { | |
| 192 | 12 | 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() { | |
| 201 | 13 | return getParameterMap().keySet().iterator(); | 
| 202 | } | |
| 203 | ||
| 204 | /** | |
| 205 | * Lazy loads the parameter map. | |
| 206 | * | |
| 207 | * @return The parameter map. | |
| 208 | */ | |
| 209 | private Map getParameterMap() { | |
| 210 | 37 | if (this.parameterMap == null) { | 
| 211 | 16 | this.parameterMap = new HashMap(); | 
| 212 | } | |
| 213 | 37 | 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) { | |
| 224 | 12 | getAttributeMap().put(name, value); | 
| 225 | 12 | } | 
| 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) { | |
| 234 | 10 | 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() { | |
| 243 | 9 | return getAttributeMap().keySet().iterator(); | 
| 244 | } | |
| 245 | ||
| 246 | /** | |
| 247 | * Lazy loads the attribute map. | |
| 248 | * | |
| 249 | * @return The attribute map. | |
| 250 | */ | |
| 251 | private Map getAttributeMap() { | |
| 252 | 31 | if (this.attributeMap == null) { | 
| 253 | 17 | this.attributeMap = new HashMap(); | 
| 254 | } | |
| 255 | 31 | 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: | |
| 265 | 6 | JspWriter out = pageContext.getOut(); | 
| 266 | 6 | Iterator attributes = getAttributeNames(); | 
| 267 | 13 | while (attributes.hasNext()) { | 
| 268 | 1 | String attribute = (String) attributes.next(); | 
| 269 | 1 | String value = getAttribute(attribute); | 
| 270 | 1 | out.print(" "); | 
| 271 | 1 | out.print(attribute); | 
| 272 | 1 | out.print("='"); | 
| 273 | 1 | out.print(value); | 
| 274 | 1 | out.print("'"); | 
| 275 | } | |
| 276 | 6 | } | 
| 277 | } | 
| 
this report was generated by version 1.0.5 of jcoverage. | 
copyright © 2003, jcoverage ltd. all rights reserved. |