| 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.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 | */ | |
| 52 | 1 | public class DefaultScripter extends Scripter { |
| 53 | ||
| 54 | /** Mapping for Java types to FormValidation.js types. */ | |
| 55 | 1 | 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() { | |
| 63 | 1 | Map knownTypes = new HashMap(); |
| 64 | ||
| 65 | // From Java primitive types | |
| 66 | 1 | knownTypes.put("java.lang.Byte", "Integer"); |
| 67 | 1 | knownTypes.put("java.lang.Short", "Integer"); |
| 68 | 1 | knownTypes.put("java.lang.Integer", "Integer"); |
| 69 | 1 | knownTypes.put("java.lang.Long", "Integer"); |
| 70 | 1 | knownTypes.put("java.lang.Float", "Decimal"); |
| 71 | 1 | knownTypes.put("java.lang.Double", "Decimal"); |
| 72 | 1 | knownTypes.put("java.util.Date", "Date"); |
| 73 | ||
| 74 | // Types from FormValidation.js library | |
| 75 | 1 | knownTypes.put("Decimal", "Decimal"); |
| 76 | 1 | knownTypes.put("Date", "Date"); |
| 77 | 1 | knownTypes.put("SignedInteger", "SignedInteger"); |
| 78 | 1 | knownTypes.put("SignedDecimal", "SignedDecimal"); |
| 79 | 1 | knownTypes.put("Currency", "Currency"); |
| 80 | 1 | knownTypes.put("Phone", "Phone"); |
| 81 | 1 | knownTypes.put("SSN", "SSN"); |
| 82 | 1 | knownTypes.put("Postal", "Postal"); |
| 83 | 1 | knownTypes.put("Email", "Email"); |
| 84 | ||
| 85 | // Note: String type is not included because this | |
| 86 | // is the "default" type in HTML | |
| 87 | 1 | 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 { | |
| 101 | 17 | out.println(); |
| 102 | 17 | out.println("<script>"); |
| 103 | ||
| 104 | // Create validations object: | |
| 105 | 17 | out.print("validations."); |
| 106 | 17 | out.print(data.getName()); |
| 107 | 17 | out.print(" = new Validator('"); |
| 108 | 17 | out.print(data.getLocalizedAttribute(BeanFilter.ATTRIBUTE_LABEL)); |
| 109 | 17 | out.println("');"); |
| 110 | ||
| 111 | // Print required validation: | |
| 112 | 17 | Object required = data.getPropertyAttribute(BeanFilter.ATTRIBUTE_REQUIRED); |
| 113 | 17 | if (required != null) { |
| 114 | 0 | out.print("validations."); |
| 115 | 0 | out.print(data.getName()); |
| 116 | 0 | out.print(".required = "); |
| 117 | 0 | out.print(required); |
| 118 | 0 | out.println(";"); |
| 119 | 17 | } else if (!data.getType().equals(ValidationData.TYPE_CHECKBOX)) { |
| 120 | // Default to making properties required if not a checkbox: | |
| 121 | 15 | out.print("validations."); |
| 122 | 15 | out.print(data.getName()); |
| 123 | 15 | out.println(".required = true;"); |
| 124 | } | |
| 125 | ||
| 126 | // Print datatype validation: | |
| 127 | 17 | Object datatype = |
| 128 | DefaultScripter.KNOWN_DATATYPES.get( | |
| 129 | data.getPropertyAttribute(BeanFilter.ATTRIBUTE_DATATYPE)); | |
| 130 | 17 | if (datatype != null) { |
| 131 | 1 | out.print("validations."); |
| 132 | 1 | out.print(data.getName()); |
| 133 | 1 | out.print(".datatype = '"); |
| 134 | 1 | out.print(datatype); |
| 135 | 1 | out.println("';"); |
| 136 | } | |
| 137 | ||
| 138 | // Print other validations: | |
| 139 | 17 | printValidation(data, out, BeanFilter.ATTRIBUTE_READONLY); |
| 140 | 17 | printValidation(data, out, BeanFilter.ATTRIBUTE_MIN); |
| 141 | 17 | printValidation(data, out, BeanFilter.ATTRIBUTE_MAX); |
| 142 | 17 | printValidation(data, out, BeanFilter.ATTRIBUTE_MINLENGTH); |
| 143 | 17 | printValidation(data, out, BeanFilter.ATTRIBUTE_MAXLENGTH); |
| 144 | 17 | printValidation(data, out, BeanFilter.ATTRIBUTE_SCALE); |
| 145 | ||
| 146 | 17 | out.println("</script>"); |
| 147 | 17 | } |
| 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 { | |
| 163 | 102 | Object attribute = data.getPropertyAttribute(validation); |
| 164 | 102 | if (attribute != null) { |
| 165 | 3 | out.print("validations."); |
| 166 | 3 | out.print(data.getName()); |
| 167 | 3 | out.print("."); |
| 168 | 3 | out.print(validation); |
| 169 | 3 | out.print(" = "); |
| 170 | 3 | out.print(attribute); |
| 171 | 3 | out.println(";"); |
| 172 | } | |
| 173 | 102 | } |
| 174 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |