Coverage details for org.chwf.plugin.defaults.DefaultScripter

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.plugin.defaults;
34  
35 import java.io.IOException;
36 import java.util.HashMap;
37 import java.util.Map;
38  
39 import javax.servlet.jsp.JspException;
40 import javax.servlet.jsp.JspWriter;
41  
42 import org.chwf.filter.BeanFilter;
43 import org.chwf.plugin.Scripter;
44 import org.chwf.plugin.ValidationData;
45  
46 /**
47  * A default user plugin implementation that uses the FormValidation.js
48  * logic for client-side validation.
49  *
50  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
51  */
521public class DefaultScripter extends Scripter {
53  
54   /** Mapping for Java types to FormValidation.js types. */
551  private static final Map KNOWN_DATATYPES = initKnownDataTypes();
56  
57   /**
58    * Initializes the KNOWN_DATATYPES map.
59    *
60    * @return The data type map.
61    */
62   private static Map initKnownDataTypes() {
631    Map knownTypes = new HashMap();
64  
65     // From Java primitive types
661    knownTypes.put("java.lang.Byte", "Integer");
671    knownTypes.put("java.lang.Short", "Integer");
681    knownTypes.put("java.lang.Integer", "Integer");
691    knownTypes.put("java.lang.Long", "Integer");
701    knownTypes.put("java.lang.Float", "Decimal");
711    knownTypes.put("java.lang.Double", "Decimal");
721    knownTypes.put("java.util.Date", "Date");
73  
74     // Types from FormValidation.js library
751    knownTypes.put("Decimal", "Decimal");
761    knownTypes.put("Date", "Date");
771    knownTypes.put("SignedInteger", "SignedInteger");
781    knownTypes.put("SignedDecimal", "SignedDecimal");
791    knownTypes.put("Currency", "Currency");
801    knownTypes.put("Phone", "Phone");
811    knownTypes.put("SSN", "SSN");
821    knownTypes.put("Postal", "Postal");
831    knownTypes.put("Email", "Email");
84  
85     // Note: String type is not included because this
86     // is the "default" type in HTML
871    return knownTypes;
88   }
89  
90   /**
91    * Print JavaScript validation logic using the syntax of the
92    * FormValidation.js library and BeanFilter property metadata.<p>
93    *
94    * @param data The validation data.
95    * @param out The JspWriter for output.
96    * @throws IOException For IO exceptions.
97    * @throws JspException If the property cannot be initialized.
98    */
99   public void printValidations(ValidationData data, JspWriter out)
100     throws IOException, JspException {
10117    out.println();
10217    out.println("<script>");
103  
104     // Create validations object:
10517    out.print("validations.");
10617    out.print(data.getName());
10717    out.print(" = new Validator('");
10817    out.print(data.getLocalizedAttribute(BeanFilter.ATTRIBUTE_LABEL));
10917    out.println("');");
110  
111     // Print required validation:
11217    Object required = data.getPropertyAttribute(BeanFilter.ATTRIBUTE_REQUIRED);
11317    if (required != null) {
1140      out.print("validations.");
1150      out.print(data.getName());
1160      out.print(".required = ");
1170      out.print(required);
1180      out.println(";");
11917    } else if (!data.getType().equals(ValidationData.TYPE_CHECKBOX)) {
120       // Default to making properties required if not a checkbox:
12115      out.print("validations.");
12215      out.print(data.getName());
12315      out.println(".required = true;");
124     }
125  
126     // Print datatype validation:
12717    Object datatype =
128       DefaultScripter.KNOWN_DATATYPES.get(
129         data.getPropertyAttribute(BeanFilter.ATTRIBUTE_DATATYPE));
13017    if (datatype != null) {
1311      out.print("validations.");
1321      out.print(data.getName());
1331      out.print(".datatype = '");
1341      out.print(datatype);
1351      out.println("';");
136     }
137  
138     // Print other validations:
13917    printValidation(data, out, BeanFilter.ATTRIBUTE_READONLY);
14017    printValidation(data, out, BeanFilter.ATTRIBUTE_MIN);
14117    printValidation(data, out, BeanFilter.ATTRIBUTE_MAX);
14217    printValidation(data, out, BeanFilter.ATTRIBUTE_MINLENGTH);
14317    printValidation(data, out, BeanFilter.ATTRIBUTE_MAXLENGTH);
14417    printValidation(data, out, BeanFilter.ATTRIBUTE_SCALE);
145  
14617    out.println("</script>");
14717  }
148  
149   /**
150    * Print individual validation.
151    *
152    * @param data The validation data.
153    * @param out The JspWriter for output.
154    * @param validation The validation name.
155    * @throws IOException For IO exceptions.
156    * @throws JspException If the property cannot be initialized.
157    */
158   private void printValidation(
159     ValidationData data,
160     JspWriter out,
161     String validation)
162     throws IOException, JspException {
163102    Object attribute = data.getPropertyAttribute(validation);
164102    if (attribute != null) {
1653      out.print("validations.");
1663      out.print(data.getName());
1673      out.print(".");
1683      out.print(validation);
1693      out.print(" = ");
1703      out.print(attribute);
1713      out.println(";");
172     }
173102  }
174 }

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.