KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > validators > IntegerRangeValidator


1 package dinamica.validators;
2
3 import java.util.HashMap JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import dinamica.*;
6
7 /**
8  * Generic validator for integer value ranges (value-from - value-to).<br>
9  * Will return FALSE if value2 &lt; value1, requires two custom attributes named "value1" and "value2",
10  * representing the field names of the two recordset fields to use
11  * in the validation logic. Returns TRUE if any of the parameters is null.
12  *
13  * <br>
14  * Creation date: 3/march/2004<br>
15  * Last Update: 3/march/2004<br>
16  * (c) 2004 Martin Cordova<br>
17  * This code is released under the LGPL license<br>
18  * @author Martin Cordova (dinamica@martincordova.com)
19  * */

20 public class IntegerRangeValidator extends AbstractValidator
21 {
22
23     /* (non-Javadoc)
24      * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
25      */

26     public boolean isValid(
27         HttpServletRequest JavaDoc req,
28         Recordset inputParams,
29         HashMap JavaDoc attribs)
30         throws Throwable JavaDoc
31     {
32         String JavaDoc v1 = (String JavaDoc)attribs.get("value1");
33         String JavaDoc v2 = (String JavaDoc)attribs.get("value2");
34         
35         if (v1==null || v2==null)
36             throw new Throwable JavaDoc("Invalid attributes 'value1' or 'value2' - cannot be null.");
37         
38         if (inputParams.isNull(v1) || inputParams.isNull(v2))
39             return true;
40             
41         int d1 = inputParams.getInt(v1);
42         int d2 = inputParams.getInt(v2);
43         
44         if ( d1 > d2 )
45             return false;
46         else
47             return true;
48             
49     }
50
51 }
52
Popular Tags