KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > validation > ValidationUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.validation;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import org.springframework.util.Assert;
23 import org.springframework.util.StringUtils;
24
25 /**
26  * Utility class offering convenient methods for invoking a {@link Validator}
27  * and for rejecting empty fields.
28  *
29  * <p>Checks for an empty field in <code>Validator</code> implementations
30  * can thus become one-liners.
31  *
32  * @author Juergen Hoeller
33  * @author Dmitriy Kopylenko
34  * @since 06.05.2003
35  * @see Validator
36  * @see Errors
37  */

38 public abstract class ValidationUtils {
39
40     private static Log logger = LogFactory.getLog(ValidationUtils.class);
41
42
43     /**
44      * Invoke the given {@link Validator} for the supplied object and
45      * {@link Errors} instance.
46      * @param validator the <code>Validator</code> to be invoked (must not be <code>null</code>)
47      * @param obj the object to bind the parameters to
48      * @param errors the {@link Errors} instance that should store the errors (must not be <code>null</code>)
49      * @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is <code>null</code>;
50      * or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
51      * the validation of the supplied object's type
52      */

53     public static void invokeValidator(Validator validator, Object JavaDoc obj, Errors errors) {
54         Assert.notNull(validator, "Validator must not be null");
55         Assert.notNull(errors, "Errors object must not be null");
56         if (logger.isDebugEnabled()) {
57             logger.debug("Invoking validator [" + validator + "]");
58         }
59         if (obj != null && !validator.supports(obj.getClass())) {
60             throw new IllegalArgumentException JavaDoc("Validator " + validator.getClass() +
61                     " does not support " + obj.getClass());
62         }
63         validator.validate(obj, errors);
64         if (logger.isDebugEnabled()) {
65             if (errors.hasErrors()) {
66                 logger.debug("Validator found " + errors.getErrorCount() + " errors");
67             }
68             else {
69                 logger.debug("Validator found no errors");
70             }
71         }
72     }
73     
74     /**
75      * Reject the given field with the given error code if the value is empty.
76      * <p>An 'empty' value in this context means either <code>null</code> or
77      * the empty string "".
78      * <p>The object whose field is being validated does not need to be passed
79      * in because the {@link Errors} instance can resolve field values by itself
80      * (it will usually hold an internal reference to the target object).
81      * @param errors the <code>Errors</code> instance to register errors on
82      * @param field the field name to check
83      * @param errorCode the error code, interpretable as message key
84      */

85     public static void rejectIfEmpty(Errors errors, String JavaDoc field, String JavaDoc errorCode) {
86         rejectIfEmpty(errors, field, errorCode, null, null);
87     }
88
89     /**
90      * Reject the given field with the given error code and default message
91      * if the value is empty.
92      * <p>An 'empty' value in this context means either <code>null</code> or
93      * the empty string "".
94      * <p>The object whose field is being validated does not need to be passed
95      * in because the {@link Errors} instance can resolve field values by itself
96      * (it will usually hold an internal reference to the target object).
97      * @param errors the <code>Errors</code> instance to register errors on
98      * @param field the field name to check
99      * @param errorCode error code, interpretable as message key
100      * @param defaultMessage fallback default message
101      */

102     public static void rejectIfEmpty(Errors errors, String JavaDoc field, String JavaDoc errorCode, String JavaDoc defaultMessage) {
103         rejectIfEmpty(errors, field, errorCode, null, defaultMessage);
104     }
105
106     /**
107      * Reject the given field with the given error code, error arguments
108      * and default message if the value is empty.
109      * <p>An 'empty' value in this context means either <code>null</code> or
110      * the empty string "".
111      * <p>The object whose field is being validated does not need to be passed
112      * in because the {@link Errors} instance can resolve field values by itself
113      * (it will usually hold an internal reference to the target object).
114      * @param errors the <code>Errors</code> instance to register errors on
115      * @param field the field name to check
116      * @param errorCode the error code, interpretable as message key
117      * @param errorArgs the error arguments, for argument binding via MessageFormat
118      * (can be <code>null</code>)
119      * @param defaultMessage fallback default message
120      */

121     public static void rejectIfEmpty(
122             Errors errors, String JavaDoc field, String JavaDoc errorCode, Object JavaDoc[] errorArgs, String JavaDoc defaultMessage) {
123
124         Assert.notNull(errors, "Errors object must not be null");
125         Object JavaDoc value = errors.getFieldValue(field);
126         if (value == null || !StringUtils.hasLength(value.toString())) {
127             errors.rejectValue(field, errorCode, errorArgs, defaultMessage);
128         }
129     }
130
131     /**
132      * Reject the given field with the given error code if the value is empty
133      * or just contains whitespace.
134      * <p>An 'empty' value in this context means either <code>null</code>,
135      * the empty string "", or consisting wholly of whitespace.
136      * <p>The object whose field is being validated does not need to be passed
137      * in because the {@link Errors} instance can resolve field values by itself
138      * (it will usually hold an internal reference to the target object).
139      * @param errors the <code>Errors</code> instance to register errors on
140      * @param field the field name to check
141      * @param errorCode the error code, interpretable as message key
142      */

143     public static void rejectIfEmptyOrWhitespace(Errors errors, String JavaDoc field, String JavaDoc errorCode) {
144         rejectIfEmptyOrWhitespace(errors, field, errorCode, null, null);
145     }
146
147     /**
148      * Reject the given field with the given error code and default message
149      * if the value is empty or just contains whitespace.
150      * <p>An 'empty' value in this context means either <code>null</code>,
151      * the empty string "", or consisting wholly of whitespace.
152      * <p>The object whose field is being validated does not need to be passed
153      * in because the {@link Errors} instance can resolve field values by itself
154      * (it will usually hold an internal reference to the target object).
155      * @param errors the <code>Errors</code> instance to register errors on
156      * @param field the field name to check
157      * @param errorCode the error code, interpretable as message key
158      * @param defaultMessage fallback default message
159      */

160     public static void rejectIfEmptyOrWhitespace(
161             Errors errors, String JavaDoc field, String JavaDoc errorCode, String JavaDoc defaultMessage) {
162
163         rejectIfEmptyOrWhitespace(errors, field, errorCode, null, defaultMessage);
164     }
165
166     /**
167      * Reject the given field with the given error code, error arguments
168      * and default message if the value is empty or just contains whitespace.
169      * <p>An 'empty' value in this context means either <code>null</code>,
170      * the empty string "", or consisting wholly of whitespace.
171      * <p>The object whose field is being validated does not need to be passed
172      * in because the {@link Errors} instance can resolve field values by itself
173      * (it will usually hold an internal reference to the target object).
174      * @param errors the <code>Errors</code> instance to register errors on
175      * @param field the field name to check
176      * @param errorCode the error code, interpretable as message key
177      * @param errorArgs the error arguments, for argument binding via MessageFormat
178      * (can be <code>null</code>)
179      * @param defaultMessage fallback default message
180      */

181     public static void rejectIfEmptyOrWhitespace(
182             Errors errors, String JavaDoc field, String JavaDoc errorCode, Object JavaDoc[] errorArgs, String JavaDoc defaultMessage) {
183
184         Assert.notNull(errors, "Errors object must not be null");
185         Object JavaDoc value = errors.getFieldValue(field);
186         if (value == null ||!StringUtils.hasText(value.toString())) {
187             errors.rejectValue(field, errorCode, errorArgs, defaultMessage);
188         }
189     }
190
191 }
192
Popular Tags