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


001:        /*
002:         * Copyright 2002-2007 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 can become
030:         * one-liners when using {@link #rejectIfEmpty} or {@link #rejectIfEmptyOrWhitespace}.
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 codea nd error arguments
112:             * 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:             */
124:            public static void rejectIfEmpty(Errors errors, String field,
125:                    String errorCode, Object[] errorArgs) {
126:                rejectIfEmpty(errors, field, errorCode, errorArgs, null);
127:            }
128:
129:            /**
130:             * Reject the given field with the given error code, error arguments
131:             * and default message if the value is empty.
132:             * <p>An 'empty' value in this context means either <code>null</code> or
133:             * the empty string "". 
134:             * <p>The object whose field is being validated does not need to be passed
135:             * in because the {@link Errors} instance can resolve field values by itself
136:             * (it will usually hold an internal reference to the target object).
137:             * @param errors the <code>Errors</code> instance to register errors on
138:             * @param field the field name to check
139:             * @param errorCode the error code, interpretable as message key
140:             * @param errorArgs the error arguments, for argument binding via MessageFormat
141:             * (can be <code>null</code>)
142:             * @param defaultMessage fallback default message
143:             */
144:            public static void rejectIfEmpty(Errors errors, String field,
145:                    String errorCode, Object[] errorArgs, String defaultMessage) {
146:
147:                Assert.notNull(errors, "Errors object must not be null");
148:                Object value = errors.getFieldValue(field);
149:                if (value == null || !StringUtils.hasLength(value.toString())) {
150:                    errors.rejectValue(field, errorCode, errorArgs,
151:                            defaultMessage);
152:                }
153:            }
154:
155:            /**
156:             * Reject the given field with the given error code if the value is empty
157:             * or just contains whitespace.
158:             * <p>An 'empty' value in this context means either <code>null</code>,
159:             * the empty string "", or consisting wholly of whitespace.
160:             * <p>The object whose field is being validated does not need to be passed
161:             * in because the {@link Errors} instance can resolve field values by itself
162:             * (it will usually hold an internal reference to the target object).
163:             * @param errors the <code>Errors</code> instance to register errors on
164:             * @param field the field name to check
165:             * @param errorCode the error code, interpretable as message key
166:             */
167:            public static void rejectIfEmptyOrWhitespace(Errors errors,
168:                    String field, String errorCode) {
169:                rejectIfEmptyOrWhitespace(errors, field, errorCode, null, null);
170:            }
171:
172:            /**
173:             * Reject the given field with the given error code and default message
174:             * if the value is empty or just contains whitespace.
175:             * <p>An 'empty' value in this context means either <code>null</code>,
176:             * the empty string "", or consisting wholly of whitespace.
177:             * <p>The object whose field is being validated does not need to be passed
178:             * in because the {@link Errors} instance can resolve field values by itself
179:             * (it will usually hold an internal reference to the target object).
180:             * @param errors the <code>Errors</code> instance to register errors on
181:             * @param field the field name to check
182:             * @param errorCode the error code, interpretable as message key
183:             * @param defaultMessage fallback default message
184:             */
185:            public static void rejectIfEmptyOrWhitespace(Errors errors,
186:                    String field, String errorCode, String defaultMessage) {
187:
188:                rejectIfEmptyOrWhitespace(errors, field, errorCode, null,
189:                        defaultMessage);
190:            }
191:
192:            /**
193:             * Reject the given field with the given error code and error arguments
194:             * if the value is empty or just contains whitespace.
195:             * <p>An 'empty' value in this context means either <code>null</code>,
196:             * the empty string "", or consisting wholly of whitespace.
197:             * <p>The object whose field is being validated does not need to be passed
198:             * in because the {@link Errors} instance can resolve field values by itself
199:             * (it will usually hold an internal reference to the target object).
200:             * @param errors the <code>Errors</code> instance to register errors on
201:             * @param field the field name to check
202:             * @param errorCode the error code, interpretable as message key
203:             * @param errorArgs the error arguments, for argument binding via MessageFormat
204:             * (can be <code>null</code>)
205:             */
206:            public static void rejectIfEmptyOrWhitespace(Errors errors,
207:                    String field, String errorCode, Object[] errorArgs) {
208:
209:                rejectIfEmptyOrWhitespace(errors, field, errorCode, errorArgs,
210:                        null);
211:            }
212:
213:            /**
214:             * Reject the given field with the given error code, error arguments
215:             * and default message if the value is empty or just contains whitespace.
216:             * <p>An 'empty' value in this context means either <code>null</code>,
217:             * the empty string "", or consisting wholly of whitespace.
218:             * <p>The object whose field is being validated does not need to be passed
219:             * in because the {@link Errors} instance can resolve field values by itself
220:             * (it will usually hold an internal reference to the target object).
221:             * @param errors the <code>Errors</code> instance to register errors on
222:             * @param field the field name to check
223:             * @param errorCode the error code, interpretable as message key
224:             * @param errorArgs the error arguments, for argument binding via MessageFormat
225:             * (can be <code>null</code>)
226:             * @param defaultMessage fallback default message
227:             */
228:            public static void rejectIfEmptyOrWhitespace(Errors errors,
229:                    String field, String errorCode, Object[] errorArgs,
230:                    String defaultMessage) {
231:
232:                Assert.notNull(errors, "Errors object must not be null");
233:                Object value = errors.getFieldValue(field);
234:                if (value == null || !StringUtils.hasText(value.toString())) {
235:                    errors.rejectValue(field, errorCode, errorArgs,
236:                            defaultMessage);
237:                }
238:            }
239:
240:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.