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