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;
  17. import org.apache.commons.beanutils.ConversionException;
  18. import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
  19. import org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import java.text.ParseException;
  22. import java.util.Locale;
  23. /**
  24. * <p>The base class for all standart type locale-sensitive converters.
  25. * It has {@link LocaleConverter} and {@link org.apache.commons.beanutils.Converter} implementations,
  26. * that convert an incoming locale-sensitive Object into an object of correspond type,
  27. * optionally using a default value or throwing a {@link ConversionException}
  28. * if a conversion error occurs.</p>
  29. *
  30. * @author Yauheny Mikulski
  31. */
  32. public abstract class BaseLocaleConverter implements LocaleConverter {
  33. // ----------------------------------------------------- Instance Variables
  34. /** All logging goes through this logger */
  35. private static Log log = LogFactory.getLog(BaseLocaleConverter.class);
  36. /** The default value specified to our Constructor, if any. */
  37. private Object defaultValue = null;
  38. /** Should we return the default value on conversion errors? */
  39. protected boolean useDefault = false;
  40. /** The locale specified to our Constructor, by default - system locale. */
  41. protected Locale locale = Locale.getDefault();
  42. /** The default pattern specified to our Constructor, if any. */
  43. protected String pattern = null;
  44. /** The flag indicating whether the given pattern string is localized or not. */
  45. protected boolean locPattern = false;
  46. // ----------------------------------------------------------- Constructors
  47. /**
  48. * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
  49. * if a conversion error occurs.
  50. * An unlocalized pattern is used for the convertion.
  51. *
  52. * @param locale The locale
  53. * @param pattern The convertion pattern
  54. */
  55. protected BaseLocaleConverter(Locale locale, String pattern) {
  56. this(null, locale, pattern, false, false);
  57. }
  58. /**
  59. * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
  60. * if a conversion error occurs.
  61. *
  62. * @param locale The locale
  63. * @param pattern The convertion pattern
  64. * @param locPattern Indicate whether the pattern is localized or not
  65. */
  66. protected BaseLocaleConverter(Locale locale, String pattern, boolean locPattern) {
  67. this(null, locale, pattern, false, locPattern);
  68. }
  69. /**
  70. * Create a {@link LocaleConverter} that will return the specified default value
  71. * if a conversion error occurs.
  72. * An unlocalized pattern is used for the convertion.
  73. *
  74. * @param defaultValue The default value to be returned
  75. * @param locale The locale
  76. * @param pattern The convertion pattern
  77. */
  78. protected BaseLocaleConverter(Object defaultValue, Locale locale, String pattern) {
  79. this(defaultValue, locale, pattern, false);
  80. }
  81. /**
  82. * Create a {@link LocaleConverter} that will return the specified default value
  83. * if a conversion error occurs.
  84. *
  85. * @param defaultValue The default value to be returned
  86. * @param locale The locale
  87. * @param pattern The convertion pattern
  88. * @param locPattern Indicate whether the pattern is localized or not
  89. */
  90. protected BaseLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
  91. this(defaultValue, locale, pattern, true, locPattern);
  92. }
  93. /**
  94. * Create a {@link LocaleConverter} that will return the specified default value
  95. * or throw a {@link ConversionException} if a conversion error occurs.
  96. *
  97. * @param defaultValue The default value to be returned
  98. * @param locale The locale
  99. * @param pattern The convertion pattern
  100. * @param useDefault Indicate whether the default value is used or not
  101. * @param locPattern Indicate whether the pattern is localized or not
  102. */
  103. private BaseLocaleConverter(Object defaultValue, Locale locale,
  104. String pattern, boolean useDefault, boolean locPattern) {
  105. if (useDefault) {
  106. this.defaultValue = defaultValue;
  107. this.useDefault = true;
  108. }
  109. if (locale != null) {
  110. this.locale = locale;
  111. }
  112. this.pattern = pattern;
  113. this.locPattern = locPattern;
  114. }
  115. // --------------------------------------------------------- Methods
  116. /**
  117. * Convert the specified locale-sensitive input object into an output object of the
  118. * specified type.
  119. *
  120. * @param value The input object to be converted
  121. * @param pattern The pattern is used for the convertion
  122. *
  123. * @exception ConversionException if conversion cannot be performed
  124. * successfully
  125. */
  126. abstract protected Object parse(Object value, String pattern) throws ParseException;
  127. /**
  128. * Convert the specified locale-sensitive input object into an output object.
  129. * The default pattern is used for the convertion.
  130. *
  131. * @param value The input object to be converted
  132. *
  133. * @exception ConversionException if conversion cannot be performed
  134. * successfully
  135. */
  136. public Object convert(Object value) {
  137. return convert(value, null);
  138. }
  139. /**
  140. * Convert the specified locale-sensitive input object into an output object.
  141. *
  142. * @param value The input object to be converted
  143. * @param pattern The pattern is used for the convertion
  144. *
  145. * @exception ConversionException if conversion cannot be performed
  146. * successfully
  147. */
  148. public Object convert(Object value, String pattern) {
  149. return convert(null, value, pattern);
  150. }
  151. /**
  152. * Convert the specified locale-sensitive input object into an output object of the
  153. * specified type. The default pattern is used for the convertion.
  154. *
  155. * @param type Data type to which this value should be converted
  156. * @param value The input object to be converted
  157. *
  158. * @exception ConversionException if conversion cannot be performed
  159. * successfully
  160. */
  161. public Object convert(Class type, Object value) {
  162. return convert(type, value, null);
  163. }
  164. /**
  165. * Convert the specified locale-sensitive input object into an output object of the
  166. * specified type.
  167. *
  168. * @param type Data type to which this value should be converted
  169. * @param value The input object to be converted
  170. * @param pattern The pattern is used for the convertion
  171. *
  172. * @exception ConversionException if conversion cannot be performed
  173. * successfully
  174. */
  175. public Object convert(Class type, Object value, String pattern) {
  176. if (value == null) {
  177. if (useDefault) {
  178. return (defaultValue);
  179. } else {
  180. // symmetric beanutils function allows null
  181. // so do not: throw new ConversionException("No value specified");
  182. log.debug("Null value specified for conversion, returing null");
  183. return null;
  184. }
  185. }
  186. try {
  187. if (pattern != null) {
  188. return parse(value, pattern);
  189. } else {
  190. return parse(value, this.pattern);
  191. }
  192. } catch (Exception e) {
  193. if (useDefault) {
  194. return (defaultValue);
  195. } else {
  196. throw new ConversionException(e);
  197. }
  198. }
  199. }
  200. }