Source Code Cross Referenced for ValidationEngine.java in  » Development » JValidate » nl » knowlogy » validation » 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 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/ValidationEngine.java,v 1.4 2007/10/31 17:00:52 roberthofstra Exp $
003:         * $Revision: 1.4 $
004:         * $Date: 2007/10/31 17:00:52 $
005:         * 
006:         * 
007:         * Created on Oct 6, 2004
008:         *
009:         * All right reserved(c) 2004, Knowlogy
010:         * 
011:         * Copyright   2004 - 2005 Knowlogy, the Netherlands. All rights reserved. 
012:         * All Knowlogy brand and product names are trademarks or registered trademarks 
013:         * of Knowlogy in the Netherlands and other countries. 
014:         * 
015:         * No part of this publication may be reproduced, transmitted, stored in a retrieval system, 
016:         * or translated into any human or computer language, in any form, or by any means, electronic, 
017:         * mechanical, magnetic, optical, chemical, manual, or otherwise, 
018:         * without the prior written permission of the copyright owner, Knowlogy.
019:         * 
020:         */
021:        package nl.knowlogy.validation;
022:
023:        import nl.knowlogy.validation.metadata.ClassMetadata;
024:
025:        import org.apache.commons.collections.FastHashMap;
026:        import org.apache.log4j.Logger;
027:
028:        /**
029:         * The ValidationEngine class is responsible for validating 
030:         * objects.
031:         * 
032:         * 
033:         * @author <a href="mailto:robert.hofstra@knowlogy.nl">Robert Hofstra, Knowlogy</a>
034:         * 
035:         */
036:        public class ValidationEngine {
037:
038:            private static final Logger logger = Logger
039:                    .getLogger(ValidationEngine.class);
040:            private static final ValidationEngine validationEngine = new ValidationEngine();
041:            private FastHashMap validatorsMap;
042:            private ValidatorSupplier validatorSupplier;
043:
044:            public static ValidationEngine getInstance() {
045:                return validationEngine;
046:            }
047:
048:            static {
049:                try {
050:                    // trick to load, if existing, the AnnotatedClassesValidatorFactory, so it's
051:                    // registers itself as a validatorsupplier
052:                    Class
053:                            .forName("nl.knowlogy.validation.annotations.AnnotatedClassesValidatorFactory");
054:                    logger.info("Loaded the AnnotatedClassesValidatorFactory");
055:                } catch (Exception ex) {
056:                    logger
057:                            .info("Not loaded the AnnotatedClassesValidatorFactory");
058:                }
059:            }
060:
061:            public static void validate(Object object, Messages messages) {
062:                getInstance().doValidate(object, messages);
063:            }
064:
065:            /**
066:             * Validates if a value is valid for the property of a javabean from clazz.
067:             * 
068:             * This is for validation a value without setting it actualy.
069:             * 
070:             * @param clazz of the javabean for which the value must be validated
071:             * @param propertyName name of the property
072:             * @param value value 
073:             * @param messages a messages container to which properties are added.
074:             */
075:            public static void validateProperty(Class clazz,
076:                    String propertyName, Object value, Messages messages) {
077:                getInstance().doValidateProperty(clazz, propertyName, value,
078:                        messages);
079:            }
080:
081:            public static void validateGroup(Object object, String groupName,
082:                    Messages messages) {
083:                getInstance().doValidateGroup(object, groupName, messages);
084:            }
085:
086:            public static void validate(Object object) {
087:                getInstance().doValidate(object);
088:            }
089:
090:            public static ClassValidator getClassValidator(Class clazz) {
091:                return getInstance().doGetValidator(clazz);
092:            }
093:
094:            public static void registerValidator(ClassValidator classValidator) {
095:                getInstance().doRegisterValidator(classValidator);
096:            }
097:
098:            public static void setValidatorSupplier(
099:                    ValidatorSupplier validatorSupplier) {
100:                getInstance().doSetValidatorSupplier(validatorSupplier);
101:            }
102:
103:            public static ClassMetadata getClassMetaData(Class clazz) {
104:                return getInstance().doGetClassMetaData(clazz);
105:            }
106:
107:            public synchronized void doRegisterValidator(
108:                    ClassValidator classValidator) {
109:                logger.debug("Registered classValidator " + classValidator);
110:                validatorsMap.setFast(false);
111:                validatorsMap.put(classValidator.getClazz().getName(),
112:                        classValidator);
113:                validatorsMap.setFast(true);
114:            }
115:
116:            protected synchronized void doSetValidatorSupplier(
117:                    ValidatorSupplier validatorSupplier) {
118:                this .validatorSupplier = validatorSupplier;
119:            }
120:
121:            protected ClassMetadata doGetClassMetaData(Class clazz) {
122:                ClassValidator classValidator = doGetValidator(clazz);
123:                if (classValidator instanceof  ClassMetadata) {
124:                    return (ClassMetadata) classValidator;
125:                }
126:                return null;
127:            }
128:
129:            protected void doValidateProperty(Class clazz, String propertyName,
130:                    Object value, Messages messages) {
131:                ClassValidator classValidator = doGetValidator(clazz);
132:                if (classValidator != null) {
133:                    classValidator.validatePropertyValue(propertyName, value,
134:                            messages);
135:                }
136:            }
137:
138:            protected ClassValidator doGetValidator(Class clazz) {
139:                ClassValidator classValidator = (ClassValidator) validationEngine.validatorsMap
140:                        .get(clazz.getName());
141:                if (classValidator == null) {
142:                    if (validatorSupplier != null) {
143:                        classValidator = validatorSupplier.getValidator(clazz);
144:                        if (classValidator != null) {
145:                            registerValidator(classValidator);
146:                        }
147:                    }
148:                    // still null
149:                    if (classValidator == null) {
150:                        try {
151:                            // force registration of xdoclet generated classes by loading the class  
152:                            String validatorClassName = clazz.getName()
153:                                    + "ClassValidator";
154:                            Class.forName(validatorClassName);
155:                            classValidator = (ClassValidator) validationEngine.validatorsMap
156:                                    .get(clazz.getName());
157:                        } catch (Exception ex) {
158:                            // do nothing 
159:                        }
160:                    }
161:                    if (classValidator == null) {
162:                        // @todo still null maybe register a Null validator for this class
163:                        // to reconize no validationEngine available
164:                    }
165:                }
166:                return classValidator;
167:            }
168:
169:            protected ClassValidator doGetValidator(Object object) {
170:                return doGetValidator(object.getClass());
171:            }
172:
173:            protected void doValidate(Object object, Messages messages) {
174:                ClassValidator classValidator = doGetValidator(object
175:                        .getClass());
176:                if (classValidator != null) {
177:                    classValidator.validate(object, messages);
178:                }
179:                if (object instanceof  Validatable) {
180:                    ((Validatable) object).validate(messages);
181:                }
182:            }
183:
184:            protected void doValidate(Object object) {
185:                Messages messages = new MessagesImpl();
186:                doValidate(object, messages);
187:                if (messages.getNumberOfErrorMessages() > 0) {
188:                    throw new ValidationException(messages);
189:                }
190:            }
191:
192:            protected void doValidateGroup(Object object, String groupName,
193:                    Messages messages) {
194:                ClassValidator classValidator = doGetValidator(object
195:                        .getClass());
196:                if (classValidator != null) {
197:                    classValidator.validateGroup(object, groupName, messages);
198:                }
199:            }
200:
201:            protected ValidationEngine() {
202:                validatorsMap = new FastHashMap();
203:                validatorsMap.setFast(true);
204:            }
205:
206:            public interface ValidatorSupplier {
207:
208:                ClassValidator getValidator(Class clazz);
209:
210:            }
211:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.