1 21 22 package org.apache.commons.validator; 23 24 import java.io.Serializable ; 25 import java.util.HashMap ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 29 34 public class Validator implements Serializable { 35 36 39 public static final String BEAN_PARAM = "java.lang.Object"; 40 41 47 public static final String VALIDATOR_ACTION_PARAM = 48 "org.apache.commons.validator.ValidatorAction"; 49 50 56 public static final String VALIDATOR_RESULTS_PARAM = 57 "org.apache.commons.validator.ValidatorResults"; 58 59 65 public static final String FORM_PARAM = "org.apache.commons.validator.Form"; 66 67 73 public static final String FIELD_PARAM = "org.apache.commons.validator.Field"; 74 75 81 public static final String VALIDATOR_PARAM = 82 "org.apache.commons.validator.Validator"; 83 84 90 public static final String LOCALE_PARAM = "java.util.Locale"; 91 92 protected ValidatorResources resources = null; 93 94 protected String formName = null; 95 96 100 protected Map parameters = new HashMap (); 101 102 105 protected int page = 0; 106 107 113 protected ClassLoader classLoader = null; 114 115 119 protected boolean useContextClassLoader = false; 120 121 124 protected boolean onlyReturnErrors = false; 125 126 134 public Validator(ValidatorResources resources) { 135 this(resources, null); 136 } 137 138 147 public Validator(ValidatorResources resources, String formName) { 148 if (resources == null) { 149 throw new IllegalArgumentException ("Resources cannot be null."); 150 } 151 152 this.resources = resources; 153 this.formName = formName; 154 } 155 156 165 public void setParameter(String parameterClassName, Object parameterValue) { 166 this.parameters.put(parameterClassName, parameterValue); 167 } 168 169 176 public Object getParameterValue(String parameterClassName) { 177 return this.parameters.get(parameterClassName); 178 } 179 180 183 public String getFormName() { 184 return formName; 185 } 186 187 190 public void setFormName(String formName) { 191 this.formName = formName; 192 } 193 194 199 public int getPage() { 200 return page; 201 } 202 203 208 public void setPage(int page) { 209 this.page = page; 210 } 211 212 221 public void clear() { 222 this.formName = null; 223 this.parameters = new HashMap (); 224 this.page = 0; 225 } 226 227 230 public boolean getUseContextClassLoader() { 231 return this.useContextClassLoader; 232 } 233 234 243 public void setUseContextClassLoader(boolean use) { 244 this.useContextClassLoader = use; 245 } 246 247 257 public ClassLoader getClassLoader() { 258 if (this.classLoader != null) { 259 return this.classLoader; 260 } 261 262 if (this.useContextClassLoader) { 263 ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); 264 if (contextLoader != null) { 265 return contextLoader; 266 } 267 } 268 269 return this.getClass().getClassLoader(); 270 } 271 272 279 public void setClassLoader(ClassLoader classLoader) { 280 this.classLoader = classLoader; 281 } 282 283 290 public ValidatorResults validate() throws ValidatorException { 291 Locale locale = (Locale ) this.getParameterValue(LOCALE_PARAM); 292 293 if (locale == null) { 294 locale = Locale.getDefault(); 295 } 296 297 this.setParameter(VALIDATOR_PARAM, this); 298 299 Form form = this.resources.getForm(locale, this.formName); 300 if (form != null) { 301 this.setParameter(FORM_PARAM, this); 302 return form.validate( 303 this.parameters, 304 this.resources.getValidatorActions(), 305 this.page); 306 } 307 308 return new ValidatorResults(); 309 } 310 311 314 public boolean getOnlyReturnErrors() { 315 return onlyReturnErrors; 316 } 317 318 323 public void setOnlyReturnErrors(boolean onlyReturnErrors) { 324 this.onlyReturnErrors = onlyReturnErrors; 325 } 326 327 } 328 | Popular Tags |