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

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 import java.util.Arrays;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Map;
40  
41 import javax.servlet.jsp.JspException;
42 import javax.servlet.jsp.JspWriter;
43  
44 import org.chwf.filter.BeanFilter;
45 import org.chwf.plugin.Scripter;
46 import org.chwf.plugin.ValidationData;
47 import org.chwf.taglib.base.TagException;
48  
49 /**
50  * Tag handler for the <code>&lt;input&gt;</code> tag.
51  *
52  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
53  */
54148public class InputTag extends FieldTagSupport implements ValidationData {
55  
56   /** Constant indicate minimum length for text areas. */
57   private static final int TEXTAREA_LENGTH = 255;
58  
59   /** Constant for true string values. */
60   public static final String TRUE_STRING = "true";
61  
62   /** List of known input types. */
631  private static final List TYPES_LIST =
64     Arrays.asList(
65       new String[] {
66         TYPE_CHECKBOX,
67         TYPE_HIDDEN,
68         TYPE_FIXED,
69         TYPE_PASSWORD,
70         TYPE_SELECT,
71         TYPE_TEXT,
72         TYPE_TEXTAREA });
73  
74   /** Boolean datatype. */
75   private static final String BOOLEAN_TYPE = "java.lang.Boolean";
76  
77   /** The input type. */
7874  private String type = null;
79  
80   /** Whether validations are supported. */
81   private Boolean validations;
82  
83   /** The input type. */
8474  private Map options = null;
85  
86   /**
87    * Clean up data for tag handler reuse and pooling. Should be overridden in
88    * tag handler. Subclasses should always invoke their superclass's cleanup
89    * method: <code>super.cleanup()</code>
90    */
91   public void cleanup() {
923    super.cleanup();
933    this.type = null;
943    this.validations = null;
953    this.options = null;
963  }
97  
98   /**
99    * Calls superclass method. Is present here because of a bug in some servlet
100    * engines that requires tag attibute setters to be defined in the tag class
101    * itself.
102    *
103    * @param objectName The object name.
104    * @throws JspException For errors.
105    */
106   public void setObject(String objectName) throws JspException {
1071    super.setObject(objectName);
1081  }
109  
110   /**
111    * Calls superclass method. Is present here because of a bug in some servlet
112    * engines that requires tag attibute setters to be defined in the tag class
113    * itself.
114    *
115    * @param property The property name.
116    * @throws JspException For errors.
117    */
118   public void setProperty(String property) throws JspException {
11932    super.setProperty(property);
12032  }
121  
122   /**
123    * Calls superclass method. Is present here because of a bug in some servlet
124    * engines that requires tag attibute setters to be defined in the tag class
125    * itself.
126    *
127    * @param name The field name.
128    */
129   public void setName(String name) {
1302    super.setName(name);
1312  }
132  
133   /**
134    * Set the input element type. Optional. The type value is converted
135    * to lowercase for consistency with the XHTML standard.
136    *
137    * @param type The input type.
138    * @throws JspException If the type is not one of the known input
139    * element types, defined as TYPE_XXX constants.
140    */
141   public void setType(String type) throws JspException {
14217    type = type.toLowerCase();
14317    if (!TYPES_LIST.contains(type)) {
1442      throw new TagException(
145         JhtmlMessages.MESSAGE_INVALID_INPUT_TAG_TYPE,
146         new Object[] { type, TYPES_LIST });
147     }
14815    this.type = type;
14915  }
150  
151   /**
152    * Gets the input element type. If no type is defined, it attempts
153    * to deduce the type for form object metadata.
154    *
155    * <ul>
156    * <li>Readonly types are printed as fixed fields.</li>
157    * <li>Fields with options are printed as select lists.</li>
158    * <li>Boolean fields are printed as checkboxes.</li>
159    * <li>Fields whose maxlength > 255 are printed as textareas.</li>
160    * <li>Otherwise, the field is printed as a text field.</li>
161    * </ul>
162    *
163    * @return The input type.
164    * @throws JspException If the property cannot be initialized
165    */
166   public String getType() throws JspException {
16745    if (this.type == null) {
168       // Check to see if field is readonly; default to "fixed":
16915      Object attribute = getPropertyAttribute(BeanFilter.ATTRIBUTE_READONLY);
17015      if ((attribute != null) && (attribute instanceof Boolean)) {
1713        Boolean readonly = (Boolean) attribute;
1723        if (readonly.booleanValue()) {
1733          this.type = TYPE_FIXED;
174         }
175       }
176     }
177  
17845    if (this.type == null) {
179       // Check to see if field has options; default to "select":
18012      Object attribute = getPropertyAttribute(BeanFilter.ATTRIBUTE_OPTIONS);
18112      if ((attribute != null) && (attribute instanceof Map)) {
1821        this.type = TYPE_SELECT;
183       }
184     }
185  
18645    if (this.type == null) {
187       // Check to see if field boolean; default to "checkbox":
18811      Object attribute = getPropertyAttribute(BeanFilter.ATTRIBUTE_DATATYPE);
18911      if ((attribute != null) && (attribute.toString().equals(BOOLEAN_TYPE))) {
1901        this.type = TYPE_CHECKBOX;
191       }
192     }
193  
19445    if (this.type == null) {
195       // Check to see if maxlength > 255; default to "textarea":
19610      Object attribute = getPropertyAttribute(BeanFilter.ATTRIBUTE_MAXLENGTH);
19710      if ((attribute != null) && (attribute instanceof Number)) {
1982        Number maxlength = (Number) attribute;
1992        if (maxlength.intValue() > TEXTAREA_LENGTH) {
2002          this.type = TYPE_TEXTAREA;
201         }
202       }
203     }
204  
20545    if (this.type == null) {
206       // If all else fails; default to "text":
2078      this.type = TYPE_TEXT;
208     }
209  
21045    return this.type;
211   }
212  
213   /**
214    * Whether validations are supported. If not specified, defaults to
215    * value in nesting formtag.
216    *
217    * @param validations Whether validations are supported.
218    */
219   public void setValidations(Boolean validations) {
2204    this.validations = validations;
2214  }
222  
223   /**
224    * Whether validations are supported. May be null.
225    *
226    * @return Whether validations are supported.
227    */
228   public Boolean getValidations() {
22923    if ((this.validations == null) && (getFormTag() != null)) {
23019      this.validations = getFormTag().getValidations();
231     }
23223    if (this.validations == null) {
23319      this.validations = Boolean.TRUE;
234     }
23523    return validations;
236   }
237  
238   /**
239    * The name of the web application variable containing input items options.
240    *
241    * @param options The variable name for options.
242    */
243   public void setOptions(String options) {
2443    Object optionMap = this.pageContext.findAttribute(options);
2453    if (optionMap instanceof Map) {
2463      this.options = (Map) optionMap;
247     }
2483  }
249  
250   /**
251    * The option map for select lists. Uses the options map specified by the
252    * options attribute or the bean filters OPTIONS attribute.
253    *
254    * @return The option map.
255    * @throws JspException If the option map cannot be found.
256    */
257   public Map getOptionsMap() throws JspException {
2584    if (this.options == null) {
2591      Object attribute = getPropertyAttribute(BeanFilter.ATTRIBUTE_OPTIONS);
2601      if ((attribute == null) || !(attribute instanceof Map)) {
2610        throw new TagException(
262           JhtmlMessages.MESSAGE_SELECT_TAG_REQUIRES_OPTIONS);
263       }
2641      this.options = (Map) attribute;
265     }
2664    return this.options;
267   }
268  
269   /**
270    * Pass-through attribute. Same meaning as "class" in HTML.
271    *
272    * @param value The attribute value.
273    */
274   public void setStyleClass(String value) {
2751    addAttribute("class", value);
2761  }
277  
278   /**
279    * Pass-through attribute. Same meaning as in HTML.
280    *
281    * @param value The attribute value.
282    */
283   public void setId(String value) {
2841    addAttribute("id", value);
2851  }
286  
287   /**
288    * Pass-through attribute. Same meaning as in HTML.
289    *
290    * @param value The attribute value.
291    */
292   public void setLang(String value) {
2931    addAttribute("lang", value);
2941  }
295  
296   /**
297    * Pass-through attribute. Same meaning as in HTML.
298    *
299    * @param value The attribute value.
300    */
301   public void setStyle(String value) {
3021    addAttribute("style", value);
3031  }
304  
305   /**
306    * Pass-through attribute. Same meaning as in HTML.
307    *
308    * @param value The attribute value.
309    */
310   public void setTitle(String value) {
3111    addAttribute("title", value);
3121  }
313  
314   // input attributes
315  
316   /**
317    * Pass-through attribute. Same meaning as in HTML.
318    *
319    * @param value The attribute value.
320    */
321   public void setAccept(String value) {
3221    addAttribute("accept", value);
3231  }
324  
325   /**
326    * Pass-through attribute. Same meaning as in HTML.
327    *
328    * @param value The attribute value.
329    */
330   public void setAccesskey(String value) {
3311    addAttribute("accesskey", value);
3321  }
333  
334   /**
335    * Pass-through attribute. Same meaning as in HTML.
336    *
337    * @param value The attribute value.
338    */
339   public void setDisabled(String value) {
3401    addAttribute("disabled", value);
3411  }
342  
343   /**
344    * Pass-through attribute. Same meaning as in HTML.
345    *
346    * @param value The attribute value.
347    */
348   public void setMaxlength(String value) {
3491    addAttribute("maxlength", value);
3501  }
351  
352   /**
353    * Pass-through attribute. Same meaning as in HTML.
354    *
355    * @param value The attribute value.
356    */
357   public void setSize(String value) {
3582    addAttribute("size", value);
3592  }
360  
361   // textarea attributes
362  
363   /**
364    * Pass-through attribute. Same meaning as in HTML.
365    *
366    * @param value The attribute value.
367    */
368   public void setCols(String value) {
3691    addAttribute("cols", value);
3701  }
371  
372   /**
373    * Pass-through attribute. Same meaning as in HTML.
374    *
375    * @param value The attribute value.
376    */
377   public void setRows(String value) {
3781    addAttribute("rows", value);
3791  }
380  
381   /**
382    * Pass-through attribute. Same meaning as in HTML.
383    *
384    * @param value The attribute value.
385    */
386   public void setWrap(String value) {
3871    addAttribute("wrap", value);
3881  }
389  
390   /**
391    * Initialization. Makes "cols" attribute default to value of "size".
392    *
393    * @throws JspException For errors.
394    */
395   public void init() throws JspException {
39617    super.init();
39717    String size = getAttribute("size");
39817    if ((size != null) && (getAttribute("cols") == null)) {
3992      addAttribute("cols", size);
400     }
40117  }
402  
403   /**
404    * Print the input element.
405    *
406    * @return SKIP_BODY
407    * @throws JspException If the property cannot be initialized.
408    * @throws IOException For IO exceptions.
409    */
410   public int doStart() throws IOException, JspException {
41111    printInputElement();
41211    return SKIP_BODY;
413   }
414  
415   /**
416    * Print the input element.
417    *
418    * @throws JspException If the property cannot be initialized.
419    * @throws IOException For IO exceptions.
420    */
421   void printInputElement() throws JspException, IOException {
42215    String type = getType();
42315    if (type.equals(TYPE_CHECKBOX)) {
4242      printCheckboxTag();
42513    } else if (type.equals(TYPE_FIXED)) {
4261      printFixedTag();
42712    } else if (type.equals(TYPE_HIDDEN)) {
4281      printHiddenTag();
42911    } else if (type.equals(TYPE_PASSWORD)) {
4301      printPasswordTag();
43110    } else if (type.equals(TYPE_SELECT)) {
4322      printSelectTag();
4338    } else if (type.equals(TYPE_TEXT)) {
4346      printTextTag();
4352    } else if (type.equals(TYPE_TEXTAREA)) {
4362      printTextareaTag();
437     } else {
4380      throw new TagException(
439         JhtmlMessages.MESSAGE_INVALID_INPUT_TAG_TYPE,
440         new Object[] { type, TYPES_LIST });
441     }
442  
44315    if (Boolean.TRUE.equals(getValidations())) {
44415      Scripter.getInstance().printValidations(this, pageContext.getOut());
445     }
44615  }
447  
448   /**
449    * Print checkbox field.
450    *
451    * @throws IOException For IO exceptions.
452    * @throws JspException If the value cannot be found.
453    */
454   private void printCheckboxTag() throws IOException, JspException {
4552    JspWriter out = pageContext.getOut();
4562    out.print("<input type='checkbox' name='");
4572    out.print(getName());
4582    out.print("' value='true'");
4592    Object value = getFieldValue();
4602    if (TRUE_STRING.equals(value)) {
4611      out.print(" checked='checked'");
462     }
4632    printAttributes();
4642    out.print(" />");
4652  }
466  
467   /**
468    * Print fixed field.
469    *
470    * @throws IOException For IO exceptions.
471    * @throws JspException If the value cannot be found.
472    */
473   private void printFixedTag() throws IOException, JspException {
4741    JspWriter out = pageContext.getOut();
4751    out.print(getFieldValue());
4761    out.print("<input type='hidden' name='");
4771    out.print(getName());
4781    out.print("' value='");
4791    out.print(getFieldValue());
4801    out.print("'");
4811    printAttributes();
4821    out.print(" />");
4831  }
484  
485   /**
486    * Print hidden field.
487    *
488    * @throws IOException For IO exceptions.
489    * @throws JspException If the value cannot be found.
490    */
491   private void printHiddenTag() throws IOException, JspException {
4921    JspWriter out = pageContext.getOut();
4931    out.print("<input type='hidden' name='");
4941    out.print(getName());
4951    out.print("' value='");
4961    out.print(getFieldValue());
4971    out.print("'");
4981    printAttributes();
4991    out.print(" />");
5001  }
501  
502   /**
503    * Print password field.
504    *
505    * @throws IOException For IO exceptions.
506    * @throws JspException If the value cannot be found.
507    */
508   private void printPasswordTag() throws IOException, JspException {
5091    JspWriter out = pageContext.getOut();
5101    out.print("<input type='password' name='");
5111    out.print(getName());
5121    out.print("' value='");
5131    out.print(getFieldValue());
5141    out.print("'");
5151    printAttributes();
5161    out.print(" />");
5171  }
518  
519   /**
520    * Print select field.
521    *
522    * @throws IOException For IO exceptions.
523    * @throws JspException If the value cannot be found.
524    */
525   private void printSelectTag() throws IOException, JspException {
5262    Map options = getOptionsMap();
5272    Iterator keys = options.keySet().iterator();
528  
5292    JspWriter out = pageContext.getOut();
5302    out.print("<select name='");
5312    out.print(getName());
5322    out.print("'");
5332    printAttributes();
5342    out.print(">");
535  
5362    String value = getFieldValue();
53710    while (keys.hasNext()) {
5386      Object key = keys.next();
5396      out.print("<option value='");
5406      out.print(key.toString());
5416      if (key.toString().equals(value)) {
5422        out.print("' selected='selected");
543       }
5446      out.print("'>");
5456      out.print(options.get(key));
5466      out.print("</option>");
547     }
5482    out.print("</select>");
5492  }
550  
551   /**
552    * Print textarea field.
553    *
554    * @throws IOException For IO exceptions.
555    * @throws JspException If the property cannot be initialized.
556    */
557   private void printTextareaTag() throws IOException, JspException {
5582    JspWriter out = pageContext.getOut();
5592    out.print("<textarea name='");
5602    out.print(getName());
5612    out.print("'");
5622    printAttributes();
5632    out.print(">");
5642    out.print(getFieldValue());
5652    out.print("</textarea>");
5662  }
567  
568   /**
569    * Print text field.
570    *
571    * @throws IOException For IO exceptions.
572    * @throws JspException If the property cannot be initialized.
573    */
574   private void printTextTag() throws IOException, JspException {
5756    JspWriter out = pageContext.getOut();
5766    out.print("<input type='text' name='");
5776    out.print(getName());
5786    out.print("' value='");
5796    out.print(getFieldValue());
5806    out.print("'");
5816    printAttributes();
5826    out.print(" />");
5836  }
584 }

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.