View Javadoc

1   /*
2    * $Id: DateLocaleConverter.java,v 1.2 2004/04/09 18:44:53 eelco12 Exp $
3    * $Revision: 1.2 $
4    * $Date: 2004/04/09 18:44:53 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package nl.openedge.baritus.converters;
33  
34  import java.text.DateFormat;
35  import java.text.ParseException;
36  import java.text.SimpleDateFormat;
37  import java.util.Date;
38  import java.util.Locale;
39  
40  /***
41   * <p>Standard {@link LocaleConverter} 
42   * implementation that converts an incoming
43   * locale-sensitive String into a <code>java.util.Date</code> object,
44   * optionally using a default value or throwing a 
45   * {@link ConversionException}
46   * if a conversion error occurs.</p>
47   *
48   * @author Yauheny Mikulski
49   * @author Michael Szlapa
50   */
51  
52  public class DateLocaleConverter extends BaseLocaleConverter
53  {
54  	
55  	private boolean lenient = false;
56  	private int dateStyle = DateFormat.SHORT;
57  
58  	// ----------------------------------------------------------- Constructors
59  
60  	/***
61  	 * Create a {@link LocaleConverter} 
62  	 * that will throw a {@link ConversionException}
63  	 * if a conversion error occurs. The locale is the default locale for
64  	 * this instance of the Java Virtual Machine and an unlocalized pattern is used
65  	 * for the convertion.
66  	 *
67  	 */
68  	public DateLocaleConverter()
69  	{
70  
71  		this(Locale.getDefault());
72  	}
73  
74  	/***
75  	 * Create a {@link LocaleConverter} 
76  	 * that will throw a {@link ConversionException}
77  	 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
78  	 *
79  	 * @param locale        The locale
80  	 */
81  	public DateLocaleConverter(Locale locale)
82  	{
83  
84  		this(locale, null);
85  	}
86  
87  	/***
88  	 * Create a {@link LocaleConverter} 
89  	 * that will throw a {@link ConversionException}
90  	 * if a conversion error occurs. An unlocalized pattern is used for the convertion.
91  	 *
92  	 * @param locale        The locale
93  	 * @param pattern       The convertion pattern
94  	 */
95  	public DateLocaleConverter(Locale locale, String pattern)
96  	{
97  
98  		super(locale, pattern, false);
99  	}
100 
101 	// --------------------------------------------------------- Methods
102 
103 	/***
104 	 * Returns whether date formatting is lenient.
105 	 *
106 	 * @return true if the <code>DateFormat</code> used for formatting is lenient
107 	 * @see java.text.DateFormat#isLenient
108 	 */
109 	public boolean isLenient()
110 	{
111 		return lenient;
112 	}
113 
114 	/***
115 	 * Specify whether or not date-time parsing should be lenient.
116 	 * 
117 	 * @param lenient true if the <code>DateFormat</code> used for formatting should be lenient
118 	 * @see java.text.DateFormat#setLenient
119 	 */
120 	public void setLenient(boolean lenient)
121 	{
122 		this.lenient = lenient;
123 	}
124 	
125 	/***
126 	 * get date style
127 	 * @return int date style as a constant from DateFormat
128 	 */
129 	public int getDateStyle()
130 	{
131 		return dateStyle;
132 	}
133 
134 	/***
135 	 * set date style
136 	 * @param dateStyle
137 	 */
138 	public void setDateStyle(int dateStyle)
139 	{
140 		this.dateStyle = dateStyle;
141 	}
142 
143 	// --------------------------------------------------------- Methods
144 
145 	/***
146 	 * Convert the specified locale-sensitive input object into an output object of the
147 	 * specified type.
148 	 *
149 	 * @param value The input object to be converted
150 	 * @param pattern The pattern is used for the convertion
151 	 *
152 	 * @exception ConversionException if conversion cannot be performed
153 	 *  successfully
154 	 */
155 	protected Object parse(Object value, String pattern) throws ConversionException
156 	{
157 		DateFormat formatter = getFormat(pattern, locale);
158 
159 		try
160 		{
161 			return formatter.parse((String) value);
162 		}
163 		catch (ParseException e)
164 		{
165 			String dpat = null;
166 			if(pattern != null)
167 			{
168 				dpat = pattern;
169 			}
170 			else if(formatter instanceof SimpleDateFormat)
171 			{
172 				dpat = ((SimpleDateFormat)formatter).toLocalizedPattern();
173 			}
174 			
175 			throw new ConversionException(e, dpat);
176 		}
177 	}
178 
179 	/***
180 	 * format value with pattern or using the default pattern
181 	 * @see nl.openedge.baritus.converters.Formatter#format(java.lang.Object, java.lang.String)
182 	 */
183 	public String format(Object value, String pattern) throws IllegalArgumentException
184 	{
185 		DateFormat format = getFormat(pattern, locale);
186 		Date date = null;
187 		if(value instanceof Date)
188 		{
189 			date = (Date)value;
190 		}
191 		else
192 		{
193 			date = (Date)convert(Date.class, value);
194 		}
195 		return format.format(date);
196 	}
197 
198 	/***
199 	 * Get date format
200 	 */
201 	private DateFormat getFormat(String pattern, Locale locale)
202 	{
203 		DateFormat format = null;
204 		if (pattern == null)
205 		{
206 			format = DateFormat.getDateInstance(dateStyle, locale);
207 		}
208 		else
209 		{
210 			SimpleDateFormat _format = new SimpleDateFormat(pattern, locale);
211 			_format.setLenient(lenient);
212 			if (locPattern)
213 			{
214 				_format.applyLocalizedPattern(pattern);
215 			}
216 			else
217 			{
218 				_format.applyPattern(pattern);
219 			}
220 			format = _format;
221 		}	
222 		return format;
223 	}
224 	
225 	/***
226 	 * Convert the specified locale-sensitive input object into an output object of the
227 	 * specified type.
228 	 *
229 	 * @param type Data type to which this value should be converted
230 	 * @param value The input object to be converted
231 	 * @param pattern The pattern is used for the convertion
232 	 *
233 	 * @exception ConversionException if conversion cannot be performed
234 	 *  successfully
235 	 */
236 	public Object convert(Class type, Object value, String pattern)
237 	{
238 		if (value == null)
239 		{
240 			return null;
241 		}
242 		
243 		return parse(value, pattern);
244 	}
245 
246 }