1 21 22 package org.apache.commons.validator; 23 24 import org.apache.commons.validator.util.ValidatorUtils; 25 26 29 public class TestValidator { 30 31 43 public static boolean validateRaiseException( 44 final Object bean, 45 final Field field) 46 throws Exception { 47 48 final String value = 49 ValidatorUtils.getValueAsString(bean, field.getProperty()); 50 51 if ("RUNTIME".equals(value)) { 52 throw new RuntimeException ("RUNTIME-EXCEPTION"); 53 54 } else if ("CHECKED".equals(value)) { 55 throw new Exception ("CHECKED-EXCEPTION"); 56 57 } else { 58 throw new ValidatorException("VALIDATOR-EXCEPTION"); 59 } 60 } 61 62 69 public static boolean validateRequired(Object bean, Field field) { 70 String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 71 72 return !GenericValidator.isBlankOrNull(value); 73 } 74 75 83 public static boolean validateByte(Object bean, Field field) { 84 String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 85 86 return GenericValidator.isByte(value); 87 } 88 89 97 public static boolean validateShort(Object bean, Field field) { 98 String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 99 100 return GenericValidator.isShort(value); 101 } 102 103 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 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 139 public static boolean validateLong(Object bean, Field field) { 140 String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 141 142 return GenericValidator.isLong(value); 143 } 144 145 153 public static boolean validateFloat(Object bean, Field field) { 154 String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 155 156 return GenericValidator.isFloat(value); 157 } 158 159 167 public static boolean validateDouble(Object bean, Field field) { 168 String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 169 170 return GenericValidator.isDouble(value); 171 } 172 173 181 public static boolean validateEmail(Object bean, Field field) { 182 String value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 183 184 return GenericValidator.isEmail(value); 185 } 186 187 public final static String FIELD_TEST_NULL = "NULL"; 188 public final static String FIELD_TEST_NOTNULL = "NOTNULL"; 189 public final static String FIELD_TEST_EQUAL = "EQUAL"; 190 191 public static boolean validateRequiredIf( 192 Object bean, 193 Field field, 194 Validator validator) { 195 196 Object form = validator.getParameterValue(Validator.BEAN_PARAM); 197 String value = null; 198 boolean required = false; 199 if (isString(bean)) { 200 value = (String ) bean; 201 } else { 202 value = ValidatorUtils.getValueAsString(bean, field.getProperty()); 203 } 204 int i = 0; 205 String fieldJoin = "AND"; 206 if (!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))) { 207 fieldJoin = field.getVarValue("fieldJoin"); 208 } 209 if (fieldJoin.equalsIgnoreCase("AND")) { 210 required = true; 211 } 212 while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i + "]"))) { 213 String dependProp = field.getVarValue("field[" + i + "]"); 214 String dependTest = field.getVarValue("fieldTest[" + i + "]"); 215 String dependTestValue = field.getVarValue("fieldValue[" + i + "]"); 216 String dependIndexed = field.getVarValue("fieldIndexed[" + i + "]"); 217 if (dependIndexed == null) 218 dependIndexed = "false"; 219 String dependVal = null; 220 boolean this_required = false; 221 if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) { 222 String key = field.getKey(); 223 if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) { 224 String ind = key.substring(0, key.indexOf(".") + 1); 225 dependProp = ind + dependProp; 226 } 227 } 228 dependVal = ValidatorUtils.getValueAsString(form, dependProp); 229 if (dependTest.equals(FIELD_TEST_NULL)) { 230 if ((dependVal != null) && (dependVal.length() > 0)) { 231 this_required = false; 232 } else { 233 this_required = true; 234 } 235 } 236 if (dependTest.equals(FIELD_TEST_NOTNULL)) { 237 if ((dependVal != null) && (dependVal.length() > 0)) { 238 this_required = true; 239 } else { 240 this_required = false; 241 } 242 } 243 if (dependTest.equals(FIELD_TEST_EQUAL)) { 244 this_required = dependTestValue.equalsIgnoreCase(dependVal); 245 } 246 if (fieldJoin.equalsIgnoreCase("AND")) { 247 required = required && this_required; 248 } else { 249 required = required || this_required; 250 } 251 i++; 252 } 253 if (required) { 254 if ((value != null) && (value.length() > 0)) { 255 return true; 256 } else { 257 return false; 258 } 259 } 260 return true; 261 } 262 263 private static Class stringClass = new String ().getClass(); 264 265 private static boolean isString(Object o) { 266 if (o == null) return true; 267 return (stringClass.isInstance(o)); 268 } 269 270 } 271 | Popular Tags |