001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.validation;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: import org.springframework.util.Assert;
023: import org.springframework.util.StringUtils;
024:
025: /**
026: * Utility class offering convenient methods for invoking a {@link Validator}
027: * and for rejecting empty fields.
028: *
029: * <p>Checks for an empty field in <code>Validator</code> implementations can become
030: * one-liners when using {@link #rejectIfEmpty} or {@link #rejectIfEmptyOrWhitespace}.
031: *
032: * @author Juergen Hoeller
033: * @author Dmitriy Kopylenko
034: * @since 06.05.2003
035: * @see Validator
036: * @see Errors
037: */
038: public abstract class ValidationUtils {
039:
040: private static Log logger = LogFactory
041: .getLog(ValidationUtils.class);
042:
043: /**
044: * Invoke the given {@link Validator} for the supplied object and
045: * {@link Errors} instance.
046: * @param validator the <code>Validator</code> to be invoked (must not be <code>null</code>)
047: * @param obj the object to bind the parameters to
048: * @param errors the {@link Errors} instance that should store the errors (must not be <code>null</code>)
049: * @throws IllegalArgumentException if either of the <code>Validator</code> or <code>Errors</code> arguments is <code>null</code>;
050: * or if the supplied <code>Validator</code> does not {@link Validator#supports(Class) support}
051: * the validation of the supplied object's type
052: */
053: public static void invokeValidator(Validator validator, Object obj,
054: Errors errors) {
055: Assert.notNull(validator, "Validator must not be null");
056: Assert.notNull(errors, "Errors object must not be null");
057: if (logger.isDebugEnabled()) {
058: logger.debug("Invoking validator [" + validator + "]");
059: }
060: if (obj != null && !validator.supports(obj.getClass())) {
061: throw new IllegalArgumentException("Validator ["
062: + validator.getClass() + "] does not support ["
063: + obj.getClass() + "]");
064: }
065: validator.validate(obj, errors);
066: if (logger.isDebugEnabled()) {
067: if (errors.hasErrors()) {
068: logger.debug("Validator found "
069: + errors.getErrorCount() + " errors");
070: } else {
071: logger.debug("Validator found no errors");
072: }
073: }
074: }
075:
076: /**
077: * Reject the given field with the given error code if the value is empty.
078: * <p>An 'empty' value in this context means either <code>null</code> or
079: * the empty string "".
080: * <p>The object whose field is being validated does not need to be passed
081: * in because the {@link Errors} instance can resolve field values by itself
082: * (it will usually hold an internal reference to the target object).
083: * @param errors the <code>Errors</code> instance to register errors on
084: * @param field the field name to check
085: * @param errorCode the error code, interpretable as message key
086: */
087: public static void rejectIfEmpty(Errors errors, String field,
088: String errorCode) {
089: rejectIfEmpty(errors, field, errorCode, null, null);
090: }
091:
092: /**
093: * Reject the given field with the given error code and default message
094: * if the value is empty.
095: * <p>An 'empty' value in this context means either <code>null</code> or
096: * the empty string "".
097: * <p>The object whose field is being validated does not need to be passed
098: * in because the {@link Errors} instance can resolve field values by itself
099: * (it will usually hold an internal reference to the target object).
100: * @param errors the <code>Errors</code> instance to register errors on
101: * @param field the field name to check
102: * @param errorCode error code, interpretable as message key
103: * @param defaultMessage fallback default message
104: */
105: public static void rejectIfEmpty(Errors errors, String field,
106: String errorCode, String defaultMessage) {
107: rejectIfEmpty(errors, field, errorCode, null, defaultMessage);
108: }
109:
110: /**
111: * Reject the given field with the given error codea nd error arguments
112: * if the value is empty.
113: * <p>An 'empty' value in this context means either <code>null</code> or
114: * the empty string "".
115: * <p>The object whose field is being validated does not need to be passed
116: * in because the {@link Errors} instance can resolve field values by itself
117: * (it will usually hold an internal reference to the target object).
118: * @param errors the <code>Errors</code> instance to register errors on
119: * @param field the field name to check
120: * @param errorCode the error code, interpretable as message key
121: * @param errorArgs the error arguments, for argument binding via MessageFormat
122: * (can be <code>null</code>)
123: */
124: public static void rejectIfEmpty(Errors errors, String field,
125: String errorCode, Object[] errorArgs) {
126: rejectIfEmpty(errors, field, errorCode, errorArgs, null);
127: }
128:
129: /**
130: * Reject the given field with the given error code, error arguments
131: * and default message if the value is empty.
132: * <p>An 'empty' value in this context means either <code>null</code> or
133: * the empty string "".
134: * <p>The object whose field is being validated does not need to be passed
135: * in because the {@link Errors} instance can resolve field values by itself
136: * (it will usually hold an internal reference to the target object).
137: * @param errors the <code>Errors</code> instance to register errors on
138: * @param field the field name to check
139: * @param errorCode the error code, interpretable as message key
140: * @param errorArgs the error arguments, for argument binding via MessageFormat
141: * (can be <code>null</code>)
142: * @param defaultMessage fallback default message
143: */
144: public static void rejectIfEmpty(Errors errors, String field,
145: String errorCode, Object[] errorArgs, String defaultMessage) {
146:
147: Assert.notNull(errors, "Errors object must not be null");
148: Object value = errors.getFieldValue(field);
149: if (value == null || !StringUtils.hasLength(value.toString())) {
150: errors.rejectValue(field, errorCode, errorArgs,
151: defaultMessage);
152: }
153: }
154:
155: /**
156: * Reject the given field with the given error code if the value is empty
157: * or just contains whitespace.
158: * <p>An 'empty' value in this context means either <code>null</code>,
159: * the empty string "", or consisting wholly of whitespace.
160: * <p>The object whose field is being validated does not need to be passed
161: * in because the {@link Errors} instance can resolve field values by itself
162: * (it will usually hold an internal reference to the target object).
163: * @param errors the <code>Errors</code> instance to register errors on
164: * @param field the field name to check
165: * @param errorCode the error code, interpretable as message key
166: */
167: public static void rejectIfEmptyOrWhitespace(Errors errors,
168: String field, String errorCode) {
169: rejectIfEmptyOrWhitespace(errors, field, errorCode, null, null);
170: }
171:
172: /**
173: * Reject the given field with the given error code and default message
174: * if the value is empty or just contains whitespace.
175: * <p>An 'empty' value in this context means either <code>null</code>,
176: * the empty string "", or consisting wholly of whitespace.
177: * <p>The object whose field is being validated does not need to be passed
178: * in because the {@link Errors} instance can resolve field values by itself
179: * (it will usually hold an internal reference to the target object).
180: * @param errors the <code>Errors</code> instance to register errors on
181: * @param field the field name to check
182: * @param errorCode the error code, interpretable as message key
183: * @param defaultMessage fallback default message
184: */
185: public static void rejectIfEmptyOrWhitespace(Errors errors,
186: String field, String errorCode, String defaultMessage) {
187:
188: rejectIfEmptyOrWhitespace(errors, field, errorCode, null,
189: defaultMessage);
190: }
191:
192: /**
193: * Reject the given field with the given error code and error arguments
194: * if the value is empty or just contains whitespace.
195: * <p>An 'empty' value in this context means either <code>null</code>,
196: * the empty string "", or consisting wholly of whitespace.
197: * <p>The object whose field is being validated does not need to be passed
198: * in because the {@link Errors} instance can resolve field values by itself
199: * (it will usually hold an internal reference to the target object).
200: * @param errors the <code>Errors</code> instance to register errors on
201: * @param field the field name to check
202: * @param errorCode the error code, interpretable as message key
203: * @param errorArgs the error arguments, for argument binding via MessageFormat
204: * (can be <code>null</code>)
205: */
206: public static void rejectIfEmptyOrWhitespace(Errors errors,
207: String field, String errorCode, Object[] errorArgs) {
208:
209: rejectIfEmptyOrWhitespace(errors, field, errorCode, errorArgs,
210: null);
211: }
212:
213: /**
214: * Reject the given field with the given error code, error arguments
215: * and default message if the value is empty or just contains whitespace.
216: * <p>An 'empty' value in this context means either <code>null</code>,
217: * the empty string "", or consisting wholly of whitespace.
218: * <p>The object whose field is being validated does not need to be passed
219: * in because the {@link Errors} instance can resolve field values by itself
220: * (it will usually hold an internal reference to the target object).
221: * @param errors the <code>Errors</code> instance to register errors on
222: * @param field the field name to check
223: * @param errorCode the error code, interpretable as message key
224: * @param errorArgs the error arguments, for argument binding via MessageFormat
225: * (can be <code>null</code>)
226: * @param defaultMessage fallback default message
227: */
228: public static void rejectIfEmptyOrWhitespace(Errors errors,
229: String field, String errorCode, Object[] errorArgs,
230: String defaultMessage) {
231:
232: Assert.notNull(errors, "Errors object must not be null");
233: Object value = errors.getFieldValue(field);
234: if (value == null || !StringUtils.hasText(value.toString())) {
235: errors.rejectValue(field, errorCode, errorArgs,
236: defaultMessage);
237: }
238: }
239:
240: }
|