1 package org.apache.turbine.services.intake.validator;
2
3
4 /* ====================================================================
5 * The Apache Software License, Version 1.1
6 *
7 * Copyright (c) 2001 The Apache Software Foundation. All rights
8 * reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * 3. The end-user documentation included with the redistribution,
23 * if any, must include the following acknowledgment:
24 * "This product includes software developed by the
25 * Apache Software Foundation (http://www.apache.org/)."
26 * Alternately, this acknowledgment may appear in the software itself,
27 * if and wherever such third-party acknowledgments normally appear.
28 *
29 * 4. The names "Apache" and "Apache Software Foundation" and
30 * "Apache Turbine" must not be used to endorse or promote products
31 * derived from this software without prior written permission. For
32 * written permission, please contact apache@apache.org.
33 *
34 * 5. Products derived from this software may not be called "Apache",
35 * "Apache Turbine", nor may "Apache" appear in their name, without
36 * prior written permission of the Apache Software Foundation.
37 *
38 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This software consists of voluntary contributions made by many
53 * individuals on behalf of the Apache Software Foundation. For more
54 * information on the Apache Software Foundation, please see
55 * <http://www.apache.org/>.
56 */
57 import java.util.Date;
58 import java.util.Map;
59 import java.util.List;
60 import java.util.ArrayList;
61 import java.text.DateFormat;
62 import java.text.SimpleDateFormat;
63 import java.text.ParseException;
64 import org.apache.turbine.util.TurbineException;
65
66
67 /***
68 * Validates numbers with the following constraints in addition to those
69 * listed in DefaultValidator.
70 *
71 * <table>
72 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
73 * <tr><td>format</td><td>see SimpleDateFormat javadoc</td>
74 * <td> </td></tr>
75 * <tr><td>formatx</td><td>see SimpleDateFormat javadoc</td>
76 * <td> </td></tr>
77 * <tr><td colspan=3>where x is >= 0 to specify multiple date
78 * formats. Only one format rule should have a message</td></tr>
79 * <tr><td>flexible</td><td>true, as long as DateFormat can parse the date,
80 * allow it, and false</td>
81 * <td>false</td></tr>
82 * </table>
83 *
84 * @author <a href="mailto:jmcnally@collab.net>John McNally</a>
85 * @version $Id: DateStringValidator.java,v 1.1 2002/07/11 13:21:40 mpoeschl Exp $
86 */
87 public class DateStringValidator
88 extends DefaultValidator
89 {
90 private static final String DEFAULT_DATE_MESSAGE = "Date could not be parsed";
91 private List dateFormats;
92 private String dateFormatMessage;
93 private boolean flexible;
94 private DateFormat df;
95 private SimpleDateFormat sdf;
96
97 public DateStringValidator(Map paramMap)
98 throws TurbineException
99 {
100 this();
101 init(paramMap);
102 }
103
104 public DateStringValidator()
105 {
106 super();
107 }
108
109 public void init(Map paramMap)
110 throws TurbineException
111 {
112 super.init(paramMap);
113 dateFormats = new ArrayList(5);
114
115 Constraint constraint = (Constraint)paramMap.get("format");
116
117 if (constraint != null)
118 {
119 dateFormats.add(constraint.getValue());
120 setDateFormatMessage(constraint.getMessage());
121 }
122
123 int i = 1;
124 constraint = (Constraint)paramMap.get("format" + i);
125
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
140 if (constraint != null)
141 {
142 flexible = Boolean.valueOf(constraint.getValue()).booleanValue();
143 }
144
145 if ((dateFormats.size() == 0) || (flexible))
146 {
147 df = DateFormat.getInstance();
148 df.setLenient(true);
149 }
150
151 if (dateFormats.size() != 0)
152 {
153 sdf = new SimpleDateFormat();
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 testValue)
166 throws ValidationException
167 {
168 try
169 {
170 parse(testValue);
171 }
172 catch (ParseException 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 parse(String s)
184 throws ParseException
185 {
186 Date date = null;
187
188 if (s == null)
189 {
190 throw new ParseException("Input string was null", -1);
191 }
192
193 for (int i = 0; i < dateFormats.size(); i++)
194 {
195 sdf.applyPattern((String)dateFormats.get(i));
196
197 try
198 {
199 date = sdf.parse(s);
200 }
201 catch (ParseException e)
202 {
203 // ignore
204 }
205
206 if (date != null)
207 {
208 break;
209 }
210 }
211
212 if ((date == null) && (df != null))
213 {
214 date = df.parse(s);
215 }
216
217 return date;
218 }
219
220 // ************************************************************
221 // ** Bean accessor methods **
222 // ************************************************************
223
224 /***
225 * Get the value of minLengthMessage.
226 * @return value of minLengthMessage.
227 */
228 public String getDateFormatMessage()
229 {
230 return dateFormatMessage;
231 }
232
233 /***
234 * Only sets the message if the new message has some information.
235 * So the last setMessage call with valid data wins. But later calls
236 * with null or empty string will not affect a previous valid setting.
237 *
238 * @param v Value to assign to minLengthMessage.
239 */
240 public void setDateFormatMessage(String v)
241 {
242 if ((v != null) && (!v.equals("")))
243 {
244 dateFormatMessage = v;
245 }
246 }
247
248 /***
249 * Get the value of dateFormats.
250 * @return value of dateFormats.
251 */
252 public List getDateFormats()
253 {
254 return dateFormats;
255 }
256
257 /***
258 * Set the value of dateFormats.
259 * @param v Value to assign to dateFormats.
260 */
261 public void setDateFormats(List v)
262 {
263 this.dateFormats = v;
264 }
265
266 /***
267 * Get the value of flexible.
268 * @return value of flexible.
269 */
270 public boolean isFlexible()
271 {
272 return flexible;
273 }
274
275 /***
276 * Set the value of flexible.
277 * @param v Value to assign to flexible.
278 */
279 public void setFlexible(boolean v)
280 {
281 this.flexible = v;
282 }
283 }
This page was automatically generated by Maven