1 16 17 package org.apache.commons.beanutils.locale.converters; 18 19 import org.apache.commons.beanutils.locale.BaseLocaleConverter; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import java.math.BigDecimal ; 24 import java.math.BigInteger ; 25 import java.text.DecimalFormat ; 26 import java.text.NumberFormat ; 27 import java.text.ParseException ; 28 import java.text.SimpleDateFormat ; 29 import java.util.Date ; 30 import java.util.Locale ; 31 32 33 43 44 public class StringLocaleConverter extends BaseLocaleConverter { 45 46 48 49 private static Log log = LogFactory.getLog(StringLocaleConverter.class); 51 52 54 62 public StringLocaleConverter() { 63 64 this(false); 65 } 66 67 75 public StringLocaleConverter(boolean locPattern) { 76 77 this(Locale.getDefault(), locPattern); 78 } 79 80 87 public StringLocaleConverter(Locale locale) { 88 89 this(locale, false); 90 } 91 92 100 public StringLocaleConverter(Locale locale, boolean locPattern) { 101 102 this(locale, (String ) null, locPattern); 103 } 104 105 113 public StringLocaleConverter(Locale locale, String pattern) { 114 115 this(locale, pattern, false); 116 } 117 118 127 public StringLocaleConverter(Locale locale, String pattern, boolean locPattern) { 128 129 super(locale, pattern, locPattern); 130 } 131 132 141 public StringLocaleConverter(Object defaultValue) { 142 143 this(defaultValue, false); 144 } 145 146 155 public StringLocaleConverter(Object defaultValue, boolean locPattern) { 156 157 this(defaultValue, Locale.getDefault(), locPattern); 158 } 159 160 168 public StringLocaleConverter(Object defaultValue, Locale locale) { 169 170 this(defaultValue, locale, false); 171 } 172 173 182 public StringLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) { 183 184 this(defaultValue, locale, null, locPattern); 185 } 186 187 196 public StringLocaleConverter(Object defaultValue, Locale locale, String pattern) { 197 198 this(defaultValue, locale, pattern, false); 199 } 200 201 211 public StringLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) { 212 213 super(defaultValue, locale, pattern, locPattern); 214 } 215 216 218 228 protected Object parse(Object value, String pattern) throws ParseException { 229 230 String result = null; 231 232 if ((value instanceof Integer ) || 233 (value instanceof Long ) || 234 (value instanceof BigInteger ) || 235 (value instanceof Byte ) || 236 (value instanceof Short )) { 237 238 result = getDecimalFormat(locale, pattern).format(((Number ) value).longValue()); 239 } 240 else if ((value instanceof Double ) || 241 (value instanceof BigDecimal ) || 242 (value instanceof Float )) { 243 244 result = getDecimalFormat(locale, pattern).format(((Number ) value).doubleValue()); 245 } 246 else if (value instanceof Date ) { 248 SimpleDateFormat dateFormat = 249 new SimpleDateFormat (pattern, locale); 250 251 result = dateFormat.format(value); 252 } 253 else { 254 result = value.toString(); 255 } 256 257 return result; 258 } 259 260 269 private DecimalFormat getDecimalFormat(Locale locale, String pattern) { 270 271 DecimalFormat numberFormat = (DecimalFormat ) NumberFormat.getInstance(locale); 272 273 if (pattern != null) { 275 if (locPattern) { 276 numberFormat.applyLocalizedPattern(pattern); 277 } else { 278 numberFormat.applyPattern(pattern); 279 } 280 } else { 281 log.warn("No pattern provided, using default."); 282 } 283 284 return numberFormat; 285 } 286 } 287 | Popular Tags |