KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > validators > CommonsValidator


1 package org.infoglue.cms.util.validators;
2
3 import org.apache.commons.validator.Field;
4 import org.apache.commons.validator.GenericTypeValidator;
5 import org.apache.commons.validator.GenericValidator;
6 import org.apache.commons.validator.Validator;
7 import org.apache.commons.validator.ValidatorException;
8 import org.apache.commons.validator.util.ValidatorUtils;
9
10 /**
11  * Contains validation methods for different unit tests.
12  */

13 public class CommonsValidator {
14           
15     /**
16      * Throws a runtime exception if the value of the argument is "RUNTIME",
17      * an exception if the value of the argument is "CHECKED", and a
18      * ValidatorException otherwise.
19      *
20      * @param value string which selects type of exception to generate
21      * @throws RuntimeException with "RUNTIME-EXCEPTION as message"
22      * if value is "RUNTIME"
23      * @throws Exception with "CHECKED-EXCEPTION" as message
24      * if value is "CHECKED"
25      * @throws ValidatorException with "VALIDATOR-EXCEPTION" as message
26      * otherwise
27      */

28     public static boolean validateRaiseException(
29         final Object JavaDoc bean,
30         final Field field)
31         throws Exception JavaDoc {
32             
33         final String JavaDoc value =
34             ValidatorUtils.getValueAsString(bean, field.getProperty());
35             
36         if ("RUNTIME".equals(value)) {
37             throw new RuntimeException JavaDoc("RUNTIME-EXCEPTION");
38             
39         } else if ("CHECKED".equals(value)) {
40             throw new Exception JavaDoc("CHECKED-EXCEPTION");
41             
42         } else {
43             throw new ValidatorException("VALIDATOR-EXCEPTION");
44         }
45     }
46                                                           
47    /**
48     * Checks if the field is required.
49     *
50     * @param value The value validation is being performed on.
51     * @return boolean If the field isn't <code>null</code> and
52     * has a length greater than zero, <code>true</code> is returned.
53     * Otherwise <code>false</code>.
54     */

55    public static boolean validateRequired(Object JavaDoc bean, Field field) {
56       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
57       return !GenericValidator.isBlankOrNull(value);
58    }
59
60    /**
61     * Checks if the field can be successfully converted to a <code>byte</code>.
62     *
63     * @param value The value validation is being performed on.
64     * @return boolean If the field can be successfully converted
65     * to a <code>byte</code> <code>true</code> is returned.
66     * Otherwise <code>false</code>.
67     */

68    public static boolean validateByte(Object JavaDoc bean, Field field) {
69       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
70
71       return GenericValidator.isByte(value);
72    }
73
74    /**
75     * Checks if the field can be successfully converted to a <code>short</code>.
76     *
77     * @param value The value validation is being performed on.
78     * @return boolean If the field can be successfully converted
79     * to a <code>short</code> <code>true</code> is returned.
80     * Otherwise <code>false</code>.
81     */

82    public static boolean validateShort(Object JavaDoc bean, Field field) {
83       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
84
85       return GenericValidator.isShort(value);
86    }
87
88    /**
89     * Checks if the field can be successfully converted to a <code>int</code>.
90     *
91     * @param value The value validation is being performed on.
92     * @return boolean If the field can be successfully converted
93     * to a <code>int</code> <code>true</code> is returned.
94     * Otherwise <code>false</code>.
95     */

96    public static boolean validateInt(Object JavaDoc bean, Field field) {
97       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
98
99       return GenericValidator.isInt(value);
100    }
101
102    /**
103     * Checks if field is positive assuming it is an integer
104     *
105     * @param value The value validation is being performed on.
106     * @param field Description of the field to be evaluated
107     * @return boolean If the integer field is greater than zero, returns
108     * true, otherwise returns false.
109     */

110    public static boolean validatePositive(Object JavaDoc bean , Field field) {
111       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
112    
113       return GenericTypeValidator.formatInt(value).intValue() > 0;
114    }
115
116    /**
117     * Checks if the field can be successfully converted to a <code>long</code>.
118     *
119     * @param value The value validation is being performed on.
120     * @return boolean If the field can be successfully converted
121     * to a <code>long</code> <code>true</code> is returned.
122     * Otherwise <code>false</code>.
123     */

124    public static boolean validateLong(Object JavaDoc bean, Field field) {
125       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
126
127       return GenericValidator.isLong(value);
128    }
129
130    /**
131     * Checks if the field can be successfully converted to a <code>float</code>.
132     *
133     * @param value The value validation is being performed on.
134     * @return boolean If the field can be successfully converted
135     * to a <code>float</code> <code>true</code> is returned.
136     * Otherwise <code>false</code>.
137     */

138    public static boolean validateFloat(Object JavaDoc bean, Field field) {
139       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
140
141       return GenericValidator.isFloat(value);
142    }
143    
144    /**
145     * Checks if the field can be successfully converted to a <code>double</code>.
146     *
147     * @param value The value validation is being performed on.
148     * @return boolean If the field can be successfully converted
149     * to a <code>double</code> <code>true</code> is returned.
150     * Otherwise <code>false</code>.
151     */

152    public static boolean validateDouble(Object JavaDoc bean, Field field) {
153       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
154
155       return GenericValidator.isDouble(value);
156    }
157
158    /**
159     * Checks if the field is an e-mail address.
160     *
161     * @param value The value validation is being performed on.
162     * @return boolean If the field is an e-mail address
163     * <code>true</code> is returned.
164     * Otherwise <code>false</code>.
165     */

166    public static boolean validateEmail(Object JavaDoc bean, Field field) {
167       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
168
169       return GenericValidator.isEmail(value);
170    }
171
172    /**
173     * Checks if the field value matches a regexp.
174     */

175    public static boolean validateRegexp(Object JavaDoc bean, Field field) {
176       String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
177       String JavaDoc regexp = field.getVarValue("regexp");
178       
179       //boolean valid = GenericValidator.matchRegexp(value, regexp);
180
boolean valid = value.matches(regexp);
181       return valid;
182    }
183    
184   public final static String JavaDoc FIELD_TEST_NULL = "NULL";
185   public final static String JavaDoc FIELD_TEST_NOTNULL = "NOTNULL";
186   public final static String JavaDoc FIELD_TEST_EQUAL = "EQUAL";
187
188   public static boolean validateRequiredIf(Object JavaDoc bean, Field field, Validator validator)
189   {
190       final String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
191       final String JavaDoc dependentValue = ValidatorUtils.getValueAsString(bean, field.getVarValue("dependent"));
192       return dependentValue == null || dependentValue.length() == 0 || (value != null && value.length() > 0);
193   }
194   
195   private static Class JavaDoc stringClass = new String JavaDoc().getClass();
196
197   private static boolean isString(Object JavaDoc o) {
198     if (o == null) return true;
199     return (stringClass.isInstance(o));
200   }
201       
202 }
203
Popular Tags