View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.beanutils.locale.converters;
18  
19  import org.apache.commons.beanutils.locale.BaseLocaleConverter;
20  import org.apache.commons.logging.LogFactory;
21  import org.apache.commons.logging.Log;
22  
23  import java.text.ParseException;
24  import java.text.ParsePosition;
25  import java.text.SimpleDateFormat;
26  import java.util.Locale;
27  
28  
29  /**
30   * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter} 
31   * implementation that converts an incoming
32   * locale-sensitive String into a <code>java.util.Date</code> object,
33   * optionally using a default value or throwing a 
34   * {@link org.apache.commons.beanutils.ConversionException}
35   * if a conversion error occurs.</p>
36   *
37   * @author Yauheny Mikulski
38   * @author Michael Szlapa
39   */
40  
41  public class DateLocaleConverter extends BaseLocaleConverter {
42  
43      // ----------------------------------------------------- Instance Variables
44  
45      /** All logging goes through this logger */
46      private static Log log = LogFactory.getLog(DateLocaleConverter.class);
47  
48      /** Should the date conversion be lenient? */
49      boolean isLenient = false;
50  
51      // ----------------------------------------------------------- Constructors
52  
53      /**
54       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
55       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
56       * if a conversion error occurs. The locale is the default locale for
57       * this instance of the Java Virtual Machine and an unlocalized pattern is used
58       * for the convertion.
59       *
60       */
61      public DateLocaleConverter() {
62  
63          this(false);
64      }
65  
66      /**
67       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
68       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
69       * if a conversion error occurs. The locale is the default locale for
70       * this instance of the Java Virtual Machine.
71       *
72       * @param locPattern    Indicate whether the pattern is localized or not
73       */
74      public DateLocaleConverter(boolean locPattern) {
75  
76          this(Locale.getDefault(), locPattern);
77      }
78  
79      /**
80       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
81       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
82       * if a conversion error occurs. An unlocalized pattern is used for the convertion.
83       *
84       * @param locale        The locale
85       */
86      public DateLocaleConverter(Locale locale) {
87  
88          this(locale, false);
89      }
90  
91      /**
92       * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
93       * that will throw a {@link org.apache.commons.beanutils.ConversionException}
94       * if a conversion error occurs.
95       *
96       * @param locale        The locale
97       * @param locPattern    Indicate whether the pattern is localized or not
98       */
99      public DateLocaleConverter(Locale locale, boolean locPattern) {
100 
101         this(locale, (String) null, locPattern);
102     }
103 
104     /**
105      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
106      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
107      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
108      *
109      * @param locale        The locale
110      * @param pattern       The convertion pattern
111      */
112     public DateLocaleConverter(Locale locale, String pattern) {
113 
114         this(locale, pattern, false);
115     }
116 
117     /**
118      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
119      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
120      * if a conversion error occurs.
121      *
122      * @param locale        The locale
123      * @param pattern       The convertion pattern
124      * @param locPattern    Indicate whether the pattern is localized or not
125      */
126     public DateLocaleConverter(Locale locale, String pattern, boolean locPattern) {
127 
128         super(locale, pattern, locPattern);
129     }
130 
131     /**
132      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
133      * that will return the specified default value
134      * if a conversion error occurs. The locale is the default locale for
135      * this instance of the Java Virtual Machine and an unlocalized pattern is used
136      * for the convertion.
137      *
138      * @param defaultValue  The default value to be returned
139      */
140     public DateLocaleConverter(Object defaultValue) {
141 
142         this(defaultValue, false);
143     }
144 
145     /**
146      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
147      * that will return the specified default value
148      * if a conversion error occurs. The locale is the default locale for
149      * this instance of the Java Virtual Machine.
150      *
151      * @param defaultValue  The default value to be returned
152      * @param locPattern    Indicate whether the pattern is localized or not
153      */
154     public DateLocaleConverter(Object defaultValue, boolean locPattern) {
155 
156         this(defaultValue, Locale.getDefault(), locPattern);
157     }
158 
159     /**
160      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
161      * that will return the specified default value
162      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
163      *
164      * @param defaultValue  The default value to be returned
165      * @param locale        The locale
166      */
167     public DateLocaleConverter(Object defaultValue, Locale locale) {
168 
169         this(defaultValue, locale, false);
170     }
171 
172     /**
173      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
174      * that will return the specified default value
175      * if a conversion error occurs.
176      *
177      * @param defaultValue  The default value to be returned
178      * @param locale        The locale
179      * @param locPattern    Indicate whether the pattern is localized or not
180      */
181     public DateLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) {
182 
183         this(defaultValue, locale, null, locPattern);
184     }
185 
186 
187     /**
188      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
189      * that will return the specified default value
190      * if a conversion error occurs. An unlocalized pattern is used for the convertion.
191      *
192      * @param defaultValue  The default value to be returned
193      * @param locale        The locale
194      * @param pattern       The convertion pattern
195      */
196     public DateLocaleConverter(Object defaultValue, Locale locale, String pattern) {
197 
198         this(defaultValue, locale, pattern, false);
199     }
200 
201     /**
202      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter} 
203      * that will return the specified default value
204      * if a conversion error occurs.
205      *
206      * @param defaultValue  The default value to be returned
207      * @param locale        The locale
208      * @param pattern       The convertion pattern
209      * @param locPattern    Indicate whether the pattern is localized or not
210      */
211     public DateLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
212 
213         super(defaultValue, locale, pattern, locPattern);
214     }
215 
216     // --------------------------------------------------------- Methods
217     
218     /**
219      * Returns whether date formatting is lenient.
220      *
221      * @return true if the <code>DateFormat</code> used for formatting is lenient
222      * @see java.text.DateFormat#isLenient
223      */
224     public boolean isLenient() {
225         return isLenient;
226     }
227     
228     /**
229      * Specify whether or not date-time parsing should be lenient.
230      * 
231      * @param lenient true if the <code>DateFormat</code> used for formatting should be lenient
232      * @see java.text.DateFormat#setLenient
233      */
234     public void setLenient(boolean lenient) {
235         isLenient = lenient;
236     }	
237 
238     // --------------------------------------------------------- Methods
239 
240     /**
241      * Convert the specified locale-sensitive input object into an output object of the
242      * specified type.
243      *
244      * @param value The input object to be converted
245      * @param pattern The pattern is used for the convertion
246      *
247      * @exception org.apache.commons.beanutils.ConversionException if conversion cannot be performed
248      *  successfully
249      */
250     protected Object parse(Object value, String pattern) throws ParseException {
251         SimpleDateFormat formatter = getFormatter(pattern, locale);
252         if (locPattern) {
253             formatter.applyLocalizedPattern(pattern);
254         }
255         else {
256             formatter.applyPattern(pattern);
257         }
258         return formatter.parse((String) value);
259     }
260 
261     /**
262      * Gets an appropriate <code>SimpleDateFormat</code> for given locale, 
263      * default Date format pattern is not provided.
264      */
265     private SimpleDateFormat getFormatter(String pattern, Locale locale) {
266         // This method is a fix for null pattern, which would cause 
267         // Null pointer exception when applied
268         // Note: that many constructors default the pattern to null, 
269         // so it only makes sense to handle nulls gracefully
270         if(pattern == null) {
271             pattern = locPattern ? 
272                 new SimpleDateFormat().toLocalizedPattern() : new SimpleDateFormat().toPattern();
273             log.warn("Null pattern was provided, defaulting to: " + pattern);
274         }
275         SimpleDateFormat format = new SimpleDateFormat(pattern, locale);
276         format.setLenient(isLenient);
277         return format;
278     }
279 }