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.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><param></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 | */ | |
64 | 30 | public 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() { | |
84 | 2 | super.cleanup(); |
85 | 2 | this.name = null; |
86 | 2 | this.property = null; |
87 | 2 | this.objectName = null; |
88 | 2 | this.object = null; |
89 | 2 | } |
90 | ||
91 | /** | |
92 | * The parameter name. | |
93 | * | |
94 | * @param name The parameter name. | |
95 | */ | |
96 | public void setName(String name) { | |
97 | 16 | this.name = name; |
98 | 16 | } |
99 | ||
100 | /** | |
101 | * The parameter name. | |
102 | * | |
103 | * @return The parameter name. | |
104 | */ | |
105 | public String getName() { | |
106 | 35 | if (this.name != null) { |
107 | 31 | return this.name; |
108 | } else { | |
109 | 4 | 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 { | |
120 | 6 | this.property = property; |
121 | 6 | } |
122 | ||
123 | /** | |
124 | * The object property being retrieved. Defaults to var if absent. | |
125 | * | |
126 | * @return The property name. | |
127 | */ | |
128 | public String getProperty() { | |
129 | 4 | 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 { | |
140 | 8 | this.objectName = objectName; |
141 | 8 | this.object = pageContext.findAttribute(objectName); |
142 | 8 | if (this.object == null) { |
143 | 0 | throw new TagException( |
144 | JutilMessages.MESSAGE_OBJECT_NOT_FOUND, | |
145 | new Object[] { objectName }); | |
146 | } | |
147 | 8 | } |
148 | ||
149 | /** | |
150 | * Get object name used to lookup the object. | |
151 | * | |
152 | * @return The object name. | |
153 | */ | |
154 | public String getObject() { | |
155 | 4 | 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() { | |
165 | 6 | if (this.object == null) { |
166 | 2 | this.object = pageContext.findAttribute(ObjectTagSupport.DEFAULT_OBJECT); |
167 | } | |
168 | 6 | 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 { | |
178 | 14 | String value = null; |
179 | 14 | value = getValueFromBodyContent(); |
180 | 14 | if (value == null || value.equals("")) { |
181 | 12 | value = findValue(); |
182 | } | |
183 | try { | |
184 | 12 | Parameterized parent = getParametrizedTag(); |
185 | 11 | parent.addParameter(getName(), value); |
186 | 8 | } catch (ClassCastException ex) { |
187 | 1 | throw new TagException(JutilMessages.MESSAGE_PARENT_NOT_PARAMETRIZED); |
188 | } catch (NullPointerException ex) { | |
189 | 3 | throw new TagException(JutilMessages.MESSAGE_NO_PARENT); |
190 | } | |
191 | 8 | 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() { | |
200 | 14 | String value = null; |
201 | 14 | if (bodyContent != null) { |
202 | 14 | value = bodyContent.getString(); |
203 | } | |
204 | 14 | if (value != null) { |
205 | 14 | value = value.trim(); |
206 | } | |
207 | 14 | 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 { | |
217 | 12 | if (this.property != null) { |
218 | 2 | Object object = findObject(); |
219 | 2 | return String.valueOf(BeanFilter.getProperty(object, this.property)); |
220 | } | |
221 | ||
222 | 10 | ServletRequest request = pageContext.getRequest(); |
223 | 10 | String value = request.getParameter(getName()); |
224 | 10 | if (value != null) { |
225 | 2 | return value; |
226 | } | |
227 | 8 | Object result = pageContext.findAttribute(getName()); |
228 | 8 | if (result == null) { |
229 | 2 | throw new TagException( |
230 | JutilMessages.MESSAGE_PARAMETER_VALUE_NOT_FOUND, | |
231 | new Object[] { getName()}); | |
232 | } | |
233 | 6 | return result.toString(); |
234 | } | |
235 | ||
236 | /** | |
237 | * Derived parametrized tag to be updated. | |
238 | * | |
239 | * @return The parametrized tag. | |
240 | */ | |
241 | protected Parameterized getParametrizedTag() { | |
242 | 6 | return (Parameterized) getParent(); |
243 | } | |
244 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |