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

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.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>&lt;field&gt;</code> tag.
46  *
47  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
48  */
4960public class FieldTag extends InputTag {
50  
51   /** Constant with default normal pattern. */
522  private static final String[] NORMAL_PATTERN =
53     {
54       "<tr><td valign='top'><b>",
55       FieldPatternTag.LABEL_KEY,
56       "</b>&nbsp;</td><td>",
57       FieldPatternTag.INPUT_KEY,
58       "</td></tr>" };
59  
60   /** Constant with default error pattern. */
611  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>&nbsp;</td><td>",
68       FieldPatternTag.INPUT_KEY,
69       "</td></tr>" };
70  
71   /** Instance variable caching the error message, if any. */
7230  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() {
801    super.cleanup();
811    this.message = null;
821  }
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 {
930    super.setObject(objectName);
940  }
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 {
10510    super.setProperty(property);
10610  }
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) {
1161    super.setName(name);
1171  }
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 {
1284    super.setType(type);
1293  }
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) {
1382    super.setValidations(validations);
1392  }
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) {
1471    super.setOptions(options);
1481  }
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) {
1561    addAttribute("class", value);
1571  }
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) {
1651    addAttribute("id", value);
1661  }
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) {
1741    addAttribute("lang", value);
1751  }
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) {
1831    addAttribute("style", value);
1841  }
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) {
1921    addAttribute("title", value);
1931  }
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) {
2031    addAttribute("accept", value);
2041  }
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) {
2121    addAttribute("accesskey", value);
2131  }
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) {
2211    addAttribute("disabled", value);
2221  }
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) {
2301    addAttribute("maxlength", value);
2311  }
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) {
2392    addAttribute("size", value);
2402  }
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) {
2501    addAttribute("cols", value);
2511  }
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) {
2591    addAttribute("rows", value);
2601  }
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) {
2681    addAttribute("wrap", value);
2691  }
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() {
2776    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() {
2869    if (this.message == null) {
2877      HttpServletRequest request =
288         (HttpServletRequest) pageContext.getRequest();
2897      Throwable error = ServletUtils.retrieveError(request);
2907      if ((error != null) && (error instanceof MultiParameterException)) {
2914        MultiParameterException paramError = (MultiParameterException) error;
2924        this.message = paramError.getMessage(getName());
293       }
294     }
2959    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 {
3064    String[] outputArray = null;
3074    if (this.hasErrorMessage()) {
3082      outputArray = getErrorPattern();
309     } else {
3102      outputArray = getNormalPattern();
311     }
31228    for (int i = 0; i < outputArray.length; i++) {
31324      if (outputArray[i] == FieldPatternTag.INPUT_KEY) {
3144        printInputElement();
31520      } else if (outputArray[i] == FieldPatternTag.LABEL_KEY) {
3164        printLabel();
31716      } else if (outputArray[i] == FieldPatternTag.ERROR_KEY) {
3182        printError();
319       } else {
32014        this.pageContext.getOut().print(outputArray[i]);
321       }
322     }
3234    return SKIP_BODY;
324   }
325  
326   /**
327    * Get normal pattern.
328    *
329    * @return The normal pattern.
330    */
331   private String[] getNormalPattern() {
3322    String[] pattern =
333       (String[]) pageContext.getAttribute(FieldPatternTag.NORMAL_PATTERN_KEY);
3342    if (pattern == null) {
3351      return NORMAL_PATTERN;
336     } else {
3371      return pattern;
338     }
339   }
340  
341   /**
342    * Get error pattern.
343    *
344    * @return The error pattern.
345    */
346   private String[] getErrorPattern() {
3472    String[] pattern =
348       (String[]) pageContext.getAttribute(FieldPatternTag.ERROR_PATTERN_KEY);
3492    if (pattern == null) {
3501      return ERROR_PATTERN;
351     } else {
3521      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 {
3622    JspWriter out = pageContext.getOut();
3632    out.print(getErrorMessage());
3642  }
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 {
3734    JspWriter out = this.pageContext.getOut();
3744    out.print("<label for='");
3754    out.print(getName());
3764    out.print("'>");
3774    out.print(deriveLabel());
3784    out.print("</label>");
3794  }
380 }

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.