1 17 package org.alfresco.service.cmr.repository.datatype; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.UnsupportedEncodingException ; 24 import java.math.BigDecimal ; 25 import java.math.BigInteger ; 26 import java.sql.Timestamp ; 27 import java.text.DecimalFormat ; 28 import java.text.ParseException ; 29 import java.util.Calendar ; 30 import java.util.Date ; 31 32 import org.alfresco.service.cmr.repository.ContentData; 33 import org.alfresco.service.cmr.repository.ContentReader; 34 import org.alfresco.service.cmr.repository.NodeRef; 35 import org.alfresco.service.cmr.repository.Path; 36 import org.alfresco.service.namespace.QName; 37 import org.alfresco.util.ISO8601DateFormat; 38 39 58 public class DefaultTypeConverter 59 { 60 61 64 public static TypeConverter INSTANCE = new TypeConverter(); 65 66 69 static 70 { 71 72 76 INSTANCE.addConverter(String .class, Boolean .class, new TypeConverter.Converter<String , Boolean >() 77 { 78 public Boolean convert(String source) 79 { 80 return Boolean.valueOf(source); 81 } 82 }); 83 84 INSTANCE.addConverter(String .class, Character .class, new TypeConverter.Converter<String , Character >() 85 { 86 public Character convert(String source) 87 { 88 if ((source == null) || (source.length() == 0)) 89 { 90 return null; 91 } 92 return Character.valueOf(source.charAt(0)); 93 } 94 }); 95 96 INSTANCE.addConverter(String .class, Number .class, new TypeConverter.Converter<String , Number >() 97 { 98 public Number convert(String source) 99 { 100 try 101 { 102 return DecimalFormat.getNumberInstance().parse(source); 103 } 104 catch (ParseException e) 105 { 106 throw new TypeConversionException("Failed to parse number " + source, e); 107 } 108 } 109 }); 110 111 INSTANCE.addConverter(String .class, Byte .class, new TypeConverter.Converter<String , Byte >() 112 { 113 public Byte convert(String source) 114 { 115 return Byte.valueOf(source); 116 } 117 }); 118 119 INSTANCE.addConverter(String .class, Short .class, new TypeConverter.Converter<String , Short >() 120 { 121 public Short convert(String source) 122 { 123 return Short.valueOf(source); 124 } 125 }); 126 127 INSTANCE.addConverter(String .class, Integer .class, new TypeConverter.Converter<String , Integer >() 128 { 129 public Integer convert(String source) 130 { 131 return Integer.valueOf(source); 132 } 133 }); 134 135 INSTANCE.addConverter(String .class, Long .class, new TypeConverter.Converter<String , Long >() 136 { 137 public Long convert(String source) 138 { 139 return Long.valueOf(source); 140 } 141 }); 142 143 INSTANCE.addConverter(String .class, Float .class, new TypeConverter.Converter<String , Float >() 144 { 145 public Float convert(String source) 146 { 147 return Float.valueOf(source); 148 } 149 }); 150 151 INSTANCE.addConverter(String .class, Double .class, new TypeConverter.Converter<String , Double >() 152 { 153 public Double convert(String source) 154 { 155 return Double.valueOf(source); 156 } 157 }); 158 159 INSTANCE.addConverter(String .class, BigInteger .class, new TypeConverter.Converter<String , BigInteger >() 160 { 161 public BigInteger convert(String source) 162 { 163 return new BigInteger (source); 164 } 165 }); 166 167 INSTANCE.addConverter(String .class, BigDecimal .class, new TypeConverter.Converter<String , BigDecimal >() 168 { 169 public BigDecimal convert(String source) 170 { 171 return new BigDecimal (source); 172 } 173 }); 174 175 INSTANCE.addConverter(String .class, Date .class, new TypeConverter.Converter<String , Date >() 176 { 177 public Date convert(String source) 178 { 179 Date date = ISO8601DateFormat.parse(source); 180 if (date == null) 181 { 182 throw new TypeConversionException("Failed to parse date " + source); 183 } 184 return date; 185 } 186 }); 187 188 INSTANCE.addConverter(String .class, Duration.class, new TypeConverter.Converter<String , Duration>() 189 { 190 public Duration convert(String source) 191 { 192 return new Duration(source); 193 } 194 }); 195 196 INSTANCE.addConverter(String .class, QName.class, new TypeConverter.Converter<String , QName>() 197 { 198 public QName convert(String source) 199 { 200 return QName.createQName(source); 201 } 202 }); 203 204 INSTANCE.addConverter(String .class, ContentData.class, new TypeConverter.Converter<String , ContentData>() 205 { 206 public ContentData convert(String source) 207 { 208 return ContentData.createContentProperty(source); 209 } 210 211 }); 212 213 INSTANCE.addConverter(String .class, NodeRef.class, new TypeConverter.Converter<String , NodeRef>() 214 { 215 public NodeRef convert(String source) 216 { 217 return new NodeRef(source); 218 } 219 220 }); 221 222 INSTANCE.addConverter(String .class, InputStream .class, new TypeConverter.Converter<String , InputStream >() 223 { 224 public InputStream convert(String source) 225 { 226 try 227 { 228 return new ByteArrayInputStream (source.getBytes("UTF-8")); 229 } 230 catch (UnsupportedEncodingException e) 231 { 232 throw new TypeConversionException("Encoding not supported", e); 233 } 234 } 235 }); 236 237 238 242 INSTANCE.addConverter(Enum .class, String .class, new TypeConverter.Converter<Enum , String >() 243 { 244 public String convert(Enum source) 245 { 246 return source.toString(); 247 } 248 }); 249 250 251 255 INSTANCE.addConverter(Number .class, Byte .class, new TypeConverter.Converter<Number , Byte >() 256 { 257 public Byte convert(Number source) 258 { 259 return Byte.valueOf(source.byteValue()); 260 } 261 }); 262 263 INSTANCE.addConverter(Number .class, Short .class, new TypeConverter.Converter<Number , Short >() 264 { 265 public Short convert(Number source) 266 { 267 return Short.valueOf(source.shortValue()); 268 } 269 }); 270 271 INSTANCE.addConverter(Number .class, Integer .class, new TypeConverter.Converter<Number , Integer >() 272 { 273 public Integer convert(Number source) 274 { 275 return Integer.valueOf(source.intValue()); 276 } 277 }); 278 279 INSTANCE.addConverter(Number .class, Long .class, new TypeConverter.Converter<Number , Long >() 280 { 281 public Long convert(Number source) 282 { 283 return Long.valueOf(source.longValue()); 284 } 285 }); 286 287 INSTANCE.addConverter(Number .class, Float .class, new TypeConverter.Converter<Number , Float >() 288 { 289 public Float convert(Number source) 290 { 291 return Float.valueOf(source.floatValue()); 292 } 293 }); 294 295 INSTANCE.addConverter(Number .class, Double .class, new TypeConverter.Converter<Number , Double >() 296 { 297 public Double convert(Number source) 298 { 299 return Double.valueOf(source.doubleValue()); 300 } 301 }); 302 303 INSTANCE.addConverter(Number .class, Date .class, new TypeConverter.Converter<Number , Date >() 304 { 305 public Date convert(Number source) 306 { 307 return new Date (source.longValue()); 308 } 309 }); 310 311 INSTANCE.addConverter(Number .class, String .class, new TypeConverter.Converter<Number , String >() 312 { 313 public String convert(Number source) 314 { 315 return source.toString(); 316 } 317 }); 318 319 INSTANCE.addConverter(Number .class, BigInteger .class, new TypeConverter.Converter<Number , BigInteger >() 320 { 321 public BigInteger convert(Number source) 322 { 323 if (source instanceof BigDecimal ) 324 { 325 return ((BigDecimal ) source).toBigInteger(); 326 } 327 else 328 { 329 return BigInteger.valueOf(source.longValue()); 330 } 331 } 332 }); 333 334 INSTANCE.addConverter(Number .class, BigDecimal .class, new TypeConverter.Converter<Number , BigDecimal >() 335 { 336 public BigDecimal convert(Number source) 337 { 338 if (source instanceof BigInteger ) 339 { 340 return new BigDecimal ((BigInteger ) source); 341 } 342 else 343 { 344 return BigDecimal.valueOf(source.longValue()); 345 } 346 } 347 }); 348 349 INSTANCE.addDynamicTwoStageConverter(Number .class, String .class, InputStream .class); 350 351 355 INSTANCE.addConverter(Timestamp .class, Date .class, new TypeConverter.Converter<Timestamp , Date >() 356 { 357 public Date convert(Timestamp source) 358 { 359 return new Date (source.getTime()); 360 } 361 }); 362 363 INSTANCE.addConverter(Date .class, Number .class, new TypeConverter.Converter<Date , Number >() 364 { 365 public Number convert(Date source) 366 { 367 return Long.valueOf(source.getTime()); 368 } 369 }); 370 371 INSTANCE.addConverter(Date .class, String .class, new TypeConverter.Converter<Date , String >() 372 { 373 public String convert(Date source) 374 { 375 return ISO8601DateFormat.format(source); 376 } 377 }); 378 379 INSTANCE.addConverter(Date .class, Calendar .class, new TypeConverter.Converter<Date , Calendar >() 380 { 381 public Calendar convert(Date source) 382 { 383 Calendar calendar = Calendar.getInstance(); 384 calendar.setTime(source); 385 return calendar; 386 } 387 }); 388 389 INSTANCE.addDynamicTwoStageConverter(Date .class, String .class, InputStream .class); 390 391 395 INSTANCE.addConverter(Boolean .class, String .class, new TypeConverter.Converter<Boolean , String >() 396 { 397 public String convert(Boolean source) 398 { 399 return source.toString(); 400 } 401 }); 402 403 INSTANCE.addDynamicTwoStageConverter(Boolean .class, String .class, InputStream .class); 404 405 409 INSTANCE.addConverter(Character .class, String .class, new TypeConverter.Converter<Character , String >() 410 { 411 public String convert(Character source) 412 { 413 return source.toString(); 414 } 415 }); 416 417 INSTANCE.addDynamicTwoStageConverter(Character .class, String .class, InputStream .class); 418 419 423 INSTANCE.addConverter(Duration.class, String .class, new TypeConverter.Converter<Duration, String >() 424 { 425 public String convert(Duration source) 426 { 427 return source.toString(); 428 } 429 430 }); 431 432 INSTANCE.addDynamicTwoStageConverter(Duration.class, String .class, InputStream .class); 433 434 438 INSTANCE.addConverter(Byte .class, String .class, new TypeConverter.Converter<Byte , String >() 439 { 440 public String convert(Byte source) 441 { 442 return source.toString(); 443 } 444 }); 445 446 INSTANCE.addDynamicTwoStageConverter(Byte .class, String .class, InputStream .class); 447 448 452 INSTANCE.addConverter(Short .class, String .class, new TypeConverter.Converter<Short , String >() 453 { 454 public String convert(Short source) 455 { 456 return source.toString(); 457 } 458 }); 459 460 INSTANCE.addDynamicTwoStageConverter(Short .class, String .class, InputStream .class); 461 462 466 INSTANCE.addConverter(Integer .class, String .class, new TypeConverter.Converter<Integer , String >() 467 { 468 public String convert(Integer source) 469 { 470 return source.toString(); 471 } 472 }); 473 474 INSTANCE.addDynamicTwoStageConverter(Integer .class, String .class, InputStream .class); 475 476 480 INSTANCE.addConverter(Long .class, String .class, new TypeConverter.Converter<Long , String >() 481 { 482 public String convert(Long source) 483 { 484 return source.toString(); 485 } 486 }); 487 488 INSTANCE.addDynamicTwoStageConverter(Long .class, String .class, InputStream .class); 489 490 494 INSTANCE.addConverter(Float .class, String .class, new TypeConverter.Converter<Float , String >() 495 { 496 public String convert(Float source) 497 { 498 return source.toString(); 499 } 500 }); 501 502 INSTANCE.addDynamicTwoStageConverter(Float .class, String .class, InputStream .class); 503 504 508 INSTANCE.addConverter(Double .class, String .class, new TypeConverter.Converter<Double , String >() 509 { 510 public String convert(Double source) 511 { 512 return source.toString(); 513 } 514 }); 515 516 INSTANCE.addDynamicTwoStageConverter(Double .class, String .class, InputStream .class); 517 518 522 INSTANCE.addConverter(BigInteger .class, String .class, new TypeConverter.Converter<BigInteger , String >() 523 { 524 public String convert(BigInteger source) 525 { 526 return source.toString(); 527 } 528 }); 529 530 INSTANCE.addDynamicTwoStageConverter(BigInteger .class, String .class, InputStream .class); 531 532 536 INSTANCE.addConverter(Calendar .class, Date .class, new TypeConverter.Converter<Calendar , Date >() 537 { 538 public Date convert(Calendar source) 539 { 540 return source.getTime(); 541 } 542 }); 543 544 INSTANCE.addConverter(Calendar .class, String .class, new TypeConverter.Converter<Calendar , String >() 545 { 546 public String convert(Calendar source) 547 { 548 return ISO8601DateFormat.format(source.getTime()); 549 } 550 }); 551 552 556 INSTANCE.addConverter(BigDecimal .class, String .class, new TypeConverter.Converter<BigDecimal , String >() 557 { 558 public String convert(BigDecimal source) 559 { 560 return source.toString(); 561 } 562 }); 563 564 INSTANCE.addDynamicTwoStageConverter(BigDecimal .class, String .class, InputStream .class); 565 566 570 INSTANCE.addConverter(QName.class, String .class, new TypeConverter.Converter<QName, String >() 571 { 572 public String convert(QName source) 573 { 574 return source.toString(); 575 } 576 }); 577 578 INSTANCE.addDynamicTwoStageConverter(QName.class, String .class, InputStream .class); 579 580 584 INSTANCE.addConverter(NodeRef.class, String .class, new TypeConverter.Converter<NodeRef, String >() 585 { 586 public String convert(NodeRef source) 587 { 588 return source.toString(); 589 } 590 }); 591 592 INSTANCE.addDynamicTwoStageConverter(NodeRef.class, String .class, InputStream .class); 593 594 598 INSTANCE.addConverter(ContentData.class, String .class, new TypeConverter.Converter<ContentData, String >() 599 { 600 public String convert(ContentData source) 601 { 602 return source.toString(); 603 } 604 }); 605 606 INSTANCE.addDynamicTwoStageConverter(ContentData.class, String .class, InputStream .class); 607 608 612 INSTANCE.addConverter(Path.class, String .class, new TypeConverter.Converter<Path, String >() 613 { 614 public String convert(Path source) 615 { 616 return source.toString(); 617 } 618 }); 619 620 INSTANCE.addDynamicTwoStageConverter(Path.class, String .class, InputStream .class); 621 622 626 INSTANCE.addConverter(ContentReader.class, InputStream .class, new TypeConverter.Converter<ContentReader, InputStream >() 627 { 628 public InputStream convert(ContentReader source) 629 { 630 return source.getContentInputStream(); 631 } 632 }); 633 634 INSTANCE.addConverter(ContentReader.class, String .class, new TypeConverter.Converter<ContentReader, String >() 635 { 636 public String convert(ContentReader source) 637 { 638 String encoding = source.getEncoding(); 639 if (encoding == null || !encoding.equals("UTF-8")) 640 { 641 throw new TypeConversionException("Cannot convert non UTF-8 streams to String."); 642 } 643 644 646 return source.getContentString(); 647 } 648 }); 649 650 INSTANCE.addDynamicTwoStageConverter(ContentReader.class, String .class, Date .class); 651 652 INSTANCE.addDynamicTwoStageConverter(ContentReader.class, String .class, Double .class); 653 654 INSTANCE.addDynamicTwoStageConverter(ContentReader.class, String .class, Long .class); 655 656 INSTANCE.addDynamicTwoStageConverter(ContentReader.class, String .class, Boolean .class); 657 658 INSTANCE.addDynamicTwoStageConverter(ContentReader.class, String .class, QName.class); 659 660 INSTANCE.addDynamicTwoStageConverter(ContentReader.class, String .class, Path.class); 661 662 INSTANCE.addDynamicTwoStageConverter(ContentReader.class, String .class, NodeRef.class); 663 664 668 INSTANCE.addConverter(InputStream .class, String .class, new TypeConverter.Converter<InputStream , String >() 669 { 670 public String convert(InputStream source) 671 { 672 try 673 { 674 ByteArrayOutputStream out = new ByteArrayOutputStream (); 675 byte[] buffer = new byte[8192]; 676 int read; 677 while ((read = source.read(buffer)) > 0) 678 { 679 out.write(buffer, 0, read); 680 } 681 byte[] data = out.toByteArray(); 682 return new String (data, "UTF-8"); 683 } 684 catch (UnsupportedEncodingException e) 685 { 686 throw new TypeConversionException("Cannot convert input stream to String.", e); 687 } 688 catch (IOException e) 689 { 690 throw new TypeConversionException("Conversion from stream to string failed", e); 691 } 692 finally 693 { 694 if (source != null) 695 { 696 try { source.close(); } catch(IOException e) {}; 697 } 698 } 699 } 700 }); 701 702 INSTANCE.addDynamicTwoStageConverter(InputStream .class, String .class, Date .class); 703 704 INSTANCE.addDynamicTwoStageConverter(InputStream .class, String .class, Double .class); 705 706 INSTANCE.addDynamicTwoStageConverter(InputStream .class, String .class, Long .class); 707 708 INSTANCE.addDynamicTwoStageConverter(InputStream .class, String .class, Boolean .class); 709 710 INSTANCE.addDynamicTwoStageConverter(InputStream .class, String .class, QName.class); 711 712 INSTANCE.addDynamicTwoStageConverter(InputStream .class, String .class, Path.class); 713 714 INSTANCE.addDynamicTwoStageConverter(InputStream .class, String .class, NodeRef.class); 715 716 } 717 718 } 719 | Popular Tags |