| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.beanutils.converters; |
| 18 | |
|
| 19 | |
import java.util.Calendar; |
| 20 | |
import java.util.Date; |
| 21 | |
import java.util.Locale; |
| 22 | |
import java.math.BigDecimal; |
| 23 | |
import java.math.BigInteger; |
| 24 | |
import java.text.NumberFormat; |
| 25 | |
import java.text.DecimalFormat; |
| 26 | |
import java.text.DecimalFormatSymbols; |
| 27 | |
import java.text.ParsePosition; |
| 28 | |
|
| 29 | |
import org.apache.commons.beanutils.ConversionException; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public abstract class NumberConverter extends AbstractConverter { |
| 86 | |
|
| 87 | 1 | private static final Integer ZERO = new Integer(0); |
| 88 | 1 | private static final Integer ONE = new Integer(1); |
| 89 | |
|
| 90 | |
private String pattern; |
| 91 | |
private boolean allowDecimals; |
| 92 | |
private boolean useLocaleFormat; |
| 93 | |
private Locale locale; |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public NumberConverter(boolean allowDecimals) { |
| 104 | 6021 | super(); |
| 105 | 6021 | this.allowDecimals = allowDecimals; |
| 106 | 6021 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public NumberConverter(boolean allowDecimals, Object defaultValue) { |
| 116 | 5001 | super(); |
| 117 | 5001 | this.allowDecimals = allowDecimals; |
| 118 | 5001 | setDefaultValue(defaultValue); |
| 119 | 5001 | } |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public boolean isAllowDecimals() { |
| 129 | 0 | return allowDecimals; |
| 130 | |
} |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
public void setUseLocaleFormat(boolean useLocaleFormat) { |
| 140 | 74 | this.useLocaleFormat = useLocaleFormat; |
| 141 | 74 | } |
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
public String getPattern() { |
| 154 | 0 | return pattern; |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public void setPattern(String pattern) { |
| 167 | 17 | this.pattern = pattern; |
| 168 | 17 | setUseLocaleFormat(true); |
| 169 | 17 | } |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
public Locale getLocale() { |
| 178 | 0 | return locale; |
| 179 | |
} |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public void setLocale(Locale locale) { |
| 187 | 33 | this.locale = locale; |
| 188 | 33 | setUseLocaleFormat(true); |
| 189 | 33 | } |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
protected String convertToString(Object value) throws Throwable { |
| 201 | |
|
| 202 | 123 | String result = null; |
| 203 | 123 | if (useLocaleFormat && value instanceof Number) { |
| 204 | 78 | NumberFormat format = getFormat(); |
| 205 | 78 | format.setGroupingUsed(false); |
| 206 | 78 | result = format.format(value); |
| 207 | 78 | if (log().isDebugEnabled()) { |
| 208 | 0 | log().debug(" Converted to String using format '" + result + "'"); |
| 209 | |
} |
| 210 | |
|
| 211 | 78 | } else { |
| 212 | 45 | result = value.toString(); |
| 213 | 45 | if (log().isDebugEnabled()) { |
| 214 | 0 | log().debug(" Converted to String using toString() '" + result + "'"); |
| 215 | |
} |
| 216 | |
} |
| 217 | 123 | return result; |
| 218 | |
|
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
protected Object convertToType(Class targetType, Object value) throws Throwable { |
| 231 | |
|
| 232 | 4461 | Class sourceType = value.getClass(); |
| 233 | |
|
| 234 | 4461 | if (value instanceof Number) { |
| 235 | 3939 | return toNumber(sourceType, targetType, (Number)value); |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | 522 | if (value instanceof Boolean) { |
| 240 | 16 | return toNumber(sourceType, targetType, ((Boolean)value).booleanValue() ? ONE : ZERO); |
| 241 | |
} |
| 242 | |
|
| 243 | |
|
| 244 | 506 | if (value instanceof Date && Long.class.equals(targetType)) { |
| 245 | 8 | return new Long(((Date)value).getTime()); |
| 246 | |
} |
| 247 | |
|
| 248 | |
|
| 249 | 498 | if (value instanceof Calendar && Long.class.equals(targetType)) { |
| 250 | 8 | return new Long(((Calendar)value).getTime().getTime()); |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | 490 | String stringValue = value.toString().trim(); |
| 255 | 490 | if (stringValue.length() == 0) { |
| 256 | 2 | return handleMissing(targetType); |
| 257 | |
} |
| 258 | |
|
| 259 | |
|
| 260 | 488 | Number number = null; |
| 261 | 488 | if (useLocaleFormat) { |
| 262 | 116 | NumberFormat format = getFormat(); |
| 263 | 116 | number = parse(sourceType, targetType, stringValue, format); |
| 264 | 84 | } else { |
| 265 | 372 | if (log().isDebugEnabled()) { |
| 266 | 0 | log().debug(" No NumberFormat, using default conversion"); |
| 267 | |
} |
| 268 | 372 | number = toNumber(sourceType, targetType, stringValue); |
| 269 | |
} |
| 270 | |
|
| 271 | |
|
| 272 | 389 | return toNumber(sourceType, targetType, number); |
| 273 | |
|
| 274 | |
} |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
private Number toNumber(Class sourceType, Class targetType, Number value) { |
| 298 | |
|
| 299 | |
|
| 300 | 4344 | if (targetType.equals(value.getClass())) { |
| 301 | 315 | return value; |
| 302 | |
} |
| 303 | |
|
| 304 | |
|
| 305 | 4029 | if (targetType.equals(Byte.class)) { |
| 306 | 769 | long longValue = value.longValue(); |
| 307 | 769 | if (longValue > Byte.MAX_VALUE) { |
| 308 | 1 | throw new ConversionException(toString(sourceType) + " value '" + value |
| 309 | |
+ "' is too large for " + toString(targetType)); |
| 310 | |
} |
| 311 | 768 | if (longValue < Byte.MIN_VALUE) { |
| 312 | 1 | throw new ConversionException(toString(sourceType) + " value '" + value |
| 313 | |
+ "' is too small " + toString(targetType)); |
| 314 | |
} |
| 315 | 767 | return new Byte(value.byteValue()); |
| 316 | |
} |
| 317 | |
|
| 318 | |
|
| 319 | 3260 | if (targetType.equals(Short.class)) { |
| 320 | 769 | long longValue = value.longValue(); |
| 321 | 769 | if (longValue > Short.MAX_VALUE) { |
| 322 | 1 | throw new ConversionException(toString(sourceType) + " value '" + value |
| 323 | |
+ "' is too large for " + toString(targetType)); |
| 324 | |
} |
| 325 | 768 | if (longValue < Short.MIN_VALUE) { |
| 326 | 1 | throw new ConversionException(toString(sourceType) + " value '" + value |
| 327 | |
+ "' is too small " + toString(targetType)); |
| 328 | |
} |
| 329 | 767 | return new Short(value.shortValue()); |
| 330 | |
} |
| 331 | |
|
| 332 | |
|
| 333 | 2491 | if (targetType.equals(Integer.class)) { |
| 334 | 101 | long longValue = value.longValue(); |
| 335 | 101 | if (longValue > Integer.MAX_VALUE) { |
| 336 | 1 | throw new ConversionException(toString(sourceType) + " value '" + value |
| 337 | |
+ "' is too large for " + toString(targetType)); |
| 338 | |
} |
| 339 | 100 | if (longValue < Integer.MIN_VALUE) { |
| 340 | 1 | throw new ConversionException(toString(sourceType) + " value '" + value |
| 341 | |
+ "' is too small " + toString(targetType)); |
| 342 | |
} |
| 343 | 99 | return new Integer(value.intValue()); |
| 344 | |
} |
| 345 | |
|
| 346 | |
|
| 347 | 2390 | if (targetType.equals(Long.class)) { |
| 348 | 776 | return new Long(value.longValue()); |
| 349 | |
} |
| 350 | |
|
| 351 | |
|
| 352 | 1614 | if (targetType.equals(Float.class)) { |
| 353 | 773 | if (value.doubleValue() > Float.MAX_VALUE) { |
| 354 | 1 | throw new ConversionException(toString(sourceType) + " value '" + value |
| 355 | |
+ "' is too large for " + toString(targetType)); |
| 356 | |
} |
| 357 | 772 | return new Float(value.floatValue()); |
| 358 | |
} |
| 359 | |
|
| 360 | |
|
| 361 | 841 | if (targetType.equals(Double.class)) { |
| 362 | 771 | return new Double(value.doubleValue()); |
| 363 | |
} |
| 364 | |
|
| 365 | |
|
| 366 | 70 | if (targetType.equals(BigDecimal.class)) { |
| 367 | 31 | if (value instanceof Float || value instanceof Double) { |
| 368 | 6 | return new BigDecimal(value.toString()); |
| 369 | 25 | } else if (value instanceof BigInteger) { |
| 370 | 1 | return new BigDecimal((BigInteger)value); |
| 371 | |
} else { |
| 372 | 24 | return BigDecimal.valueOf(value.longValue()); |
| 373 | |
} |
| 374 | |
} |
| 375 | |
|
| 376 | |
|
| 377 | 39 | if (targetType.equals(BigInteger.class)) { |
| 378 | 31 | if (value instanceof BigDecimal) { |
| 379 | 1 | return ((BigDecimal)value).toBigInteger(); |
| 380 | |
} else { |
| 381 | 30 | return BigInteger.valueOf(value.longValue()); |
| 382 | |
} |
| 383 | |
} |
| 384 | |
|
| 385 | 8 | String msg = toString(getClass()) + " cannot handle conversion to '" |
| 386 | |
+ toString(targetType) + "'"; |
| 387 | 8 | if (log().isWarnEnabled()) { |
| 388 | 8 | log().warn(" " + msg); |
| 389 | |
} |
| 390 | 8 | throw new ConversionException(msg); |
| 391 | |
|
| 392 | |
} |
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
private Number toNumber(Class sourceType, Class targetType, String value) { |
| 415 | |
|
| 416 | |
|
| 417 | 372 | if (targetType.equals(Byte.class)) { |
| 418 | 35 | return new Byte(value); |
| 419 | |
} |
| 420 | |
|
| 421 | |
|
| 422 | 337 | if (targetType.equals(Short.class)) { |
| 423 | 38 | return new Short(value); |
| 424 | |
} |
| 425 | |
|
| 426 | |
|
| 427 | 299 | if (targetType.equals(Integer.class)) { |
| 428 | 164 | return new Integer(value); |
| 429 | |
} |
| 430 | |
|
| 431 | |
|
| 432 | 135 | if (targetType.equals(Long.class)) { |
| 433 | 33 | return new Long(value); |
| 434 | |
} |
| 435 | |
|
| 436 | |
|
| 437 | 102 | if (targetType.equals(Float.class)) { |
| 438 | 32 | return new Float(value); |
| 439 | |
} |
| 440 | |
|
| 441 | |
|
| 442 | 70 | if (targetType.equals(Double.class)) { |
| 443 | 38 | return new Double(value); |
| 444 | |
} |
| 445 | |
|
| 446 | |
|
| 447 | 32 | if (targetType.equals(BigDecimal.class)) { |
| 448 | 14 | return new BigDecimal(value); |
| 449 | |
} |
| 450 | |
|
| 451 | |
|
| 452 | 18 | if (targetType.equals(BigInteger.class)) { |
| 453 | 18 | return new BigInteger(value); |
| 454 | |
} |
| 455 | |
|
| 456 | 0 | String msg = toString(getClass()) + " cannot handle conversion from '" + |
| 457 | |
toString(sourceType) + "' to '" + toString(targetType) + "'"; |
| 458 | 0 | if (log().isWarnEnabled()) { |
| 459 | 0 | log().warn(" " + msg); |
| 460 | |
} |
| 461 | 0 | throw new ConversionException(msg); |
| 462 | |
} |
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
public String toString() { |
| 470 | 155 | StringBuffer buffer = new StringBuffer(); |
| 471 | 155 | buffer.append(toString(getClass())); |
| 472 | 155 | buffer.append("[UseDefault="); |
| 473 | 155 | buffer.append(isUseDefault()); |
| 474 | 155 | buffer.append(", UseLocaleFormat="); |
| 475 | 155 | buffer.append(useLocaleFormat); |
| 476 | 155 | if (pattern != null) { |
| 477 | 0 | buffer.append(", Pattern="); |
| 478 | 0 | buffer.append(pattern); |
| 479 | |
} |
| 480 | 155 | if (locale != null) { |
| 481 | 0 | buffer.append(", Locale="); |
| 482 | 0 | buffer.append(locale); |
| 483 | |
} |
| 484 | 155 | buffer.append(']'); |
| 485 | 155 | return buffer.toString(); |
| 486 | |
} |
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
private NumberFormat getFormat() { |
| 494 | 194 | NumberFormat format = null; |
| 495 | 194 | if (pattern != null) { |
| 496 | 114 | if (locale == null) { |
| 497 | 32 | if (log().isDebugEnabled()) { |
| 498 | 0 | log().debug(" Using pattern '" + pattern + "'"); |
| 499 | |
} |
| 500 | 32 | format = new DecimalFormat(pattern); |
| 501 | |
} else { |
| 502 | 82 | if (log().isDebugEnabled()) { |
| 503 | 0 | log().debug(" Using pattern '" + pattern + "'" + |
| 504 | |
" with Locale[" + locale + "]"); |
| 505 | |
} |
| 506 | 82 | DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale); |
| 507 | 82 | format = new DecimalFormat(pattern, symbols); |
| 508 | 82 | } |
| 509 | |
} else { |
| 510 | 80 | if (locale == null) { |
| 511 | 40 | if (log().isDebugEnabled()) { |
| 512 | 0 | log().debug(" Using default Locale format"); |
| 513 | |
} |
| 514 | 40 | format = NumberFormat.getInstance(); |
| 515 | |
} else { |
| 516 | 40 | if (log().isDebugEnabled()) { |
| 517 | 0 | log().debug(" Using Locale[" + locale + "] format"); |
| 518 | |
} |
| 519 | 40 | format = NumberFormat.getInstance(locale); |
| 520 | |
} |
| 521 | |
} |
| 522 | 194 | if (!allowDecimals) { |
| 523 | 134 | format.setParseIntegerOnly(true); |
| 524 | |
} |
| 525 | 194 | return format; |
| 526 | |
} |
| 527 | |
|
| 528 | |
|
| 529 | |
|
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | |
|
| 537 | |
|
| 538 | |
private Number parse(Class sourceType, Class targetType, String value, NumberFormat format) { |
| 539 | 116 | ParsePosition pos = new ParsePosition(0); |
| 540 | 116 | Number parsedNumber = format.parse(value, pos); |
| 541 | 116 | if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedNumber == null) { |
| 542 | 32 | String msg = "Error converting from '" + toString(sourceType) + "' to '" + toString(targetType) + "'"; |
| 543 | 32 | if (format instanceof DecimalFormat) { |
| 544 | 32 | msg += " using pattern '" + ((DecimalFormat)format).toPattern() + "'"; |
| 545 | |
} |
| 546 | 32 | if (locale != null) { |
| 547 | 24 | msg += " for locale=[" + locale + "]"; |
| 548 | |
} |
| 549 | 32 | if (log().isDebugEnabled()) { |
| 550 | 0 | log().debug(" " + msg); |
| 551 | |
} |
| 552 | 32 | throw new ConversionException(msg); |
| 553 | |
} |
| 554 | 84 | return parsedNumber; |
| 555 | |
} |
| 556 | |
|
| 557 | |
} |