KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > bpcatalog > validator > DateValidator


1 /* Copyright 2005 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: http://developer.sun.com/berkeley_license.html
2 $Id: DateValidator.java,v 1.1 2005/03/08 22:08:46 gmurray71 Exp $ */

3
4 package com.sun.j2ee.blueprints.bpcatalog.validator;
5
6 import java.util.*;
7 import java.util.regex.*;
8
9 import javax.faces.FactoryFinder;
10 import javax.faces.component.UIComponent;
11 import javax.faces.component.UIInput;
12 import javax.faces.context.FacesContext;
13 import javax.faces.application.FacesMessage;
14 import javax.faces.validator.Validator;
15 import javax.faces.validator.ValidatorException;
16 import javax.faces.component.StateHolder;
17
18 import javax.faces.application.ApplicationFactory;
19 import javax.faces.application.Application;
20
21
22
23 /**
24  *
25  */

26
27 public class DateValidator implements Validator, StateHolder {
28
29
30     public static final String JavaDoc DATE_INVALID_MESSAGE_ID =
31         "DateInvalid";
32
33     private String JavaDoc datePattern = null;
34     private boolean transientValue = false;
35
36     public DateValidator() {
37         super();
38     }
39
40
41     public DateValidator(String JavaDoc datePattern) {
42         super();
43         this.datePattern = datePattern;
44     }
45
46
47     public String JavaDoc getDatePatern() {
48
49         return datePattern;
50     }
51
52
53     public void setDatePattern(String JavaDoc datePattern) {
54         this.datePattern = datePattern;
55     }
56     
57     public void validate(FacesContext context, UIComponent component,
58              Object JavaDoc inputValue) {
59         
60         boolean valid = false;
61         String JavaDoc value = (String JavaDoc)inputValue;
62         try {
63             valid = Pattern.matches(datePattern, value);
64         } catch(PatternSyntaxException pse) {
65            System.out.println("Date:Validator: There is a problem with the regular expression!");
66         }
67         if (!valid) {
68             FacesMessage error = new FacesMessage("Date " + value +
69                             " is invalid. Please enter a date conforming to " +
70                              datePattern);
71             throw new ValidatorException(error);
72         }
73     }
74     
75     
76     public Object JavaDoc saveState(FacesContext context) {
77         Object JavaDoc values[] = new Object JavaDoc[1];
78         values[0] = datePattern;
79         return (values);
80     }
81
82
83     public void restoreState(FacesContext context, Object JavaDoc state) {
84         Object JavaDoc values[] = (Object JavaDoc[]) state;
85         datePattern = (String JavaDoc) values[0];
86     }
87
88
89     public boolean isTransient() {
90         return (this.transientValue);
91     }
92
93
94     public void setTransient(boolean transientValue) {
95         this.transientValue = transientValue;
96     }
97 }
98
Popular Tags