Source Code Cross Referenced for Validator.java in  » Library » Apache-commons-validator-1.3.1-src » org » apache » commons » validator » 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 » Library » Apache commons validator 1.3.1 src » org.apache.commons.validator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.validator;
018:
019:        import java.io.Serializable;
020:        import java.util.HashMap;
021:        import java.util.Locale;
022:        import java.util.Map;
023:
024:        /**
025:         * Validations are processed by the validate method. An instance of
026:         * <code>ValidatorResources</code> is used to define the validators
027:         * (validation methods) and the validation rules for a JavaBean.
028:         *
029:         * @version $Revision: 478345 $ $Date: 2006-11-22 22:25:19 +0000 (Wed, 22 Nov 2006) $
030:         */
031:        public class Validator implements  Serializable {
032:
033:            /**
034:             * Resources key the JavaBean is stored to perform validation on.
035:             */
036:            public static final String BEAN_PARAM = "java.lang.Object";
037:
038:            /**
039:             * Resources key the <code>ValidatorAction</code> is stored under.
040:             * This will be automatically passed into a validation method
041:             * with the current <code>ValidatorAction</code> if it is
042:             * specified in the method signature.
043:             */
044:            public static final String VALIDATOR_ACTION_PARAM = "org.apache.commons.validator.ValidatorAction";
045:
046:            /**
047:             * Resources key the <code>ValidatorResults</code> is stored under.
048:             * This will be automatically passed into a validation method
049:             * with the current <code>ValidatorResults</code> if it is
050:             * specified in the method signature.
051:             */
052:            public static final String VALIDATOR_RESULTS_PARAM = "org.apache.commons.validator.ValidatorResults";
053:
054:            /**
055:             * Resources key the <code>Form</code> is stored under.
056:             * This will be automatically passed into a validation method
057:             * with the current <code>Form</code> if it is
058:             * specified in the method signature.
059:             */
060:            public static final String FORM_PARAM = "org.apache.commons.validator.Form";
061:
062:            /**
063:             * Resources key the <code>Field</code> is stored under.
064:             * This will be automatically passed into a validation method
065:             * with the current <code>Field</code> if it is
066:             * specified in the method signature.
067:             */
068:            public static final String FIELD_PARAM = "org.apache.commons.validator.Field";
069:
070:            /**
071:             * Resources key the <code>Validator</code> is stored under.
072:             * This will be automatically passed into a validation method
073:             * with the current <code>Validator</code> if it is
074:             * specified in the method signature.
075:             */
076:            public static final String VALIDATOR_PARAM = "org.apache.commons.validator.Validator";
077:
078:            /**
079:             * Resources key the <code>Locale</code> is stored.
080:             * This will be used to retrieve the appropriate
081:             * <code>FormSet</code> and <code>Form</code> to be
082:             * processed.
083:             */
084:            public static final String LOCALE_PARAM = "java.util.Locale";
085:
086:            /**
087:             * The Validator Resources.
088:             */
089:            protected ValidatorResources resources = null;
090:
091:            /**
092:             * The name of the form to validate
093:             */
094:            protected String formName = null;
095:
096:            /**
097:             * The name of the field on the form to validate
098:             * @since 1.2.0
099:             */
100:            protected String fieldName = null;
101:
102:            /**
103:             * Maps validation method parameter class names to the objects to be passed
104:             * into the method.
105:             */
106:            protected Map parameters = new HashMap();
107:
108:            /**
109:             * The current page number to validate.
110:             */
111:            protected int page = 0;
112:
113:            /**
114:             * The class loader to use for instantiating application objects.
115:             * If not specified, the context class loader, or the class loader
116:             * used to load Digester itself, is used, based on the value of the
117:             * <code>useContextClassLoader</code> variable.
118:             */
119:            protected ClassLoader classLoader = null;
120:
121:            /**
122:             * Whether or not to use the Context ClassLoader when loading classes
123:             * for instantiating new objects.  Default is <code>false</code>.
124:             */
125:            protected boolean useContextClassLoader = false;
126:
127:            /**
128:             * Set this to true to not return Fields that pass validation.  Only return failures.
129:             */
130:            protected boolean onlyReturnErrors = false;
131:
132:            /**
133:             * Construct a <code>Validator</code> that will
134:             * use the <code>ValidatorResources</code>
135:             * passed in to retrieve pluggable validators
136:             * the different sets of validation rules.
137:             *
138:             * @param resources <code>ValidatorResources</code> to use during validation.
139:             */
140:            public Validator(ValidatorResources resources) {
141:                this (resources, null);
142:            }
143:
144:            /**
145:             * Construct a <code>Validator</code> that will
146:             * use the <code>ValidatorResources</code>
147:             * passed in to retrieve pluggable validators
148:             * the different sets of validation rules.
149:             *
150:             * @param resources <code>ValidatorResources</code> to use during validation.
151:             * @param formName Key used for retrieving the set of validation rules.
152:             */
153:            public Validator(ValidatorResources resources, String formName) {
154:                if (resources == null) {
155:                    throw new IllegalArgumentException(
156:                            "Resources cannot be null.");
157:                }
158:
159:                this .resources = resources;
160:                this .formName = formName;
161:            }
162:
163:            /**
164:             * Construct a <code>Validator</code> that will
165:             * use the <code>ValidatorResources</code>
166:             * passed in to retrieve pluggable validators
167:             * the different sets of validation rules.
168:             *
169:             * @param resources <code>ValidatorResources</code> to use during validation.
170:             * @param formName Key used for retrieving the set of validation rules.
171:             * @param fieldName Key used for retrieving the set of validation rules for a field
172:             * @since 1.2.0
173:             */
174:            public Validator(ValidatorResources resources, String formName,
175:                    String fieldName) {
176:                if (resources == null) {
177:                    throw new IllegalArgumentException(
178:                            "Resources cannot be null.");
179:                }
180:
181:                this .resources = resources;
182:                this .formName = formName;
183:                this .fieldName = fieldName;
184:            }
185:
186:            /**
187:             * Set a parameter of a pluggable validation method.
188:             *
189:             * @param parameterClassName The full class name of the parameter of the
190:             * validation method that corresponds to the value/instance passed in with it.
191:             *
192:             * @param parameterValue The instance that will be passed into the
193:             * validation method.
194:             */
195:            public void setParameter(String parameterClassName,
196:                    Object parameterValue) {
197:                this .parameters.put(parameterClassName, parameterValue);
198:            }
199:
200:            /**
201:             * Returns the value of the specified parameter that will be used during the
202:             * processing of validations.
203:             *
204:             * @param parameterClassName The full class name of the parameter of the
205:             * validation method that corresponds to the value/instance passed in with it.
206:             * @return value of the specified parameter.
207:             */
208:            public Object getParameterValue(String parameterClassName) {
209:                return this .parameters.get(parameterClassName);
210:            }
211:
212:            /**
213:             * Gets the form name which is the key to a set of validation rules.
214:             * @return the name of the form.
215:             */
216:            public String getFormName() {
217:                return formName;
218:            }
219:
220:            /**
221:             * Sets the form name which is the key to a set of validation rules.
222:             * @param formName the name of the form.
223:             */
224:            public void setFormName(String formName) {
225:                this .formName = formName;
226:            }
227:
228:            /**
229:             * Sets the name of the field to validate in a form (optional)
230:             *
231:             * @param fieldName The name of the field in a form set
232:             * @since 1.2.0
233:             */
234:            public void setFieldName(String fieldName) {
235:                this .fieldName = fieldName;
236:            }
237:
238:            /**
239:             * Gets the page.  This in conjunction with the page property of
240:             * a <code>Field<code> can control the processing of fields. If the field's
241:             * page is less than or equal to this page value, it will be processed.
242:             * @return the page number.
243:             */
244:            public int getPage() {
245:                return page;
246:            }
247:
248:            /**
249:             * Sets the page.  This in conjunction with the page property of
250:             * a <code>Field<code> can control the processing of fields. If the field's page
251:             * is less than or equal to this page value, it will be processed.
252:             * @param page the page number.
253:             */
254:            public void setPage(int page) {
255:                this .page = page;
256:            }
257:
258:            /**
259:             * Clears the form name, resources that were added, and the page that was
260:             * set (if any).  This can be called to reinitialize the Validator instance
261:             * so it can be reused.  The form name (key to set of validation rules) and any
262:             * resources needed, like the JavaBean being validated, will need to
263:             * set and/or added to this instance again.  The
264:             * <code>ValidatorResources</code> will not be removed since it can be used
265:             * again and is thread safe.
266:             */
267:            public void clear() {
268:                this .formName = null;
269:                this .fieldName = null;
270:                this .parameters = new HashMap();
271:                this .page = 0;
272:            }
273:
274:            /**
275:             * Return the boolean as to whether the context classloader should be used.
276:             * @return whether the context classloader should be used.
277:             */
278:            public boolean getUseContextClassLoader() {
279:                return this .useContextClassLoader;
280:            }
281:
282:            /**
283:             * Determine whether to use the Context ClassLoader (the one found by
284:             * calling <code>Thread.currentThread().getContextClassLoader()</code>)
285:             * to resolve/load classes that are defined in various rules.  If not
286:             * using Context ClassLoader, then the class-loading defaults to
287:             * using the calling-class' ClassLoader.
288:             *
289:             * @param use determines whether to use Context ClassLoader.
290:             */
291:            public void setUseContextClassLoader(boolean use) {
292:                this .useContextClassLoader = use;
293:            }
294:
295:            /**
296:             * Return the class loader to be used for instantiating application objects
297:             * when required.  This is determined based upon the following rules:
298:             * <ul>
299:             * <li>The class loader set by <code>setClassLoader()</code>, if any</li>
300:             * <li>The thread context class loader, if it exists and the
301:             *     <code>useContextClassLoader</code> property is set to true</li>
302:             * <li>The class loader used to load the Digester class itself.
303:             * </ul>
304:             * @return the class loader.
305:             */
306:            public ClassLoader getClassLoader() {
307:                if (this .classLoader != null) {
308:                    return this .classLoader;
309:                }
310:
311:                if (this .useContextClassLoader) {
312:                    ClassLoader contextLoader = Thread.currentThread()
313:                            .getContextClassLoader();
314:                    if (contextLoader != null) {
315:                        return contextLoader;
316:                    }
317:                }
318:
319:                return this .getClass().getClassLoader();
320:            }
321:
322:            /**
323:             * Set the class loader to be used for instantiating application objects
324:             * when required.
325:             *
326:             * @param classLoader The new class loader to use, or <code>null</code>
327:             *  to revert to the standard rules
328:             */
329:            public void setClassLoader(ClassLoader classLoader) {
330:                this .classLoader = classLoader;
331:            }
332:
333:            /**
334:             * Performs validations based on the configured resources.
335:             *
336:             * @return The <code>Map</code> returned uses the property of the
337:             * <code>Field</code> for the key and the value is the number of error the
338:             * field had.
339:             * @throws ValidatorException If an error occurs during validation
340:             */
341:            public ValidatorResults validate() throws ValidatorException {
342:                Locale locale = (Locale) this .getParameterValue(LOCALE_PARAM);
343:
344:                if (locale == null) {
345:                    locale = Locale.getDefault();
346:                }
347:
348:                this .setParameter(VALIDATOR_PARAM, this );
349:
350:                Form form = this .resources.getForm(locale, this .formName);
351:                if (form != null) {
352:                    this .setParameter(FORM_PARAM, form);
353:                    return form.validate(this .parameters, this .resources
354:                            .getValidatorActions(), this .page, this .fieldName);
355:                }
356:
357:                return new ValidatorResults();
358:            }
359:
360:            /**
361:             * Returns true if the Validator is only returning Fields that fail validation.
362:             * @return whether only failed fields are returned.
363:             */
364:            public boolean getOnlyReturnErrors() {
365:                return onlyReturnErrors;
366:            }
367:
368:            /**
369:             * Configures which Fields the Validator returns from the validate() method.  Set this
370:             * to true to only return Fields that failed validation.  By default, validate() returns
371:             * all fields.
372:             * @param onlyReturnErrors whether only failed fields are returned.
373:             */
374:            public void setOnlyReturnErrors(boolean onlyReturnErrors) {
375:                this.onlyReturnErrors = onlyReturnErrors;
376:            }
377:
378:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.