Source Code Cross Referenced for ParameterChecks.java in  » UML » AndroMDA-3.2 » org » andromda » cartridges » jsf » 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 » UML » AndroMDA 3.2 » org.andromda.cartridges.jsf.validator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.andromda.cartridges.jsf.validator;
002:
003:        import java.io.File;
004:        import java.io.FileNotFoundException;
005:        import java.io.Serializable;
006:        import java.net.URL;
007:        import java.text.DateFormat;
008:        import java.util.Collection;
009:        import java.util.Date;
010:        import java.util.Locale;
011:        import java.util.Map;
012:
013:        import javax.faces.component.UIInput;
014:        import javax.faces.component.ValueHolder;
015:        import javax.faces.context.FacesContext;
016:
017:        import org.apache.commons.lang.ObjectUtils;
018:        import org.apache.commons.lang.StringUtils;
019:        import org.apache.commons.validator.Field;
020:        import org.apache.commons.validator.GenericTypeValidator;
021:        import org.apache.commons.validator.GenericValidator;
022:        import org.apache.commons.validator.ValidatorAction;
023:
024:        /**
025:         * <p>
026:         * This class contains the default validations that are used in the
027:         * validator-rules.xml file.
028:         * </p>
029:         * <p>
030:         * In general passing in a null or blank will return a null Object or a false
031:         * boolean. However, nulls and blanks do not result in an error being added to
032:         * the errors.
033:         * </p>
034:         */
035:        public class ParameterChecks implements  Serializable {
036:            public static final String FIELD_TEST_NULL = "NULL";
037:            public static final String FIELD_TEST_NOTNULL = "NOTNULL";
038:            public static final String FIELD_TEST_EQUAL = "EQUAL";
039:
040:            /**
041:             * Checks if the field isn't null and length of the field is greater than
042:             * zero not including whitespace.
043:             *
044:             * @param context the faces context
045:             * @param object the value of the field being validated.
046:             * @param parameters Any field parameters from the validation.xml.
047:             * @param errors The <code>Map</code> object to add errors to if any
048:             *        validation errors occur.
049:             * @param action The <code>ValidatorAction</code> that is currently being
050:             *        performed.
051:             * @param field The <code>Field</code> object associated with the current
052:             *        field being validated.
053:             */
054:            public static void validateRequired(FacesContext context,
055:                    Object object, Map parameters, Collection errors,
056:                    ValidatorAction action, Field field) {
057:                String value = null;
058:                if (object instanceof  String) {
059:                    value = (String) object;
060:                } else {
061:                    value = ObjectUtils.toString(object);
062:                }
063:                if (StringUtils.isBlank(value)) {
064:                    errors.add(ValidatorMessages.getMessage(action, field,
065:                            context));
066:                }
067:            }
068:
069:            /**
070:             * Checks if the parameter isn't null based on the values of other fields.
071:             *
072:             * @param context the faces context
073:             * @param object the value of the field being validated.
074:             * @param parameters Any field parameters from the validation.xml.
075:             * @param errors The <code>Map</code> object to add errors to if any
076:             *        validation errors occur.
077:             * @param action The <code>ValidatorAction</code> that is currently being
078:             *        performed.
079:             * @param field The <code>Field</code> object associated with the current
080:             *        field being validated.
081:             */
082:            public static void validateRequiredIf(FacesContext context,
083:                    Object object, Map parameters, Collection errors,
084:                    ValidatorAction action, Field field) {
085:                boolean required = false;
086:
087:                final String value = ObjectUtils.toString(object);
088:
089:                int ctr = 0;
090:                String fieldJoin = "AND";
091:                if (!StringUtils.isBlank(field.getVarValue("fieldJoin"))) {
092:                    fieldJoin = field.getVarValue("fieldJoin");
093:                }
094:
095:                if (fieldJoin.equalsIgnoreCase("AND")) {
096:                    required = true;
097:                }
098:
099:                while (!StringUtils.isBlank(field.getVarValue("field[" + ctr
100:                        + "]"))) {
101:                    String dependProp = field.getVarValue("field[" + ctr + "]");
102:                    String dependTest = field.getVarValue("fieldTest[" + ctr
103:                            + "]");
104:                    String dependTestValue = field.getVarValue("fieldValue["
105:                            + ctr + "]");
106:                    String dependIndexed = field.getVarValue("fieldIndexed["
107:                            + ctr + "]");
108:
109:                    if (dependIndexed == null) {
110:                        dependIndexed = "false";
111:                    }
112:
113:                    String dependVal = null;
114:                    boolean this Required = false;
115:                    if (field.isIndexed()
116:                            && dependIndexed.equalsIgnoreCase("true")) {
117:                        String key = field.getKey();
118:                        if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
119:                            String ind = key.substring(0, key.indexOf(".") + 1);
120:                            dependProp = ind + dependProp;
121:                        }
122:                    }
123:
124:                    dependVal = (String) parameters.get(dependProp);
125:                    if (dependTest.equals(FIELD_TEST_NULL)) {
126:                        if ((dependVal != null) && (dependVal.length() > 0)) {
127:                            this Required = false;
128:                        } else {
129:                            this Required = true;
130:                        }
131:                    }
132:
133:                    if (dependTest.equals(FIELD_TEST_NOTNULL)) {
134:                        if ((dependVal != null) && (dependVal.length() > 0)) {
135:                            this Required = true;
136:                        } else {
137:                            this Required = false;
138:                        }
139:                    }
140:
141:                    if (dependTest.equals(FIELD_TEST_EQUAL)) {
142:                        this Required = dependTestValue
143:                                .equalsIgnoreCase(dependVal);
144:                    }
145:
146:                    if (fieldJoin.equalsIgnoreCase("AND")) {
147:                        required = required && this Required;
148:                    } else {
149:                        required = required || this Required;
150:                    }
151:
152:                    ctr++;
153:                }
154:
155:                if (required) {
156:                    if (StringUtils.isBlank(value)) {
157:                        errors.add(ValidatorMessages.getMessage(action, field,
158:                                context));
159:                    }
160:                }
161:            }
162:
163:            /**
164:             * Checks if the parameter matches the regular expression in the field's
165:             * mask attribute.
166:             *
167:             * @param context the faces context
168:             * @param object the value of the field being validated.
169:             * @param parameters Any field parameters from the validation.xml.
170:             * @param errors The <code>Map</code> object to add errors to if any
171:             *        validation errors occur.
172:             * @param action The <code>ValidatorAction</code> that is currently being
173:             *        performed.
174:             * @param field The <code>Field</code> object associated with the current
175:             *        field being validated.
176:             */
177:            public static void validateMask(FacesContext context,
178:                    Object object, Map parameters, Collection errors,
179:                    ValidatorAction action, Field field) {
180:                final String mask = field.getVarValue("mask");
181:                final String value = ObjectUtils.toString(object);
182:                if (StringUtils.isNotBlank(value)
183:                        && !GenericValidator.matchRegexp(value, mask)) {
184:                    errors.add(ValidatorMessages.getMessage(action, field,
185:                            context));
186:                }
187:            }
188:
189:            /**
190:             * Checks if the field can safely be converted to a byte primitive.
191:             *
192:             * @param context the faces context
193:             * @param object the value of the field being validated.
194:             * @param parameters Any field parameters from the validation.xml.
195:             * @param errors The <code>Map</code> object to add errors to if any
196:             *        validation errors occur.
197:             * @param action The <code>ValidatorAction</code> that is currently being
198:             *        performed.
199:             * @param field The <code>Field</code> object associated with the current
200:             *        field being validated.
201:             */
202:            public static void validateByte(FacesContext context,
203:                    Object object, Map parameters, Collection errors,
204:                    ValidatorAction action, Field field) {
205:                Byte result = null;
206:                String value = ObjectUtils.toString(object);
207:                if (StringUtils.isNotBlank(value)) {
208:                    result = GenericTypeValidator.formatByte(value);
209:
210:                    if (result == null) {
211:                        errors.add(ValidatorMessages.getMessage(action, field,
212:                                context));
213:                    }
214:                }
215:            }
216:
217:            /**
218:             * Checks if the field can safely be converted to a short primitive.
219:             *
220:             * @param context the faces context
221:             * @param object the value of the field being validated.
222:             * @param parameters Any field parameters from the validation.xml.
223:             * @param errors The <code>Map</code> object to add errors to if any
224:             *        validation errors occur.
225:             * @param action The <code>ValidatorAction</code> that is currently being
226:             *        performed.
227:             * @param field The <code>Field</code> object associated with the current
228:             *        field being validated.
229:             */
230:            public static void validateShort(FacesContext context,
231:                    Object object, Map parameters, Collection errors,
232:                    ValidatorAction action, Field field) {
233:                Short result = null;
234:                String value = ObjectUtils.toString(object);
235:                if (StringUtils.isNotBlank(value)) {
236:                    result = GenericTypeValidator.formatShort(value);
237:
238:                    if (result == null) {
239:                        errors.add(ValidatorMessages.getMessage(action, field,
240:                                context));
241:                    }
242:                }
243:            }
244:
245:            /**
246:             * Checks if the field can safely be converted to an int primitive.
247:             *
248:             * @param context the faces context
249:             * @param object the value of the field being validated.
250:             * @param parameters Any field parameters from the validation.xml.
251:             * @param errors The <code>Map</code> object to add errors to if any
252:             *        validation errors occur.
253:             * @param action The <code>ValidatorAction</code> that is currently being
254:             *        performed.
255:             * @param field The <code>Field</code> object associated with the current
256:             *        field being validated.
257:             */
258:            public static void validateInteger(FacesContext context,
259:                    Object object, Map parameters, Collection errors,
260:                    ValidatorAction action, Field field) {
261:                Integer result = null;
262:                String value = ObjectUtils.toString(object);
263:                if (StringUtils.isNotBlank(value)) {
264:                    result = GenericTypeValidator.formatInt(value);
265:                    if (result == null) {
266:                        errors.add(ValidatorMessages.getMessage(action, field,
267:                                context));
268:                    }
269:                }
270:            }
271:
272:            /**
273:             * Checks if the field can safely be converted to a long primitive.
274:             *
275:             * @param context the faces context
276:             * @param object the value of the field being validated.
277:             * @param parameters Any field parameters from the validation.xml.
278:             * @param errors The <code>Map</code> object to add errors to if any
279:             *        validation errors occur.
280:             * @param action The <code>ValidatorAction</code> that is currently being
281:             *        performed.
282:             * @param field The <code>Field</code> object associated with the current
283:             *        field being validated.
284:             */
285:            public static void validateLong(FacesContext context,
286:                    Object object, Map parameters, Collection errors,
287:                    ValidatorAction action, Field field) {
288:                Long result = null;
289:                String value = ObjectUtils.toString(object);
290:                if (StringUtils.isNotBlank(value)) {
291:                    result = GenericTypeValidator.formatLong(value);
292:                    if (result == null) {
293:                        errors.add(ValidatorMessages.getMessage(action, field,
294:                                context));
295:                    }
296:                }
297:            }
298:
299:            /**
300:             * Checks if the field can safely be converted to a float primitive.
301:             *
302:             * @param context the faces context
303:             * @param object the value of the field being validated.
304:             * @param parameters Any field parameters from the validation.xml.
305:             * @param errors The <code>Map</code> object to add errors to if any
306:             *        validation errors occur.
307:             * @param action The <code>ValidatorAction</code> that is currently being
308:             *        performed.
309:             * @param field The <code>Field</code> object associated with the current
310:             *        field being validated.
311:             */
312:            public static void validateFloat(FacesContext context,
313:                    Object object, Map parameters, Collection errors,
314:                    ValidatorAction action, Field field) {
315:                Float result = null;
316:                String value = ObjectUtils.toString(object);
317:                if (StringUtils.isNotBlank(value)) {
318:                    result = GenericTypeValidator.formatFloat(value);
319:
320:                    if (result == null) {
321:                        errors.add(ValidatorMessages.getMessage(action, field,
322:                                context));
323:                    }
324:                }
325:            }
326:
327:            /**
328:             * Checks if the field can safely be converted to a double primitive.
329:             *
330:             * @param context the faces context
331:             * @param object the value of the field being validated.
332:             * @param parameters Any field parameters from the validation.xml.
333:             * @param errors The <code>Map</code> object to add errors to if any
334:             *        validation errors occur.
335:             * @param action The <code>ValidatorAction</code> that is currently being
336:             *        performed.
337:             * @param field The <code>Field</code> object associated with the current
338:             *        field being validated.
339:             */
340:            public static void validateDouble(FacesContext context,
341:                    Object object, Map parameters, Collection errors,
342:                    ValidatorAction action, Field field) {
343:                Double result = null;
344:                String value = ObjectUtils.toString(object);
345:                if (StringUtils.isNotBlank(value)) {
346:                    result = GenericTypeValidator.formatDouble(value);
347:
348:                    if (result == null) {
349:                        errors.add(ValidatorMessages.getMessage(action, field,
350:                                context));
351:                    }
352:                }
353:            }
354:
355:            /**
356:             * Checks if the field is a valid date. If the field has a datePattern
357:             * variable, that will be used to format
358:             * <code>java.text.SimpleDateFormat</code>. If the field has a
359:             * datePatternStrict variable, that will be used to format
360:             * <code>java.text.SimpleDateFormat</code> and the length will be checked
361:             * so '2/12/1999' will not pass validation with the format 'MM/dd/yyyy'
362:             * because the month isn't two digits. If no datePattern variable is
363:             * specified, then the field gets the DateFormat.SHORT format for the
364:             * locale. The setLenient method is set to <code>false</code> for all
365:             * variations.  If the <code>object</code> is a date instance, then validation is not performed.
366:             *
367:             * @param context the faces context
368:             * @param object the value of the field being validated.
369:             * @param parameters Any field parameters from the validation.xml.
370:             * @param errors The <code>Map</code> object to add errors to if any
371:             *        validation errors occur.
372:             * @param action The <code>ValidatorAction</code> that is currently being
373:             *        performed.
374:             * @param field The <code>Field</code> object associated with the current
375:             *        field being validated.
376:             */
377:            public static void validateDate(FacesContext context,
378:                    Object object, Map parameters, Collection errors,
379:                    ValidatorAction action, Field field) {
380:                // - only validate if the object is not already a date or calendar
381:                if (!(object instanceof  java.util.Date)
382:                        && !(object instanceof  java.util.Calendar)) {
383:                    Date result = null;
384:                    String value = ObjectUtils.toString(object);
385:                    String datePattern = field.getVarValue("datePattern");
386:                    String datePatternStrict = field
387:                            .getVarValue("datePatternStrict");
388:                    Locale locale = Locale.getDefault();
389:
390:                    if (StringUtils.isNotBlank(value)) {
391:                        if (datePattern != null && datePattern.length() > 0) {
392:                            result = GenericTypeValidator.formatDate(value,
393:                                    datePattern, false);
394:                        } else if (datePatternStrict != null
395:                                && datePatternStrict.length() > 0) {
396:                            result = GenericTypeValidator.formatDate(value,
397:                                    datePatternStrict, true);
398:                        } else {
399:                            result = GenericTypeValidator.formatDate(value,
400:                                    locale);
401:                        }
402:                        if (result == null) {
403:                            errors.add(ValidatorMessages.getMessage(action,
404:                                    field, context));
405:                        }
406:                    }
407:                }
408:            }
409:
410:            /**
411:             * Checks if a fields value is within a range (min &amp; max specified in
412:             * the vars attribute).
413:             *
414:             * @param context the faces context
415:             * @param object the value of the field being validated.
416:             * @param parameters Any field parameters from the validation.xml.
417:             * @param errors The <code>Map</code> object to add errors to if any
418:             *        validation errors occur.
419:             * @param action The <code>ValidatorAction</code> that is currently being
420:             *        performed.
421:             * @param field The <code>Field</code> object associated with the current
422:             *        field being validated.
423:             */
424:            public static void validateLongRange(FacesContext context,
425:                    Object object, Map parameters, Collection errors,
426:                    ValidatorAction action, Field field) {
427:                final String value = ObjectUtils.toString(object);
428:                if (StringUtils.isNotBlank(value)) {
429:                    try {
430:                        long intValue = Long.parseLong(value);
431:                        long min = Long.parseLong(field.getVarValue("min"));
432:                        long max = Long.parseLong(field.getVarValue("max"));
433:
434:                        if (!GenericValidator.isInRange(intValue, min, max)) {
435:                            errors.add(ValidatorMessages.getMessage(action,
436:                                    field, context));
437:                        }
438:                    } catch (Exception exception) {
439:                        errors.add(ValidatorMessages.getMessage(action, field,
440:                                context));
441:                    }
442:                }
443:            }
444:
445:            /**
446:             * Checks if a fields value is within a range (min &amp; max specified in
447:             * the vars attribute).
448:             *
449:             * @param context the faces context
450:             * @param object the value of the field being validated.
451:             * @param parameters Any field parameters from the validation.xml.
452:             * @param errors The <code>Map</code> object to add errors to if any
453:             *        validation errors occur.
454:             * @param action The <code>ValidatorAction</code> that is currently being
455:             *        performed.
456:             * @param field The <code>Field</code> object associated with the current
457:             *        field being validated.
458:             */
459:            public static void validateDoubleRange(FacesContext context,
460:                    Object object, Map parameters, Collection errors,
461:                    ValidatorAction action, Field field) {
462:                final String value = ObjectUtils.toString(object);
463:                if (StringUtils.isNotBlank(value)) {
464:                    try {
465:                        double doubleValue = Double.parseDouble(value);
466:                        double min = Double.parseDouble(field
467:                                .getVarValue("min"));
468:                        double max = Double.parseDouble(field
469:                                .getVarValue("max"));
470:
471:                        if (!GenericValidator.isInRange(doubleValue, min, max)) {
472:                            errors.add(ValidatorMessages.getMessage(action,
473:                                    field, context));
474:                        }
475:                    } catch (Exception exception) {
476:                        errors.add(ValidatorMessages.getMessage(action, field,
477:                                context));
478:                    }
479:                }
480:            }
481:
482:            /**
483:             * Checks if a fields value is within a range (min &amp; max specified in
484:             * the vars attribute).
485:             *
486:             * @param context the faces context
487:             * @param object the value of the field being validated.
488:             * @param parameters Any field parameters from the validation.xml.
489:             * @param errors The <code>Map</code> object to add errors to if any
490:             *        validation errors occur.
491:             * @param action The <code>ValidatorAction</code> that is currently being
492:             *        performed.
493:             * @param field The <code>Field</code> object associated with the current
494:             *        field being validated.
495:             */
496:            public static void validateFloatRange(FacesContext context,
497:                    Object object, Map parameters, Collection errors,
498:                    ValidatorAction action, Field field) {
499:                final String value = ObjectUtils.toString(object);
500:                if (StringUtils.isNotBlank(value)) {
501:                    try {
502:                        float floatValue = Float.parseFloat(value);
503:                        float min = Float.parseFloat(field.getVarValue("min"));
504:                        float max = Float.parseFloat(field.getVarValue("max"));
505:
506:                        if (!GenericValidator.isInRange(floatValue, min, max)) {
507:                            errors.add(ValidatorMessages.getMessage(action,
508:                                    field, context));
509:                        }
510:                    } catch (Exception exception) {
511:                        errors.add(ValidatorMessages.getMessage(action, field,
512:                                context));
513:                    }
514:                }
515:            }
516:
517:            /**
518:             * Checks if the field is a valid credit card number.
519:             *
520:             * @param context the faces context
521:             * @param object the value of the field being validated.
522:             * @param parameters Any field parameters from the validation.xml.
523:             * @param errors The <code>Map</code> object to add errors to if any
524:             *        validation errors occur.
525:             * @param action The <code>ValidatorAction</code> that is currently being
526:             *        performed.
527:             * @param field The <code>Field</code> object associated with the current
528:             *        field being validated.
529:             */
530:            public static void validateCreditCard(FacesContext context,
531:                    Object object, Map parameters, Collection errors,
532:                    ValidatorAction action, Field field) {
533:                Long result = null;
534:                final String value = ObjectUtils.toString(object);
535:                if (StringUtils.isNotBlank(value)) {
536:                    result = GenericTypeValidator.formatCreditCard(value);
537:
538:                    if (result == null) {
539:                        errors.add(ValidatorMessages.getMessage(action, field,
540:                                context));
541:                    }
542:                }
543:            }
544:
545:            /**
546:             * Checks if a field has a valid exception-mail address.
547:             *
548:             * @param context the faces context
549:             * @param object the value of the field being validated.
550:             * @param parameters Any field parameters from the validation.xml.
551:             * @param errors The <code>Map</code> object to add errors to if any
552:             *        validation errors occur.
553:             * @param action The <code>ValidatorAction</code> that is currently being
554:             *        performed.
555:             * @param field The <code>Field</code> object associated with the current
556:             *        field being validated.
557:             */
558:            public static void validateEmail(FacesContext context,
559:                    Object object, Map parameters, Collection errors,
560:                    ValidatorAction action, Field field) {
561:                final String value = ObjectUtils.toString(object);
562:                if (StringUtils.isNotBlank(value)
563:                        && !GenericValidator.isEmail(value)) {
564:                    errors.add(ValidatorMessages.getMessage(action, field,
565:                            context));
566:                }
567:            }
568:
569:            /**
570:             * Checks if the field's length is less than or equal to the maximum value.
571:             * A <code>Null</code> will be considered an error.
572:             *
573:             * @param context the faces context
574:             * @param object the value of the field being validated.
575:             * @param parameters Any field parameters from the validation.xml.
576:             * @param errors The <code>Map</code> object to add errors to if any
577:             *        validation errors occur.
578:             * @param action The <code>ValidatorAction</code> that is currently being
579:             *        performed.
580:             * @param field The <code>Field</code> object associated with the current
581:             *        field being validated.
582:             */
583:            public static void validateMaxLength(FacesContext context,
584:                    Object object, Map parameters, Collection errors,
585:                    ValidatorAction action, Field field) {
586:                final String value = ObjectUtils.toString(object);
587:                if (StringUtils.isNotBlank(value)) {
588:                    try {
589:                        int max = Integer.parseInt(field
590:                                .getVarValue("maxlength"));
591:
592:                        if (!GenericValidator.maxLength(value, max)) {
593:                            errors.add(ValidatorMessages.getMessage(action,
594:                                    field, context));
595:                        }
596:                    } catch (Exception exception) {
597:                        errors.add(ValidatorMessages.getMessage(action, field,
598:                                context));
599:                    }
600:                }
601:            }
602:
603:            /**
604:             * Checks if the field's length is greater than or equal to the minimum
605:             * value. A <code>Null</code> will be considered an error.
606:             *
607:             * @param context the faces context
608:             * @param object the value of the field being validated.
609:             * @param parameters Any field parameters from the validation.xml.
610:             * @param errors The <code>Map</code> object to add errors to if any
611:             *        validation errors occur.
612:             * @param action The <code>ValidatorAction</code> that is currently being
613:             *        performed.
614:             * @param field The <code>Field</code> object associated with the current
615:             *        field being validated.
616:             */
617:            public static void validateMinLength(FacesContext context,
618:                    Object object, Map parameters, Collection errors,
619:                    ValidatorAction action, Field field) {
620:                String value = null;
621:                if (object != null) {
622:                    value = String.valueOf(parameters.get(field.getProperty()));
623:                }
624:
625:                if (!StringUtils.isBlank(value)) {
626:                    try {
627:                        int min = Integer.parseInt(field
628:                                .getVarValue("minlength"));
629:
630:                        if (!GenericValidator.minLength(value, min)) {
631:                            errors.add(ValidatorMessages.getMessage(action,
632:                                    field, context));
633:                        }
634:                    } catch (Exception exception) {
635:                        errors.add(ValidatorMessages.getMessage(action, field,
636:                                context));
637:                    }
638:                }
639:            }
640:
641:            /**
642:             * <p>
643:             * Validates whether the URL string passed in is a valid URL or not. Does
644:             * this by attempting to construct a java.net.URL instance and checking
645:             * whether or not, it's valid.
646:             * </p>
647:             *
648:             * @param context the faces context
649:             * @param object the value of the field being validated.
650:             * @param parameters Any field parameters from the validation.xml.
651:             * @param errors The <code>Map</code> object to add errors to if any
652:             *        validation errors occur.
653:             * @param action The <code>ValidatorAction</code> that is currently being
654:             *        performed.
655:             * @param field The <code>Field</code> object associated with the current
656:             *        field being validated.
657:             */
658:            public static void validateUrl(FacesContext context, Object object,
659:                    Map parameters, Collection errors, ValidatorAction action,
660:                    Field field) {
661:                boolean valid = true;
662:                String urlString = ObjectUtils.toString(object);
663:                try {
664:                    URL url = new URL(urlString);
665:
666:                    // first check to see if it can be used as a File
667:                    File file = new File(url.getFile());
668:                    valid = file.exists();
669:
670:                    // if the file doesn't exist, check to see if we can get the
671:                    // contents
672:                    // as a URL
673:                    try {
674:                        url.openStream();
675:                        valid = true;
676:                    } catch (FileNotFoundException ex) {
677:                        // if the flag isn't valid it means it
678:                        // failed the existence
679:                        if (!valid) {
680:                            valid = false;
681:                        }
682:                    }
683:                } catch (Exception exception) {
684:                    valid = false;
685:                }
686:                if (!valid) {
687:                    errors.add(ValidatorMessages.getMessage(action, field,
688:                            context));
689:                }
690:            }
691:
692:            /**
693:             *  Checks if the field is a valid time. If the field has a timePattern variable,
694:             *  that will be used to format <code>java.text.SimpleDateFormat</code>.
695:             *
696:             * @param context the faces context
697:             * @param object the value of the field being validated.
698:             * @param parameters Any field parameters from the validation.xml.
699:             * @param errors The <code>Map</code> object to add errors to if any
700:             *        validation errors occur.
701:             * @param action The <code>ValidatorAction</code> that is currently being
702:             *        performed.
703:             * @param field The <code>Field</code> object associated with the current
704:             *        field being validated.
705:             */
706:            public static void validateTime(FacesContext context,
707:                    Object object, Map parameters, Collection errors,
708:                    ValidatorAction action, Field field) {
709:                // - only validate if the object is not already a date
710:                if (!(object instanceof  java.util.Date)
711:                        || !(object instanceof  java.util.Calendar)) {
712:                    final String value = ObjectUtils.toString(object);
713:                    final String timePattern = field.getVarValue("timePattern");
714:
715:                    if (StringUtils.isNotBlank(value)) {
716:                        try {
717:                            if (timePattern != null && timePattern.length() > 0) {
718:                                final java.text.DateFormat timeFormatter = new java.text.SimpleDateFormat(
719:                                        timePattern);
720:                                timeFormatter.parse(value);
721:                            } else {
722:                                DateFormat.getTimeInstance().parse(value);
723:                            }
724:                        } catch (Exception exception) {
725:                            errors.add(ValidatorMessages.getMessage(action,
726:                                    field, context));
727:                        }
728:                    }
729:                }
730:            }
731:
732:            /**
733:             *  Checks if the field's value is equal to another field's value on the same form.
734:             *
735:             * @param context the faces context
736:             * @param object the value of the field being validated.
737:             * @param parameters Any field parameters from the validation.xml.
738:             * @param errors The <code>Map</code> object to add errors to if any
739:             *        validation errors occur.
740:             * @param action The <code>ValidatorAction</code> that is currently being
741:             *        performed.
742:             * @param field The <code>Field</code> object associated with the current
743:             *        field being validated.
744:             */
745:            public static void validateEqual(FacesContext context,
746:                    Object object, Map parameters, Collection errors,
747:                    ValidatorAction action, Field field) {
748:                final String value = ObjectUtils.toString(object);
749:                if (StringUtils.isNotBlank(value)) {
750:                    final String equalFieldName = field
751:                            .getVarValue("fieldName");
752:                    final ValueHolder equalField = (ValueHolder) context
753:                            .getViewRoot().findComponent(equalFieldName);
754:                    Object equalFieldValue = null;
755:                    // - we check for whether or not its actually a UIInput because sometimes it isn't 
756:                    //   depending on the framework (even though it should be).
757:                    if (equalField instanceof  UIInput) {
758:                        equalFieldValue = ((UIInput) equalField)
759:                                .getSubmittedValue();
760:                    }
761:                    // - we just ignore null values because it means it wasn't a UIInput instance
762:                    if (equalFieldValue != null
763:                            && !equalFieldValue.equals(value)) {
764:                        errors.add(ValidatorMessages.getMessage(action, field,
765:                                context));
766:                    }
767:
768:                }
769:            }
770:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.