1 16 17 package org.apache.taglibs.standard.lang.jstl; 18 19 import java.beans.PropertyEditor ; 20 import java.beans.PropertyEditorManager ; 21 22 231 232 public class Coercions 233 { 234 239 public static Object coerce (Object pValue, 240 Class pClass, 241 Logger pLogger) 242 throws ELException 243 { 244 if (pClass == String .class) { 245 return coerceToString (pValue, pLogger); 246 } 247 else if (isPrimitiveNumberClass (pClass)) { 248 return coerceToPrimitiveNumber (pValue, pClass, pLogger); 249 } 250 else if (pClass == Character .class || 251 pClass == Character.TYPE) { 252 return coerceToCharacter (pValue, pLogger); 253 } 254 else if (pClass == Boolean .class || 255 pClass == Boolean.TYPE) { 256 return coerceToBoolean (pValue, pLogger); 257 } 258 else { 259 return coerceToObject (pValue, pClass, pLogger); 260 } 261 } 262 263 269 static boolean isPrimitiveNumberClass (Class pClass) 270 { 271 return 272 pClass == Byte .class || 273 pClass == Byte.TYPE || 274 pClass == Short .class || 275 pClass == Short.TYPE || 276 pClass == Integer .class || 277 pClass == Integer.TYPE || 278 pClass == Long .class || 279 pClass == Long.TYPE || 280 pClass == Float .class || 281 pClass == Float.TYPE || 282 pClass == Double .class || 283 pClass == Double.TYPE; 284 } 285 286 291 public static String coerceToString (Object pValue, 292 Logger pLogger) 293 throws ELException 294 { 295 if (pValue == null) { 296 return ""; 297 } 298 else if (pValue instanceof String ) { 299 return (String ) pValue; 300 } 301 else { 302 try { 303 return pValue.toString (); 304 } 305 catch (Exception exc) { 306 if (pLogger.isLoggingError ()) { 307 pLogger.logError (Constants.TOSTRING_EXCEPTION, 308 exc, 309 pValue.getClass ().getName ()); 310 } 311 return ""; 312 } 313 } 314 } 315 316 321 public static Number coerceToPrimitiveNumber (Object pValue, 322 Class pClass, 323 Logger pLogger) 324 throws ELException 325 { 326 if (pValue == null || 327 "".equals (pValue)) { 328 return coerceToPrimitiveNumber (0, pClass); 329 } 330 else if (pValue instanceof Character ) { 331 char val = ((Character ) pValue).charValue (); 332 return coerceToPrimitiveNumber ((short) val, pClass); 333 } 334 else if (pValue instanceof Boolean ) { 335 if (pLogger.isLoggingError ()) { 336 pLogger.logError (Constants.BOOLEAN_TO_NUMBER, 337 pValue, 338 pClass.getName ()); 339 } 340 return coerceToPrimitiveNumber (0, pClass); 341 } 342 else if (pValue.getClass () == pClass) { 343 return (Number ) pValue; 344 } 345 else if (pValue instanceof Number ) { 346 return coerceToPrimitiveNumber ((Number ) pValue, pClass); 347 } 348 else if (pValue instanceof String ) { 349 try { 350 return coerceToPrimitiveNumber ((String ) pValue, pClass); 351 } 352 catch (Exception exc) { 353 if (pLogger.isLoggingError ()) { 354 pLogger.logError 355 (Constants.STRING_TO_NUMBER_EXCEPTION, 356 (String ) pValue, 357 pClass.getName ()); 358 } 359 return coerceToPrimitiveNumber (0, pClass); 360 } 361 } 362 else { 363 if (pLogger.isLoggingError ()) { 364 pLogger.logError 365 (Constants.COERCE_TO_NUMBER, 366 pValue.getClass ().getName (), 367 pClass.getName ()); 368 } 369 return coerceToPrimitiveNumber (0, pClass); 370 } 371 } 372 373 379 public static Integer coerceToInteger (Object pValue, 380 Logger pLogger) 381 throws ELException 382 { 383 if (pValue == null) { 384 return null; 385 } 386 else if (pValue instanceof Character ) { 387 return PrimitiveObjects.getInteger 388 ((int) (((Character ) pValue).charValue ())); 389 } 390 else if (pValue instanceof Boolean ) { 391 if (pLogger.isLoggingWarning ()) { 392 pLogger.logWarning (Constants.BOOLEAN_TO_NUMBER, 393 pValue, 394 Integer .class.getName ()); 395 } 396 return PrimitiveObjects.getInteger 397 (((Boolean ) pValue).booleanValue () ? 1 : 0); 398 } 399 else if (pValue instanceof Integer ) { 400 return (Integer ) pValue; 401 } 402 else if (pValue instanceof Number ) { 403 return PrimitiveObjects.getInteger (((Number ) pValue).intValue ()); 404 } 405 else if (pValue instanceof String ) { 406 try { 407 return Integer.valueOf ((String ) pValue); 408 } 409 catch (Exception exc) { 410 if (pLogger.isLoggingWarning ()) { 411 pLogger.logWarning 412 (Constants.STRING_TO_NUMBER_EXCEPTION, 413 (String ) pValue, 414 Integer .class.getName ()); 415 } 416 return null; 417 } 418 } 419 else { 420 if (pLogger.isLoggingWarning ()) { 421 pLogger.logWarning 422 (Constants.COERCE_TO_NUMBER, 423 pValue.getClass ().getName (), 424 Integer .class.getName ()); 425 } 426 return null; 427 } 428 } 429 430 435 static Number coerceToPrimitiveNumber (long pValue, 436 Class pClass) 437 throws ELException 438 { 439 if (pClass == Byte .class || pClass == Byte.TYPE) { 440 return PrimitiveObjects.getByte ((byte) pValue); 441 } 442 else if (pClass == Short .class || pClass == Short.TYPE) { 443 return PrimitiveObjects.getShort ((short) pValue); 444 } 445 else if (pClass == Integer .class || pClass == Integer.TYPE) { 446 return PrimitiveObjects.getInteger ((int) pValue); 447 } 448 else if (pClass == Long .class || pClass == Long.TYPE) { 449 return PrimitiveObjects.getLong ((long) pValue); 450 } 451 else if (pClass == Float .class || pClass == Float.TYPE) { 452 return PrimitiveObjects.getFloat ((float) pValue); 453 } 454 else if (pClass == Double .class || pClass == Double.TYPE) { 455 return PrimitiveObjects.getDouble ((double) pValue); 456 } 457 else { 458 return PrimitiveObjects.getInteger (0); 459 } 460 } 461 462 467 static Number coerceToPrimitiveNumber (double pValue, 468 Class pClass) 469 throws ELException 470 { 471 if (pClass == Byte .class || pClass == Byte.TYPE) { 472 return PrimitiveObjects.getByte ((byte) pValue); 473 } 474 else if (pClass == Short .class || pClass == Short.TYPE) { 475 return PrimitiveObjects.getShort ((short) pValue); 476 } 477 else if (pClass == Integer .class || pClass == Integer.TYPE) { 478 return PrimitiveObjects.getInteger ((int) pValue); 479 } 480 else if (pClass == Long .class || pClass == Long.TYPE) { 481 return PrimitiveObjects.getLong ((long) pValue); 482 } 483 else if (pClass == Float .class || pClass == Float.TYPE) { 484 return PrimitiveObjects.getFloat ((float) pValue); 485 } 486 else if (pClass == Double .class || pClass == Double.TYPE) { 487 return PrimitiveObjects.getDouble ((double) pValue); 488 } 489 else { 490 return PrimitiveObjects.getInteger (0); 491 } 492 } 493 494 499 static Number coerceToPrimitiveNumber (Number pValue, 500 Class pClass) 501 throws ELException 502 { 503 if (pClass == Byte .class || pClass == Byte.TYPE) { 504 return PrimitiveObjects.getByte (pValue.byteValue ()); 505 } 506 else if (pClass == Short .class || pClass == Short.TYPE) { 507 return PrimitiveObjects.getShort (pValue.shortValue ()); 508 } 509 else if (pClass == Integer .class || pClass == Integer.TYPE) { 510 return PrimitiveObjects.getInteger (pValue.intValue ()); 511 } 512 else if (pClass == Long .class || pClass == Long.TYPE) { 513 return PrimitiveObjects.getLong (pValue.longValue ()); 514 } 515 else if (pClass == Float .class || pClass == Float.TYPE) { 516 return PrimitiveObjects.getFloat (pValue.floatValue ()); 517 } 518 else if (pClass == Double .class || pClass == Double.TYPE) { 519 return PrimitiveObjects.getDouble (pValue.doubleValue ()); 520 } 521 else { 522 return PrimitiveObjects.getInteger (0); 523 } 524 } 525 526 531 static Number coerceToPrimitiveNumber (String pValue, 532 Class pClass) 533 throws ELException 534 { 535 if (pClass == Byte .class || pClass == Byte.TYPE) { 536 return Byte.valueOf (pValue); 537 } 538 else if (pClass == Short .class || pClass == Short.TYPE) { 539 return Short.valueOf (pValue); 540 } 541 else if (pClass == Integer .class || pClass == Integer.TYPE) { 542 return Integer.valueOf (pValue); 543 } 544 else if (pClass == Long .class || pClass == Long.TYPE) { 545 return Long.valueOf (pValue); 546 } 547 else if (pClass == Float .class || pClass == Float.TYPE) { 548 return Float.valueOf (pValue); 549 } 550 else if (pClass == Double .class || pClass == Double.TYPE) { 551 return Double.valueOf (pValue); 552 } 553 else { 554 return PrimitiveObjects.getInteger (0); 555 } 556 } 557 558 563 public static Character coerceToCharacter (Object pValue, 564 Logger pLogger) 565 throws ELException 566 { 567 if (pValue == null || 568 "".equals (pValue)) { 569 return PrimitiveObjects.getCharacter ((char) 0); 570 } 571 else if (pValue instanceof Character ) { 572 return (Character ) pValue; 573 } 574 else if (pValue instanceof Boolean ) { 575 if (pLogger.isLoggingError ()) { 576 pLogger.logError (Constants.BOOLEAN_TO_CHARACTER, pValue); 577 } 578 return PrimitiveObjects.getCharacter ((char) 0); 579 } 580 else if (pValue instanceof Number ) { 581 return PrimitiveObjects.getCharacter 582 ((char) ((Number ) pValue).shortValue ()); 583 } 584 else if (pValue instanceof String ) { 585 String str = (String ) pValue; 586 return PrimitiveObjects.getCharacter (str.charAt (0)); 587 } 588 else { 589 if (pLogger.isLoggingError ()) { 590 pLogger.logError 591 (Constants.COERCE_TO_CHARACTER, 592 pValue.getClass ().getName ()); 593 } 594 return PrimitiveObjects.getCharacter ((char) 0); 595 } 596 } 597 598 603 public static Boolean coerceToBoolean (Object pValue, 604 Logger pLogger) 605 throws ELException 606 { 607 if (pValue == null || 608 "".equals (pValue)) { 609 return Boolean.FALSE; 610 } 611 else if (pValue instanceof Boolean ) { 612 return (Boolean ) pValue; 613 } 614 else if (pValue instanceof String ) { 615 String str = (String ) pValue; 616 try { 617 return Boolean.valueOf (str); 618 } 619 catch (Exception exc) { 620 if (pLogger.isLoggingError ()) { 621 pLogger.logError 622 (Constants.STRING_TO_BOOLEAN, 623 exc, 624 (String ) pValue); 625 } 626 return Boolean.FALSE; 627 } 628 } 629 else { 630 if (pLogger.isLoggingError ()) { 631 pLogger.logError 632 (Constants.COERCE_TO_BOOLEAN, 633 pValue.getClass ().getName ()); 634 } 635 return Boolean.TRUE; 636 } 637 } 638 639 645 public static Object coerceToObject (Object pValue, 646 Class pClass, 647 Logger pLogger) 648 throws ELException 649 { 650 if (pValue == null) { 651 return null; 652 } 653 else if (pClass.isAssignableFrom (pValue.getClass ())) { 654 return pValue; 655 } 656 else if (pValue instanceof String ) { 657 String str = (String ) pValue; 658 PropertyEditor pe = PropertyEditorManager.findEditor (pClass); 659 if (pe == null) { 660 if ("".equals (str)) { 661 return null; 662 } 663 else { 664 if (pLogger.isLoggingError ()) { 665 pLogger.logError 666 (Constants.NO_PROPERTY_EDITOR, 667 str, 668 pClass.getName ()); 669 } 670 return null; 671 } 672 } 673 try { 674 pe.setAsText (str); 675 return pe.getValue (); 676 } 677 catch (IllegalArgumentException exc) { 678 if ("".equals (str)) { 679 return null; 680 } 681 else { 682 if (pLogger.isLoggingError ()) { 683 pLogger.logError 684 (Constants.PROPERTY_EDITOR_ERROR, 685 exc, 686 pValue, 687 pClass.getName ()); 688 } 689 return null; 690 } 691 } 692 } 693 else { 694 if (pLogger.isLoggingError ()) { 695 pLogger.logError 696 (Constants.COERCE_TO_OBJECT, 697 pValue.getClass ().getName (), 698 pClass.getName ()); 699 } 700 return null; 701 } 702 } 703 704 712 public static Object applyArithmeticOperator 713 (Object pLeft, 714 Object pRight, 715 ArithmeticOperator pOperator, 716 Logger pLogger) 717 throws ELException 718 { 719 if (pLeft == null && 720 pRight == null) { 721 if (pLogger.isLoggingWarning ()) { 722 pLogger.logWarning 723 (Constants.ARITH_OP_NULL, 724 pOperator.getOperatorSymbol ()); 725 } 726 return PrimitiveObjects.getInteger (0); 727 } 728 729 else if (isFloatingPointType (pLeft) || 730 isFloatingPointType (pRight) || 731 isFloatingPointString (pLeft) || 732 isFloatingPointString (pRight)) { 733 double left = 734 coerceToPrimitiveNumber (pLeft, Double .class, pLogger). 735 doubleValue (); 736 double right = 737 coerceToPrimitiveNumber (pRight, Double .class, pLogger). 738 doubleValue (); 739 return 740 PrimitiveObjects.getDouble (pOperator.apply (left, right, pLogger)); 741 } 742 743 else { 744 long left = 745 coerceToPrimitiveNumber (pLeft, Long .class, pLogger). 746 longValue (); 747 long right = 748 coerceToPrimitiveNumber (pRight, Long .class, pLogger). 749 longValue (); 750 return 751 PrimitiveObjects.getLong (pOperator.apply (left, right, pLogger)); 752 } 753 } 754 755 761 public static Object applyRelationalOperator 762 (Object pLeft, 763 Object pRight, 764 RelationalOperator pOperator, 765 Logger pLogger) 766 throws ELException 767 { 768 if (isFloatingPointType (pLeft) || 769 isFloatingPointType (pRight)) { 770 double left = 771 coerceToPrimitiveNumber (pLeft, Double .class, pLogger). 772 doubleValue (); 773 double right = 774 coerceToPrimitiveNumber (pRight, Double .class, pLogger). 775 doubleValue (); 776 return 777 PrimitiveObjects.getBoolean (pOperator.apply (left, right, pLogger)); 778 } 779 780 else if (isIntegerType (pLeft) || 781 isIntegerType (pRight)) { 782 long left = 783 coerceToPrimitiveNumber (pLeft, Long .class, pLogger). 784 longValue (); 785 long right = 786 coerceToPrimitiveNumber (pRight, Long .class, pLogger). 787 longValue (); 788 return 789 PrimitiveObjects.getBoolean (pOperator.apply (left, right, pLogger)); 790 } 791 792 else if (pLeft instanceof String || 793 pRight instanceof String ) { 794 String left = coerceToString (pLeft, pLogger); 795 String right = coerceToString (pRight, pLogger); 796 return 797 PrimitiveObjects.getBoolean (pOperator.apply (left, right, pLogger)); 798 } 799 800 else if (pLeft instanceof Comparable ) { 801 try { 802 int result = ((Comparable ) pLeft).compareTo (pRight); 803 return 804 PrimitiveObjects.getBoolean 805 (pOperator.apply (result, -result, pLogger)); 806 } 807 catch (Exception exc) { 808 if (pLogger.isLoggingError ()) { 809 pLogger.logError 810 (Constants.COMPARABLE_ERROR, 811 exc, 812 pLeft.getClass ().getName (), 813 (pRight == null) ? "null" : pRight.getClass ().getName (), 814 pOperator.getOperatorSymbol ()); 815 } 816 return Boolean.FALSE; 817 } 818 } 819 820 else if (pRight instanceof Comparable ) { 821 try { 822 int result = ((Comparable ) pRight).compareTo (pLeft); 823 return 824 PrimitiveObjects.getBoolean 825 (pOperator.apply (-result, result, pLogger)); 826 } 827 catch (Exception exc) { 828 if (pLogger.isLoggingError ()) { 829 pLogger.logError 830 (Constants.COMPARABLE_ERROR, 831 exc, 832 pRight.getClass ().getName (), 833 (pLeft == null) ? "null" : pLeft.getClass ().getName (), 834 pOperator.getOperatorSymbol ()); 835 } 836 return Boolean.FALSE; 837 } 838 } 839 840 else { 841 if (pLogger.isLoggingError ()) { 842 pLogger.logError 843 (Constants.ARITH_OP_BAD_TYPE, 844 pOperator.getOperatorSymbol (), 845 pLeft.getClass ().getName (), 846 pRight.getClass ().getName ()); 847 } 848 return Boolean.FALSE; 849 } 850 } 851 852 858 public static Object applyEqualityOperator 859 (Object pLeft, 860 Object pRight, 861 EqualityOperator pOperator, 862 Logger pLogger) 863 throws ELException 864 { 865 if (pLeft == pRight) { 866 return PrimitiveObjects.getBoolean (pOperator.apply (true, pLogger)); 867 } 868 869 else if (pLeft == null || 870 pRight == null) { 871 return PrimitiveObjects.getBoolean (pOperator.apply (false, pLogger)); 872 } 873 874 else if (isFloatingPointType (pLeft) || 875 isFloatingPointType (pRight)) { 876 double left = 877 coerceToPrimitiveNumber (pLeft, Double .class, pLogger). 878 doubleValue (); 879 double right = 880 coerceToPrimitiveNumber (pRight, Double .class, pLogger). 881 doubleValue (); 882 return 883 PrimitiveObjects.getBoolean 884 (pOperator.apply (left == right, pLogger)); 885 } 886 887 else if (isIntegerType (pLeft) || 888 isIntegerType (pRight)) { 889 long left = 890 coerceToPrimitiveNumber (pLeft, Long .class, pLogger). 891 longValue (); 892 long right = 893 coerceToPrimitiveNumber (pRight, Long .class, pLogger). 894 longValue (); 895 return 896 PrimitiveObjects.getBoolean 897 (pOperator.apply (left == right, pLogger)); 898 } 899 900 else if (pLeft instanceof Boolean || 901 pRight instanceof Boolean ) { 902 boolean left = coerceToBoolean (pLeft, pLogger).booleanValue (); 903 boolean right = coerceToBoolean (pRight, pLogger).booleanValue (); 904 return 905 PrimitiveObjects.getBoolean 906 (pOperator.apply (left == right, pLogger)); 907 } 908 909 else if (pLeft instanceof String || 910 pRight instanceof String ) { 911 String left = coerceToString (pLeft, pLogger); 912 String right = coerceToString (pRight, pLogger); 913 return 914 PrimitiveObjects.getBoolean 915 (pOperator.apply (left.equals (right), pLogger)); 916 } 917 918 else { 919 try { 920 return 921 PrimitiveObjects.getBoolean 922 (pOperator.apply (pLeft.equals (pRight), pLogger)); 923 } 924 catch (Exception exc) { 925 if (pLogger.isLoggingError ()) { 926 pLogger.logError 927 (Constants.ERROR_IN_EQUALS, 928 exc, 929 pLeft.getClass ().getName (), 930 pRight.getClass ().getName (), 931 pOperator.getOperatorSymbol ()); 932 } 933 return Boolean.FALSE; 934 } 935 } 936 } 937 938 943 public static boolean isFloatingPointType (Object pObject) 944 { 945 return 946 pObject != null && 947 isFloatingPointType (pObject.getClass ()); 948 } 949 950 955 public static boolean isFloatingPointType (Class pClass) 956 { 957 return 958 pClass == Float .class || 959 pClass == Float.TYPE || 960 pClass == Double .class || 961 pClass == Double.TYPE; 962 } 963 964 970 public static boolean isFloatingPointString (Object pObject) 971 { 972 if (pObject instanceof String ) { 973 String str = (String ) pObject; 974 int len = str.length (); 975 for (int i = 0; i < len; i++) { 976 char ch = str.charAt (i); 977 if (ch == '.' || 978 ch == 'e' || 979 ch == 'E') { 980 return true; 981 } 982 } 983 return false; 984 } 985 else { 986 return false; 987 } 988 } 989 990 995 public static boolean isIntegerType (Object pObject) 996 { 997 return 998 pObject != null && 999 isIntegerType (pObject.getClass ()); 1000 } 1001 1002 1007 public static boolean isIntegerType (Class pClass) 1008 { 1009 return 1010 pClass == Byte .class || 1011 pClass == Byte.TYPE || 1012 pClass == Short .class || 1013 pClass == Short.TYPE || 1014 pClass == Character .class || 1015 pClass == Character.TYPE || 1016 pClass == Integer .class || 1017 pClass == Integer.TYPE || 1018 pClass == Long .class || 1019 pClass == Long.TYPE; 1020 } 1021 1022 1024} 1025 | Popular Tags |