Source Code Cross Referenced for AnnotatedClassesValidatorFactory.java in  » Development » JValidate » nl » knowlogy » validation » annotations » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » JValidate » nl.knowlogy.validation.annotations 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.