1. /*
  2. * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v 1.34 2004/02/21 17:10:29 rleland Exp $
  3. * $Revision: 1.34 $
  4. * $Date: 2004/02/21 17:10:29 $
  5. *
  6. * ====================================================================
  7. * Copyright 2001-2004 The Apache Software Foundation
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. package org.apache.commons.validator;
  22. import java.io.Serializable;
  23. import java.util.HashMap;
  24. import java.util.Locale;
  25. import java.util.Map;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. /**
  29. * Validations are processed by the validate method. An instance of
  30. * <code>ValidatorResources</code> is used to define the validators
  31. * (validation methods) and the validation rules for a JavaBean.
  32. */
  33. public class Validator implements Serializable {
  34. /**
  35. * Logger.
  36. * @deprecated Subclasses should use their own logging instance.
  37. */
  38. protected static Log log = LogFactory.getLog(Validator.class);
  39. /**
  40. * Resources key the JavaBean is stored to perform validation on.
  41. */
  42. public static final String BEAN_PARAM = "java.lang.Object";
  43. /**
  44. * Resources key the JavaBean is stored to perform validation on.
  45. * @deprecated Use BEAN_PARAM instead.
  46. */
  47. public static final String BEAN_KEY = BEAN_PARAM;
  48. /**
  49. * Resources key the <code>ValidatorAction</code> is stored under.
  50. * This will be automatically passed into a validation method
  51. * with the current <code>ValidatorAction</code> if it is
  52. * specified in the method signature.
  53. */
  54. public static final String VALIDATOR_ACTION_PARAM =
  55. "org.apache.commons.validator.ValidatorAction";
  56. /**
  57. * Resources key the <code>ValidatorAction</code> is stored under.
  58. * This will be automatically passed into a validation method
  59. * with the current <code>ValidatorAction</code> if it is
  60. * specified in the method signature.
  61. * @deprecated Use VALIDATOR_ACTION_PARAM instead.
  62. */
  63. public static final String VALIDATOR_ACTION_KEY = VALIDATOR_ACTION_PARAM;
  64. /**
  65. * Resources key the <code>Field</code> is stored under.
  66. * This will be automatically passed into a validation method
  67. * with the current <code>Field</code> if it is
  68. * specified in the method signature.
  69. */
  70. public static final String FIELD_PARAM = "org.apache.commons.validator.Field";
  71. /**
  72. * Resources key the <code>Field</code> is stored under.
  73. * This will be automatically passed into a validation method
  74. * with the current <code>Field</code> if it is
  75. * specified in the method signature.
  76. * @deprecated Use FIELD_PARAM instead.
  77. */
  78. public static final String FIELD_KEY = FIELD_PARAM;
  79. /**
  80. * Resources key the <code>Validator</code> is stored under.
  81. * This will be automatically passed into a validation method
  82. * with the current <code>Validator</code> if it is
  83. * specified in the method signature.
  84. */
  85. public static final String VALIDATOR_PARAM =
  86. "org.apache.commons.validator.Validator";
  87. /**
  88. * Resources key the <code>Validator</code> is stored under.
  89. * This will be automatically passed into a validation method
  90. * with the current <code>Validator</code> if it is
  91. * specified in the method signature.
  92. * @deprecated Use VALIDATOR_PARAM instead.
  93. */
  94. public static final String VALIDATOR_KEY = VALIDATOR_PARAM;
  95. /**
  96. * Resources key the <code>Locale</code> is stored.
  97. * This will be used to retrieve the appropriate
  98. * <code>FormSet</code> and <code>Form</code> to be
  99. * processed.
  100. */
  101. public static final String LOCALE_PARAM = "java.util.Locale";
  102. /**
  103. * Resources key the <code>Locale</code> is stored.
  104. * This will be used to retrieve the appropriate
  105. * <code>FormSet</code> and <code>Form</code> to be
  106. * processed.
  107. * @deprecated Use LOCALE_PARAM instead.
  108. */
  109. public static final String LOCALE_KEY = LOCALE_PARAM;
  110. protected ValidatorResources resources = null;
  111. protected String formName = null;
  112. /**
  113. * Maps validation method parameter class names to the objects to be passed
  114. * into the method.
  115. */
  116. protected Map parameters = new HashMap();
  117. /**
  118. * @deprecated Use parameters instead.
  119. */
  120. protected HashMap hResources = (HashMap) this.parameters;
  121. /**
  122. * The current page number to validate.
  123. */
  124. protected int page = 0;
  125. /**
  126. * The class loader to use for instantiating application objects.
  127. * If not specified, the context class loader, or the class loader
  128. * used to load Digester itself, is used, based on the value of the
  129. * <code>useContextClassLoader</code> variable.
  130. */
  131. protected ClassLoader classLoader = null;
  132. /**
  133. * Whether or not to use the Context ClassLoader when loading classes
  134. * for instantiating new objects. Default is <code>false</code>.
  135. */
  136. protected boolean useContextClassLoader = false;
  137. /**
  138. * Set this to true to not return Fields that pass validation. Only return failures.
  139. */
  140. protected boolean onlyReturnErrors = false;
  141. /**
  142. * Construct a <code>Validator</code> that will
  143. * use the <code>ValidatorResources</code>
  144. * passed in to retrieve pluggable validators
  145. * the different sets of validation rules.
  146. *
  147. * @param resources <code>ValidatorResources</code> to use during validation.
  148. */
  149. public Validator(ValidatorResources resources) {
  150. this(resources, null);
  151. }
  152. /**
  153. * Construct a <code>Validator</code> that will
  154. * use the <code>ValidatorResources</code>
  155. * passed in to retrieve pluggable validators
  156. * the different sets of validation rules.
  157. *
  158. * @param resources <code>ValidatorResources</code> to use during validation.
  159. * @param formName Key used for retrieving the set of validation rules.
  160. */
  161. public Validator(ValidatorResources resources, String formName) {
  162. if (resources == null) {
  163. throw new IllegalArgumentException("Resources cannot be null.");
  164. }
  165. this.resources = resources;
  166. this.formName = formName;
  167. }
  168. /**
  169. * Add a resource to be used during the processing
  170. * of validations.
  171. *
  172. * @param parameterClassName The full class name of the parameter of the
  173. * validation method that corresponds to the value/instance passed in with it.
  174. *
  175. * @param parameterValue The instance that will be passed into the
  176. * validation method.
  177. * @deprecated Use setParameter(String, Object) instead.
  178. */
  179. public void addResource(String parameterClassName, Object parameterValue) {
  180. this.setParameter(parameterClassName, parameterValue);
  181. }
  182. /**
  183. * Set a parameter of a pluggable validation method.
  184. *
  185. * @param parameterClassName The full class name of the parameter of the
  186. * validation method that corresponds to the value/instance passed in with it.
  187. *
  188. * @param parameterValue The instance that will be passed into the
  189. * validation method.
  190. */
  191. public void setParameter(String parameterClassName, Object parameterValue) {
  192. this.parameters.put(parameterClassName, parameterValue);
  193. }
  194. /**
  195. * Get a resource to be used during the processing of validations.
  196. *
  197. * @param parameterClassName The full class name of the parameter of the
  198. * validation method that corresponds to the value/instance passed in with it.
  199. * @deprecated Use getParameterValue(String) instead.
  200. */
  201. public Object getResource(String parameterClassName) {
  202. return this.getParameterValue(parameterClassName);
  203. }
  204. /**
  205. * Returns the value of the specified parameter that will be used during the
  206. * processing of validations.
  207. *
  208. * @param parameterClassName The full class name of the parameter of the
  209. * validation method that corresponds to the value/instance passed in with it.
  210. */
  211. public Object getParameterValue(String parameterClassName) {
  212. return this.parameters.get(parameterClassName);
  213. }
  214. /**
  215. * Gets the form name which is the key to a set of validation rules.
  216. */
  217. public String getFormName() {
  218. return formName;
  219. }
  220. /**
  221. * Sets the form name which is the key to a set of validation rules.
  222. */
  223. public void setFormName(String formName) {
  224. this.formName = formName;
  225. }
  226. /**
  227. * Gets the page. This in conjunction with the page property of
  228. * a <code>Field<code> can control the processing of fields. If the field's
  229. * page is less than or equal to this page value, it will be processed.
  230. */
  231. public int getPage() {
  232. return page;
  233. }
  234. /**
  235. * Sets the page. This in conjunction with the page property of
  236. * a <code>Field<code> can control the processing of fields. If the field's page
  237. * is less than or equal to this page value, it will be processed.
  238. */
  239. public void setPage(int page) {
  240. this.page = page;
  241. }
  242. /**
  243. * Clears the form name, resources that were added, and the page that was
  244. * set (if any). This can be called to reinitialize the Validator instance
  245. * so it can be reused. The form name (key to set of validation rules) and any
  246. * resources needed, like the JavaBean being validated, will need to
  247. * set and/or added to this instance again. The
  248. * <code>ValidatorResources</code> will not be removed since it can be used
  249. * again and is thread safe.
  250. */
  251. public void clear() {
  252. this.formName = null;
  253. this.parameters = new HashMap();
  254. this.hResources = (HashMap) this.parameters;
  255. this.page = 0;
  256. }
  257. /**
  258. * Return the boolean as to whether the context classloader should be used.
  259. */
  260. public boolean getUseContextClassLoader() {
  261. return this.useContextClassLoader;
  262. }
  263. /**
  264. * Determine whether to use the Context ClassLoader (the one found by
  265. * calling <code>Thread.currentThread().getContextClassLoader()</code>)
  266. * to resolve/load classes that are defined in various rules. If not
  267. * using Context ClassLoader, then the class-loading defaults to
  268. * using the calling-class' ClassLoader.
  269. *
  270. * @param use determines whether to use Context ClassLoader.
  271. */
  272. public void setUseContextClassLoader(boolean use) {
  273. this.useContextClassLoader = use;
  274. }
  275. /**
  276. * Return the class loader to be used for instantiating application objects
  277. * when required. This is determined based upon the following rules:
  278. * <ul>
  279. * <li>The class loader set by <code>setClassLoader()</code>, if any</li>
  280. * <li>The thread context class loader, if it exists and the
  281. * <code>useContextClassLoader</code> property is set to true</li>
  282. * <li>The class loader used to load the Digester class itself.
  283. * </ul>
  284. */
  285. public ClassLoader getClassLoader() {
  286. if (this.classLoader != null) {
  287. return this.classLoader;
  288. }
  289. if (this.useContextClassLoader) {
  290. ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
  291. if (contextLoader != null) {
  292. return contextLoader;
  293. }
  294. }
  295. return this.getClass().getClassLoader();
  296. }
  297. /**
  298. * Set the class loader to be used for instantiating application objects
  299. * when required.
  300. *
  301. * @param classLoader The new class loader to use, or <code>null</code>
  302. * to revert to the standard rules
  303. */
  304. public void setClassLoader(ClassLoader classLoader) {
  305. this.classLoader = classLoader;
  306. }
  307. /**
  308. * Performs validations based on the configured resources.
  309. *
  310. * @return The <code>Map</code> returned uses the property of the
  311. * <code>Field</code> for the key and the value is the number of error the
  312. * field had.
  313. */
  314. public ValidatorResults validate() throws ValidatorException {
  315. Locale locale = (Locale) this.getParameterValue(LOCALE_PARAM);
  316. if (locale == null) {
  317. locale = Locale.getDefault();
  318. }
  319. this.setParameter(VALIDATOR_PARAM, this);
  320. Form form = this.resources.getForm(locale, this.formName);
  321. if (form != null) {
  322. return form.validate(
  323. this.parameters,
  324. this.resources.getValidatorActions(),
  325. this.page);
  326. }
  327. return new ValidatorResults();
  328. }
  329. /**
  330. * Returns true if the Validator is only returning Fields that fail validation.
  331. */
  332. public boolean getOnlyReturnErrors() {
  333. return onlyReturnErrors;
  334. }
  335. /**
  336. * Configures which Fields the Validator returns from the validate() method. Set this
  337. * to true to only return Fields that failed validation. By default, validate() returns
  338. * all fields.
  339. */
  340. public void setOnlyReturnErrors(boolean onlyReturnErrors) {
  341. this.onlyReturnErrors = onlyReturnErrors;
  342. }
  343. }