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.locale.converters.*;
  18. import org.apache.commons.beanutils.Converter;
  19. import org.apache.commons.collections.FastHashMap;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import java.lang.reflect.Array;
  23. import java.math.BigDecimal;
  24. import java.math.BigInteger;
  25. import java.sql.Date;
  26. import java.sql.Time;
  27. import java.sql.Timestamp;
  28. import java.util.Locale;
  29. /**
  30. * <p>Utility methods for converting locale-sensitive String scalar values to objects of the
  31. * specified Class, String arrays to arrays of the specified Class and
  32. * object to locale-sensitive String scalar value.</p>
  33. *
  34. * <p>This class provides the implementations used by the static utility methods in
  35. * {@link LocaleConvertUtils}.</p>
  36. *
  37. * <p>The actual {@link LocaleConverter} instance to be used
  38. * can be registered for each possible destination Class. Unless you override them, standard
  39. * {@link LocaleConverter} instances are provided for all of the following
  40. * destination Classes:</p>
  41. * <ul>
  42. * <li>java.lang.BigDecimal</li>
  43. * <li>java.lang.BigInteger</li>
  44. * <li>byte and java.lang.Byte</li>
  45. * <li>double and java.lang.Double</li>
  46. * <li>float and java.lang.Float</li>
  47. * <li>int and java.lang.Integer</li>
  48. * <li>long and java.lang.Long</li>
  49. * <li>short and java.lang.Short</li>
  50. * <li>java.lang.String</li>
  51. * <li>java.sql.Date</li>
  52. * <li>java.sql.Time</li>
  53. * <li>java.sql.Timestamp</li>
  54. * </ul>
  55. *
  56. * <p>For backwards compatibility, the standard locale converters
  57. * for primitive types (and the corresponding wrapper classes).
  58. *
  59. * If you prefer to have another {@link LocaleConverter}
  60. * thrown instead, replace the standard {@link LocaleConverter} instances
  61. * with ones created with the one of the appropriate constructors.
  62. *
  63. * It's important that {@link LocaleConverter} should be registered for
  64. * the specified locale and Class (or primitive type).
  65. *
  66. * @author Yauheny Mikulski
  67. * @since 1.7
  68. */
  69. public class LocaleConvertUtilsBean {
  70. /**
  71. * Gets singleton instance.
  72. * This is the same as the instance used by the default {@link LocaleBeanUtilsBean} singleton.
  73. */
  74. public static LocaleConvertUtilsBean getInstance() {
  75. return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance().getLocaleConvertUtils();
  76. }
  77. // ----------------------------------------------------- Instance Variables
  78. /** The locale - default for convertion. */
  79. private Locale defaultLocale = Locale.getDefault();
  80. /** Indicate whether the pattern is localized or not */
  81. private boolean applyLocalized = false;
  82. /** The <code>Log</code> instance for this class. */
  83. private Log log = LogFactory.getLog(LocaleConvertUtils.class);
  84. /** Every entry of the mapConverters is:
  85. * key = locale
  86. * value = FastHashMap of converters for the certain locale.
  87. */
  88. private FastHashMap mapConverters = new FastHashMap();
  89. // --------------------------------------------------------- Constructors
  90. /**
  91. * Makes the state by default (deregisters all converters for all locales)
  92. * and then registers default locale converters.
  93. */
  94. public LocaleConvertUtilsBean() {
  95. deregister();
  96. }
  97. // --------------------------------------------------------- Properties
  98. /**
  99. * getter for defaultLocale
  100. */
  101. public Locale getDefaultLocale() {
  102. return defaultLocale;
  103. }
  104. /**
  105. * setter for defaultLocale
  106. */
  107. public void setDefaultLocale(Locale locale) {
  108. if (locale == null) {
  109. defaultLocale = Locale.getDefault();
  110. }
  111. else {
  112. defaultLocale = locale;
  113. }
  114. }
  115. /**
  116. * getter for applyLocalized
  117. */
  118. public boolean getApplyLocalized() {
  119. return applyLocalized;
  120. }
  121. /**
  122. * setter for applyLocalized
  123. */
  124. public void setApplyLocalized(boolean newApplyLocalized) {
  125. applyLocalized = newApplyLocalized;
  126. }
  127. // --------------------------------------------------------- Methods
  128. /**
  129. * Convert the specified locale-sensitive value into a String.
  130. *
  131. * @param value The Value to be converted
  132. *
  133. * @exception org.apache.commons.beanutils.ConversionException if thrown by an underlying Converter
  134. */
  135. public String convert(Object value) {
  136. return convert(value, defaultLocale, null);
  137. }
  138. /**
  139. * Convert the specified locale-sensitive value into a String
  140. * using the convertion pattern.
  141. *
  142. * @param value The Value to be converted
  143. * @param pattern The convertion pattern
  144. *
  145. * @exception ConversionException if thrown by an underlying Converter
  146. */
  147. public String convert(Object value, String pattern) {
  148. return convert(value, defaultLocale, pattern);
  149. }
  150. /**
  151. * Convert the specified locale-sensitive value into a String
  152. * using the paticular convertion pattern.
  153. *
  154. * @param value The Value to be converted
  155. * @param locale The locale
  156. * @param pattern The convertion pattern
  157. *
  158. * @exception ConversionException if thrown by an underlying Converter
  159. */
  160. public String convert(Object value, Locale locale, String pattern) {
  161. LocaleConverter converter = lookup(String.class, locale);
  162. return (String) converter.convert(String.class, value, pattern);
  163. }
  164. /**
  165. * Convert the specified value to an object of the specified class (if
  166. * possible). Otherwise, return a String representation of the value.
  167. *
  168. * @param value The String scalar value to be converted
  169. * @param clazz The Data type to which this value should be converted.
  170. *
  171. * @exception ConversionException if thrown by an underlying Converter
  172. */
  173. public Object convert(String value, Class clazz) {
  174. return convert(value, clazz, defaultLocale, null);
  175. }
  176. /**
  177. * Convert the specified value to an object of the specified class (if
  178. * possible) using the convertion pattern. Otherwise, return a String
  179. * representation of the value.
  180. *
  181. * @param value The String scalar value to be converted
  182. * @param clazz The Data type to which this value should be converted.
  183. * @param pattern The convertion pattern
  184. *
  185. * @exception ConversionException if thrown by an underlying Converter
  186. */
  187. public Object convert(String value, Class clazz, String pattern) {
  188. return convert(value, clazz, defaultLocale, pattern);
  189. }
  190. /**
  191. * Convert the specified value to an object of the specified class (if
  192. * possible) using the convertion pattern. Otherwise, return a String
  193. * representation of the value.
  194. *
  195. * @param value The String scalar value to be converted
  196. * @param clazz The Data type to which this value should be converted.
  197. * @param locale The locale
  198. * @param pattern The convertion pattern
  199. *
  200. * @exception ConversionException if thrown by an underlying Converter
  201. */
  202. public Object convert(String value, Class clazz, Locale locale, String pattern) {
  203. if (log.isDebugEnabled()) {
  204. log.debug("Convert string " + value + " to class " +
  205. clazz.getName() + " using " + locale.toString() +
  206. " locale and " + pattern + " pattern");
  207. }
  208. LocaleConverter converter = lookup(clazz, locale);
  209. if (converter == null) {
  210. converter = (LocaleConverter) lookup(String.class, locale);
  211. }
  212. if (log.isTraceEnabled()) {
  213. log.trace(" Using converter " + converter);
  214. }
  215. return (converter.convert(clazz, value, pattern));
  216. }
  217. /**
  218. * Convert an array of specified values to an array of objects of the
  219. * specified class (if possible) using the convertion pattern.
  220. *
  221. * @param values Value to be converted (may be null)
  222. * @param clazz Java array or element class to be converted to
  223. * @param pattern The convertion pattern
  224. *
  225. * @exception ConversionException if thrown by an underlying Converter
  226. */
  227. public Object convert(String values[], Class clazz, String pattern) {
  228. return convert(values, clazz, getDefaultLocale(), pattern);
  229. }
  230. /**
  231. * Convert an array of specified values to an array of objects of the
  232. * specified class (if possible) .
  233. *
  234. * @param values Value to be converted (may be null)
  235. * @param clazz Java array or element class to be converted to
  236. *
  237. * @exception ConversionException if thrown by an underlying Converter
  238. */
  239. public Object convert(String values[], Class clazz) {
  240. return convert(values, clazz, getDefaultLocale(), null);
  241. }
  242. /**
  243. * Convert an array of specified values to an array of objects of the
  244. * specified class (if possible) using the convertion pattern.
  245. *
  246. * @param values Value to be converted (may be null)
  247. * @param clazz Java array or element class to be converted to
  248. * @param locale The locale
  249. * @param pattern The convertion pattern
  250. *
  251. * @exception ConversionException if thrown by an underlying Converter
  252. */
  253. public Object convert(String values[], Class clazz, Locale locale, String pattern) {
  254. Class type = clazz;
  255. if (clazz.isArray()) {
  256. type = clazz.getComponentType();
  257. }
  258. if (log.isDebugEnabled()) {
  259. log.debug("Convert String[" + values.length + "] to class " +
  260. type.getName() + "[] using " + locale.toString() +
  261. " locale and " + pattern + " pattern");
  262. }
  263. Object array = Array.newInstance(type, values.length);
  264. for (int i = 0; i < values.length; i++) {
  265. Array.set(array, i, convert(values[i], type, locale, pattern));
  266. }
  267. return (array);
  268. }
  269. /**
  270. * Register a custom {@link LocaleConverter} for the specified destination
  271. * <code>Class</code>, replacing any previously registered converter.
  272. *
  273. * @param converter The LocaleConverter to be registered
  274. * @param clazz The Destination class for conversions performed by this
  275. * Converter
  276. * @param locale The locale
  277. */
  278. public void register(LocaleConverter converter, Class clazz, Locale locale) {
  279. lookup(locale).put(clazz, converter);
  280. }
  281. /**
  282. * Remove any registered {@link LocaleConverter}.
  283. */
  284. public void deregister() {
  285. FastHashMap defaultConverter = lookup(defaultLocale);
  286. mapConverters.setFast(false);
  287. mapConverters.clear();
  288. mapConverters.put(defaultLocale, defaultConverter);
  289. mapConverters.setFast(true);
  290. }
  291. /**
  292. * Remove any registered {@link LocaleConverter} for the specified locale
  293. *
  294. * @param locale The locale
  295. */
  296. public void deregister(Locale locale) {
  297. mapConverters.remove(locale);
  298. }
  299. /**
  300. * Remove any registered {@link LocaleConverter} for the specified locale and Class.
  301. *
  302. * @param clazz Class for which to remove a registered Converter
  303. * @param locale The locale
  304. */
  305. public void deregister(Class clazz, Locale locale) {
  306. lookup(locale).remove(clazz);
  307. }
  308. /**
  309. * Look up and return any registered {@link LocaleConverter} for the specified
  310. * destination class and locale; if there is no registered Converter, return
  311. * <code>null</code>.
  312. *
  313. * @param clazz Class for which to return a registered Converter
  314. * @param locale The Locale
  315. */
  316. public LocaleConverter lookup(Class clazz, Locale locale) {
  317. LocaleConverter converter = (LocaleConverter) lookup(locale).get(clazz);
  318. if (log.isTraceEnabled()) {
  319. log.trace("LocaleConverter:" + converter);
  320. }
  321. return converter;
  322. }
  323. /**
  324. * Look up and return any registered FastHashMap instance for the specified locale;
  325. * if there is no registered one, return <code>null</code>.
  326. *
  327. * @param locale The Locale
  328. * @return The FastHashMap instance contains the all {@link LocaleConverter} types for
  329. * the specified locale.
  330. */
  331. protected FastHashMap lookup(Locale locale) {
  332. FastHashMap localeConverters;
  333. if (locale == null) {
  334. localeConverters = (FastHashMap) mapConverters.get(defaultLocale);
  335. }
  336. else {
  337. localeConverters = (FastHashMap) mapConverters.get(locale);
  338. if (localeConverters == null) {
  339. localeConverters = create(locale);
  340. mapConverters.put(locale, localeConverters);
  341. }
  342. }
  343. return localeConverters;
  344. }
  345. /**
  346. * Create all {@link LocaleConverter} types for specified locale.
  347. *
  348. * @param locale The Locale
  349. * @return The FastHashMap instance contains the all {@link LocaleConverter} types
  350. * for the specified locale.
  351. */
  352. protected FastHashMap create(Locale locale) {
  353. FastHashMap converter = new FastHashMap();
  354. converter.setFast(false);
  355. converter.put(BigDecimal.class, new BigDecimalLocaleConverter(locale, applyLocalized));
  356. converter.put(BigInteger.class, new BigIntegerLocaleConverter(locale, applyLocalized));
  357. converter.put(Byte.class, new ByteLocaleConverter(locale, applyLocalized));
  358. converter.put(Byte.TYPE, new ByteLocaleConverter(locale, applyLocalized));
  359. converter.put(Double.class, new DoubleLocaleConverter(locale, applyLocalized));
  360. converter.put(Double.TYPE, new DoubleLocaleConverter(locale, applyLocalized));
  361. converter.put(Float.class, new FloatLocaleConverter(locale, applyLocalized));
  362. converter.put(Float.TYPE, new FloatLocaleConverter(locale, applyLocalized));
  363. converter.put(Integer.class, new IntegerLocaleConverter(locale, applyLocalized));
  364. converter.put(Integer.TYPE, new IntegerLocaleConverter(locale, applyLocalized));
  365. converter.put(Long.class, new LongLocaleConverter(locale, applyLocalized));
  366. converter.put(Long.TYPE, new LongLocaleConverter(locale, applyLocalized));
  367. converter.put(Short.class, new ShortLocaleConverter(locale, applyLocalized));
  368. converter.put(Short.TYPE, new ShortLocaleConverter(locale, applyLocalized));
  369. converter.put(String.class, new StringLocaleConverter(locale, applyLocalized));
  370. // conversion format patterns of java.sql.* types should correspond to default
  371. // behaviour of toString and valueOf methods of these classes
  372. converter.put(java.sql.Date.class, new SqlDateLocaleConverter(locale, "yyyy-MM-dd"));
  373. converter.put(java.sql.Time.class, new SqlTimeLocaleConverter(locale, "HH:mm:ss"));
  374. converter.put( java.sql.Timestamp.class,
  375. new SqlTimestampLocaleConverter(locale, "yyyy-MM-dd HH:mm:ss.S")
  376. );
  377. converter.setFast(true);
  378. return converter;
  379. }
  380. }