KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > validators > TimeValidator


1 package dinamica.validators;
2
3 import java.util.HashMap JavaDoc;
4 import javax.servlet.http.HttpServletRequest JavaDoc;
5 import dinamica.*;
6
7 /**
8  * This validator returns FALSE if the corresponding field
9  * cannot be parsed as a valid time with format HH:mm<br>
10  * If parameter's value is NULL then the validator returns TRUE
11  * <br><br>
12  * Requires the following custom attributes:<br>
13  * <ul>
14  * <li> parameter: Name of the request parameter to validate. This parameter
15  * MUST be defined in validator.xml.
16  * </ul>
17  *
18  * (c) 2005 Martin Cordova<br>
19  * This code is released under the LGPL license<br>
20  * Dinamica Framework - http://www.martincordova.com<br>
21  * @author Martin Cordova (dinamica@martincordova.com)
22  * */

23 public class TimeValidator extends AbstractValidator
24 {
25
26     /* (non-Javadoc)
27      * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
28      */

29     public boolean isValid(HttpServletRequest JavaDoc req, Recordset inputParams, HashMap JavaDoc attribs) throws Throwable JavaDoc
30     {
31
32         boolean flag = true;
33         
34         //detect if sql parameter was passed to the validator
35
boolean bParam = attribs.containsKey("parameter");
36
37         if (!bParam)
38         {
39
40             throw new Throwable JavaDoc("[" + this.getClass().getName() + "] Missing attribute [parameter] in validator.xml");
41
42         }
43         else
44         {
45
46             //read config
47
String JavaDoc paramName = (String JavaDoc)attribs.get("parameter");
48                         
49             //get parameter value if available
50
if (!inputParams.isNull(paramName))
51             {
52                 String JavaDoc time = inputParams.getString(paramName);
53                 
54                 //validate as TIME with 24hrs format
55
try {
56                     StringUtil.getDateObject(time,"HH:mm");
57                 } catch (Throwable JavaDoc e) {
58                     flag = false;
59                 }
60             }
61
62         }
63         
64         return flag;
65
66     }
67
68 }
69
Popular Tags