Source Code Cross Referenced for Validator.java in  » J2EE » webwork-2.2.6 » com » opensymphony » xwork » 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 » J2EE » webwork 2.2.6 » com.opensymphony.xwork.validator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2007 by OpenSymphony
003:         * All rights reserved.
004:         */
005:        package com.opensymphony.xwork.validator;
006:
007:        /**
008:         * <!-- START SNIPPET: validatorFlavours -->
009:         * <p>The validators supplied by the Xwork distribution (and any validators you 
010:         * might write yourself) come in two different flavors:</p>
011:         *
012:         * <ol>
013:         *  <li> Plain Validators / Non-Field validators </li>
014:         *  <li> FieldValidators </li>
015:         * </ol>
016:         *
017:         * <p>Plain Validators (such as the ExpressionValidator) perform validation checks 
018:         * that are not inherently tied to a single specified field. When you declare a 
019:         * plain Validator in your -validation.xml file you do not associate a fieldname 
020:         * attribute with it. (You should avoid using plain Validators within the 
021:         * <field-validator> syntax described below.)</p>
022:         *
023:         * <p>FieldValidators (such as the EmailValidator) are designed to perform 
024:         * validation checks on a single field. They require that you specify a fieldname 
025:         * attribute in your -validation.xml file. There are two different (but equivalent) 
026:         * XML syntaxes you can use to declare FieldValidators (see "<validator> vs. 
027:         * <field-Validator> syntax" below).</p>
028:         *
029:         * <p>There are two places where the differences between the two validator flavors 
030:         * are important to keep in mind:</p>
031:         *
032:         * <ol>
033:         *   <li> when choosing the xml syntax used for declaring a validator 
034:         *       (either <validator> or <field-validator>)</li>
035:         *   <li> when using the short-circuit capability</li>
036:         * </ol>
037:         *
038:         * <p><b>NOTE:</b>Note that you do not declare what "flavor" of validator you are 
039:         * using in your -validation.xml file, you just declare the name of the validator 
040:         * to use and WebWork will know whether it's a "plain Validator" or a "FieldValidator" 
041:         * by looking at the validation class that the validator's programmer chose 
042:         * to implement.</p>
043:         * <!-- END SNIPPET: validatorFlavours -->
044:         * 
045:         * 
046:         * 
047:         * 
048:         * <!-- START SNIPPET: validationRules -->
049:         * <p>To define validation rules for an Action, create a file named ActionName-validation.xml 
050:         * in the same package as the Action. You may also create alias-specific validation rules which 
051:         * add to the default validation rules defined in ActionName-validation.xml by creating 
052:         * another file in the same directory named ActionName-aliasName-validation.xml. In both 
053:         * cases, ActionName is the name of the Action class, and aliasName is the name of the 
054:         * Action alias defined in the xwork.xml configuration for the Action.</p>
055:         * 
056:         * <p>The framework will also search up the inheritance tree of the Action to 
057:         * find validation rules for directly implemented interfaces and parent classes of the Action. 
058:         * This is particularly powerful when combined with ModelDriven Actions and the VisitorFieldValidator. 
059:         * Here's an example of how validation rules are discovered. Given the following class structure:</p>
060:         * 
061:         * <ul>
062:         *   <li>interface Animal;</li>
063:         *   <li>interface Quadraped extends Animal;</li>
064:         *   <li>class AnimalImpl implements Animal;</li>
065:         *   <li>class QuadrapedImpl extends AnimalImpl implements Quadraped;</li>
066:         *   <li>class Dog extends QuadrapedImpl;</li>
067:         * </ul>
068:         * 
069:         * <p>The framework method will look for the following config files if Dog is to be validated:</p>
070:         *
071:         * <ul>
072:         *   <li>Animal</li>
073:         *   <li>Animal-aliasname</li>
074:         *   <li>AnimalImpl</li>
075:         * 	 <li>AnimalImpl-aliasname</li>
076:         *   <li>Quadraped</li>
077:         *   <li>Quadraped-aliasname</li>
078:         *   <li>QuadrapedImpl</li>
079:         *   <li>QuadrapedImpl-aliasname</li>
080:         *   <li>Dog</li>
081:         *   <li>Dog-aliasname</li>
082:         * </ul>
083:         *
084:         * <p>While this process is similar to what the XW:Localization framework does 
085:         * when finding messages, there are some subtle differences. The most important 
086:         * difference is that validation rules are discovered from the parent downwards.
087:         * </p>
088:         * 
089:         * <p><b>NOTE:</b>Child's *-validation.xml will override parent's *-validation.xml 
090:         * according to the class hierarchi defined above.</p>
091:         * <!-- END SNIPPET: validationRules -->
092:         * 
093:         * 
094:         * <!-- START SNIPPET: validatorVsFieldValidators1 -->
095:         * <p>There are two ways you can define validators in your -validation.xml file:</p>
096:         * <ol>
097:         *  <li> &lt;validator&gt; </li>
098:         *  <li> &lt;field-validator&gt; </li>
099:         * </ol>
100:         * <p>Keep the following in mind when using either syntax:</p>
101:         * 
102:         * <p><b>Non-Field-Validator</b>
103:         * The &lt;validator&gt; element allows you to declare both types of validators 
104:         * (either a plain Validator a field-specific FieldValidator).</p>
105:         * <!-- END SNIPPET: validatorVsFieldValidators1 -->
106:         *
107:         *<pre>
108:         * <!-- START SNIPPET: nonFieldValidatorUsingValidatorSyntax -->
109:         *    &lt;!-- Declaring a plain Validator using the &lt;validator&gt; syntax: --&gt;
110:         *
111:         *    &lt;validator type="expression&gt;
112:         *          &lt;param name="expression">foo gt bar&lt;/param&gt;
113:         *          &lt;message&gt;foo must be great than bar.&lt;/message&gt;
114:         *    &lt;/validator&gt;
115:         * <!-- END SNIPPET: nonFieldValidatorUsingValidatorSyntax -->
116:         * </pre>
117:         * 
118:         * <pre>
119:         * <!-- START SNIPPET: fieldValidatorUsingValidatorSyntax -->
120:         *    &lt;!-- Declaring a field validator using the &lt;validator&gt; syntax; --&gt;
121:         *
122:         *    &lt;validator type="required"&gt;
123:         *         &lt;param name="fieldName"&gt;bar&lt;/param&gt;
124:         *         &lt;message&gt;You must enter a value for bar.&lt;/message&gt;
125:         *    &lt/validator&gt;
126:         * <!-- END SNIPPET: fieldValidatorUsingValidatorSyntax -->
127:         * </pre>
128:         *
129:         *
130:         * <!-- START SNIPPET: validatorVsFieldValidators2 -->
131:         * <p><b>field-validator</b>
132:         * The &lt;field-validator&gt; elements are basically the same as the &lt;validator&gt; elements 
133:         * except that they inherit the fieldName attribute from the enclosing &lt;field&gt; element. 
134:         * FieldValidators defined within a &lt;field-validator&gt; element will have their fieldName 
135:         * automatically filled with the value of the parent &lt;field&gt; element's fieldName 
136:         * attribute. The reason for this structure is to conveniently group the validators 
137:         * for a particular field under one element, otherwise the fieldName attribute 
138:         * would have to be repeated, over and over, for each individual &lt;validator&gt;.</p>
139:         * 
140:         * <p><b>HINT:</b>
141:         * It is always better to defined field-validator inside a &lt;field&gt; tag instead of 
142:         * using a &lt;validator&gt; tag and supplying fieldName as its param as the xml code itself 
143:         * is clearer (grouping of field is clearer)</p>
144:         * 
145:         * <p><b>NOTE:</b>
146:         * Note that you should only use FieldValidators (not plain Validators) within a 
147:         * <field-validator> block. A plain Validator inside a &lt;field&gt; will not be 
148:         * allowed and would generate error when parsing the xml, as it is not allowed in 
149:         * the defined dtd (xwork-validator-1.0.2.dtd)</p>
150:         * <!-- END SNIPPET: validatorVsFieldValidators2 -->
151:         *
152:         * <pre>
153:         * <!-- START SNIPPET: fieldValidatorUsingFieldValidatorSyntax -->
154:         * Declaring a FieldValidator using the &lt;field-validator&gt; syntax:
155:         * 
156:         * &lt;field name="email_address"&gt;
157:         *   &lt;field-validator type="required"&gt;
158:         *       &lt;message&gt;You cannot leave the email address field empty.&lt;/message&gt;
159:         *   &lt;/field-validator&gt;
160:         *   &lt;field-validator type="email"&gt;
161:         *       &lt;message&gt;The email address you entered is not valid.&lt;/message&gt;
162:         *   &lt;/field-validator&gt;
163:         * &lt;/field&gt;
164:         * <!-- END SNIPPET: fieldValidatorUsingFieldValidatorSyntax -->
165:         * </pre>
166:         * 
167:         * 
168:         * <!-- START SNIPPET: validatorVsFieldValidators3 -->
169:         * <p>The choice is yours. It's perfectly legal to only use <validator> elements 
170:         * without the <field> elements and set the fieldName attribute for each of them. 
171:         * The following are effectively equal:</P>
172:         * <!-- END SNIPPET: validatorVsFieldValidators3 -->
173:         * 
174:         * <pre>
175:         * <!-- START-SNIPPET: similarVaidatorDeclaredInDiffSyntax -->
176:         * &lt;field name="email_address"&gt;
177:         *   &lt;field-validator type="required"&gt;
178:         *       &lt;message&gt;You cannot leave the email address field empty.&lt;/message&gt;
179:         *   &lt;/field-validator&gt;
180:         *   &lt;field-validator type="email"&gt;
181:         *       &lt;message&gt;The email address you entered is not valid.&lt;/message&gt;
182:         *   &lt;/field-validator&gt;
183:         * &lt;/field&gt;
184:         *
185:         *
186:         * &lt;validator type="required"&gt;
187:         *   &lt;param name="fieldName"&gt;email_address&lt;/param&gt;
188:         *   &lt;message&gt;You cannot leave the email address field empty.&lt;/message&gt;
189:         * &lt;/validator&gt;
190:         * &lt;validator type="email"&gt;
191:         *   &lt;param name="fieldName"&gt;email_address&lt;/param&gt;
192:         *   &lt;message&gt;The email address you entered is not valid.&lt;/message&gt;
193:         * &lt;/validator&gt;
194:         * <!-- END SNIPPET: similarVaidatorDeclaredInDiffSyntax -->
195:         * </pre>
196:         *
197:         *
198:         * <!-- START SNIPPET: shortCircuitingValidators1 -->
199:         * <p>Beginning with XWork 1.0.1 (bundled with WebWork 2.1), it is possible 
200:         * to short-circuit a stack of validators. Here is another sample config file 
201:         * containing validation rules from the Xwork test cases: Notice that some of the 
202:         * &lt;field-validator&gt; and &lt;validator&gt; elements have the short-circuit 
203:         * attribute set to true.</p>
204:         * <!-- END SNIPPET : shortCircuitingValidators1 -->
205:         *
206:         *<pre>
207:         * &lt;!-- START SNIPPET: exShortCircuitingValidators --&gt;
208:         * &lt;!DOCTYPE validators PUBLIC 
209:         *         "-//OpenSymphony Group//XWork Validator 1.0.2//EN" 
210:         *         "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"&gt;
211:         * &lt;validators&gt;
212:         *   &lt;!-- Field Validators for email field --&gt;
213:         *   &lt;field name="email"&gt;
214:         *       &lt;field-validator type="required" short-circuit="true"&gt;
215:         *           &lt;message&gt;You must enter a value for email.&lt;/message&gt;
216:         *       &lt;/field-validator&gt;
217:         *       &lt;field-validator type="email" short-circuit="true"&gt;
218:         *           &lt;message&gt;Not a valid e-mail.&lt;/message&gt;
219:         *       &lt;/field-validator&gt;
220:         *   &lt;/field&gt;
221:         *   &lt;!-- Field Validators for email2 field --&gt;
222:         *   &lt;field name="email2"&gt;
223:         *      &lt;field-validator type="required"&gt;
224:         *           &lt;message&gt;You must enter a value for email2.&lt;/message&gt;
225:         *       &lt;/field-validator&gt;
226:         *      &lt;field-validator type="email"&gt;
227:         *           &lt;message&gt;Not a valid e-mail2.&lt;/message&gt;
228:         *       &lt;/field-validator&gt;
229:         *   &lt;/field&gt;
230:         *   &lt;!-- Plain Validator 1 --&gt;
231:         *   &lt;validator type="expression"&gt;
232:         *       &lt;param name="expression"&gt;email.equals(email2)&lt;/param&gt;
233:         *       &lt;message&gt;Email not the same as email2&lt;/message&gt;
234:         *   &lt;/validator&gt;
235:         *   &lt;!-- Plain Validator 2 --&gt;
236:         *   &lt;validator type="expression" short-circuit="true"&gt;
237:         *       &lt;param name="expression"&gt;email.startsWith('mark')&lt;/param&gt;
238:         *       &lt;message&gt;Email does not start with mark&lt;/message&gt;
239:         *   &lt;/validator&gt;
240:         * &lt;/validators&gt;
241:         * &lt;!-- END SNIPPET: exShortCircuitingValidators --&gt;
242:         *</pre>
243:         *
244:         * <!-- START SNIPPET:shortCircuitingValidators2  -->
245:         * <p><b>short-circuiting and Validator flavors</b></p>
246:         * <p>Plain validator takes precedence over field-validator. They get validated 
247:         * first in the order they are defined and then the field-validator in the order 
248:         * they are defined. Failure of a particular validator marked as short-circuit 
249:         * will prevent the evaluation of subsequent validators and an error (action 
250:         * error or field error depending on the type of validator) will be added to 
251:         * the ValidationContext of the object being validated.</p>
252:         *
253:         * <p>In the example above, the actual execution of validator would be as follows:</p>
254:         * 
255:         * <ol>
256:         *  <li> Plain Validator 1</li>
257:         *  <li> Plain Validator 2</li>
258:         *  <li> Field Validators for email field</li>
259:         *  <li> Field Validators for email2 field</li>
260:         * </ol>
261:         *
262:         * <p>Since Field Validator 2 is short-circuited, if its validation failed, 
263:         * it will causes Field validators for email field and Field validators for email2 
264:         * field to not be validated as well.</p>
265:         * 
266:         * <p><b>Usefull Information:</b>
267:         * More complecated validation should probably be done in the validate() 
268:         * method on the action itself (assuming the action implements Validatable 
269:         * interface which ActionSupport already does).</p>
270:         * 
271:         * <p>
272:         * A plain Validator (non FieldValidator) that gets short-circuited will 
273:         * completely break out of the validation stack – no other validators will be 
274:         * evaluated and plain validator takes precedence over field validator meaning 
275:         * that they get evaluated in the order they are defined before field validator 
276:         * gets a chance to be evaludated again according to their order defined.
277:         * </p>
278:         * <!-- END SNIPPET: shortCircuitingValidators2 -->
279:         * 
280:         * 
281:         * <!-- START SNIPPET: scAndValidatorFlavours1 -->
282:         * <p><b>Short cuircuiting and validator flavours</b></p>
283:         * <p>A FieldValidator that gets short-circuited will only prevent other 
284:         * FieldValidators for the same field from being evaluated. Note that this 
285:         * "same field" behavior applies regardless of whether the <validator> or 
286:         * <field-validator> syntax was used to declare the validation rule. 
287:         * By way of example, given this -validation.xml file:</p>
288:         * <!-- END SNIPPET: scAndValidatorFlavours1 -->
289:         * 
290:         * <pre>
291:         * <!-- START SNIPPET: exScAndValidatorFlavours -->
292:         * &lt;validator type="required" short-circuit="true"&gt;
293:         *   &lt;param name="fieldName"&gt;bar&lt;/param&gt;
294:         *   &lt;message&gt;You must enter a value for bar.&lt;/message&gt;
295:         * &lt;/validator&gt;
296:         *
297:         * &lt;validator type="expression"&gt;
298:         *   &lt;param name="expression">foo gt bar&lt;/param&gt;
299:         *   &lt;message&gt;foo must be great than bar.&lt;/message&gt;
300:         * &lt;/validator&gt;
301:         * <!-- END SNIPPET: exScAndValidatorFlavours -->
302:         * </pre>
303:         * 
304:         * <!-- START SNIPPET: scAndValidatorFlavours2 -->
305:         * <p>both validators will be run, even if the "required" validator short-circuits.
306:         * "required" validators are FieldValidator's and will not short-circuit the plain 
307:         * ExpressionValidator because FieldValidators only short-circuit other checks on 
308:         * that same field. Since the plain Validator is not field specific, it is 
309:         * not short-circuited.</p>
310:         * <!-- END SNIPPET: scAndValidatorFlavours2 -->
311:         * 
312:         * 
313:         * <!-- START SNIPPET: howXworkFindsValidatorForAction -->
314:         * <p>As mentioned above, the framework will also search up the inheritance tree 
315:         * of the action to find default validations for interfaces and parent classes of 
316:         * the Action. If you are using the short-circuit attribute and relying on 
317:         * default validators higher up in the inheritance tree, make sure you don't 
318:         * accidentally short-circuit things higher in the tree that you really want!</p>
319:         * <p>
320:         * The effect of having common validators on both 
321:         * </p>
322:         * <ul>
323:         * 	<li>&lt;actionClass&gt;-validation.xml</li>
324:         *     <li>&lt;actionClass&gt;-&lt;actionAlias&gt;-validation.xml</li>
325:         * </ul>
326:         * <p>
327:         * It should be noted that the nett effect will be validation on both the validators available 
328:         * in both validation configuration file. For example if we have 'requiredstring' validators defined
329:         * in both validation xml file for field named 'address', we will see 2 validation error indicating that
330:         * the the address cannot be empty (assuming validation failed). This is due to WebWork 
331:         * will merge validators found in both validation configuration files.
332:         * </p>
333:         * <p>
334:         * The logic behind this design decision is such that we could have common validators in 
335:         * &lt;actionClass&gt;-validation.xml and more context specific validators to be located
336:         * in &lt;actionClass&gt;-&lt;actionAlias&gt;-validation.xml
337:         * </p>
338:         * <!-- END SNIPPET: howXworkFindsValidatorForAction -->
339:         *
340:         * @author Jason Carreira
341:         * @author tmjee
342:         * 
343:         * @Version $Date: 2007-06-06 19:41:31 +0200 (Wed, 06 Jun 2007) $ $Id: Validator.java 1534 2007-06-06 17:41:31Z tm_jee $
344:         */
345:        public interface Validator {
346:
347:            void setDefaultMessage(String message);
348:
349:            String getDefaultMessage();
350:
351:            String getMessage(Object object);
352:
353:            void setMessageKey(String key);
354:
355:            String getMessageKey();
356:
357:            /**
358:             * This method will be called before validate with a non-null ValidatorContext.
359:             *
360:             * @param validatorContext
361:             */
362:            void setValidatorContext(ValidatorContext validatorContext);
363:
364:            ValidatorContext getValidatorContext();
365:
366:            /**
367:             * The validation implementation must guarantee that setValidatorContext will
368:             * be called with a non-null ValidatorContext before validate is called.
369:             *
370:             * @param object
371:             * @throws ValidationException
372:             */
373:            void validate(Object object) throws ValidationException;
374:
375:            void setValidatorType(String type);
376:
377:            String getValidatorType();
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.