001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils.locale;
019:
020: import org.apache.commons.beanutils.locale.converters.BigDecimalLocaleConverter;
021: import org.apache.commons.beanutils.locale.converters.BigIntegerLocaleConverter;
022: import org.apache.commons.beanutils.locale.converters.ByteLocaleConverter;
023: import org.apache.commons.beanutils.locale.converters.DoubleLocaleConverter;
024: import org.apache.commons.beanutils.locale.converters.FloatLocaleConverter;
025: import org.apache.commons.beanutils.locale.converters.IntegerLocaleConverter;
026: import org.apache.commons.beanutils.locale.converters.LongLocaleConverter;
027: import org.apache.commons.beanutils.locale.converters.ShortLocaleConverter;
028: import org.apache.commons.beanutils.locale.converters.StringLocaleConverter;
029: import org.apache.commons.beanutils.locale.converters.SqlDateLocaleConverter;
030: import org.apache.commons.beanutils.locale.converters.SqlTimeLocaleConverter;
031: import org.apache.commons.beanutils.locale.converters.SqlTimestampLocaleConverter;
032:
033: import org.apache.commons.collections.FastHashMap;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import java.lang.reflect.Array;
038: import java.math.BigDecimal;
039: import java.math.BigInteger;
040: import java.util.Locale;
041:
042: /**
043: * <p>Utility methods for converting locale-sensitive String scalar values to objects of the
044: * specified Class, String arrays to arrays of the specified Class and
045: * object to locale-sensitive String scalar value.</p>
046: *
047: * <p>This class provides the implementations used by the static utility methods in
048: * {@link LocaleConvertUtils}.</p>
049: *
050: * <p>The actual {@link LocaleConverter} instance to be used
051: * can be registered for each possible destination Class. Unless you override them, standard
052: * {@link LocaleConverter} instances are provided for all of the following
053: * destination Classes:</p>
054: * <ul>
055: * <li>java.lang.BigDecimal</li>
056: * <li>java.lang.BigInteger</li>
057: * <li>byte and java.lang.Byte</li>
058: * <li>double and java.lang.Double</li>
059: * <li>float and java.lang.Float</li>
060: * <li>int and java.lang.Integer</li>
061: * <li>long and java.lang.Long</li>
062: * <li>short and java.lang.Short</li>
063: * <li>java.lang.String</li>
064: * <li>java.sql.Date</li>
065: * <li>java.sql.Time</li>
066: * <li>java.sql.Timestamp</li>
067: * </ul>
068: *
069: * <p>For backwards compatibility, the standard locale converters
070: * for primitive types (and the corresponding wrapper classes).
071: *
072: * If you prefer to have another {@link LocaleConverter}
073: * thrown instead, replace the standard {@link LocaleConverter} instances
074: * with ones created with the one of the appropriate constructors.
075: *
076: * It's important that {@link LocaleConverter} should be registered for
077: * the specified locale and Class (or primitive type).
078: *
079: * @author Yauheny Mikulski
080: * @since 1.7
081: */
082: public class LocaleConvertUtilsBean {
083:
084: /**
085: * Gets singleton instance.
086: * This is the same as the instance used by the default {@link LocaleBeanUtilsBean} singleton.
087: * @return the singleton instance
088: */
089: public static LocaleConvertUtilsBean getInstance() {
090: return LocaleBeanUtilsBean.getLocaleBeanUtilsInstance()
091: .getLocaleConvertUtils();
092: }
093:
094: // ----------------------------------------------------- Instance Variables
095:
096: /** The locale - default for convertion. */
097: private Locale defaultLocale = Locale.getDefault();
098:
099: /** Indicate whether the pattern is localized or not */
100: private boolean applyLocalized = false;
101:
102: /** The <code>Log</code> instance for this class. */
103: private Log log = LogFactory.getLog(LocaleConvertUtils.class);
104:
105: /** Every entry of the mapConverters is:
106: * key = locale
107: * value = FastHashMap of converters for the certain locale.
108: */
109: private FastHashMap mapConverters = new FastHashMap();
110:
111: // --------------------------------------------------------- Constructors
112:
113: /**
114: * Makes the state by default (deregisters all converters for all locales)
115: * and then registers default locale converters.
116: */
117: public LocaleConvertUtilsBean() {
118: deregister();
119: }
120:
121: // --------------------------------------------------------- Properties
122:
123: /**
124: * getter for defaultLocale.
125: * @return the default locale
126: */
127: public Locale getDefaultLocale() {
128:
129: return defaultLocale;
130: }
131:
132: /**
133: * setter for defaultLocale.
134: * @param locale the default locale
135: */
136: public void setDefaultLocale(Locale locale) {
137:
138: if (locale == null) {
139: defaultLocale = Locale.getDefault();
140: } else {
141: defaultLocale = locale;
142: }
143: }
144:
145: /**
146: * getter for applyLocalized
147: *
148: * @return <code>true</code> if pattern is localized,
149: * otherwise <code>false</code>
150: */
151: public boolean getApplyLocalized() {
152: return applyLocalized;
153: }
154:
155: /**
156: * setter for applyLocalized
157: *
158: * @param newApplyLocalized <code>true</code> if pattern is localized,
159: * otherwise <code>false</code>
160: */
161: public void setApplyLocalized(boolean newApplyLocalized) {
162: applyLocalized = newApplyLocalized;
163: }
164:
165: // --------------------------------------------------------- Methods
166:
167: /**
168: * Convert the specified locale-sensitive value into a String.
169: *
170: * @param value The Value to be converted
171: * @return the converted value
172: *
173: * @throws org.apache.commons.beanutils.ConversionException if thrown by an
174: * underlying Converter
175: */
176: public String convert(Object value) {
177: return convert(value, defaultLocale, null);
178: }
179:
180: /**
181: * Convert the specified locale-sensitive value into a String
182: * using the conversion pattern.
183: *
184: * @param value The Value to be converted
185: * @param pattern The convertion pattern
186: * @return the converted value
187: *
188: * @throws org.apache.commons.beanutils.ConversionException if thrown by an
189: * underlying Converter
190: */
191: public String convert(Object value, String pattern) {
192: return convert(value, defaultLocale, pattern);
193: }
194:
195: /**
196: * Convert the specified locale-sensitive value into a String
197: * using the paticular convertion pattern.
198: *
199: * @param value The Value to be converted
200: * @param locale The locale
201: * @param pattern The convertion pattern
202: * @return the converted value
203: *
204: * @throws org.apache.commons.beanutils.ConversionException if thrown by an
205: * underlying Converter
206: */
207: public String convert(Object value, Locale locale, String pattern) {
208:
209: LocaleConverter converter = lookup(String.class, locale);
210:
211: return (String) converter.convert(String.class, value, pattern);
212: }
213:
214: /**
215: * Convert the specified value to an object of the specified class (if
216: * possible). Otherwise, return a String representation of the value.
217: *
218: * @param value The String scalar value to be converted
219: * @param clazz The Data type to which this value should be converted.
220: * @return the converted value
221: *
222: * @throws org.apache.commons.beanutils.ConversionException if thrown by an
223: * underlying Converter
224: */
225: public Object convert(String value, Class clazz) {
226:
227: return convert(value, clazz, defaultLocale, null);
228: }
229:
230: /**
231: * Convert the specified value to an object of the specified class (if
232: * possible) using the convertion pattern. Otherwise, return a String
233: * representation of the value.
234: *
235: * @param value The String scalar value to be converted
236: * @param clazz The Data type to which this value should be converted.
237: * @param pattern The convertion pattern
238: * @return the converted value
239: *
240: * @throws org.apache.commons.beanutils.ConversionException if thrown by an
241: * underlying Converter
242: */
243: public Object convert(String value, Class clazz, String pattern) {
244:
245: return convert(value, clazz, defaultLocale, pattern);
246: }
247:
248: /**
249: * Convert the specified value to an object of the specified class (if
250: * possible) using the convertion pattern. Otherwise, return a String
251: * representation of the value.
252: *
253: * @param value The String scalar value to be converted
254: * @param clazz The Data type to which this value should be converted.
255: * @param locale The locale
256: * @param pattern The convertion pattern
257: * @return the converted value
258: *
259: * @throws org.apache.commons.beanutils.ConversionException if thrown by an
260: * underlying Converter
261: */
262: public Object convert(String value, Class clazz, Locale locale,
263: String pattern) {
264:
265: if (log.isDebugEnabled()) {
266: log.debug("Convert string " + value + " to class "
267: + clazz.getName() + " using " + locale.toString()
268: + " locale and " + pattern + " pattern");
269: }
270:
271: LocaleConverter converter = lookup(clazz, locale);
272:
273: if (converter == null) {
274: converter = lookup(String.class, locale);
275: }
276: if (log.isTraceEnabled()) {
277: log.trace(" Using converter " + converter);
278: }
279:
280: return (converter.convert(clazz, value, pattern));
281: }
282:
283: /**
284: * Convert an array of specified values to an array of objects of the
285: * specified class (if possible) using the convertion pattern.
286: *
287: * @param values Value to be converted (may be null)
288: * @param clazz Java array or element class to be converted to
289: * @param pattern The convertion pattern
290: * @return the converted value
291: *
292: * @throws org.apache.commons.beanutils.ConversionException if thrown by an
293: * underlying Converter
294: */
295: public Object convert(String[] values, Class clazz, String pattern) {
296:
297: return convert(values, clazz, getDefaultLocale(), pattern);
298: }
299:
300: /**
301: * Convert an array of specified values to an array of objects of the
302: * specified class (if possible) .
303: *
304: * @param values Value to be converted (may be null)
305: * @param clazz Java array or element class to be converted to
306: * @return the converted value
307: *
308: * @throws org.apache.commons.beanutils.ConversionException if thrown by an
309: * underlying Converter
310: */
311: public Object convert(String[] values, Class clazz) {
312:
313: return convert(values, clazz, getDefaultLocale(), null);
314: }
315:
316: /**
317: * Convert an array of specified values to an array of objects of the
318: * specified class (if possible) using the convertion pattern.
319: *
320: * @param values Value to be converted (may be null)
321: * @param clazz Java array or element class to be converted to
322: * @param locale The locale
323: * @param pattern The convertion pattern
324: * @return the converted value
325: *
326: * @throws org.apache.commons.beanutils.ConversionException if thrown by an
327: * underlying Converter
328: */
329: public Object convert(String[] values, Class clazz, Locale locale,
330: String pattern) {
331:
332: Class type = clazz;
333: if (clazz.isArray()) {
334: type = clazz.getComponentType();
335: }
336: if (log.isDebugEnabled()) {
337: log.debug("Convert String[" + values.length + "] to class "
338: + type.getName() + "[] using " + locale.toString()
339: + " locale and " + pattern + " pattern");
340: }
341:
342: Object array = Array.newInstance(type, values.length);
343: for (int i = 0; i < values.length; i++) {
344: Array.set(array, i, convert(values[i], type, locale,
345: pattern));
346: }
347:
348: return (array);
349: }
350:
351: /**
352: * Register a custom {@link LocaleConverter} for the specified destination
353: * <code>Class</code>, replacing any previously registered converter.
354: *
355: * @param converter The LocaleConverter to be registered
356: * @param clazz The Destination class for conversions performed by this
357: * Converter
358: * @param locale The locale
359: */
360: public void register(LocaleConverter converter, Class clazz,
361: Locale locale) {
362:
363: lookup(locale).put(clazz, converter);
364: }
365:
366: /**
367: * Remove any registered {@link LocaleConverter}.
368: */
369: public void deregister() {
370:
371: FastHashMap defaultConverter = lookup(defaultLocale);
372:
373: mapConverters.setFast(false);
374:
375: mapConverters.clear();
376: mapConverters.put(defaultLocale, defaultConverter);
377:
378: mapConverters.setFast(true);
379: }
380:
381: /**
382: * Remove any registered {@link LocaleConverter} for the specified locale
383: *
384: * @param locale The locale
385: */
386: public void deregister(Locale locale) {
387:
388: mapConverters.remove(locale);
389: }
390:
391: /**
392: * Remove any registered {@link LocaleConverter} for the specified locale and Class.
393: *
394: * @param clazz Class for which to remove a registered Converter
395: * @param locale The locale
396: */
397: public void deregister(Class clazz, Locale locale) {
398:
399: lookup(locale).remove(clazz);
400: }
401:
402: /**
403: * Look up and return any registered {@link LocaleConverter} for the specified
404: * destination class and locale; if there is no registered Converter, return
405: * <code>null</code>.
406: *
407: * @param clazz Class for which to return a registered Converter
408: * @param locale The Locale
409: * @return The registered locale Converter, if any
410: */
411: public LocaleConverter lookup(Class clazz, Locale locale) {
412:
413: LocaleConverter converter = (LocaleConverter) lookup(locale)
414: .get(clazz);
415:
416: if (log.isTraceEnabled()) {
417: log.trace("LocaleConverter:" + converter);
418: }
419:
420: return converter;
421: }
422:
423: /**
424: * Look up and return any registered FastHashMap instance for the specified locale;
425: * if there is no registered one, return <code>null</code>.
426: *
427: * @param locale The Locale
428: * @return The FastHashMap instance contains the all {@link LocaleConverter} types for
429: * the specified locale.
430: * @deprecated This method will be modified to return a Map in the next release.
431: */
432: protected FastHashMap lookup(Locale locale) {
433: FastHashMap localeConverters;
434:
435: if (locale == null) {
436: localeConverters = (FastHashMap) mapConverters
437: .get(defaultLocale);
438: } else {
439: localeConverters = (FastHashMap) mapConverters.get(locale);
440:
441: if (localeConverters == null) {
442: localeConverters = create(locale);
443: mapConverters.put(locale, localeConverters);
444: }
445: }
446:
447: return localeConverters;
448: }
449:
450: /**
451: * Create all {@link LocaleConverter} types for specified locale.
452: *
453: * @param locale The Locale
454: * @return The FastHashMap instance contains the all {@link LocaleConverter} types
455: * for the specified locale.
456: * @deprecated This method will be modified to return a Map in the next release.
457: */
458: protected FastHashMap create(Locale locale) {
459:
460: FastHashMap converter = new FastHashMap();
461: converter.setFast(false);
462:
463: converter.put(BigDecimal.class, new BigDecimalLocaleConverter(
464: locale, applyLocalized));
465: converter.put(BigInteger.class, new BigIntegerLocaleConverter(
466: locale, applyLocalized));
467:
468: converter.put(Byte.class, new ByteLocaleConverter(locale,
469: applyLocalized));
470: converter.put(Byte.TYPE, new ByteLocaleConverter(locale,
471: applyLocalized));
472:
473: converter.put(Double.class, new DoubleLocaleConverter(locale,
474: applyLocalized));
475: converter.put(Double.TYPE, new DoubleLocaleConverter(locale,
476: applyLocalized));
477:
478: converter.put(Float.class, new FloatLocaleConverter(locale,
479: applyLocalized));
480: converter.put(Float.TYPE, new FloatLocaleConverter(locale,
481: applyLocalized));
482:
483: converter.put(Integer.class, new IntegerLocaleConverter(locale,
484: applyLocalized));
485: converter.put(Integer.TYPE, new IntegerLocaleConverter(locale,
486: applyLocalized));
487:
488: converter.put(Long.class, new LongLocaleConverter(locale,
489: applyLocalized));
490: converter.put(Long.TYPE, new LongLocaleConverter(locale,
491: applyLocalized));
492:
493: converter.put(Short.class, new ShortLocaleConverter(locale,
494: applyLocalized));
495: converter.put(Short.TYPE, new ShortLocaleConverter(locale,
496: applyLocalized));
497:
498: converter.put(String.class, new StringLocaleConverter(locale,
499: applyLocalized));
500:
501: // conversion format patterns of java.sql.* types should correspond to default
502: // behaviour of toString and valueOf methods of these classes
503: converter.put(java.sql.Date.class, new SqlDateLocaleConverter(
504: locale, "yyyy-MM-dd"));
505: converter.put(java.sql.Time.class, new SqlTimeLocaleConverter(
506: locale, "HH:mm:ss"));
507: converter.put(java.sql.Timestamp.class,
508: new SqlTimestampLocaleConverter(locale,
509: "yyyy-MM-dd HH:mm:ss.S"));
510:
511: converter.setFast(true);
512:
513: return converter;
514: }
515: }
|