Source code: de/caffeine/jargus/Validation.java
1 /*
2 * This file is part of Jargus.
3 *
4 * Jargus is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Jargus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with Jargus; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * copyright : (C) 2001 by Thomas Foertsch
19 * email : thomas@caffeine.de
20 */
21
22 package de.caffeine.jargus;
23
24 //java
25 import java.util.Map;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Collection;
29 import java.util.ArrayList;
30 import java.util.Enumeration;
31
32 //servlet
33 import javax.servlet.http.*;
34 import javax.servlet.*;
35
36 //caffeine
37 import de.caffeine.util.*;
38
39 /**
40 * <p>This class provides an simple framework for http parameter validation,
41 * useful in java servlets, jsp pages (using taglibs) and other java based
42 * application with the need of validation of parameters before processing.</p>
43 * <p>It comes with common validation cases, which can be extended with appropriate
44 * validations by implementing validator interface.</p>
45 * <p>Design of these classes was lead by the feeling that implementation of
46 * validation and processing of validation results have to be separated and
47 * plugable. It contains three basic elements Validator which carry out validation
48 * as simple and encapsulated operations, ValidationHandler which receive
49 * notifications of the results of the done validations and Validation
50 * which does does the background processing of starting Validator objects,
51 * notify ValidationHandler and handling provided parameters.</p>
52 * <p>Validation is the only fixed part in this design, as Validator
53 * and ValidationHandlers are merely interfaces with example implementations.
54 * </p>
55 * <h4>Usage:</h4>
56 * <p>Create a Validation object:<br>
57 * <pre>
58 * Validation pv = new Validation();
59 * </pre>
60 * </p>
61 * <p>
62 * Create a object implemeting ValidationHandler interface and set it as ValidationHandler for
63 * Validation object:<br>
64 * <pre>
65 * ValidationHandler handler = new SimpleValidationHandler();
66 * pv.setHandler(handler);
67 * </pre>
68 * ValidationHandler object will be notified in case of validation errors
69 * and warning (not implemented yet).
70 * </p>
71 * <p>
72 * Set parameter keys and values submitted from client:<br>
73 * <pre>
74 * pv.addParameter("parameter1", "1v");
75 * pv.addParameter("parameter2", "2v");
76 * </pre>
77 * aquivalent to:<br>
78 * <pre>
79 * HashMap parameters;
80 * pv.setParameters(parameters);
81 * </pre>
82 * or: <br>
83 * <pre>
84 * ServletRequest request;
85 * pv.addParameters(request);
86 * </pre>
87 * <p>Different kinds of validation can be managed using objects of classes implementing
88 * Validator interface. These object are added with addCheck(Validator) method.
89 * </p>
90 * <pre>
91 * Validator existValidator = new ExistValidator("parameter1");
92 * pv.addCheck(existValidator);
93 * </pre>
94 * <p>
95 * <p>Depending on the kind of validation optional and mandatory arguments
96 * can be provided.</p>
97 * <pre>
98 * EqualValidator ev = new EqualValidator();
99 * ev.addValidationParameter("e1");
100 * ev.addValidationParameter("e2");
101 * ev.addValidationArgument("2");
102 * pv.addCheck(ev);
103 *
104 * ContainsValidator contains = new ContainsValidator();
105 * contains.addValidationParameter("email");
106 * contains.addValidationArgument("@");
107 * pv.addCheck(contains);
108 * </pre>
109 * <p>The actual validation is started with validate methode. Which will
110 * execute all added validators and notify ValidationHandler object in
111 * case of validation errors.</p>
112 * <pre>
113 * pv.validate();
114 * </pre>
115 *
116 * @see Validator
117 * @see ValidationHandler
118 *
119 * @author Thomas Foertsch
120 * @version $Id: Validation.java,v 1.2 2001/04/25 18:17:32 blob79 Exp $
121 */
122 public class Validation {
123
124 /**
125 * All known handlers
126 */
127 private ValidationHandler _handler;
128
129 /**
130 * Parameters of this Request
131 */
132 private HashMap _parameters;
133
134 /**
135 * Executed Checks
136 */
137 private ArrayList _checks;
138
139 /**
140 * @link aggregation
141 */
142 private Validator lnkValidator;
143
144 /**
145 *
146 */
147 public Validation() {
148 //init
149
150 _parameters = new HashMap();
151 _checks = new ArrayList();
152 }
153
154 /**
155 * Set Parameters of current Request.
156 */
157 public void setParameters(HashMap parameters){
158 this._parameters = parameters;
159
160 }
161
162 /**
163 * Set Parameters of current Request.
164 * @param request
165 */
166 public void setParameters(ServletRequest request){
167 Enumeration paramKeys = request.getParameterNames();
168 while (paramKeys.hasMoreElements()){
169 Object key = paramKeys.nextElement();
170
171 _parameters.put(key, request.getParameter((String) key));
172
173 }
174
175 }
176 /**
177 * Add Parameter.
178 */
179 public void addParameter(String key, String value){
180 this._parameters.put(key,value);
181 }
182
183 /**
184 * @return Parameter value
185 */
186 public String getParameter(String key){
187 return (String) this._parameters.get(key);
188 }
189
190 /**
191 * Add a new Check.
192 * @param validator Validator which performs the check
193 */
194 public void addCheck(Validator validator){
195
196 _checks.add(validator);
197
198 }
199
200 /**
201 * Remove a Validator
202 */
203 public void removeCheck(Validator validator){
204 _checks.remove(validator);
205 }
206
207 /**
208 * Remove all Validators
209 */
210 public void removeAllChecks(){
211 _checks.clear();
212
213 }
214
215 /**
216 * Add handler which will be notified in case of errors and warnings.
217 */
218 public void setHandler(ValidationHandler handler){
219 this._handler = handler;
220
221 }
222
223 /**
224 * Get handler.
225 */
226 public ValidationHandler getHandler(){
227 return _handler;
228
229 }
230
231 /**
232 * Process validation.
233 * @return Whether validation failed
234 */
235 public boolean validate(){
236 _handler.start();
237
238 boolean failed = validate(_checks);
239
240 if (failed) _handler.ok();
241 else _handler.failed();
242
243 return failed;
244 }
245
246 /**
247 * Process validation
248 */
249 private boolean validate(ArrayList parametersToCheck){
250
251 boolean allValide = true;
252
253 //Call all Validators
254 Iterator itParametersToCheck = parametersToCheck.iterator();
255 while(itParametersToCheck.hasNext()){
256 Validator va = (Validator) itParametersToCheck.next();
257
258 if (!va.validate(_parameters)){
259 //notify
260 _handler.error(va);
261 allValide = false;
262
263 }
264
265 }
266 return allValide;
267
268 }
269
270
271
272
273 }