001: /*
002: * Copyright 2002-2006 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
030: * can thus become one-liners.
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 code, error arguments
112: * and default message 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: * @param defaultMessage fallback default message
124: */
125: public static void rejectIfEmpty(Errors errors, String field,
126: String errorCode, Object[] errorArgs, String defaultMessage) {
127:
128: Assert.notNull(errors, "Errors object must not be null");
129: Object value = errors.getFieldValue(field);
130: if (value == null || !StringUtils.hasLength(value.toString())) {
131: errors.rejectValue(field, errorCode, errorArgs,
132: defaultMessage);
133: }
134: }
135:
136: /**
137: * Reject the given field with the given error code if the value is empty
138: * or just contains whitespace.
139: * <p>An 'empty' value in this context means either <code>null</code>,
140: * the empty string "", or consisting wholly of whitespace.
141: * <p>The object whose field is being validated does not need to be passed
142: * in because the {@link Errors} instance can resolve field values by itself
143: * (it will usually hold an internal reference to the target object).
144: * @param errors the <code>Errors</code> instance to register errors on
145: * @param field the field name to check
146: * @param errorCode the error code, interpretable as message key
147: */
148: public static void rejectIfEmptyOrWhitespace(Errors errors,
149: String field, String errorCode) {
150: rejectIfEmptyOrWhitespace(errors, field, errorCode, null, null);
151: }
152:
153: /**
154: * Reject the given field with the given error code and default message
155: * if the value is empty or just contains whitespace.
156: * <p>An 'empty' value in this context means either <code>null</code>,
157: * the empty string "", or consisting wholly of whitespace.
158: * <p>The object whose field is being validated does not need to be passed
159: * in because the {@link Errors} instance can resolve field values by itself
160: * (it will usually hold an internal reference to the target object).
161: * @param errors the <code>Errors</code> instance to register errors on
162: * @param field the field name to check
163: * @param errorCode the error code, interpretable as message key
164: * @param defaultMessage fallback default message
165: */
166: public static void rejectIfEmptyOrWhitespace(Errors errors,
167: String field, String errorCode, String defaultMessage) {
168:
169: rejectIfEmptyOrWhitespace(errors, field, errorCode, null,
170: defaultMessage);
171: }
172:
173: /**
174: * Reject the given field with the given error code, error arguments
175: * and default message if the value is empty or just contains whitespace.
176: * <p>An 'empty' value in this context means either <code>null</code>,
177: * the empty string "", or consisting wholly of whitespace.
178: * <p>The object whose field is being validated does not need to be passed
179: * in because the {@link Errors} instance can resolve field values by itself
180: * (it will usually hold an internal reference to the target object).
181: * @param errors the <code>Errors</code> instance to register errors on
182: * @param field the field name to check
183: * @param errorCode the error code, interpretable as message key
184: * @param errorArgs the error arguments, for argument binding via MessageFormat
185: * (can be <code>null</code>)
186: * @param defaultMessage fallback default message
187: */
188: public static void rejectIfEmptyOrWhitespace(Errors errors,
189: String field, String errorCode, Object[] errorArgs,
190: String defaultMessage) {
191:
192: Assert.notNull(errors, "Errors object must not be null");
193: Object value = errors.getFieldValue(field);
194: if (value == null || !StringUtils.hasText(value.toString())) {
195: errors.rejectValue(field, errorCode, errorArgs,
196: defaultMessage);
197: }
198: }
199:
200: }
|