View Javadoc

1   package org.apache.turbine.services.intake.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.text.DateFormat;
23  import java.text.ParseException;
24  
25  import java.util.Date;
26  
27  import org.apache.commons.lang.StringUtils;
28  
29  import org.apache.turbine.services.intake.IntakeException;
30  import org.apache.turbine.services.intake.validator.DateStringValidator;
31  import org.apache.turbine.services.intake.xmlmodel.XmlField;
32  import org.apache.turbine.util.TurbineRuntimeException;
33  
34  /***
35   * Field for date inputs as free form text.  The parsing of date strings
36   * is dependent on any rules that are defined, so this field will expect that
37   * any validator will be (or extend) DateStringValidator.
38   *
39   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
40   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
41   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
42   * @version $Id: DateStringField.java 534527 2007-05-02 16:10:59Z tv $
43   */
44  public class DateStringField
45          extends Field
46  {
47      /*** date format. Fallback if no validator is defined */
48      private static DateFormat df;
49  
50      static
51      {
52          df = DateFormat.getInstance();
53          df.setLenient(true);
54      }
55  
56      /***
57       * Constructor.
58       *
59       * @param field xml field definition object
60       * @param group xml group definition object
61       * @throws IntakeException thrown by superclass
62       */
63      public DateStringField(XmlField field, Group group)
64              throws IntakeException
65      {
66          super(field, group);
67      }
68  
69      /***
70       * Sets the default value for a DateString field
71       *
72       * @param prop Parameter for the default values
73       */
74      public void setDefaultValue(String prop)
75      {
76          defaultValue = null;
77  
78          if (prop == null)
79          {
80              return;
81          }
82  
83          try
84          {
85              defaultValue = getDate(prop);
86          }
87          catch (ParseException e)
88          {
89              throw new TurbineRuntimeException("Could not parse " + prop
90                      + " into a valid Date for the default value", e);
91          }
92      }
93  
94      /***
95       * Set the empty Value. This value is used if Intake
96       * maps a field to a parameter returned by the user and
97       * the corresponding field is either empty (empty string)
98       * or non-existant.
99       *
100      * @param prop The value to use if the field is empty.
101      */
102     public void setEmptyValue(String prop)
103     {
104         emptyValue = null;
105 
106         if (prop == null)
107         {
108             return;
109         }
110 
111         try
112         {
113             emptyValue = getDate(prop);
114         }
115         catch (ParseException e)
116         {
117             throw new TurbineRuntimeException("Could not parse " + prop
118                     + " into a valid Date for the empty value", e);
119         }
120     }
121 
122     /***
123      * A suitable validator.
124      *
125      * @return "DateStringValidator"
126      */
127     protected String getDefaultValidator()
128     {
129         return DateStringValidator.class.getName();
130     }
131 
132     /***
133      * Sets the value of the field from data in the parser.
134      */
135     protected void doSetValue()
136     {
137         if (isMultiValued)
138         {
139             String[] inputs = parser.getStrings(getKey());
140             Date[] values = new Date[inputs.length];
141             for (int i = 0; i < inputs.length; i++)
142             {
143                 try
144                 {
145                     values[i] = StringUtils.isNotEmpty(inputs[i])
146                             ? getDate(inputs[i]) : (Date) getEmptyValue();
147                 }
148                 catch (ParseException e)
149                 {
150                     values[i] = null;
151                 }
152             }
153             setTestValue(values);
154         }
155         else
156         {
157             String val = parser.getString(getKey());
158             try
159             {
160                 setTestValue(StringUtils.isNotEmpty(val) ? getDate(val) : (Date) getEmptyValue());
161             }
162             catch (ParseException e)
163             {
164                 setTestValue(null);
165             }
166         }
167     }
168 
169     /***
170      * Parses a test date string using the Validator if is exists and
171      * is an instance of DateStringValidator.  Otherwise, DateFormat.parse()
172      * is used.
173      *
174      * @param dateString The string date to parse
175      * @return A <code>Date</code> object
176      * @throws ParseException The date could not be parsed.
177      */
178     private Date getDate(String dateString)
179             throws ParseException
180     {
181         Date date = null;
182         // FIXME: Canonicalize user-entered date strings.
183         if (validator != null && validator instanceof DateStringValidator)
184         {
185             date = ((DateStringValidator) validator).parse(dateString);
186         }
187         else
188         {
189             date = df.parse(dateString);
190         }
191         return date;
192     }
193 
194     /***
195      * returns a String representation
196      *
197      * @return a String representation
198      */
199     public String toString()
200     {
201         String s = null;
202         Object value = getValue();
203         if (value == null)
204         {
205             s = "";
206         }
207         else if (value instanceof String)
208         {
209             s = (String) value;
210         }
211         else if (validator != null && validator instanceof DateStringValidator)
212         {
213             s = ((DateStringValidator) validator).format((Date) value);
214         }
215         else
216         {
217             s = df.format((Date) value);
218         }
219         return s;
220     }
221 }