Source code: org/javahispano/canyamo/util/html/form/FormValidator.java
1 /*
2 Caņamo, portal framework
3 Copyright (c) 2002
4 Alberto Molpeceres, javaHispano (http://www.javahispano.org)
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 Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14 Neither the name of Alberto Molpeceres, javaHispano nor the names of its
15 contributors may be used to endorse or promote products derived from
16 this software without specific prior written permission.
17 .
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.javahispano.canyamo.util.html.form;
31
32 import java.util.*;
33 import java.text.SimpleDateFormat;
34 import javax.xml.parsers.*;
35 import org.xml.sax.*;
36 import org.xml.sax.helpers.*;
37 import org.javahispano.canyamo.core.WorkData;
38 import org.javahispano.canyamo.core.CanyamoLog;
39 import org.javahispano.canyamo.core.config.CanyamoConfig;
40
41 /**
42 * Validate and parse an HTML forms. This class has two primary functions: <br>
43 * <br>
44 * - First, be sure that all requiered fields are entered<br>
45 * - Second, be sure that entered data follows the given rules<br>
46 * <br>
47 * <br>
48 * The configuration info is given in a XML file. <br>
49 * <br>
50 * TO DO: talk about config-XML's format <br>
51 * <br>
52 *
53 *
54 *@author <A href="al AT javahispano DOT org">Alberto Molpeceres</A>
55 *@created 18 October 2002
56 *@version 1.0
57 */
58 public class FormValidator {
59
60 /** Description of the Field */
61 protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
62
63 /** Description of the Field */
64 protected Map fields = null;
65 /** Description of the Field */
66 protected List rules = null;
67
68 /** Description of the Field */
69 protected WorkData data;
70
71
72 /**
73 * Constructor for the FormValidator object
74 *
75 *@param configFile Description of Parameter
76 */
77 public FormValidator(String configFile) {
78 this.fields = new HashMap();
79 this.rules = new ArrayList();
80 try {
81 configFile = CanyamoConfig.getValue("config-root") + configFile;
82 SAXParserFactory spf = SAXParserFactory.newInstance();
83 SAXParser saxParser = spf.newSAXParser();
84 XMLReader xmlReader = saxParser.getXMLReader();
85 xmlReader.setContentHandler(new FormHandler(this));
86 CanyamoLog.log("[FormValidator] loading form config from "
87 + configFile);
88 xmlReader.parse(configFile);
89 } catch (Exception e) {
90 CanyamoLog.error("[FormValidator] Unable to load configFile: " +
91 e.getMessage());
92 }
93 }
94
95
96 /**
97 * Returns an Object of the given type for the given parameter.
98 *
99 *@param key Parameter's name
100 *@param data Description of Parameter
101 *@return Object of the given type with the value
102 */
103 public Object getParameter(WorkData data, String key) {
104 String type = (String) fields.get(key);
105 type = type.toLowerCase();
106 String value = data.getParameter(key);
107 if ((value != null) && (!"SET".equals(value.toUpperCase()))) {
108 try {
109 if (type.equals("int")) {
110 return new Integer(value.trim());
111 } else if (type.equals("float")) {
112 return new Float(value.trim());
113 } else if (type.equals("date")) {
114 return FormValidator.sdf.parse(value.trim());
115 }
116 } catch (Exception e) {
117 CanyamoLog.error("[FormValidator.getParameter] Cannot convert object: "
118 + e.getMessage());
119 return value;
120 }
121 }
122 //test if have a "set rule"
123 else {
124 Object fieldValue = data.getAttribute(key);
125 if (fieldValue != null) {
126 return fieldValue;
127 }
128 }
129 return value;
130 }
131
132
133 /**
134 * Validates a posted form following the rules descripted in it's xml config
135 * file.
136 *
137 *@param data User's request
138 *@exception FormException Exception if posted data don't match the given
139 * rules
140 */
141 public void validate(WorkData data)
142 throws FormException {
143 Iterator i = rules.iterator();
144 FormRule rule;
145 while (i.hasNext()) {
146 rule = (FormRule) i.next();
147 CanyamoLog.debug("[FormValidator] Testing form rule: " + rule);
148 rule.test((String) fields.get(rule.getField()), data);
149 }
150 }
151
152
153 /**
154 * Sets the format for date-fields
155 *
156 *@param format Description of Parameter
157 */
158 protected void setDateFormat(String format) {
159 sdf = new SimpleDateFormat(format);
160 }
161
162
163 /**
164 * Adds a feature to the Field attribute of the FormValidator object
165 *
166 *@param name The feature to be added to the Field attribute
167 *@param type The feature to be added to the Field attribute
168 */
169 protected void addField(String name, String type) {
170 fields.put(name, type);
171 }
172
173
174 /**
175 * Adds a feature to the Rule attribute of the FormValidator object
176 *
177 *@param rule The feature to be added to the Rule attribute
178 */
179 protected void addRule(FormRule rule) {
180 rules.add(rule);
181 }
182
183 }
184