KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > intake > validator > DateStringValidator


1 package org.apache.fulcrum.intake.validator;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.Date JavaDoc;
58 import java.util.Map JavaDoc;
59 import java.util.List JavaDoc;
60 import java.util.ArrayList JavaDoc;
61 import java.text.DateFormat JavaDoc;
62 import java.text.SimpleDateFormat JavaDoc;
63 import java.text.ParseException JavaDoc;
64 import org.apache.fulcrum.ServiceException;
65
66 /**
67  * Validates numbers with the following constraints in addition to those
68  * listed in DefaultValidator.
69  *
70  * <table>
71  * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
72  * <tr><td>format</td><td>see SimpleDateFormat javadoc</td>
73  * <td>&nbsp;</td></tr>
74  * <tr><td>formatx</td><td>see SimpleDateFormat javadoc</td>
75  * <td>&nbsp;</td></tr>
76  * <tr><td colspan=3>where x is &gt;= 0 to specify multiple date
77  * formats. Only one format rule should have a message</td></tr>
78  * <tr><td>flexible</td><td>true, as long as DateFormat can parse the date,
79  * allow it, and false</td>
80  * <td>false</td></tr>
81  * </table>
82  *
83  * @author <a HREF="mailto:jmcnally@collab.net>John McNally</a>
84  * @version $Id: DateStringValidator.java,v 1.1 2004/11/12 10:26:02 epugh Exp $
85  */

86 public class DateStringValidator
87     extends DefaultValidator
88 {
89     private static final String JavaDoc DEFAULT_DATE_MESSAGE =
90         "Date could not be parsed";
91
92     private List JavaDoc dateFormats;
93     private String JavaDoc dateFormatMessage;
94     private boolean flexible;
95     private DateFormat JavaDoc df;
96     private SimpleDateFormat JavaDoc sdf;
97
98
99     public DateStringValidator(Map JavaDoc paramMap)
100         throws ServiceException
101     {
102         this();
103         init(paramMap);
104     }
105
106     public DateStringValidator()
107     {
108         super();
109     }
110
111     public void init(Map JavaDoc paramMap)
112         throws ServiceException
113     {
114         super.init(paramMap);
115         dateFormats = new ArrayList JavaDoc(5);
116
117         Constraint constraint = (Constraint)paramMap.get("format");
118         if ( constraint != null )
119         {
120             dateFormats.add(constraint.getValue());
121             setDateFormatMessage(constraint.getMessage());
122         }
123
124         int i = 1;
125         constraint = (Constraint)paramMap.get("format" + i);
126         while ( constraint != null )
127         {
128             dateFormats.add(constraint.getValue());
129             setDateFormatMessage(constraint.getMessage());
130             constraint = (Constraint)paramMap.get("format" + (++i));
131         }
132
133         if ( dateFormatMessage == null || dateFormatMessage.equals("") )
134         {
135             dateFormatMessage = DEFAULT_DATE_MESSAGE;
136         }
137
138         constraint = (Constraint)paramMap.get("flexible");
139         if ( constraint != null )
140         {
141             flexible = Boolean.valueOf(constraint.getValue()).booleanValue();
142         }
143
144         if ( dateFormats.size() == 0 || flexible )
145         {
146             df = DateFormat.getInstance();
147             df.setLenient(true);
148         }
149         if (dateFormats.size() != 0)
150         {
151             sdf = new SimpleDateFormat JavaDoc();
152         }
153         
154     }
155
156
157     /**
158      * Determine whether a testValue meets the criteria specified
159      * in the constraints defined for this validator
160      *
161      * @param testValue a <code>String</code> to be tested
162      * @exception ValidationException containing an error message if the
163      * testValue did not pass the validation tests.
164      */

165     protected void doAssertValidity(String JavaDoc testValue)
166         throws ValidationException
167     {
168         try
169         {
170             parse(testValue);
171         }
172         catch (ParseException JavaDoc e)
173         {
174             message = dateFormatMessage;
175             throw new ValidationException(dateFormatMessage);
176         }
177     }
178
179     /**
180      * Parses the String s according to the rules/formats for this
181      * validator.
182      */

183     public Date JavaDoc parse(String JavaDoc s)
184         throws ParseException JavaDoc
185     {
186         Date JavaDoc date = null;
187
188         if ( s == null )
189         {
190             throw new ParseException JavaDoc("Input string was null", -1);
191         }
192
193         for ( int i=0; i<dateFormats.size(); i++)
194         {
195             sdf.applyPattern((String JavaDoc)dateFormats.get(i));
196             try
197             {
198                 date = sdf.parse(s);
199             }
200             catch (ParseException JavaDoc e)
201             {
202                 // ignore
203
}
204             if ( date != null )
205             {
206                 break;
207             }
208         }
209         
210         if ( date == null && df != null )
211         {
212             date = df.parse(s);
213         }
214
215         return date;
216     }
217
218     public String JavaDoc format(Date JavaDoc date)
219     {
220         String JavaDoc s = null;
221         if (date != null)
222         {
223             sdf.applyPattern((String JavaDoc)dateFormats.get(0));
224             s= sdf.format(date);
225         }
226         return s;
227     }
228
229
230     // ************************************************************
231
// ** Bean accessor methods **
232
// ************************************************************
233

234     /**
235      * Get the value of minLengthMessage.
236      * @return value of minLengthMessage.
237      */

238     public String JavaDoc getDateFormatMessage()
239     {
240         return dateFormatMessage;
241     }
242
243     /**
244      * Only sets the message if the new message has some information.
245      * So the last setMessage call with valid data wins. But later calls
246      * with null or empty string will not affect a previous valid setting.
247      *
248      * @param v Value to assign to minLengthMessage.
249      */

250     public void setDateFormatMessage(String JavaDoc v)
251     {
252         if ( v != null && !v.equals("") )
253         {
254             dateFormatMessage = v;
255         }
256     }
257
258     /**
259      * Get the value of dateFormats.
260      * @return value of dateFormats.
261      */

262     public List JavaDoc getDateFormats()
263     {
264         return dateFormats;
265     }
266     
267     /**
268      * Set the value of dateFormats.
269      * @param v Value to assign to dateFormats.
270      */

271     public void setDateFormats(List JavaDoc v)
272     {
273         this.dateFormats = v;
274     }
275     
276     /**
277      * Get the value of flexible.
278      * @return value of flexible.
279      */

280     public boolean isFlexible()
281     {
282         return flexible;
283     }
284     
285     /**
286      * Set the value of flexible.
287      * @param v Value to assign to flexible.
288      */

289     public void setFlexible(boolean v)
290     {
291         this.flexible = v;
292     }
293     
294 }
295
Popular Tags