1 17 package org.alfresco.service.cmr.repository.datatype; 18 19 import java.math.BigDecimal ; 20 import java.math.BigInteger ; 21 import java.text.DecimalFormat ; 22 import java.text.ParseException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Date ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 import org.alfresco.service.cmr.dictionary.DictionaryException; 31 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 32 import org.alfresco.service.cmr.repository.NodeRef; 33 import org.alfresco.service.cmr.repository.Path; 34 import org.alfresco.service.namespace.QName; 35 import org.alfresco.util.CachingDateFormat; 36 import org.alfresco.util.ParameterCheck; 37 38 61 public class ValueConverter 62 { 63 72 public static Object convert(DataTypeDefinition propertyType, Object value) 73 { 74 ParameterCheck.mandatory("Property type definition", propertyType); 75 76 Class javaClass = null; 78 String javaClassName = propertyType.getJavaClassName(); 79 try 80 { 81 javaClass = Class.forName(javaClassName); 82 } 83 catch (ClassNotFoundException e) 84 { 85 throw new DictionaryException("Java class " + javaClassName + " of property type " + propertyType.getName() + " is invalid", e); 86 } 87 88 return convert(javaClass, value); 89 } 90 91 92 102 public static <T> T convert(Class <T> c, Object value) 103 { 104 if (value instanceof Collection ) 106 { 107 Object valueObject = null; 108 for (Object o : (Collection ) value) 109 { 110 valueObject = o; 111 break; 112 } 113 return convert(c, valueObject); 114 } 115 116 if(value == null) 117 { 118 return null; 119 } 120 121 if (c.isPrimitive()) 123 { 124 throw new UnsupportedOperationException ("Can not convert direct to primitive type " + c.getName()); 126 } 127 128 if (c.isInstance(value)) 130 { 131 return c.cast(value); 132 } 133 134 Converter converter = getConversion(value.getClass(), c); 136 return (T) converter.convert(value); 137 138 } 139 140 146 public static boolean isMultiValued(Object value) 147 { 148 return ((value instanceof Collection ) && (((Collection ) value).size() > 1)); 149 } 150 151 156 public static int size(Object value) 157 { 158 if (value instanceof Collection ) 159 { 160 return ((Collection ) value).size(); 161 } 162 else 163 { 164 return 1; 165 } 166 } 167 168 179 public static <T> Iterator <T> iterator(Class <T> c, Object value) 180 { 181 Collection coll = createCollection(value); 182 Iterator it = coll.iterator(); 183 184 Iterator <T> retit = new ConvertingIterator<T>(it, c); 185 186 return retit; 187 } 188 189 private static Collection createCollection(Object value) 190 { 191 Collection coll; 192 if (ValueConverter.isMultiValued(value)) 193 { 194 coll = (Collection ) value; 195 } 196 else 197 { 198 ArrayList <Object > list = new ArrayList <Object >(1); 199 list.add(value); 200 coll = list; 201 } 202 return coll; 203 } 204 205 212 public static <T> Collection <T> getCollection(Class <T> c, Object value) 213 { 214 Collection coll = createCollection(value); 215 return new ConvertingCollection<T>(coll, c); 216 } 217 218 224 private static class ConvertingIterator<T> implements Iterator <T> 225 { 226 Iterator it; 227 228 Class type; 229 230 ConvertingIterator(Iterator it, Class type) 231 { 232 this.it = it; 233 this.type = type; 234 } 235 236 public boolean hasNext() 237 { 238 return it.hasNext(); 239 } 240 241 public T next() 242 { 243 return (T) ValueConverter.convert(type, it.next()); 244 } 245 246 public void remove() 247 { 248 throw new UnsupportedOperationException (); 249 } 250 251 } 252 253 private static class ConvertingCollection<T> implements Collection <T> 254 { 255 private Collection <?> base; 256 private Class c; 257 258 ConvertingCollection(Collection <?> base, Class c) 259 { 260 super(); 261 this.base = base; 262 this.c = c; 263 } 264 265 public boolean add(T o) 266 { 267 throw new UnsupportedOperationException (); 268 } 269 270 public boolean addAll(Collection c) 271 { 272 throw new UnsupportedOperationException (); 273 } 274 275 public void clear() 276 { 277 throw new UnsupportedOperationException (); 278 } 279 280 public boolean contains(Object o) 281 { 282 return base.contains(o); 283 } 284 285 public boolean containsAll(Collection <?> c) 286 { 287 return base.containsAll(c); 288 } 289 290 public boolean equals(Object o) 291 { 292 return base.equals(o); 293 } 294 295 public int hashCode() 296 { 297 return base.hashCode(); 298 } 299 300 public boolean isEmpty() 301 { 302 return base.isEmpty(); 303 } 304 305 public Iterator <T> iterator() 306 { 307 Iterator it = base.iterator(); 308 Iterator <T> retit = new ConvertingIterator<T>(it, c); 309 return retit; 310 } 311 312 public boolean remove(Object o) 313 { 314 throw new UnsupportedOperationException (); 315 } 316 317 public boolean removeAll(Collection c) 318 { 319 throw new UnsupportedOperationException (); 320 } 321 322 public boolean retainAll(Collection c) 323 { 324 throw new UnsupportedOperationException (); 325 } 326 327 public int size() 328 { 329 return base.size(); 330 } 331 332 public Object [] toArray() 333 { 334 throw new UnsupportedOperationException (); 336 } 337 338 public <O> O[] toArray(O[] a) 339 { 340 throw new UnsupportedOperationException (); 342 } 343 344 345 } 346 347 354 public static boolean booleanValue(Object value) 355 { 356 return convert(Boolean .class, value).booleanValue(); 357 } 358 359 360 367 public static char charValue(Object value) 368 { 369 return convert(Character .class, value).charValue(); 370 } 371 372 373 380 public static byte byteValue(Object value) 381 { 382 if (value instanceof Number ) 383 { 384 return ((Number ) value).byteValue(); 385 } 386 return convert(Number .class, value).byteValue(); 387 } 388 389 396 public static short shortValue(Object value) 397 { 398 if (value instanceof Number ) 399 { 400 return ((Number ) value).shortValue(); 401 } 402 return convert(Number .class, value).shortValue(); 403 } 404 405 406 413 public static int intValue(Object value) 414 { 415 if (value instanceof Number ) 416 { 417 return ((Number ) value).intValue(); 418 } 419 return convert(Number .class, value).intValue(); 420 } 421 422 423 430 public static long longValue(Object value) 431 { 432 if (value instanceof Number ) 433 { 434 return ((Number ) value).longValue(); 435 } 436 return convert(Number .class, value).longValue(); 437 } 438 439 446 public static float floatValue(Object value) 447 { 448 if (value instanceof Number ) 449 { 450 return ((Number ) value).floatValue(); 451 } 452 return convert(Number .class, value).floatValue(); 453 } 454 455 456 463 public static double doubleValue(Object value) 464 { 465 if (value instanceof Number ) 466 { 467 return ((Number ) value).doubleValue(); 468 } 469 return convert(Number .class, value).doubleValue(); 470 } 471 472 481 private static <F, T> Converter getConversion(Class <F> source, Class <T> dest) 482 { 483 Converter<?, ?> converter = null; 484 Class clazz = source; 485 do 486 { 487 Map <Class , Converter> map = conversions.get(clazz); 488 if (map == null) 489 { 490 continue; 491 } 492 converter = map.get(dest); 493 if (converter == null) 494 { 495 Converter<?, ?> first = map.get(Number .class); 496 Converter<?, ?> second = null; 497 if (first != null) 498 { 499 map = conversions.get(Number .class); 500 if (map != null) 501 { 502 second = map.get(dest); 503 } 504 } 505 if (second != null) 506 { 507 converter = new TwoStageConverter<F, T, Number >(first, second); 508 } 509 510 } 511 } 512 while ((converter == null) && ((clazz = clazz.getSuperclass()) != null)); 513 514 if (converter == null) 515 { 516 throw new UnsupportedOperationException ("There are is no conversion registered from source type " + source.getName() + " to " + dest); 517 518 } 519 return converter; 520 } 521 522 531 public static <F, T> void addConverter(Class <F> source, Class <T> destination, Converter<F, T> converter) 532 { 533 Map <Class , Converter> map = conversions.get(source); 534 if (map == null) 535 { 536 map = new HashMap <Class , Converter>(); 537 conversions.put(source, map); 538 } 539 map.put(destination, converter); 540 } 541 542 545 static Map <Class , Map <Class , Converter>> conversions = new HashMap <Class , Map <Class , Converter>>(); 546 547 550 static 551 { 552 556 Map <Class , Converter> map = new HashMap <Class , Converter>(); 557 conversions.put(String .class, map); 558 map.put(Boolean .class, new Converter<String , Boolean >() 559 { 560 public Boolean convert(String source) 561 { 562 return Boolean.valueOf(source); 563 } 564 565 }); 566 567 map.put(Character .class, new Converter<String , Character >() 568 { 569 public Character convert(String source) 570 { 571 if ((source == null) || (source.length() == 0)) 572 { 573 return null; 574 } 575 return Character.valueOf(source.charAt(0)); 576 } 577 578 }); 579 580 map.put(Number .class, new Converter<String , Number >() 581 { 582 public Number convert(String source) 583 { 584 try 585 { 586 return DecimalFormat.getNumberInstance().parse(source); 587 } 588 catch (ParseException e) 589 { 590 throw new RuntimeException (e); 591 } 592 } 593 594 }); 595 596 map.put(Byte .class, new Converter<String , Byte >() 597 { 598 public Byte convert(String source) 599 { 600 return Byte.valueOf(source); 601 } 602 603 }); 604 605 map.put(Short .class, new Converter<String , Short >() 606 { 607 public Short convert(String source) 608 { 609 return Short.valueOf(source); 610 } 611 612 }); 613 614 map.put(Integer .class, new Converter<String , Integer >() 615 { 616 public Integer convert(String source) 617 { 618 return Integer.valueOf(source); 619 } 620 621 }); 622 623 map.put(Long .class, new Converter<String , Long >() 624 { 625 public Long convert(String source) 626 { 627 return Long.valueOf(source); 628 } 629 630 }); 631 632 map.put(Float .class, new Converter<String , Float >() 633 { 634 public Float convert(String source) 635 { 636 return Float.valueOf(source); 637 } 638 639 }); 640 641 map.put(Double .class, new Converter<String , Double >() 642 { 643 public Double convert(String source) 644 { 645 return Double.valueOf(source); 646 } 647 648 }); 649 650 map.put(BigInteger .class, new Converter<String , BigInteger >() 651 { 652 public BigInteger convert(String source) 653 { 654 return new BigInteger (source); 655 } 656 657 }); 658 659 map.put(BigDecimal .class, new Converter<String , BigDecimal >() 660 { 661 public BigDecimal convert(String source) 662 { 663 return new BigDecimal (source); 664 } 665 666 }); 667 668 map.put(Date .class, new Converter<String , Date >() 669 { 670 public Date convert(String source) 671 { 672 try 673 { 674 return CachingDateFormat.getDateFormat().parse(source); 675 } 676 catch (ParseException e) 677 { 678 throw new RuntimeException (e); 679 } 680 } 681 682 }); 683 684 map.put(Duration.class, new Converter<String , Duration>() 685 { 686 public Duration convert(String source) 687 { 688 return new Duration(source); 689 } 690 691 }); 692 693 map.put(QName.class, new Converter<String , QName>() 694 { 695 public QName convert(String source) 696 { 697 return QName.createQName(source); 698 } 699 700 }); 701 702 map.put(NodeRef.class, new Converter<String , NodeRef>() 703 { 704 public NodeRef convert(String source) 705 { 706 return new NodeRef(source); 707 } 708 709 }); 710 711 715 map = new HashMap <Class , Converter>(); 716 conversions.put(Number .class, map); 717 718 map.put(Byte .class, new Converter<Number , Byte >() 719 { 720 public Byte convert(Number source) 721 { 722 return Byte.valueOf(source.byteValue()); 723 } 724 725 }); 726 727 map.put(Short .class, new Converter<Number , Short >() 728 { 729 public Short convert(Number source) 730 { 731 return Short.valueOf(source.shortValue()); 732 } 733 734 }); 735 736 map.put(Integer .class, new Converter<Number , Integer >() 737 { 738 public Integer convert(Number source) 739 { 740 return Integer.valueOf(source.intValue()); 741 } 742 743 }); 744 745 map.put(Long .class, new Converter<Number , Long >() 746 { 747 public Long convert(Number source) 748 { 749 return Long.valueOf(source.longValue()); 750 } 751 752 }); 753 754 map.put(Float .class, new Converter<Number , Float >() 755 { 756 public Float convert(Number source) 757 { 758 return Float.valueOf(source.floatValue()); 759 } 760 761 }); 762 763 map.put(Double .class, new Converter<Number , Double >() 764 { 765 public Double convert(Number source) 766 { 767 return Double.valueOf(source.doubleValue()); 768 } 769 770 }); 771 772 map.put(Date .class, new Converter<Number , Date >() 773 { 774 public Date convert(Number source) 775 { 776 return new Date (source.longValue()); 777 } 778 779 }); 780 781 map.put(String .class, new Converter<Number , String >() 782 { 783 public String convert(Number source) 784 { 785 return source.toString(); 786 } 787 788 }); 789 790 map.put(BigInteger .class, new Converter<Number , BigInteger >() 791 { 792 public BigInteger convert(Number source) 793 { 794 if (source instanceof BigDecimal ) 795 { 796 return ((BigDecimal ) source).toBigInteger(); 797 } 798 else 799 { 800 return BigInteger.valueOf(source.longValue()); 801 } 802 } 803 804 }); 805 806 map.put(BigDecimal .class, new Converter<Number , BigDecimal >() 807 { 808 public BigDecimal convert(Number source) 809 { 810 if (source instanceof BigInteger ) 811 { 812 return new BigDecimal ((BigInteger ) source); 813 } 814 else 815 { 816 return BigDecimal.valueOf(source.longValue()); 817 } 818 } 819 820 }); 821 822 826 map = new HashMap <Class , Converter>(); 827 conversions.put(Date .class, map); 828 829 map.put(Number .class, new Converter<Date , Number >() 830 { 831 public Number convert(Date source) 832 { 833 return Long.valueOf(source.getTime()); 834 } 835 836 }); 837 838 map.put(String .class, new Converter<Date , String >() 839 { 840 public String convert(Date source) 841 { 842 return CachingDateFormat.getDateFormat().format(source); 843 } 844 845 }); 846 847 851 map = new HashMap <Class , Converter>(); 852 conversions.put(Boolean .class, map); 853 854 map.put(String .class, new Converter<Boolean , String >() 855 { 856 public String convert(Boolean source) 857 { 858 return source.toString(); 859 } 860 861 }); 862 863 867 map = new HashMap <Class , Converter>(); 868 conversions.put(Character .class, map); 869 870 map.put(String .class, new Converter<Character , String >() 871 { 872 public String convert(Character source) 873 { 874 return source.toString(); 875 } 876 877 }); 878 879 883 map = new HashMap <Class , Converter>(); 884 conversions.put(Duration.class, map); 885 886 map.put(String .class, new Converter<Duration, String >() 887 { 888 public String convert(Duration source) 889 { 890 return source.toString(); 891 } 892 893 }); 894 895 899 900 map = new HashMap <Class , Converter>(); 901 conversions.put(Byte .class, map); 902 903 map.put(String .class, new Converter<Byte , String >() 904 { 905 public String convert(Byte source) 906 { 907 return source.toString(); 908 } 909 910 }); 911 912 916 map = new HashMap <Class , Converter>(); 917 conversions.put(Short .class, map); 918 919 map.put(String .class, new Converter<Short , String >() 920 { 921 public String convert(Short source) 922 { 923 return source.toString(); 924 } 925 926 }); 927 928 932 map = new HashMap <Class , Converter>(); 933 conversions.put(Integer .class, map); 934 935 map.put(String .class, new Converter<Integer , String >() 936 { 937 public String convert(Integer source) 938 { 939 return source.toString(); 940 } 941 942 }); 943 944 948 map = new HashMap <Class , Converter>(); 949 conversions.put(Long .class, map); 950 951 map.put(String .class, new Converter<Long , String >() 952 { 953 public String convert(Long source) 954 { 955 return source.toString(); 956 } 957 958 }); 959 960 964 map = new HashMap <Class , Converter>(); 965 conversions.put(Float .class, map); 966 967 map.put(String .class, new Converter<Float , String >() 968 { 969 public String convert(Float source) 970 { 971 return source.toString(); 972 } 973 974 }); 975 976 980 map = new HashMap <Class , Converter>(); 981 conversions.put(Double .class, map); 982 983 map.put(String .class, new Converter<Double , String >() 984 { 985 public String convert(Double source) 986 { 987 return source.toString(); 988 } 989 990 }); 991 992 996 map = new HashMap <Class , Converter>(); 997 conversions.put(BigInteger .class, map); 998 999 map.put(String .class, new Converter<BigInteger , String >() 1000 { 1001 public String convert(BigInteger source) 1002 { 1003 return source.toString(); 1004 } 1005 1006 }); 1007 1008 1012 map = new HashMap <Class , Converter>(); 1013 conversions.put(BigDecimal .class, map); 1014 1015 map.put(String .class, new Converter<BigDecimal , String >() 1016 { 1017 public String convert(BigDecimal source) 1018 { 1019 return source.toString(); 1020 } 1021 1022 }); 1023 1024 1028 map = new HashMap <Class , Converter>(); 1029 conversions.put(QName.class, map); 1030 1031 map.put(String .class, new Converter<QName, String >() 1032 { 1033 public String convert(QName source) 1034 { 1035 return source.toString(); 1036 } 1037 1038 }); 1039 1040 1044 map = new HashMap <Class , Converter>(); 1045 conversions.put(NodeRef.class, map); 1046 1047 map.put(String .class, new Converter<NodeRef, String >() 1048 { 1049 public String convert(NodeRef source) 1050 { 1051 return source.toString(); 1052 } 1053 1054 }); 1055 1056 1060 map = new HashMap <Class , Converter>(); 1061 conversions.put(Path.class, map); 1062 1063 map.put(String .class, new Converter<Path, String >() 1064 { 1065 public String convert(Path source) 1066 { 1067 return source.toString(); 1068 } 1069 1070 }); 1071 1072 } 1073 1074 1076 1083 public static interface Converter<F, T> 1084 { 1085 public T convert(F source); 1086 } 1087 1088 1097 public static class TwoStageConverter<F, T, M> implements Converter<F, T> 1098 { 1099 Converter first; 1100 1101 Converter second; 1102 1103 TwoStageConverter(Converter first, Converter second) 1104 { 1105 this.first = first; 1106 this.second = second; 1107 } 1108 1109 public T convert(F source) 1110 { 1111 return (T) second.convert((M) first.convert(source)); 1112 } 1113 } 1114 1115} 1116 | Popular Tags |