001: package nl.knowlogy.validation.annotations;
002:
003: import java.lang.annotation.Annotation;
004: import java.lang.reflect.Method;
005: import java.util.ArrayList;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import nl.knowlogy.validation.ClassValidator;
010: import nl.knowlogy.validation.ClassValidatorImpl;
011: import nl.knowlogy.validation.ValidationEngine;
012: import nl.knowlogy.validation.ValidationEngine.ValidatorSupplier;
013: import nl.knowlogy.validation.validators.AllowedValuesValidator;
014: import nl.knowlogy.validation.validators.CustomPropertyValidator;
015: import nl.knowlogy.validation.validators.EmailValidator;
016: import nl.knowlogy.validation.validators.IsNotBlankValidator;
017: import nl.knowlogy.validation.validators.IsRequiredValidator;
018: import nl.knowlogy.validation.validators.IsValidValidator;
019: import nl.knowlogy.validation.validators.MaxLengthValidator;
020: import nl.knowlogy.validation.validators.MaxSizeValidator;
021: import nl.knowlogy.validation.validators.MinLengthValidator;
022: import nl.knowlogy.validation.validators.MinSizeValidator;
023: import nl.knowlogy.validation.validators.PatternValidator;
024: import nl.knowlogy.validation.validators.RangeValidator;
025:
026: import org.apache.commons.lang.StringUtils;
027:
028: public class AnnotatedClassesValidatorFactory implements
029: ValidatorSupplier {
030:
031: private static final AnnotatedClassesValidatorFactory instance = new AnnotatedClassesValidatorFactory();
032:
033: static {
034: ValidationEngine.setValidatorSupplier(instance);
035: }
036:
037: public ClassValidator getValidator(Class clazz) {
038: ClassValidator classValidator;
039: if (clazz.getAnnotation(ValidateClass.class) != null) {
040: classValidator = createClassValidator(clazz);
041: } else {
042: classValidator = null;
043: }
044: return classValidator;
045: }
046:
047: private AnnotatedClassesValidatorFactory() {
048: registerAnnotationValidatorCreator(new ValidateAllowedValuesIter());
049: registerAnnotationValidatorCreator(new ValidateCustomIter());
050: registerAnnotationValidatorCreator(new ValidateIsNotBlankIter());
051: registerAnnotationValidatorCreator(new ValidateIsRequiredIter());
052: registerAnnotationValidatorCreator(new ValidateIsValidIter());
053: registerAnnotationValidatorCreator(new ValidateMaxLengthIter());
054: registerAnnotationValidatorCreator(new ValidateMaxSizeIter());
055: registerAnnotationValidatorCreator(new ValidateMinLengthIter());
056: registerAnnotationValidatorCreator(new ValidateMinSizeIter());
057: registerAnnotationValidatorCreator(new ValidatePatternIter());
058: registerAnnotationValidatorCreator(new ValidateEmailIter());
059: registerAnnotationValidatorCreator(new ValidateRangeIter());
060: }
061:
062: public static AnnotatedClassesValidatorFactory getInstance() {
063: return instance;
064: }
065:
066: private synchronized ClassValidator createClassValidator(Class clazz) {
067: ClassValidator classValidator = new ClassValidatorImpl(clazz);
068:
069: Method[] methods = clazz.getMethods();
070:
071: String propertyName;
072: for (Method method : methods) {
073:
074: propertyName = methodToPropertyName(method.getName());
075:
076: Iterator annotationValidatorsCreatorsIter = annotationValidatorCreators
077: .iterator();
078: while (annotationValidatorsCreatorsIter.hasNext()) {
079: AnnotationValidatorCreator annotationValidatorCreator = (AnnotationValidatorCreator) annotationValidatorsCreatorsIter
080: .next();
081: Annotation annotation = method
082: .getAnnotation(annotationValidatorCreator
083: .getAnnotationClass());
084: if (annotation != null) {
085: annotationValidatorCreator
086: .createAndRegisterValidator(annotation,
087: classValidator, propertyName);
088: }
089: }
090:
091: }
092: //validatorMap.put(clazz, classValidator);
093: return classValidator;
094:
095: }
096:
097: private List<AnnotationValidatorCreator> annotationValidatorCreators = new ArrayList<AnnotationValidatorCreator>();
098:
099: private synchronized void registerAnnotationValidatorCreator(
100: AnnotationValidatorCreator annotationValidatorCreator) {
101: annotationValidatorCreators.add(annotationValidatorCreator);
102: }
103:
104: private String methodToPropertyName(String methodName) {
105: return StringUtils.uncapitalize(methodName.substring(3,
106: methodName.length()));
107: }
108:
109: public interface AnnotationValidatorCreator {
110:
111: public Class getAnnotationClass();
112:
113: public void createAndRegisterValidator(Annotation annotation,
114: ClassValidator classValidator, String propertyName);
115: }
116:
117: class ValidateAllowedValuesIter implements
118: AnnotationValidatorCreator {
119:
120: public Class getAnnotationClass() {
121: return ValidateAllowedValues.class;
122: }
123:
124: public void createAndRegisterValidator(Annotation annotation,
125: ClassValidator classValidator, String propertyName) {
126: ValidateAllowedValues validateAllowedValues = (ValidateAllowedValues) annotation;
127: classValidator.add(new AllowedValuesValidator(propertyName,
128: validateAllowedValues.allowedValues(),
129: validateAllowedValues.errorCode()));
130:
131: }
132:
133: }
134:
135: class ValidateCustomIter implements AnnotationValidatorCreator {
136:
137: public Class getAnnotationClass() {
138: return ValidateCustom.class;
139: }
140:
141: public void createAndRegisterValidator(Annotation annotation,
142: ClassValidator classValidator, String propertyName) {
143: ValidateCustom validateCustom = (ValidateCustom) annotation;
144: classValidator.add(new CustomPropertyValidator(
145: propertyName, validateCustom.validatorClassName(),
146: validateCustom.errorCode()));
147:
148: }
149:
150: }
151:
152: class ValidateIsNotBlankIter implements AnnotationValidatorCreator {
153:
154: public Class getAnnotationClass() {
155: return ValidateIsNotBlank.class;
156: }
157:
158: public void createAndRegisterValidator(Annotation annotation,
159: ClassValidator classValidator, String propertyName) {
160: ValidateIsNotBlank validateIsNotBlank = (ValidateIsNotBlank) annotation;
161: classValidator.add(new IsNotBlankValidator(propertyName,
162: validateIsNotBlank.errorCode()));
163:
164: }
165:
166: }
167:
168: class ValidateEmailIter implements AnnotationValidatorCreator {
169:
170: public Class getAnnotationClass() {
171: return ValidateEmail.class;
172: }
173:
174: public void createAndRegisterValidator(Annotation annotation,
175: ClassValidator classValidator, String propertyName) {
176: ValidateEmail validateEmail = (ValidateEmail) annotation;
177: classValidator.add(new EmailValidator(propertyName,
178: validateEmail.errorCode()));
179:
180: }
181:
182: }
183:
184: class ValidateRangeIter implements AnnotationValidatorCreator {
185:
186: public Class getAnnotationClass() {
187: return ValidateRange.class;
188: }
189:
190: public void createAndRegisterValidator(Annotation annotation,
191: ClassValidator classValidator, String propertyName) {
192: ValidateRange validateRange = (ValidateRange) annotation;
193: classValidator.add(new RangeValidator(propertyName,
194: new Long(validateRange.min()), new Long(
195: validateRange.max()), validateRange
196: .errorCode()));
197: }
198: }
199:
200: class ValidateIsRequiredIter implements AnnotationValidatorCreator {
201:
202: public Class getAnnotationClass() {
203: return ValidateIsRequired.class;
204: }
205:
206: public void createAndRegisterValidator(Annotation annotation,
207: ClassValidator classValidator, String propertyName) {
208: ValidateIsRequired validateIsRequired = (ValidateIsRequired) annotation;
209: classValidator.add(new IsRequiredValidator(propertyName,
210: validateIsRequired.errorCode()));
211: }
212:
213: }
214:
215: class ValidateMaxSizeIter implements AnnotationValidatorCreator {
216:
217: public Class getAnnotationClass() {
218: return ValidateMaxSize.class;
219: }
220:
221: public void createAndRegisterValidator(Annotation annotation,
222: ClassValidator classValidator, String propertyName) {
223: ValidateMaxSize validateMaxSize = (ValidateMaxSize) annotation;
224: classValidator.add(new MaxSizeValidator(propertyName,
225: new Long(validateMaxSize.size()), validateMaxSize
226: .errorCode()));
227: }
228:
229: }
230:
231: class ValidateMaxLengthIter implements AnnotationValidatorCreator {
232:
233: public Class getAnnotationClass() {
234: return ValidateMaxLength.class;
235: }
236:
237: public void createAndRegisterValidator(Annotation annotation,
238: ClassValidator classValidator, String propertyName) {
239: ValidateMaxLength validateMaxLength = (ValidateMaxLength) annotation;
240: classValidator.add(new MaxLengthValidator(propertyName,
241: new Long(validateMaxLength.length()),
242: validateMaxLength.errorCode()));
243: }
244:
245: }
246:
247: class ValidateIsValidIter implements AnnotationValidatorCreator {
248:
249: public Class getAnnotationClass() {
250: return ValidateIsValid.class;
251: }
252:
253: public void createAndRegisterValidator(Annotation annotation,
254: ClassValidator classValidator, String propertyName) {
255: ValidateIsValid validateIsValid = (ValidateIsValid) annotation;
256: classValidator.add(new IsValidValidator(propertyName,
257: validateIsValid.errorCode()));
258: }
259:
260: }
261:
262: class ValidateMinLengthIter implements AnnotationValidatorCreator {
263:
264: public Class getAnnotationClass() {
265: return ValidateMinLength.class;
266: }
267:
268: public void createAndRegisterValidator(Annotation annotation,
269: ClassValidator classValidator, String propertyName) {
270: ValidateMinLength validateMinLength = (ValidateMinLength) annotation;
271: classValidator.add(new MinLengthValidator(propertyName,
272: new Long(validateMinLength.length()),
273: validateMinLength.errorCode()));
274: }
275:
276: }
277:
278: class ValidateMinSizeIter implements AnnotationValidatorCreator {
279:
280: public Class getAnnotationClass() {
281: return ValidateMinSize.class;
282: }
283:
284: public void createAndRegisterValidator(Annotation annotation,
285: ClassValidator classValidator, String propertyName) {
286: ValidateMinSize validateMinSize = (ValidateMinSize) annotation;
287: classValidator.add(new MinSizeValidator(propertyName,
288: new Long(validateMinSize.size()), validateMinSize
289: .errorCode()));
290: }
291: }
292:
293: class ValidatePatternIter implements AnnotationValidatorCreator {
294:
295: public Class getAnnotationClass() {
296: return ValidatePattern.class;
297: }
298:
299: public void createAndRegisterValidator(Annotation annotation,
300: ClassValidator classValidator, String propertyName) {
301: ValidatePattern validatePattern = (ValidatePattern) annotation;
302: classValidator.add(new PatternValidator(propertyName,
303: validatePattern.pattern(), validatePattern
304: .errorCode()));
305: }
306:
307: }
308:
309: }
|