| 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.io.IOException; | |
| 36 | ||
| 37 | import javax.servlet.http.HttpServletRequest; | |
| 38 | import javax.servlet.jsp.JspException; | |
| 39 | import javax.servlet.jsp.JspWriter; | |
| 40 | ||
| 41 | import org.chwf.servlet.MultiParameterException; | |
| 42 | import org.chwf.servlet.ServletUtils; | |
| 43 | ||
| 44 | /** | |
| 45 | * Tag handler for the <code><field></code> tag. | |
| 46 | * | |
| 47 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 48 | */ | |
| 49 | 60 | public class FieldTag extends InputTag { |
| 50 | ||
| 51 | /** Constant with default normal pattern. */ | |
| 52 | 2 | private static final String[] NORMAL_PATTERN = |
| 53 | { | |
| 54 | "<tr><td valign='top'><b>", | |
| 55 | FieldPatternTag.LABEL_KEY, | |
| 56 | "</b> </td><td>", | |
| 57 | FieldPatternTag.INPUT_KEY, | |
| 58 | "</td></tr>" }; | |
| 59 | ||
| 60 | /** Constant with default error pattern. */ | |
| 61 | 1 | private static final String[] ERROR_PATTERN = |
| 62 | { | |
| 63 | "<tr><td colspan='2'><font color='red'><b>", | |
| 64 | FieldPatternTag.ERROR_KEY, | |
| 65 | "</b></font></td></tr><tr><td valign='top'><b>", | |
| 66 | FieldPatternTag.LABEL_KEY, | |
| 67 | "</b> </td><td>", | |
| 68 | FieldPatternTag.INPUT_KEY, | |
| 69 | "</td></tr>" }; | |
| 70 | ||
| 71 | /** Instance variable caching the error message, if any. */ | |
| 72 | 30 | private String message = null; |
| 73 | ||
| 74 | /** | |
| 75 | * Clean up data for tag handler reuse and pooling. Should be overridden in | |
| 76 | * tag handler. Subclasses should always invoke their superclass's cleanup | |
| 77 | * method: <code>super.cleanup()</code> | |
| 78 | */ | |
| 79 | public void cleanup() { | |
| 80 | 1 | super.cleanup(); |
| 81 | 1 | this.message = null; |
| 82 | 1 | } |
| 83 | ||
| 84 | /** | |
| 85 | * Calls superclass method. Is present here because of a bug in some servlet | |
| 86 | * engines that requires tag attibute setters to be defined in the tag class | |
| 87 | * itself. | |
| 88 | * | |
| 89 | * @param objectName The object name. | |
| 90 | * @throws JspException For errors. | |
| 91 | */ | |
| 92 | public void setObject(String objectName) throws JspException { | |
| 93 | 0 | super.setObject(objectName); |
| 94 | 0 | } |
| 95 | ||
| 96 | /** | |
| 97 | * Calls superclass method. Is present here because of a bug in some servlet | |
| 98 | * engines that requires tag attibute setters to be defined in the tag class | |
| 99 | * itself. | |
| 100 | * | |
| 101 | * @param property The property name. | |
| 102 | * @throws JspException For errors. | |
| 103 | */ | |
| 104 | public void setProperty(String property) throws JspException { | |
| 105 | 10 | super.setProperty(property); |
| 106 | 10 | } |
| 107 | ||
| 108 | /** | |
| 109 | * Calls superclass method. Is present here because of a bug in some servlet | |
| 110 | * engines that requires tag attibute setters to be defined in the tag class | |
| 111 | * itself. | |
| 112 | * | |
| 113 | * @param name The field name. | |
| 114 | */ | |
| 115 | public void setName(String name) { | |
| 116 | 1 | super.setName(name); |
| 117 | 1 | } |
| 118 | ||
| 119 | /** | |
| 120 | * Set the input element type. Optional. The type value is converted | |
| 121 | * to lowercase for consistency with the XHTML standard. | |
| 122 | * | |
| 123 | * @param type The input type. | |
| 124 | * @throws JspException If the type is not one of the known input | |
| 125 | * element types, defined as TYPE_XXX constants. | |
| 126 | */ | |
| 127 | public void setType(String type) throws JspException { | |
| 128 | 4 | super.setType(type); |
| 129 | 3 | } |
| 130 | ||
| 131 | /** | |
| 132 | * Whether validations are supported. If not specified, defaults to | |
| 133 | * value in nesting formtag. | |
| 134 | * | |
| 135 | * @param validations Whether validations are supported. | |
| 136 | */ | |
| 137 | public void setValidations(Boolean validations) { | |
| 138 | 2 | super.setValidations(validations); |
| 139 | 2 | } |
| 140 | ||
| 141 | /** | |
| 142 | * The name of the web application variable containing input items options. | |
| 143 | * | |
| 144 | * @param options The variable name for options. | |
| 145 | */ | |
| 146 | public void setOptions(String options) { | |
| 147 | 1 | super.setOptions(options); |
| 148 | 1 | } |
| 149 | ||
| 150 | /** | |
| 151 | * Pass-through attribute. Same meaning as "class" in HTML. | |
| 152 | * | |
| 153 | * @param value The attribute value. | |
| 154 | */ | |
| 155 | public void setStyleClass(String value) { | |
| 156 | 1 | addAttribute("class", value); |
| 157 | 1 | } |
| 158 | ||
| 159 | /** | |
| 160 | * Pass-through attribute. Same meaning as in HTML. | |
| 161 | * | |
| 162 | * @param value The attribute value. | |
| 163 | */ | |
| 164 | public void setId(String value) { | |
| 165 | 1 | addAttribute("id", value); |
| 166 | 1 | } |
| 167 | ||
| 168 | /** | |
| 169 | * Pass-through attribute. Same meaning as in HTML. | |
| 170 | * | |
| 171 | * @param value The attribute value. | |
| 172 | */ | |
| 173 | public void setLang(String value) { | |
| 174 | 1 | addAttribute("lang", value); |
| 175 | 1 | } |
| 176 | ||
| 177 | /** | |
| 178 | * Pass-through attribute. Same meaning as in HTML. | |
| 179 | * | |
| 180 | * @param value The attribute value. | |
| 181 | */ | |
| 182 | public void setStyle(String value) { | |
| 183 | 1 | addAttribute("style", value); |
| 184 | 1 | } |
| 185 | ||
| 186 | /** | |
| 187 | * Pass-through attribute. Same meaning as in HTML. | |
| 188 | * | |
| 189 | * @param value The attribute value. | |
| 190 | */ | |
| 191 | public void setTitle(String value) { | |
| 192 | 1 | addAttribute("title", value); |
| 193 | 1 | } |
| 194 | ||
| 195 | // input attributes | |
| 196 | ||
| 197 | /** | |
| 198 | * Pass-through attribute. Same meaning as in HTML. | |
| 199 | * | |
| 200 | * @param value The attribute value. | |
| 201 | */ | |
| 202 | public void setAccept(String value) { | |
| 203 | 1 | addAttribute("accept", value); |
| 204 | 1 | } |
| 205 | ||
| 206 | /** | |
| 207 | * Pass-through attribute. Same meaning as in HTML. | |
| 208 | * | |
| 209 | * @param value The attribute value. | |
| 210 | */ | |
| 211 | public void setAccesskey(String value) { | |
| 212 | 1 | addAttribute("accesskey", value); |
| 213 | 1 | } |
| 214 | ||
| 215 | /** | |
| 216 | * Pass-through attribute. Same meaning as in HTML. | |
| 217 | * | |
| 218 | * @param value The attribute value. | |
| 219 | */ | |
| 220 | public void setDisabled(String value) { | |
| 221 | 1 | addAttribute("disabled", value); |
| 222 | 1 | } |
| 223 | ||
| 224 | /** | |
| 225 | * Pass-through attribute. Same meaning as in HTML. | |
| 226 | * | |
| 227 | * @param value The attribute value. | |
| 228 | */ | |
| 229 | public void setMaxlength(String value) { | |
| 230 | 1 | addAttribute("maxlength", value); |
| 231 | 1 | } |
| 232 | ||
| 233 | /** | |
| 234 | * Pass-through attribute. Same meaning as in HTML. | |
| 235 | * | |
| 236 | * @param value The attribute value. | |
| 237 | */ | |
| 238 | public void setSize(String value) { | |
| 239 | 2 | addAttribute("size", value); |
| 240 | 2 | } |
| 241 | ||
| 242 | // textarea attributes | |
| 243 | ||
| 244 | /** | |
| 245 | * Pass-through attribute. Same meaning as in HTML. | |
| 246 | * | |
| 247 | * @param value The attribute value. | |
| 248 | */ | |
| 249 | public void setCols(String value) { | |
| 250 | 1 | addAttribute("cols", value); |
| 251 | 1 | } |
| 252 | ||
| 253 | /** | |
| 254 | * Pass-through attribute. Same meaning as in HTML. | |
| 255 | * | |
| 256 | * @param value The attribute value. | |
| 257 | */ | |
| 258 | public void setRows(String value) { | |
| 259 | 1 | addAttribute("rows", value); |
| 260 | 1 | } |
| 261 | ||
| 262 | /** | |
| 263 | * Pass-through attribute. Same meaning as in HTML. | |
| 264 | * | |
| 265 | * @param value The attribute value. | |
| 266 | */ | |
| 267 | public void setWrap(String value) { | |
| 268 | 1 | addAttribute("wrap", value); |
| 269 | 1 | } |
| 270 | ||
| 271 | /** | |
| 272 | * True if the field has an error message. | |
| 273 | * | |
| 274 | * @return True if the field has an error message. | |
| 275 | */ | |
| 276 | public boolean hasErrorMessage() { | |
| 277 | 6 | return (this.getErrorMessage() != null); |
| 278 | } | |
| 279 | ||
| 280 | /** | |
| 281 | * The error message (or <code>null</code> if there is no message). | |
| 282 | * | |
| 283 | * @return The error message. | |
| 284 | */ | |
| 285 | public String getErrorMessage() { | |
| 286 | 9 | if (this.message == null) { |
| 287 | 7 | HttpServletRequest request = |
| 288 | (HttpServletRequest) pageContext.getRequest(); | |
| 289 | 7 | Throwable error = ServletUtils.retrieveError(request); |
| 290 | 7 | if ((error != null) && (error instanceof MultiParameterException)) { |
| 291 | 4 | MultiParameterException paramError = (MultiParameterException) error; |
| 292 | 4 | this.message = paramError.getMessage(getName()); |
| 293 | } | |
| 294 | } | |
| 295 | 9 | return this.message; |
| 296 | } | |
| 297 | ||
| 298 | /** | |
| 299 | * Print the field info. | |
| 300 | * | |
| 301 | * @return SKIP_BODY | |
| 302 | * @throws JspException If the property cannot be initialized. | |
| 303 | * @throws IOException For IO exceptions. | |
| 304 | */ | |
| 305 | public int doStart() throws IOException, JspException { | |
| 306 | 4 | String[] outputArray = null; |
| 307 | 4 | if (this.hasErrorMessage()) { |
| 308 | 2 | outputArray = getErrorPattern(); |
| 309 | } else { | |
| 310 | 2 | outputArray = getNormalPattern(); |
| 311 | } | |
| 312 | 28 | for (int i = 0; i < outputArray.length; i++) { |
| 313 | 24 | if (outputArray[i] == FieldPatternTag.INPUT_KEY) { |
| 314 | 4 | printInputElement(); |
| 315 | 20 | } else if (outputArray[i] == FieldPatternTag.LABEL_KEY) { |
| 316 | 4 | printLabel(); |
| 317 | 16 | } else if (outputArray[i] == FieldPatternTag.ERROR_KEY) { |
| 318 | 2 | printError(); |
| 319 | } else { | |
| 320 | 14 | this.pageContext.getOut().print(outputArray[i]); |
| 321 | } | |
| 322 | } | |
| 323 | 4 | return SKIP_BODY; |
| 324 | } | |
| 325 | ||
| 326 | /** | |
| 327 | * Get normal pattern. | |
| 328 | * | |
| 329 | * @return The normal pattern. | |
| 330 | */ | |
| 331 | private String[] getNormalPattern() { | |
| 332 | 2 | String[] pattern = |
| 333 | (String[]) pageContext.getAttribute(FieldPatternTag.NORMAL_PATTERN_KEY); | |
| 334 | 2 | if (pattern == null) { |
| 335 | 1 | return NORMAL_PATTERN; |
| 336 | } else { | |
| 337 | 1 | return pattern; |
| 338 | } | |
| 339 | } | |
| 340 | ||
| 341 | /** | |
| 342 | * Get error pattern. | |
| 343 | * | |
| 344 | * @return The error pattern. | |
| 345 | */ | |
| 346 | private String[] getErrorPattern() { | |
| 347 | 2 | String[] pattern = |
| 348 | (String[]) pageContext.getAttribute(FieldPatternTag.ERROR_PATTERN_KEY); | |
| 349 | 2 | if (pattern == null) { |
| 350 | 1 | return ERROR_PATTERN; |
| 351 | } else { | |
| 352 | 1 | return pattern; |
| 353 | } | |
| 354 | } | |
| 355 | ||
| 356 | /** | |
| 357 | * Print error associated with field. | |
| 358 | * | |
| 359 | * @throws IOException For write errors. | |
| 360 | */ | |
| 361 | private void printError() throws IOException { | |
| 362 | 2 | JspWriter out = pageContext.getOut(); |
| 363 | 2 | out.print(getErrorMessage()); |
| 364 | 2 | } |
| 365 | ||
| 366 | /** | |
| 367 | * Print label associated with field. | |
| 368 | * | |
| 369 | * @throws IOException For write errors. | |
| 370 | * @throws JspException If not object is define. | |
| 371 | */ | |
| 372 | private void printLabel() throws IOException, JspException { | |
| 373 | 4 | JspWriter out = this.pageContext.getOut(); |
| 374 | 4 | out.print("<label for='"); |
| 375 | 4 | out.print(getName()); |
| 376 | 4 | out.print("'>"); |
| 377 | 4 | out.print(deriveLabel()); |
| 378 | 4 | out.print("</label>"); |
| 379 | 4 | } |
| 380 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |