001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.validator.factory;
016:
017: import java.lang.reflect.Method;
018: import java.util.List;
019:
020: import org.strecks.util.ReflectHelper;
021: import org.strecks.validator.Validator;
022: import org.strecks.validator.internal.ValidatorWrapper;
023: import org.strecks.validator.message.DefaultMessageParameterProvider;
024: import org.strecks.validator.message.MessageParameterProvider;
025:
026: /**
027: * Convenience base class of <code>ValidatorFactory</code> implementations
028: * @author Phil Zoio
029: */
030: public abstract class BaseFactory implements ValidatorFactory {
031:
032: /**
033: * Returns a <code>ValidatorWrapper</code> which encapsulates all the validations for a property
034: * @param validator
035: * the <code>Validator</code> instance wrapped by the <code>ValidatorWrapper</code>
036: * @param key
037: * the message key used to look up the relevant error message on validation failure
038: * @param order
039: * the order in which this validator should be called. Validators are called in ascending order
040: * @param parameters
041: * additional parameters supplied to the validator. For example, a date validator will require a pattern
042: * parameter, and an integer range validator will require minimum and maximum ranges
043: * @param method
044: * the setter method for the property being validated
045: * @return
046: */
047: protected ValidatorWrapper create(Validator validator, String key,
048: int order, List<Object> parameters, Method method) {
049: MessageParameterProvider provider = newMessageParameterProvider();
050: ValidatorWrapper wrapper = newWrapper(validator, key, order,
051: parameters, method, provider);
052: return wrapper;
053: }
054:
055: ValidatorWrapper newWrapper(Validator validator, String key,
056: int order, List<Object> parameters, Method method,
057: MessageParameterProvider provider) {
058:
059: Class<?> parameterizedType = ReflectHelper.getGenericType(
060: validator.getClass(), Validator.class);
061:
062: boolean useConvertedValue = shouldUseConvertedValue(validator,
063: method);
064:
065: if (useConvertedValue) {
066: return new ValidatorWrapper(key, order, parameters,
067: validator, method, provider, useConvertedValue,
068: parameterizedType);
069: } else {
070: return new ValidatorWrapper(key, order, parameters,
071: validator, method, provider);
072: }
073: }
074:
075: boolean shouldUseConvertedValue(Validator validator, Method method) {
076: Class<?> returnType = method.getReturnType();
077: Class<?> parameterizedType = ReflectHelper.getGenericType(
078: validator.getClass(), Validator.class);
079:
080: boolean useConvertedValue = false;
081:
082: // if no parametized type, then use the raw value for validation
083: if (parameterizedType == null)
084: useConvertedValue = false;
085:
086: else {
087: if (parameterizedType.isAssignableFrom(returnType)) {
088: // if return type can be assigned to parameterized type, then use the raw value for validation
089: useConvertedValue = false;
090: } else {
091: // if return type cannot be assigned to parameterized type, then use converted value for validation
092: // e.g. return type is String, parameterized type is Integer
093: useConvertedValue = true;
094: }
095: }
096: return useConvertedValue;
097: }
098:
099: MessageParameterProvider newMessageParameterProvider() {
100: MessageParameterProvider provider = getMessageParameterProvider();
101:
102: if (provider == null)
103: return new DefaultMessageParameterProvider();
104: else
105: return provider;
106: }
107:
108: protected MessageParameterProvider getMessageParameterProvider() {
109: return null;
110: }
111: }
|