| 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.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 | */ | |
| 55 | 102 | public 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() { | |
| 78 | 5 | super.cleanup(); |
| 79 | 5 | this.beanFilter = null; |
| 80 | 5 | this.propertyFilter = null; |
| 81 | 5 | this.name = null; |
| 82 | 5 | this.formTag = null; |
| 83 | 5 | this.formTagRetrieved = false; |
| 84 | 5 | } |
| 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() { | |
| 92 | 39 | if (!this.formTagRetrieved) { |
| 93 | 20 | this.formTag = (FormTag) findAncestorWithClass(this, FormTag.class); |
| 94 | 20 | this.formTagRetrieved = true; |
| 95 | } | |
| 96 | 39 | 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) { | |
| 105 | 6 | this.name = name; |
| 106 | 6 | } |
| 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() { | |
| 114 | 70 | if (this.name == null) { |
| 115 | 65 | return getProperty(); |
| 116 | } else { | |
| 117 | 5 | 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 { | |
| 135 | 18 | String value = null; |
| 136 | ||
| 137 | // Initialize field value from prior request: | |
| 138 | 18 | RequestParameterMap priorRequest = getCachedRequestData(); |
| 139 | 18 | if (priorRequest != null) { |
| 140 | 0 | value = priorRequest.getParameter(getName()); |
| 141 | } | |
| 142 | ||
| 143 | // Initialize field value from the form object: | |
| 144 | 18 | if (value == null) { |
| 145 | 18 | Object formObject = findObject(); |
| 146 | 18 | if (formObject != null) { |
| 147 | 18 | PropertyFilter propertyFilter = getPropertyFilter(); |
| 148 | 18 | if (propertyFilter.isReadable()) { |
| 149 | try { | |
| 150 | 18 | value = propertyFilter.get(formObject); |
| 151 | 18 | } catch (OperationException ex) { |
| 152 | 0 | 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: | |
| 161 | 18 | if (value == null) { |
| 162 | 0 | value = ""; |
| 163 | } | |
| 164 | ||
| 165 | 18 | 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 { | |
| 177 | 33 | if (this.beanFilter == null) { |
| 178 | 33 | Object object = findObject(); |
| 179 | 33 | this.beanFilter = BeanFilter.findFilter(object); |
| 180 | } | |
| 181 | 33 | 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 { | |
| 192 | 187 | 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 { | |
| 203 | 27 | PropertyFilter propertyFilter = getPropertyFilter(); |
| 204 | 27 | Locale locale = UserLocale.getLocale(); |
| 205 | 27 | 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() { | |
| 214 | 18 | 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 { | |
| 227 | 232 | if (this.propertyFilter == null) { |
| 228 | 32 | this.propertyFilter = getBeanFilter().getPropertyFilter(getProperty()); |
| 229 | } | |
| 230 | 231 | 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 { | |
| 240 | 8 | return (String) getLocalizedAttribute(BeanFilter.ATTRIBUTE_LABEL); |
| 241 | } | |
| 242 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |