Source Code Cross Referenced for ValidationUtils.java in  » J2EE » spring-framework-2.0.6 » org » springframework » 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 » J2EE » spring framework 2.0.6 » org.springframework.validation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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