1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.beanutils2.locale.converters;
19
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 import org.apache.commons.beanutils2.ConversionException;
30 import org.apache.commons.beanutils2.locale.BaseLocaleConverter;
31 import org.apache.commons.beanutils2.locale.LocaleConverter;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 /**
36 * Standard {@link org.apache.commons.beanutils2.locale.LocaleConverter} implementation that converts an incoming locale-sensitive object into a {@link String}
37 * object, optionally using a default value or throwing a {@link org.apache.commons.beanutils2.ConversionException} if a conversion error occurs.
38 */
39 public class StringLocaleConverter extends BaseLocaleConverter<String> {
40
41 /**
42 * Builds instances of {@link StringLocaleConverter}.
43 */
44 public static class Builder extends BaseLocaleConverter.Builder<Builder, String> {
45
46 /**
47 * Constructs a new instance.
48 */
49 public Builder() {
50 // empty
51 }
52
53 /**
54 * Gets a new instance.
55 * <p>
56 * Defaults construct a {@link LocaleConverter} that will throw a {@link ConversionException} if a conversion error occurs. The locale is the default
57 * locale for this instance of the Java Virtual Machine and an unlocalized pattern is used for the conversion.
58 * </p>
59 *
60 * @return a new instance.
61 */
62 @Override
63 public StringLocaleConverter get() {
64 return new StringLocaleConverter(defaultValue, locale, pattern, useDefault || defaultValue != null, localizedPattern);
65 }
66
67 }
68
69 /** All logging goes through this logger */
70 private static final Log LOG = LogFactory.getLog(StringLocaleConverter.class);
71
72 /**
73 * Constructs a new builder.
74 *
75 * @return a new builder.
76 */
77 public static Builder builder() {
78 return new Builder();
79 }
80
81 private StringLocaleConverter(final String defaultValue, final Locale locale, final String pattern, final boolean useDefault, final boolean locPattern) {
82 super(defaultValue, locale, pattern, useDefault, locPattern);
83 }
84
85 /**
86 * Gets an instance of DecimalFormat.
87 *
88 * @param locale The locale
89 * @param pattern The pattern is used for the conversion
90 * @return The format for the locale and pattern
91 * @throws ConversionException if conversion cannot be performed successfully
92 * @throws IllegalArgumentException if an error occurs parsing a String to a Number
93 */
94 private DecimalFormat getDecimalFormat(final Locale locale, final String pattern) {
95 final DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale);
96
97 // if some constructors default pattern to null, it makes only sense to handle null pattern gracefully
98 if (pattern != null) {
99 if (localizedPattern) {
100 numberFormat.applyLocalizedPattern(pattern);
101 } else {
102 numberFormat.applyPattern(pattern);
103 }
104 } else {
105 LOG.debug("No pattern provided, using default.");
106 }
107
108 return numberFormat;
109 }
110
111 /**
112 * Parses the specified locale-sensitive input object into an output object of the specified type.
113 *
114 * @param value The input object to be converted
115 * @param pattern The pattern is used for the conversion
116 * @return The converted value
117 * @throws ConversionException if conversion cannot be performed successfully
118 * @throws ParseException if an error occurs
119 */
120 @Override
121 protected String parse(final Object value, final String pattern) throws ParseException {
122 String result = null;
123
124 if (value instanceof Integer || value instanceof Long || value instanceof BigInteger || value instanceof Byte || value instanceof Short) {
125 result = getDecimalFormat(locale, pattern).format(((Number) value).longValue());
126 } else if (value instanceof Double || value instanceof BigDecimal || value instanceof Float) {
127 result = getDecimalFormat(locale, pattern).format(((Number) value).doubleValue());
128 } else if (value instanceof Date) { // java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp
129 result = new SimpleDateFormat(pattern, locale).format(value);
130 } else {
131 result = value.toString();
132 }
133
134 return result;
135 }
136 }