| 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 javax.servlet.jsp.JspException; | |
| 36 | ||
| 37 | import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; | |
| 38 | import org.chwf.filter.BeanFilter; | |
| 39 | ||
| 40 | /** | |
| 41 | * A base tag for supporting manipulating object values. It is | |
| 42 | * intended for tags with var, object and/or property attributes. | |
| 43 | * | |
| 44 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 45 | */ | |
| 46 | 312 | public class ObjectTagSupport extends LifeCycleTagSupport { |
| 47 | ||
| 48 | /** A constant used to store the default object in the page context. */ | |
| 49 | 2 | public static final String DEFAULT_OBJECT = |
| 50 | ObjectTagSupport.class.getName() + ":DEFAULT_OBJECT"; | |
| 51 | ||
| 52 | /** Empty object array */ | |
| 53 | 1 | private static final Object[] EMPTY_ARRAY = new Object[0]; |
| 54 | ||
| 55 | /** Expression language attribute. */ | |
| 56 | private static final String EL_ATTRIBUTE = "el"; | |
| 57 | ||
| 58 | /** The variable name used to store retrieved properties. */ | |
| 59 | private String var; | |
| 60 | ||
| 61 | /** The object property. */ | |
| 62 | private String property; | |
| 63 | ||
| 64 | /** The object name (used for lookup). */ | |
| 65 | private String objectName; | |
| 66 | ||
| 67 | /** The object. */ | |
| 68 | private Object object; | |
| 69 | ||
| 70 | /** The object. */ | |
| 71 | private String el; | |
| 72 | ||
| 73 | /** | |
| 74 | * Clean up data for tag handler reuse and pooling. Should be overridden in | |
| 75 | * tag handler. Subclasses should always invoke their superclass's cleanup | |
| 76 | * method: <code>super.cleanup()</code> | |
| 77 | */ | |
| 78 | public void cleanup() { | |
| 79 | 20 | super.cleanup(); |
| 80 | 20 | this.var = null; |
| 81 | 20 | this.property = null; |
| 82 | 20 | this.objectName = null; |
| 83 | 20 | this.object = null; |
| 84 | 20 | this.el = null; |
| 85 | 20 | } |
| 86 | ||
| 87 | /** | |
| 88 | * The variable name used to store an object. Should be required if a tag | |
| 89 | * uses it. The tag should also invoke the <code>storeDefaultObject()<code> | |
| 90 | * method in its <code>doStart()</code> method. | |
| 91 | * | |
| 92 | * @param var The variable name. | |
| 93 | */ | |
| 94 | public void setVar(String var) { | |
| 95 | 6 | this.var = var; |
| 96 | 6 | } |
| 97 | ||
| 98 | /** | |
| 99 | * The variable name used to store an object. | |
| 100 | * | |
| 101 | * @return The variable name. | |
| 102 | */ | |
| 103 | public String getVar() { | |
| 104 | 6 | return this.var; |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * The object property being retrieved. Defaults to var if absent. | |
| 109 | * | |
| 110 | * @param property The property name. | |
| 111 | * @throws JspException If the property cannot be initialized. | |
| 112 | */ | |
| 113 | public void setProperty(String property) throws JspException { | |
| 114 | 95 | this.property = property; |
| 115 | 95 | } |
| 116 | ||
| 117 | /** | |
| 118 | * The object property being retrieved. Defaults to var if absent. | |
| 119 | * | |
| 120 | * @return The property name. | |
| 121 | */ | |
| 122 | public String getProperty() { | |
| 123 | 154 | if (this.property == null) { |
| 124 | 12 | return this.var; |
| 125 | } else { | |
| 126 | 142 | return this.property; |
| 127 | } | |
| 128 | } | |
| 129 | ||
| 130 | /** | |
| 131 | * The object name. Always optional. The tag will use the default object if | |
| 132 | * no object can is specified. | |
| 133 | * | |
| 134 | * @param objectName The object's name for lookup. | |
| 135 | * @throws JspException If the object cannot be found. | |
| 136 | */ | |
| 137 | public void setObject(String objectName) throws JspException { | |
| 138 | 147 | this.objectName = objectName; |
| 139 | 147 | this.object = pageContext.findAttribute(objectName); |
| 140 | 147 | if (this.object == null) { |
| 141 | 2 | throw new TagException( |
| 142 | TagException.MESSAGE_OBJECT_NOT_FOUND, | |
| 143 | new Object[] { objectName }); | |
| 144 | } | |
| 145 | 145 | } |
| 146 | ||
| 147 | /** | |
| 148 | * Get object name used to lookup the object. | |
| 149 | * | |
| 150 | * @return The object name. | |
| 151 | */ | |
| 152 | public String getObject() { | |
| 153 | 7 | return this.objectName; |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * Find the object for the tag or the default object if no object was | |
| 158 | * specified. | |
| 159 | * | |
| 160 | * @return The object. | |
| 161 | * @throws JspException If object not defined. | |
| 162 | */ | |
| 163 | public Object findObject() throws JspException { | |
| 164 | 105 | if (this.object == null) { |
| 165 | 37 | this.object = pageContext.findAttribute(DEFAULT_OBJECT); |
| 166 | 37 | if (this.object == null) { |
| 167 | 0 | throw new TagException(TagException.MESSAGE_OBJECT_NOT_DEFINED); |
| 168 | } | |
| 169 | } | |
| 170 | 105 | return this.object; |
| 171 | } | |
| 172 | ||
| 173 | /** | |
| 174 | * Store the default object. This method should be invoked in the tag | |
| 175 | * handler's <code>doStart()</code> method if the tag uses the var | |
| 176 | * attribute. | |
| 177 | * | |
| 178 | * @param object The new default object. | |
| 179 | */ | |
| 180 | public void storeDefaultObject(Object object) { | |
| 181 | 92 | if (object != null) { |
| 182 | 92 | pageContext.setAttribute(DEFAULT_OBJECT, object); |
| 183 | } | |
| 184 | 92 | } |
| 185 | ||
| 186 | /** | |
| 187 | * The JSTL Expression Language statement. | |
| 188 | * | |
| 189 | * @param el The JSTL Expression Language statement. | |
| 190 | */ | |
| 191 | public void setEl(String el) { | |
| 192 | 12 | this.el = el; |
| 193 | 12 | } |
| 194 | ||
| 195 | /** | |
| 196 | * The JSTL Expression Language statement. | |
| 197 | * | |
| 198 | * @return The JSTL Expression Language statement. | |
| 199 | */ | |
| 200 | public String getEl() { | |
| 201 | 39 | return this.el; |
| 202 | } | |
| 203 | ||
| 204 | /** | |
| 205 | * The tag result. The result is derived from one of the following, in | |
| 206 | * order of precedence: | |
| 207 | * | |
| 208 | * <ol> | |
| 209 | * <li>The result of the JSTL Expression Language statement.</li> | |
| 210 | * <li>The object property.</li> | |
| 211 | * <li>The object value.</li> | |
| 212 | * <li>The default object.</li> | |
| 213 | * </ol> | |
| 214 | * | |
| 215 | * @return The tag result. | |
| 216 | * @throws JspException For parsing failures. | |
| 217 | */ | |
| 218 | public Object getTagResult() throws JspException { | |
| 219 | 9 | return getTagResult(Object.class); |
| 220 | } | |
| 221 | ||
| 222 | /** | |
| 223 | * The tag result. As {@link #getTagResult()}, except that the result is | |
| 224 | * coerced to the expected class value. If the result cannot be coerced, | |
| 225 | * an error message is thrown. | |
| 226 | * | |
| 227 | * @param expectedClass The expected class value. | |
| 228 | * @return The tag result. | |
| 229 | * @throws JspException For parsing failures or if the result cannot be | |
| 230 | * correctly coerced. | |
| 231 | */ | |
| 232 | public Object getTagResult(Class expectedClass) throws JspException { | |
| 233 | 27 | if (getEl() != null) { |
| 234 | 6 | return getElResult(expectedClass); |
| 235 | 21 | } else if (findObject() != null) { |
| 236 | 21 | if (getProperty() == null) { |
| 237 | 4 | return getObjectResult(expectedClass); |
| 238 | } else { | |
| 239 | 17 | return getPropertyResult(expectedClass); |
| 240 | } | |
| 241 | } | |
| 242 | 0 | return null; |
| 243 | } | |
| 244 | ||
| 245 | /** | |
| 246 | * Get the result from an expression. | |
| 247 | * | |
| 248 | * @param expectedClass The expected class. | |
| 249 | * @return The tag result. | |
| 250 | * @throws JspException For parsing failures or if the result cannot be | |
| 251 | * correctly coerced. | |
| 252 | */ | |
| 253 | private Object getElResult(Class expectedClass) throws JspException { | |
| 254 | 6 | return ExpressionEvaluatorManager.evaluate( |
| 255 | EL_ATTRIBUTE, | |
| 256 | getEl(), | |
| 257 | expectedClass, | |
| 258 | this, | |
| 259 | this.pageContext); | |
| 260 | } | |
| 261 | ||
| 262 | /** | |
| 263 | * Get the result from the tag object. | |
| 264 | * | |
| 265 | * @param expectedClass The expected class. | |
| 266 | * @return The tag result. | |
| 267 | * @throws JspException If the result cannot be correctly coerced. | |
| 268 | */ | |
| 269 | private Object getObjectResult(Class expectedClass) throws JspException { | |
| 270 | 4 | Object value = findObject(); |
| 271 | 4 | if (!expectedClass.isInstance(value)) { |
| 272 | 0 | Object[] args = { getObject(), expectedClass.getName()}; |
| 273 | 0 | throw new TagException(TagException.MESSAGE_OBJECT_UNEXPECTED_TYPE, args); |
| 274 | } | |
| 275 | 4 | return value; |
| 276 | } | |
| 277 | ||
| 278 | /** | |
| 279 | * Get the result from the tag object property. | |
| 280 | * | |
| 281 | * @param expectedClass The expected class. | |
| 282 | * @return The tag result. | |
| 283 | * @throws JspException If the result cannot be correctly coerced. | |
| 284 | */ | |
| 285 | private Object getPropertyResult(Class expectedClass) throws JspException { | |
| 286 | 17 | Object value = BeanFilter.getProperty(findObject(), getProperty()); |
| 287 | 17 | if (!expectedClass.isInstance(value)) { |
| 288 | 0 | Object[] args = { getProperty(), getObject(), expectedClass.getName()}; |
| 289 | 0 | throw new TagException( |
| 290 | TagException.MESSAGE_PROPERTY_UNEXPECTED_TYPE, | |
| 291 | args); | |
| 292 | } | |
| 293 | 17 | return value; |
| 294 | } | |
| 295 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |