01: package dinamica.validators;
02:
03: import java.util.HashMap;
04: import javax.servlet.http.HttpServletRequest;
05: import dinamica.*;
06:
07: /**
08: * This validator returns FALSE if the corresponding field
09: * 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: /* (non-Javadoc)
26: * @see dinamica.AbstractValidator#isValid(javax.servlet.http.HttpServletRequest, dinamica.Recordset, java.util.HashMap)
27: */
28: public boolean isValid(HttpServletRequest req,
29: Recordset inputParams, HashMap<String, String> attribs)
30: throws Throwable {
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: throw new Throwable(
40: "["
41: + this .getClass().getName()
42: + "] Missing attribute [parameter] in validator.xml");
43:
44: } else {
45:
46: //read config
47: String paramName = (String) attribs.get("parameter");
48:
49: //get parameter value if available
50: if (!inputParams.isNull(paramName)) {
51: String time = inputParams.getString(paramName);
52:
53: //validate as TIME with 24hrs format
54: try {
55: StringUtil.getDateObject(time, "HH:mm");
56: } catch (Throwable e) {
57: flag = false;
58: }
59: }
60:
61: }
62:
63: return flag;
64:
65: }
66:
67: }
|