KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > actions > validator > ValidatorBean


1 /*
2  * ValidatorBean.java
3  *
4  * Created on 24. August 2003, 17:37
5  */

6
7 package org.contineo.actions.validator;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10
11 import org.apache.commons.validator.Field;
12 import org.apache.commons.validator.GenericValidator;
13 import org.apache.commons.validator.ValidatorAction;
14 import org.apache.commons.validator.ValidatorUtil;
15 import org.apache.log4j.Level;
16 import org.apache.log4j.Logger;
17 import org.apache.struts.action.ActionErrors;
18 import org.apache.struts.validator.Resources;
19 import org.contineo.core.LoggingManager;
20 /**
21  *
22  * @author Michael Scholz
23  */

24 public class ValidatorBean {
25     
26     /** Creates a new instance of ValidatorBean */
27     public ValidatorBean() {
28     }
29
30     public static boolean validateIdentical(Object JavaDoc bean,ValidatorAction va,Field field,ActionErrors errors,HttpServletRequest JavaDoc request) {
31         String JavaDoc value = ValidatorUtil.getValueAsString(bean,field.getProperty());
32         String JavaDoc sProperty2 = field.getVarValue("secondProperty");
33         String JavaDoc value2 = ValidatorUtil.getValueAsString(bean, sProperty2);
34         if (!GenericValidator.isBlankOrNull(value)) {
35             try {
36                 if (!value.equals(value2)) {
37                     errors.add(field.getKey(),Resources.getActionError(request, va, field));
38                     return false;
39                 }
40             } catch (Exception JavaDoc e) {
41                 logError(e.getMessage());
42                 errors.add(field.getKey(), Resources.getActionError(request, va, field));
43             return false;
44             }
45         }
46         return true;
47     }
48     
49     public static boolean validateList(Object JavaDoc bean,ValidatorAction va,Field field,ActionErrors errors,HttpServletRequest JavaDoc request) {
50         String JavaDoc value = ValidatorUtil.getValueAsString(bean,field.getProperty());
51         String JavaDoc size = field.getVarValue("count");
52         //String value2 = ValidatorUtil.getValueAsString(bean, size);
53
boolean valid = true;
54         try {
55             int count = Integer.parseInt(size);
56             for (int i=0; i<count; i++) {
57                 String JavaDoc prop = field.getVarValue("field" + String.valueOf(i));
58                 String JavaDoc elem = ValidatorUtil.getValueAsString(bean, prop);
59                 if (GenericValidator.isBlankOrNull(elem))
60                     valid = false;
61             }
62             if (GenericValidator.isBlankOrNull(value))
63                 valid = false;
64         } catch (Exception JavaDoc e) {
65             logError(e.getMessage());
66             valid = false;
67         }
68         if (!valid)
69             errors.add(field.getKey(),Resources.getActionError(request, va, field));
70         return true;
71     }
72     
73     private static void logError(String JavaDoc message) {
74         Logger logger = LoggingManager.getLogger(org.contineo.actions.validator.ValidatorBean.class);
75         if (logger.isEnabledFor(Level.ERROR))
76             logger.error(message);
77     }
78 }
79
Popular Tags