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. package org.apache.commons.beanutils.locale.converters;
  17. import org.apache.commons.beanutils.locale.BaseLocaleConverter;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.apache.commons.logging.Log;
  20. import java.text.ParseException;
  21. import java.text.ParsePosition;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Locale;
  24. /**
  25. * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
  26. * implementation that converts an incoming
  27. * locale-sensitive String into a <code>java.util.Date</code> object,
  28. * optionally using a default value or throwing a
  29. * {@link org.apache.commons.beanutils.ConversionException}
  30. * if a conversion error occurs.</p>
  31. *
  32. * @author Yauheny Mikulski
  33. * @author Michael Szlapa
  34. */
  35. public class DateLocaleConverter extends BaseLocaleConverter {
  36. // ----------------------------------------------------- Instance Variables
  37. /** All logging goes through this logger */
  38. private static Log log = LogFactory.getLog(DateLocaleConverter.class);
  39. /** Should the date conversion be lenient? */
  40. boolean isLenient = false;
  41. // ----------------------------------------------------------- Constructors
  42. /**
  43. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  44. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  45. * if a conversion error occurs. The locale is the default locale for
  46. * this instance of the Java Virtual Machine and an unlocalized pattern is used
  47. * for the convertion.
  48. *
  49. */
  50. public DateLocaleConverter() {
  51. this(false);
  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.
  58. *
  59. * @param locPattern Indicate whether the pattern is localized or not
  60. */
  61. public DateLocaleConverter(boolean locPattern) {
  62. this(Locale.getDefault(), locPattern);
  63. }
  64. /**
  65. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  66. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  67. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  68. *
  69. * @param locale The locale
  70. */
  71. public DateLocaleConverter(Locale locale) {
  72. this(locale, false);
  73. }
  74. /**
  75. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  76. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  77. * if a conversion error occurs.
  78. *
  79. * @param locale The locale
  80. * @param locPattern Indicate whether the pattern is localized or not
  81. */
  82. public DateLocaleConverter(Locale locale, boolean locPattern) {
  83. this(locale, (String) null, locPattern);
  84. }
  85. /**
  86. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  87. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  88. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  89. *
  90. * @param locale The locale
  91. * @param pattern The convertion pattern
  92. */
  93. public DateLocaleConverter(Locale locale, String pattern) {
  94. this(locale, pattern, false);
  95. }
  96. /**
  97. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  98. * that will throw a {@link org.apache.commons.beanutils.ConversionException}
  99. * if a conversion error occurs.
  100. *
  101. * @param locale The locale
  102. * @param pattern The convertion pattern
  103. * @param locPattern Indicate whether the pattern is localized or not
  104. */
  105. public DateLocaleConverter(Locale locale, String pattern, boolean locPattern) {
  106. super(locale, pattern, locPattern);
  107. }
  108. /**
  109. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  110. * that will return the specified default value
  111. * if a conversion error occurs. The locale is the default locale for
  112. * this instance of the Java Virtual Machine and an unlocalized pattern is used
  113. * for the convertion.
  114. *
  115. * @param defaultValue The default value to be returned
  116. */
  117. public DateLocaleConverter(Object defaultValue) {
  118. this(defaultValue, false);
  119. }
  120. /**
  121. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  122. * that will return the specified default value
  123. * if a conversion error occurs. The locale is the default locale for
  124. * this instance of the Java Virtual Machine.
  125. *
  126. * @param defaultValue The default value to be returned
  127. * @param locPattern Indicate whether the pattern is localized or not
  128. */
  129. public DateLocaleConverter(Object defaultValue, boolean locPattern) {
  130. this(defaultValue, Locale.getDefault(), locPattern);
  131. }
  132. /**
  133. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  134. * that will return the specified default value
  135. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  136. *
  137. * @param defaultValue The default value to be returned
  138. * @param locale The locale
  139. */
  140. public DateLocaleConverter(Object defaultValue, Locale locale) {
  141. this(defaultValue, locale, false);
  142. }
  143. /**
  144. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  145. * that will return the specified default value
  146. * if a conversion error occurs.
  147. *
  148. * @param defaultValue The default value to be returned
  149. * @param locale The locale
  150. * @param locPattern Indicate whether the pattern is localized or not
  151. */
  152. public DateLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) {
  153. this(defaultValue, locale, null, locPattern);
  154. }
  155. /**
  156. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  157. * that will return the specified default value
  158. * if a conversion error occurs. An unlocalized pattern is used for the convertion.
  159. *
  160. * @param defaultValue The default value to be returned
  161. * @param locale The locale
  162. * @param pattern The convertion pattern
  163. */
  164. public DateLocaleConverter(Object defaultValue, Locale locale, String pattern) {
  165. this(defaultValue, locale, pattern, false);
  166. }
  167. /**
  168. * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
  169. * that will return the specified default value
  170. * if a conversion error occurs.
  171. *
  172. * @param defaultValue The default value to be returned
  173. * @param locale The locale
  174. * @param pattern The convertion pattern
  175. * @param locPattern Indicate whether the pattern is localized or not
  176. */
  177. public DateLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
  178. super(defaultValue, locale, pattern, locPattern);
  179. }
  180. // --------------------------------------------------------- Methods
  181. /**
  182. * Returns whether date formatting is lenient.
  183. *
  184. * @return true if the <code>DateFormat</code> used for formatting is lenient
  185. * @see java.text.DateFormat#isLenient
  186. */
  187. public boolean isLenient() {
  188. return isLenient;
  189. }
  190. /**
  191. * Specify whether or not date-time parsing should be lenient.
  192. *
  193. * @param lenient true if the <code>DateFormat</code> used for formatting should be lenient
  194. * @see java.text.DateFormat#setLenient
  195. */
  196. public void setLenient(boolean lenient) {
  197. isLenient = lenient;
  198. }
  199. // --------------------------------------------------------- Methods
  200. /**
  201. * Convert the specified locale-sensitive input object into an output object of the
  202. * specified type.
  203. *
  204. * @param value The input object to be converted
  205. * @param pattern The pattern is used for the convertion
  206. *
  207. * @exception org.apache.commons.beanutils.ConversionException if conversion cannot be performed
  208. * successfully
  209. */
  210. protected Object parse(Object value, String pattern) throws ParseException {
  211. SimpleDateFormat formatter = getFormatter(pattern, locale);
  212. if (locPattern) {
  213. formatter.applyLocalizedPattern(pattern);
  214. }
  215. else {
  216. formatter.applyPattern(pattern);
  217. }
  218. return formatter.parse((String) value);
  219. }
  220. /**
  221. * Gets an appropriate <code>SimpleDateFormat</code> for given locale,
  222. * default Date format pattern is not provided.
  223. */
  224. private SimpleDateFormat getFormatter(String pattern, Locale locale) {
  225. // This method is a fix for null pattern, which would cause
  226. // Null pointer exception when applied
  227. // Note: that many constructors default the pattern to null,
  228. // so it only makes sense to handle nulls gracefully
  229. if(pattern == null) {
  230. pattern = locPattern ?
  231. new SimpleDateFormat().toLocalizedPattern() : new SimpleDateFormat().toPattern();
  232. log.warn("Null pattern was provided, defaulting to: " + pattern);
  233. }
  234. SimpleDateFormat format = new SimpleDateFormat(pattern, locale);
  235. format.setLenient(isLenient);
  236. return format;
  237. }
  238. }