Coverage details for org.chwf.taglib.jutil.ParamTag

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.jutil;
34  
35 import javax.servlet.ServletRequest;
36 import javax.servlet.jsp.JspException;
37  
38 import org.chwf.filter.BeanFilter;
39 import org.chwf.taglib.base.LifeCycleBodyTagSupport;
40 import org.chwf.taglib.base.ObjectTagSupport;
41 import org.chwf.taglib.base.Parameterized;
42 import org.chwf.taglib.base.TagException;
43  
44 /**
45  * Tag handler for the <code>&lt;param&gt;</code> tag. It has one attribute,
46  * "name", which is the name of the generated parameter. This handler
47  * sets the parameter or attribute value in one of two ways:
48  *
49  * <ul>
50  * <li>If the trimmed tag body is not an empty string, it sets the
51  * value equal to the processed contents of the tag body.</li>
52  * <li>Otherwise, it sets the parameter value to the first matching web
53  * application variable, searching first request parameters, then
54  * web application scopes in order (page to application).</li>
55  * </ul>
56  *
57  * Once the parameter is set, the handler retrieves its parent tag. If the
58  * parent tag implements the <code>Parameterized</code> interface, it
59  * calls the tags <code>addParameter()</code> method. If not, it throws a
60  * <code>JspException</code>.
61  *
62  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
63  */
6430public class ParamTag extends LifeCycleBodyTagSupport {
65  
66   /** The parameter name. */
67   private String name;
68  
69   /** The object property. */
70   private String property;
71  
72   /** The object name (used for lookup). */
73   private String objectName;
74  
75   /** The object. */
76   private Object object;
77  
78   /**
79    * Clean up data for tag handler reuse and pooling. Should be overridden in
80    * tag handler. Subclasses should always invoke their superclass's cleanup
81    * method: <code>super.cleanup()</code>
82    */
83   public void cleanup() {
842    super.cleanup();
852    this.name = null;
862    this.property = null;
872    this.objectName = null;
882    this.object = null;
892  }
90  
91   /**
92    * The parameter name.
93    *
94    * @param name The parameter name.
95    */
96   public void setName(String name) {
9716    this.name = name;
9816  }
99  
100   /**
101    * The parameter name.
102    *
103    * @return The parameter name.
104    */
105   public String getName() {
10635    if (this.name != null) {
10731      return this.name;
108     } else {
1094      return this.property;
110     }
111   }
112  
113   /**
114    * The object property being retrieved. Defaults to var if absent.
115    *
116    * @param property The property name.
117    * @throws JspException If the property cannot be initialized.
118    */
119   public void setProperty(String property) throws JspException {
1206    this.property = property;
1216  }
122  
123   /**
124    * The object property being retrieved. Defaults to var if absent.
125    *
126    * @return The property name.
127    */
128   public String getProperty() {
1294    return this.property;
130   }
131  
132   /**
133    * The object name. Always optional. The tag will use the default object if
134    * no object can is specified.
135    *
136    * @param objectName The object's name for lookup.
137    * @throws JspException If the object filter cannot be found.
138    */
139   public void setObject(String objectName) throws JspException {
1408    this.objectName = objectName;
1418    this.object = pageContext.findAttribute(objectName);
1428    if (this.object == null) {
1430      throw new TagException(
144         JutilMessages.MESSAGE_OBJECT_NOT_FOUND,
145         new Object[] { objectName });
146     }
1478  }
148  
149   /**
150    * Get object name used to lookup the object.
151    *
152    * @return The object name.
153    */
154   public String getObject() {
1554    return this.objectName;
156   }
157  
158   /**
159    * Find the object for the tag or the default object if no object was
160    * specified.
161    *
162    * @return The object.
163    */
164   public Object findObject() {
1656    if (this.object == null) {
1662      this.object = pageContext.findAttribute(ObjectTagSupport.DEFAULT_OBJECT);
167     }
1686    return this.object;
169   }
170  
171   /**
172    * Set parameter value from tag body or scoped variable.
173    *
174    * @return SKIP_BODY
175    * @throws JspException If not nested in a parametrized tag.
176    */
177   public int doEnd() throws JspException {
17814    String value = null;
17914    value = getValueFromBodyContent();
18014    if (value == null || value.equals("")) {
18112      value = findValue();
182     }
183     try {
18412      Parameterized parent = getParametrizedTag();
18511      parent.addParameter(getName(), value);
1868    } catch (ClassCastException ex) {
1871      throw new TagException(JutilMessages.MESSAGE_PARENT_NOT_PARAMETRIZED);
188     } catch (NullPointerException ex) {
1893      throw new TagException(JutilMessages.MESSAGE_NO_PARENT);
190     }
1918    return EVAL_PAGE;
192   }
193  
194   /**
195    * Derive value from body content.
196    *
197    * @return Null if there is no content.
198    */
199   private String getValueFromBodyContent() {
20014    String value = null;
20114    if (bodyContent != null) {
20214      value = bodyContent.getString();
203     }
20414    if (value != null) {
20514      value = value.trim();
206     }
20714    return value;
208   }
209  
210   /**
211    * Find param value if tag body is empty.
212    *
213    * @return The parameter value.
214    * @throws JspException If the value is not found.
215    */
216   private String findValue() throws JspException {
21712    if (this.property != null) {
2182      Object object = findObject();
2192      return String.valueOf(BeanFilter.getProperty(object, this.property));
220     }
221  
22210    ServletRequest request = pageContext.getRequest();
22310    String value = request.getParameter(getName());
22410    if (value != null) {
2252      return value;
226     }
2278    Object result = pageContext.findAttribute(getName());
2288    if (result == null) {
2292      throw new TagException(
230         JutilMessages.MESSAGE_PARAMETER_VALUE_NOT_FOUND,
231         new Object[] { getName()});
232     }
2336    return result.toString();
234   }
235  
236   /**
237    * Derived parametrized tag to be updated.
238    *
239    * @return The parametrized tag.
240    */
241   protected Parameterized getParametrizedTag() {
2426    return (Parameterized) getParent();
243   }
244 }

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.