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

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 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  */
46312public class ObjectTagSupport extends LifeCycleTagSupport {
47  
48   /** A constant used to store the default object in the page context. */
492  public static final String DEFAULT_OBJECT =
50     ObjectTagSupport.class.getName() + ":DEFAULT_OBJECT";
51  
52   /** Empty object array */
531  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() {
7920    super.cleanup();
8020    this.var = null;
8120    this.property = null;
8220    this.objectName = null;
8320    this.object = null;
8420    this.el = null;
8520  }
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) {
956    this.var = var;
966  }
97  
98   /**
99    * The variable name used to store an object.
100    *
101    * @return The variable name.
102    */
103   public String getVar() {
1046    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 {
11495    this.property = property;
11595  }
116  
117   /**
118    * The object property being retrieved. Defaults to var if absent.
119    *
120    * @return The property name.
121    */
122   public String getProperty() {
123154    if (this.property == null) {
12412      return this.var;
125     } else {
126142      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 {
138147    this.objectName = objectName;
139147    this.object = pageContext.findAttribute(objectName);
140147    if (this.object == null) {
1412      throw new TagException(
142         TagException.MESSAGE_OBJECT_NOT_FOUND,
143         new Object[] { objectName });
144     }
145145  }
146  
147   /**
148    * Get object name used to lookup the object.
149    *
150    * @return The object name.
151    */
152   public String getObject() {
1537    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 {
164105    if (this.object == null) {
16537      this.object = pageContext.findAttribute(DEFAULT_OBJECT);
16637      if (this.object == null) {
1670        throw new TagException(TagException.MESSAGE_OBJECT_NOT_DEFINED);
168       }
169     }
170105    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) {
18192    if (object != null) {
18292      pageContext.setAttribute(DEFAULT_OBJECT, object);
183     }
18492  }
185  
186   /**
187    * The JSTL Expression Language statement.
188    *
189    * @param el The JSTL Expression Language statement.
190    */
191   public void setEl(String el) {
19212    this.el = el;
19312  }
194  
195   /**
196    * The JSTL Expression Language statement.
197    *
198    * @return The JSTL Expression Language statement.
199    */
200   public String getEl() {
20139    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 {
2199    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 {
23327    if (getEl() != null) {
2346      return getElResult(expectedClass);
23521    } else if (findObject() != null) {
23621      if (getProperty() == null) {
2374        return getObjectResult(expectedClass);
238       } else {
23917        return getPropertyResult(expectedClass);
240       }
241     }
2420    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 {
2546    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 {
2704    Object value = findObject();
2714    if (!expectedClass.isInstance(value)) {
2720      Object[] args = { getObject(), expectedClass.getName()};
2730      throw new TagException(TagException.MESSAGE_OBJECT_UNEXPECTED_TYPE, args);
274     }
2754    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 {
28617    Object value = BeanFilter.getProperty(findObject(), getProperty());
28717    if (!expectedClass.isInstance(value)) {
2880      Object[] args = { getProperty(), getObject(), expectedClass.getName()};
2890      throw new TagException(
290         TagException.MESSAGE_PROPERTY_UNEXPECTED_TYPE,
291         args);
292     }
29317    return value;
294   }
295 }

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.