1 7 package com.inversoft.util.typevalidator; 8 9 10 import java.text.MessageFormat ; 11 import java.util.Locale ; 12 13 import com.inversoft.beans.BeanException; 14 import com.inversoft.beans.NestedBeanProperty; 15 import com.inversoft.error.BasicError; 16 import com.inversoft.error.ErrorRegistry; 17 import com.inversoft.error.PropertyError; 18 19 20 29 public abstract class BaseTypeValidator implements TypeValidator { 30 31 34 public BaseTypeValidator() { 35 } 37 38 39 56 public BasicError validate(Object value, Object params, String message, 57 Object [] mesgParams) { 58 String error = internalValidate(value, params, message, null, mesgParams); 59 BasicError be = null; 60 if (error != null) { 61 be = new BasicError(error); 62 } 63 64 return be; 65 } 66 67 90 public BasicError validate(Object value, Object params, String bundleName, 91 String key, Locale locale, Object [] mesgParams, Object caller) { 92 String error = ErrorRegistry.getError(bundleName, key, locale, caller); 93 error = internalValidate(value, params, error, null, mesgParams); 94 95 BasicError be = null; 96 if (error != null) { 97 be = new BasicError(error); 98 } 99 100 return be; 101 } 102 103 122 public PropertyError validate(String propertyName, Object value, Object params, 123 String message, Object [] mesgParams) { 124 String error = internalValidate(value, params, message, null, mesgParams); 125 PropertyError pe = null; 126 if (error != null) { 127 pe = new PropertyError(propertyName, error); 128 } 129 130 return pe; 131 } 132 133 158 public PropertyError validate(String propertyName, Object value, Object params, 159 String bundleName, String key, Locale locale, Object [] mesgParams, 160 Object caller) { 161 String error = ErrorRegistry.getError(bundleName, key, locale, caller); 162 error = internalValidate(value, params, error, locale, mesgParams); 163 164 PropertyError pe = null; 165 if (error != null) { 166 pe = new PropertyError(propertyName, error); 167 } 168 169 return pe; 170 } 171 172 193 public PropertyError fetchAndValidate(String propertyName, Object bean, Object params, 194 String message, Object [] mesgParams) 195 throws BeanException { 196 NestedBeanProperty nbp = new NestedBeanProperty(propertyName, bean.getClass()); 197 String error = internalValidate(nbp.getPropertyValue(bean), params, 198 message, null, mesgParams); 199 PropertyError pe = null; 200 if (error != null) { 201 pe = new PropertyError(propertyName, error); 202 } 203 204 return pe; 205 } 206 207 234 public PropertyError fetchAndValidate(String propertyName, Object bean, Object params, 235 String bundleName, String key, Locale locale, Object [] mesgParams, 236 Object caller) 237 throws BeanException { 238 NestedBeanProperty nbp = new NestedBeanProperty(propertyName, bean.getClass()); 239 String error = ErrorRegistry.getError(bundleName, key, locale, caller); 240 error = internalValidate(nbp.getPropertyValue(bean), params, error, 241 locale, mesgParams); 242 243 PropertyError pe = null; 244 if (error != null) { 245 pe = new PropertyError(propertyName, error); 246 } 247 248 return pe; 249 } 250 251 273 protected abstract String internalValidate(Object value, Object params, 274 String message, Locale locale, Object [] mesgParams); 275 276 286 protected String getErrorMessage(String message, String defaultMesg, 287 Object [] mesgParams) { 288 String error = message; 289 if (error == null) { 290 error = defaultMesg; 291 } 292 293 if (mesgParams != null && error != null) { 295 error = MessageFormat.format(error, mesgParams); 296 } 297 298 return error; 299 } 300 } | Popular Tags |