Coverage details for org.chwf.taglib.jhtml.FieldTagSupport

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.jhtml;
34  
35 import java.util.Locale;
36  
37 import javax.servlet.jsp.JspException;
38  
39 import org.chwf.filter.BeanFilter;
40 import org.chwf.filter.FilterException;
41 import org.chwf.filter.InitializationException;
42 import org.chwf.filter.OperationException;
43 import org.chwf.filter.PropertyFilter;
44 import org.chwf.i18n.UserLocale;
45 import org.chwf.servlet.RequestParameterMap;
46 import org.chwf.taglib.base.ObjectTagSupport;
47 import org.chwf.taglib.base.TagException;
48  
49 /**
50  * Superclass of tags that represent form fields. It includes methods to
51  * facilitate object-property meta-data manipulation with filters.
52  *
53  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
54  */
55102public class FieldTagSupport extends ObjectTagSupport {
56  
57   /** The filter. */
58   private BeanFilter beanFilter;
59  
60   /** Instance variable to cache the property filter. */
61   private PropertyFilter propertyFilter;
62  
63   /** Instance variable to cache the property filter. */
64   private String name;
65  
66   /** Parent form tag. */
67   private FormTag formTag;
68  
69   /** Flag indicating formTag was retrieved. */
70   private boolean formTagRetrieved;
71  
72   /**
73    * Clean up data for tag handler reuse and pooling. Should be overridden in
74    * tag handler. Subclasses should always invoke their superclass's cleanup
75    * method: <code>super.cleanup()</code>
76    */
77   public void cleanup() {
785    super.cleanup();
795    this.beanFilter = null;
805    this.propertyFilter = null;
815    this.name = null;
825    this.formTag = null;
835    this.formTagRetrieved = false;
845  }
85  
86   /**
87    * Parent form tag. Lazy loads the tag.
88    *
89    * @return Parent form tag or <code>null</code> if there is none.
90    */
91   public FormTag getFormTag() {
9239    if (!this.formTagRetrieved) {
9320      this.formTag = (FormTag) findAncestorWithClass(this, FormTag.class);
9420      this.formTagRetrieved = true;
95     }
9639    return formTag;
97   }
98  
99   /**
100    * The form field name. Defaults to the property name if absent.
101    *
102    * @param name The field name.
103    */
104   public void setName(String name) {
1056    this.name = name;
1066  }
107  
108   /**
109    * The form field name. Defaults to the property name if absent.
110    *
111    * @return The field name.
112    */
113   public String getName() {
11470    if (this.name == null) {
11565      return getProperty();
116     } else {
1175      return this.name;
118     }
119   }
120  
121   /**
122    * <p>Returns the field value. This field value is derived from:</p>
123    *
124    * <ol>
125    * <li>The <code>RequestParameterMap</code> containing the prior
126    * request parameters (for handling errors).</li>
127    * <li>The property value of the associated form object.</li>
128    * <li>An empty string if no other value is found.</li>
129    * </ol>
130    *
131    * @return The field value as a string.
132    * @throws JspException If property value cannot be read.
133    */
134   public String getFieldValue() throws JspException {
13518    String value = null;
136  
137     // Initialize field value from prior request:
13818    RequestParameterMap priorRequest = getCachedRequestData();
13918    if (priorRequest != null) {
1400      value = priorRequest.getParameter(getName());
141     }
142  
143     // Initialize field value from the form object:
14418    if (value == null) {
14518      Object formObject = findObject();
14618      if (formObject != null) {
14718        PropertyFilter propertyFilter = getPropertyFilter();
14818        if (propertyFilter.isReadable()) {
149           try {
15018            value = propertyFilter.get(formObject);
15118          } catch (OperationException ex) {
1520            throw new TagException(
153               JhtmlMessages.MESSAGE_PROPERTY_READ_ERROR,
154               new Object[] { propertyFilter.getName(), ex.getMessage()});
155           }
156         }
157       }
158     }
159  
160     // Default to an empty string:
16118    if (value == null) {
1620      value = "";
163     }
164  
16518    return value;
166   }
167  
168   /**
169    * Get the object filter.
170    *
171    * @return The filter.
172    * @throws InitializationException If the filter cannot be initialized.
173    * @throws JspException If object not defined.
174    */
175   public BeanFilter getBeanFilter()
176     throws JspException, InitializationException {
17733    if (this.beanFilter == null) {
17833      Object object = findObject();
17933      this.beanFilter = BeanFilter.findFilter(object);
180     }
18133    return this.beanFilter;
182   }
183  
184   /**
185    * Returns the named attribute for the property.
186    *
187    * @param attribute The attribute name.
188    * @return The attribute value.
189    * @throws JspException If the property filter cannot be initialized.
190    */
191   public Object getPropertyAttribute(String attribute) throws JspException {
192187    return getPropertyFilter().getAttribute(attribute);
193   }
194  
195   /**
196    * Returns the named and localized attribute for the property. <p>
197    *
198    * @param attribute The attribute name.
199    * @return The attribute value.
200    * @throws JspException If the property filter cannot be initialized.
201    */
202   public Object getLocalizedAttribute(String attribute) throws JspException {
20327    PropertyFilter propertyFilter = getPropertyFilter();
20427    Locale locale = UserLocale.getLocale();
20527    return propertyFilter.getLocalizedAttribute(attribute, locale);
206   }
207  
208   /**
209    * Returns cached request data for error handling.
210    *
211    * @return Cached request parameters in a map (if any).
212    */
213   private RequestParameterMap getCachedRequestData() {
21418    return (RequestParameterMap) pageContext.findAttribute(
215       RequestParameterMap.CACHING_NAME);
216   }
217  
218   /**
219    * Returns the PropertyFilter for the property.
220    *
221    * @return The property filter.
222    * @throws FilterException If the property filter cannot be initialized.
223    * @throws JspException If object not defined.
224    */
225   private PropertyFilter getPropertyFilter()
226     throws JspException, FilterException {
227232    if (this.propertyFilter == null) {
22832      this.propertyFilter = getBeanFilter().getPropertyFilter(getProperty());
229     }
230231    return this.propertyFilter;
231   }
232  
233   /**
234    * Derive field label.
235    *
236    * @return The field label.
237    * @throws JspException If object not defined.
238    */
239   public String deriveLabel() throws JspException {
2408    return (String) getLocalizedAttribute(BeanFilter.ATTRIBUTE_LABEL);
241   }
242 }

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.