KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > model > DateStringField


1 package org.apache.turbine.services.intake.model;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.text.DateFormat JavaDoc;
20 import java.text.ParseException JavaDoc;
21
22 import java.util.Date JavaDoc;
23
24 import org.apache.commons.lang.StringUtils;
25
26 import org.apache.turbine.services.intake.IntakeException;
27 import org.apache.turbine.services.intake.validator.DateStringValidator;
28 import org.apache.turbine.services.intake.xmlmodel.XmlField;
29 import org.apache.turbine.util.TurbineRuntimeException;
30
31 /**
32  * Field for date inputs as free form text. The parsing of date strings
33  * is dependent on any rules that are defined, so this field will expect that
34  * any validator will be (or extend) DateStringValidator.
35  *
36  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
37  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
38  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
39  * @version $Id: DateStringField.java,v 1.9.2.2 2004/05/20 03:16:39 seade Exp $
40  */

41 public class DateStringField
42         extends Field
43 {
44     /** date format */
45     private DateFormat JavaDoc df = null;
46
47     /**
48      * Constructor.
49      *
50      * @param field xml field definition object
51      * @param group xml group definition object
52      * @throws IntakeException thrown by superclass
53      */

54     public DateStringField(XmlField field, Group group)
55             throws IntakeException
56     {
57         super(field, group);
58
59         if (validator == null || !(validator instanceof DateStringValidator))
60         {
61             df = DateFormat.getInstance();
62             df.setLenient(true);
63         }
64     }
65
66     /**
67      * Sets the default value for a DateString field
68      *
69      * @param prop Parameter for the default values
70      */

71     public void setDefaultValue(String JavaDoc prop)
72     {
73         defaultValue = null;
74
75         if (prop == null)
76         {
77             return;
78         }
79
80         try
81         {
82             defaultValue = getDate(prop);
83         }
84         catch (ParseException JavaDoc e)
85         {
86             throw new TurbineRuntimeException("Could not parse " + prop
87                     + " into a valid Date for the default value", e);
88         }
89     }
90
91     /**
92      * Set the empty Value. This value is used if Intake
93      * maps a field to a parameter returned by the user and
94      * the corresponding field is either empty (empty string)
95      * or non-existant.
96      *
97      * @param prop The value to use if the field is empty.
98      */

99     public void setEmptyValue(String JavaDoc prop)
100     {
101         emptyValue = null;
102
103         if (prop == null)
104         {
105             return;
106         }
107
108         try
109         {
110             emptyValue = getDate(prop);
111         }
112         catch (ParseException JavaDoc e)
113         {
114             throw new TurbineRuntimeException("Could not parse " + prop
115                     + " into a valid Date for the empty value", e);
116         }
117     }
118
119     /**
120      * A suitable validator.
121      *
122      * @return "DateStringValidator"
123      */

124     protected String JavaDoc getDefaultValidator()
125     {
126         return DateStringValidator.class.getName();
127     }
128
129     /**
130      * Sets the value of the field from data in the parser.
131      */

132     protected void doSetValue()
133     {
134         if (isMultiValued)
135         {
136             String JavaDoc[] inputs = parser.getStrings(getKey());
137             Date JavaDoc[] values = new Date JavaDoc[inputs.length];
138             for (int i = 0; i < inputs.length; i++)
139             {
140                 try
141                 {
142                     values[i] = StringUtils.isNotEmpty(inputs[i])
143                             ? getDate(inputs[i]) : (Date JavaDoc) getEmptyValue();
144                 }
145                 catch (ParseException JavaDoc e)
146                 {
147                     values[i] = null;
148                 }
149             }
150             setTestValue(values);
151         }
152         else
153         {
154             String JavaDoc val = parser.getString(getKey());
155             try
156             {
157                 setTestValue(StringUtils.isNotEmpty(val) ? getDate(val) : (Date JavaDoc) getEmptyValue());
158             }
159             catch (ParseException JavaDoc e)
160             {
161                 setTestValue(null);
162             }
163         }
164     }
165
166     /**
167      * Parses a test date string using the Validator if is exists and
168      * is an instance of DateStringValidator. Otherwise, DateFormat.parse()
169      * is used.
170      *
171      * @param dateString The string date to parse
172      * @return A <code>Date</code> object
173      * @throws ParseException The date could not be parsed.
174      */

175     private Date JavaDoc getDate(String JavaDoc dateString)
176             throws ParseException JavaDoc
177     {
178         Date JavaDoc date = null;
179         // FIXME: Canonicalize user-entered date strings.
180
if (validator != null && validator instanceof DateStringValidator)
181         {
182             date = ((DateStringValidator) validator).parse(dateString);
183         }
184         else
185         {
186             date = df.parse(dateString);
187         }
188         return date;
189     }
190
191     /**
192      * returns a String representation
193      *
194      * @return a String representation
195      */

196     public String JavaDoc toString()
197     {
198         String JavaDoc s = null;
199         Object JavaDoc value = getValue();
200         if (value == null)
201         {
202             s = "";
203         }
204         else if (value instanceof String JavaDoc)
205         {
206             s = (String JavaDoc) value;
207         }
208         else if (validator != null && validator instanceof DateStringValidator)
209         {
210             s = ((DateStringValidator) validator).format((Date JavaDoc) value);
211         }
212         else
213         {
214             s = df.format((Date JavaDoc) value);
215         }
216         return s;
217     }
218 }
219
Popular Tags