KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > validators > DateValidator


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DateValidator.java,v 1.11 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms.validators;
21
22 import java.util.*;
23
24 import org.enhydra.barracuda.core.forms.*;
25 import org.enhydra.barracuda.plankton.*;
26 import org.apache.log4j.*;
27
28 /**
29  * This validator ensures that a value is a valid date
30  *
31  * @author Robert Leftwich <digital@ix.net.au>
32  * @author Christian Cryder [christianc@granitepeaks.com]
33  * @author Jacob Kjome <hoju@visi.com>
34  * @author Diez B. Roggisch <diez.roggisch@artnology.com>
35  * @version %I%, %G%
36  * @since 1.0
37  */

38 public class DateValidator extends DefaultFormValidator {
39
40     /**
41      * Public constructor.
42      *
43      */

44     public DateValidator() {
45         super();
46     }
47
48     /**
49      * Public constructor.
50      *
51      * @param ierrmsg the message associated with this error
52      */

53     public DateValidator(String JavaDoc ierrmsg) {
54         super(ierrmsg);
55     }
56
57     /**
58      * Validate a FormElement to make sure the value is a valid date
59      * Validation is not supported for
60      * FormType.BOOLEAN
61      * FormType,INTEGER
62      * FormType.LONG
63      * FormType.SHORT
64      * FormType.DOUBLE
65      * FormType.FLOAT
66      *
67      * @param val the value to compare the element value to
68      * @param element the form element that contains the val
69      * to validate elements by comparing them with other elements)
70      * @param deferExceptions do we want to deferValidation exceptions
71      * and attempt to validate all elements so that we can process
72      * all the exceptions at once
73      * @throws ValidationException if the element is not valid
74      */

75     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
76         // ilc_022102.1_start
77
// val is always going to be OrigVal, make no judgment on null values
78
if (this.isNull(val, element))
79           return;
80         // ilc_022102.1_end
81

82         // ilc_022102.2_start
83
// comment out we already using origVal.
84
/*
85         // dbr_122801.1_start If the original Value wasn't null, there might have been a parsing excpetion
86         Object oVal = element.getOrigVal();
87         if (oVal!=null
88             && oVal instanceof String
89             && !((String)oVal).trim().equals("")
90             && element.getParseException()!=null) {
91                 throw this.generateException(element,
92                             deferExceptions,
93                             oVal + " is not a valid date because " + element.getParseException());
94         }
95         // dbr_122801.1_end
96
97         // cannot validate a null or empty string value, use NotNullvalidator if you want to disallow nulls
98         if (val==null || val.toString().length()==0) return;
99         */

100         // ilc_022102.2_end
101

102         //if element is null, or not form type=STRING or DATE, err because this should
103
//not be considered a valid option (note that these exceptions are always
104
//immediate since they typically represent a programming error)
105
if (element==null) throw new ValidationException(val, "Object val:"+val+" is associated with a null FormElement");
106
107         FormType formType = element.getType();
108         if (!(formType.equals(FormType.DATE) || formType.equals(FormType.STRING))) {
109             throw new ValidationException(val, "Unsupported validation: "+
110                         val+" is of FormType " +
111                         formType.toString() +
112                         " and cannot be validated by this validator");
113         }
114
115
116         // ilc_022102.3_start
117
// testing against origVal it should always be a string, but test cases
118
// want to test against Date
119
if (val instanceof Date){
120           return;
121         }
122         else if (formType.equals(FormType.DATE) && element.getParseException()!=null) {
123             throw this.generateException(element, deferExceptions, val.toString()
124                                                                    + " is not a valid date, because "
125                                                                    + element.getParseException());
126         }
127         else if (formType.equals(FormType.STRING)) {
128           try {
129             Date dateVal = (Date)FormType.DATE.parse(val.toString());
130             if (localLogger.isInfoEnabled())
131               localLogger.info("Date val = " + dateVal.toString());
132           } catch (ParseException ex) {
133             throw this.generateException(element, deferExceptions, val.toString()
134                                                                    + " is not a valid date, because "
135                                                                    + ex);
136           }
137         }
138         // ilc_022102.3_end
139

140         // ilc_022102.4_start
141
// commented out the followin and replace with the above
142
/*
143         if (formType.equals(FormType.DATE)) {
144             return;
145         } else if (formType.equals(FormType.STRING)) {
146             // attempt to convert it to a date type
147             try {
148                 Date dateVal = (Date)FormType.DATE.parse(val.toString());
149                 if (localLogger.isInfoEnabled()) localLogger.info("Date val = " + dateVal.toString());
150             } catch (ParseException ex) {
151                 throw this.generateException(element,
152                             deferExceptions,
153                             val.toString() + " is not a valid date, because " + ex);
154             }
155         }
156         */

157         // ilc_022102.4_end
158
}
159 }
160
Popular Tags