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