View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator;
18  
19  import org.apache.commons.validator.util.ValidatorUtils;
20                                                            
21  /**                                                       
22   * Contains validation methods for different unit tests.
23   *
24   * @version $Revision$
25   */
26  public class GenericValidatorImpl {
27            
28      /**
29       * Throws a runtime exception if the value of the argument is "RUNTIME", 
30       * an exception if the value of the argument is "CHECKED", and a 
31       * ValidatorException otherwise.
32       * 
33       * @throws RuntimeException with "RUNTIME-EXCEPTION as message"
34       * if value is "RUNTIME"
35       * @throws Exception with "CHECKED-EXCEPTION" as message 
36       * if value is "CHECKED"
37       * @throws ValidatorException with "VALIDATOR-EXCEPTION" as message  
38       * otherwise
39       */
40      public static boolean validateRaiseException(
41          final Object bean,
42          final Field field)
43          throws Exception {
44              
45          final String value =
46              ValidatorUtils.getValueAsString(bean, field.getProperty());
47              
48          if ("RUNTIME".equals(value)) {
49              throw new RuntimeException("RUNTIME-EXCEPTION");
50              
51          } else if ("CHECKED".equals(value)) {
52              throw new Exception("CHECKED-EXCEPTION");
53              
54          } else {
55              throw new ValidatorException("VALIDATOR-EXCEPTION");
56          }
57      }
58                                                            
59     /**
60      * Checks if the field is required.
61      *
62      * @return boolean If the field isn't <code>null</code> and
63      * has a length greater than zero, <code>true</code> is returned.  
64      * Otherwise <code>false</code>.
65      */
66     public static boolean validateRequired(Object bean, Field field) {
67        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
68  
69        return !GenericValidator.isBlankOrNull(value);
70     }
71  
72     /**
73      * Checks if the field can be successfully converted to a <code>byte</code>.
74      *
75      * @param bean The value validation is being performed on.
76      * @param field the field to use
77      * @return    boolean        If the field can be successfully converted 
78      *                           to a <code>byte</code> <code>true</code> is returned.  
79      *                           Otherwise <code>false</code>.
80      */
81     public static boolean validateByte(Object bean, Field field) {
82        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
83  
84        return GenericValidator.isByte(value);
85     }
86  
87     /**
88      * Checks if the field can be successfully converted to a <code>short</code>.
89      *
90      * @param bean The value validation is being performed on.
91      * @param field the field to use
92      * @return    boolean        If the field can be successfully converted 
93      *                           to a <code>short</code> <code>true</code> is returned.  
94      *                           Otherwise <code>false</code>.
95      */
96     public static boolean validateShort(Object bean, Field field) {
97        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
98  
99        return GenericValidator.isShort(value);
100    }
101 
102    /**
103     * Checks if the field can be successfully converted to a <code>int</code>.
104     *
105     * @param bean The value validation is being performed on.
106     * @param field the field to use
107     * @return    boolean        If the field can be successfully converted 
108     *                           to a <code>int</code> <code>true</code> is returned.  
109     *                           Otherwise <code>false</code>.
110     */
111    public static boolean validateInt(Object bean, Field field) {
112       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
113 
114       return GenericValidator.isInt(value);
115    }
116 
117    /**
118     * Checks if field is positive assuming it is an integer
119     * 
120     * @param    bean       The value validation is being performed on.
121     * @param    field       Description of the field to be evaluated
122     * @return   boolean     If the integer field is greater than zero, returns
123     *                        true, otherwise returns false.
124     */
125    public static boolean validatePositive(Object bean , Field field) {
126       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
127    
128       return GenericTypeValidator.formatInt(value).intValue() > 0;                                                      
129    }
130 
131    /**
132     * Checks if the field can be successfully converted to a <code>long</code>.
133     *
134     * @param bean The value validation is being performed on.
135     * @param field the field to use
136     * @return    boolean        If the field can be successfully converted 
137     *                           to a <code>long</code> <code>true</code> is returned.  
138     *                           Otherwise <code>false</code>.
139     */
140    public static boolean validateLong(Object bean, Field field) {
141       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
142 
143       return GenericValidator.isLong(value);
144    }
145 
146    /**
147     * Checks if the field can be successfully converted to a <code>float</code>.
148     *
149     * @param bean The value validation is being performed on.
150     * @param field the field to use
151     * @return    boolean        If the field can be successfully converted 
152     *                           to a <code>float</code> <code>true</code> is returned.  
153     *                           Otherwise <code>false</code>.
154     */
155    public static boolean validateFloat(Object bean, Field field) {
156       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
157 
158       return GenericValidator.isFloat(value);
159    }
160    
161    /**
162     * Checks if the field can be successfully converted to a <code>double</code>.
163     *
164     * @param bean The value validation is being performed on.
165     * @param field the field to use
166     * @return    boolean        If the field can be successfully converted 
167     *                           to a <code>double</code> <code>true</code> is returned.  
168     *                           Otherwise <code>false</code>.
169     */
170    public static boolean validateDouble(Object bean, Field field) {
171       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
172 
173       return GenericValidator.isDouble(value);
174    }
175 
176    /**
177     * Checks if the field is an e-mail address.
178     *
179     * @param bean The value validation is being performed on.
180     * @param field the field to use
181     * @return    boolean        If the field is an e-mail address
182     *                           <code>true</code> is returned.  
183     *                           Otherwise <code>false</code>.
184     */
185    public static boolean validateEmail(Object bean, Field field) {
186       String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
187 
188       return GenericValidator.isEmail(value);
189    }
190 
191   public final static String FIELD_TEST_NULL = "NULL";
192   public final static String FIELD_TEST_NOTNULL = "NOTNULL";
193   public final static String FIELD_TEST_EQUAL = "EQUAL";
194 
195     public static boolean validateRequiredIf(
196         Object bean,
197         Field field,
198         Validator validator) {
199     
200         Object form = validator.getParameterValue(Validator.BEAN_PARAM);
201         String value = null;
202         boolean required = false;
203         if (isStringOrNull(bean)) {
204             value = (String) bean;
205         } else {
206             value = ValidatorUtils.getValueAsString(bean, field.getProperty());
207         }
208         int i = 0;
209         String fieldJoin = "AND";
210         if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) {
211             fieldJoin = field.getVarValue("fieldJoin");
212         }
213         if (fieldJoin.equalsIgnoreCase("AND")) {
214             required = true;
215         }
216         while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) {
217             String dependProp = field.getVarValue("field[" + i + "]");
218             String dependTest = field.getVarValue("fieldTest[" + i + "]");
219             String dependTestValue = field.getVarValue("fieldValue[" + i + "]");
220             String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]");
221             if (dependIndexed == null) {
222                 dependIndexed = "false";
223             }
224             String dependVal = null;
225             boolean this_required = false;
226             if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
227                 String key = field.getKey();
228                 if ((key.contains("[")) && (key.contains("]"))) {
229                     String ind = key.substring(0, key.indexOf(".") + 1);
230                     dependProp = ind + dependProp;
231                 }
232             }
233             dependVal = ValidatorUtils.getValueAsString(form, dependProp);
234             if (dependTest.equals(FIELD_TEST_NULL)) {
235                 if ((dependVal != null) && (dependVal.length() > 0)) {
236                     this_required = false;
237                 } else {
238                     this_required = true;
239                 }
240             }
241             if (dependTest.equals(FIELD_TEST_NOTNULL)) {
242                 if ((dependVal != null) && (dependVal.length() > 0)) {
243                     this_required = true;
244                 } else {
245                     this_required = false;
246                 }
247             }
248             if (dependTest.equals(FIELD_TEST_EQUAL)) {
249                 this_required = dependTestValue.equalsIgnoreCase(dependVal);
250             }
251             if (fieldJoin.equalsIgnoreCase("AND")) {
252                 required = required && this_required;
253             } else {
254                 required = required || this_required;
255             }
256             i++;
257         }
258         if (required) {
259             if ((value != null) && (value.length() > 0)) {
260                 return true;
261             } else {
262                 return false;
263             }
264         }
265         return true;
266     }
267   
268   private static boolean isStringOrNull(Object o) {
269     if (o == null) {
270         return true; // TODO this condition is not exercised by any tests currently
271     }
272     return (o instanceof String);
273   }
274       
275 }