| 1 7 8 package java.awt.datatransfer; 9 10 import java.awt.Toolkit ; 11 import java.io.*; 12 import java.nio.*; 13 import java.util.*; 14 15 import sun.awt.datatransfer.DataTransferer; 16 17 34 public class DataFlavor implements Externalizable, Cloneable { 35 36 private static final long serialVersionUID = 8367026044764648243L; 37 private static final Class ioInputStreamClass = java.io.InputStream .class; 38 39 48 protected final static Class <?> tryToLoadClass(String className, 49 ClassLoader fallback) 50 throws ClassNotFoundException  51 { 52 ClassLoader systemClassLoader = (ClassLoader ) 53 java.security.AccessController.doPrivileged( 54 new java.security.PrivilegedAction () { 55 public Object run() { 56 ClassLoader cl = Thread.currentThread(). 57 getContextClassLoader(); 58 return (cl != null) 59 ? cl 60 : ClassLoader.getSystemClassLoader(); 61 } 62 }); 63 64 try { 65 return Class.forName(className, true, systemClassLoader); 66 } catch (ClassNotFoundException e2) { 67 if (fallback != null) { 68 return Class.forName(className, true, fallback); 69 } else { 70 throw new ClassNotFoundException (className); 71 } 72 } 73 } 74 75 78 static private DataFlavor createConstant(Class rc, String prn) { 79 try { 80 return new DataFlavor (rc, prn); 81 } catch (Exception e) { 82 return null; 83 } 84 } 85 86 89 static private DataFlavor createConstant(String mt, String prn) { 90 try { 91 return new DataFlavor (mt, prn); 92 } catch (Exception e) { 93 return null; 94 } 95 } 96 97 105 public static final DataFlavor stringFlavor = createConstant(java.lang.String .class, "Unicode String"); 106 107 115 public static final DataFlavor imageFlavor = createConstant("image/x-java-image; class=java.awt.Image", "Image"); 116 117 133 @Deprecated  134 public static final DataFlavor plainTextFlavor = createConstant("text/plain; charset=unicode; class=java.io.InputStream", "Plain Text"); 135 136 144 public static final String javaSerializedObjectMimeType = "application/x-java-serialized-object"; 145 146 153 public static final DataFlavor javaFileListFlavor = createConstant("application/x-java-file-list;class=java.util.List", null); 154 155 168 public static final String javaJVMLocalObjectMimeType = "application/x-java-jvm-local-objectref"; 169 170 178 public static final String javaRemoteObjectMimeType = "application/x-java-remote-object"; 179 180 188 public DataFlavor() { 189 super(); 190 } 191 192 198 private DataFlavor(String primaryType, String subType, MimeTypeParameterList params, Class representationClass, String humanPresentableName) { 199 super(); 200 if (primaryType == null) { 201 throw new NullPointerException ("primaryType"); 202 } 203 if (subType == null) { 204 throw new NullPointerException ("subType"); 205 } 206 if (representationClass == null) { 207 throw new NullPointerException ("representationClass"); 208 } 209 210 if (params == null) params = new MimeTypeParameterList (); 211 212 params.set("class", representationClass.getName()); 213 214 if (humanPresentableName == null) { 215 humanPresentableName = (String )params.get("humanPresentableName"); 216 217 if (humanPresentableName == null) 218 humanPresentableName = primaryType + "/" + subType; 219 } 220 221 try { 222 mimeType = new MimeType (primaryType, subType, params); 223 } catch (MimeTypeParseException mtpe) { 224 throw new IllegalArgumentException ("MimeType Parse Exception: " + mtpe.getMessage()); 225 } 226 227 this.representationClass = representationClass; 228 this.humanPresentableName = humanPresentableName; 229 230 mimeType.removeParameter("humanPresentableName"); 231 } 232 233 248 public DataFlavor(Class <?> representationClass, String humanPresentableName) { 249 this("application", "x-java-serialized-object", null, representationClass, humanPresentableName); 250 if (representationClass == null) { 251 throw new NullPointerException ("representationClass"); 252 } 253 } 254 255 284 public DataFlavor(String mimeType, String humanPresentableName) { 285 super(); 286 if (mimeType == null) { 287 throw new NullPointerException ("mimeType"); 288 } 289 try { 290 initialize(mimeType, humanPresentableName, this.getClass().getClassLoader()); 291 } catch (MimeTypeParseException mtpe) { 292 throw new IllegalArgumentException ("failed to parse:" + mimeType); 293 } catch (ClassNotFoundException cnfe) { 294 throw new IllegalArgumentException ("can't find specified class: " + cnfe.getMessage()); 295 } 296 } 297 298 324 public DataFlavor(String mimeType, String humanPresentableName, ClassLoader classLoader) throws ClassNotFoundException { 325 super(); 326 if (mimeType == null) { 327 throw new NullPointerException ("mimeType"); 328 } 329 try { 330 initialize(mimeType, humanPresentableName, classLoader); 331 } catch (MimeTypeParseException mtpe) { 332 throw new IllegalArgumentException ("failed to parse:" + mimeType); 333 } 334 } 335 336 352 public DataFlavor(String mimeType) throws ClassNotFoundException { 353 super(); 354 if (mimeType == null) { 355 throw new NullPointerException ("mimeType"); 356 } 357 try { 358 initialize(mimeType, null, this.getClass().getClassLoader()); 359 } catch (MimeTypeParseException mtpe) { 360 throw new IllegalArgumentException ("failed to parse:" + mimeType); 361 } 362 } 363 364 378 private void initialize(String mimeType, String humanPresentableName, ClassLoader classLoader) throws MimeTypeParseException , ClassNotFoundException { 379 if (mimeType == null) { 380 throw new NullPointerException ("mimeType"); 381 } 382 383 this.mimeType = new MimeType (mimeType); 385 String rcn = getParameter("class"); 386 387 if (rcn == null) { 388 if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType())) 389 390 throw new IllegalArgumentException ("no representation class specified for:" + mimeType); 391 else 392 representationClass = java.io.InputStream .class; } else { representationClass = DataFlavor.tryToLoadClass(rcn, classLoader); 395 } 396 397 this.mimeType.setParameter("class", representationClass.getName()); 398 399 if (humanPresentableName == null) { 400 humanPresentableName = this.mimeType.getParameter("humanPresentableName"); 401 if (humanPresentableName == null) 402 humanPresentableName = this.mimeType.getPrimaryType() + "/" + this.mimeType.getSubType(); 403 } 404 405 this.humanPresentableName = humanPresentableName; 407 this.mimeType.removeParameter("humanPresentableName"); } 409 410 422 public String toString() { 423 String string = getClass().getName(); 424 string += "["+paramString()+"]"; 425 return string; 426 } 427 428 private String paramString() { 429 String params = ""; 430 params += "mimetype="; 431 if (mimeType == null) { 432 params += "null"; 433 } else { 434 params += mimeType.getBaseType(); 435 } 436 params += ";representationclass="; 437 if (representationClass == null) { 438 params += "null"; 439 } else { 440 params += representationClass.getName(); 441 } 442 if (DataTransferer.isFlavorCharsetTextType(this) && 443 (isRepresentationClassInputStream() || 444 isRepresentationClassByteBuffer() || 445 DataTransferer.byteArrayClass.equals(representationClass))) 446 { 447 params += ";charset=" + DataTransferer.getTextCharset(this); 448 } 449 return params; 450 } 451 452 467 public static final DataFlavor getTextPlainUnicodeFlavor() { 468 String encoding = null; 469 DataTransferer transferer = DataTransferer.getInstance(); 470 if (transferer != null) { 471 encoding = transferer.getDefaultUnicodeEncoding(); 472 } 473 return new DataFlavor ( 474 "text/plain;charset="+encoding 475 +";class=java.io.InputStream", "Plain Text"); 476 } 477 478 594 public static final DataFlavor selectBestTextFlavor( 595 DataFlavor [] availableFlavors) { 596 if (availableFlavors == null || availableFlavors.length == 0) { 597 return null; 598 } 599 600 if (textFlavorComparator == null) { 601 textFlavorComparator = new TextFlavorComparator(); 602 } 603 604 DataFlavor bestFlavor = 605 (DataFlavor )Collections.max(Arrays.asList(availableFlavors), 606 textFlavorComparator); 607 608 if (!bestFlavor.isFlavorTextType()) { 609 return null; 610 } 611 612 return bestFlavor; 613 } 614 615 private static Comparator textFlavorComparator; 616 617 static class TextFlavorComparator 618 extends DataTransferer.DataFlavorComparator { 619 620 640 public int compare(Object obj1, Object obj2) { 641 DataFlavor flavor1 = (DataFlavor )obj1; 642 DataFlavor flavor2 = (DataFlavor )obj2; 643 644 if (flavor1.isFlavorTextType()) { 645 if (flavor2.isFlavorTextType()) { 646 return super.compare(obj1, obj2); 647 } else { 648 return 1; 649 } 650 } else if (flavor2.isFlavorTextType()) { 651 return -1; 652 } else { 653 return 0; 654 } 655 } 656 } 657 658 698 public Reader getReaderForText(Transferable transferable) 699 throws UnsupportedFlavorException , IOException 700 { 701 Object transferObject = transferable.getTransferData(this); 702 if (transferObject == null) { 703 throw new IllegalArgumentException  704 ("getTransferData() returned null"); 705 } 706 707 if (transferObject instanceof Reader) { 708 return (Reader)transferObject; 709 } else if (transferObject instanceof String ) { 710 return new StringReader((String )transferObject); 711 } else if (transferObject instanceof CharBuffer) { 712 CharBuffer buffer = (CharBuffer)transferObject; 713 int size = buffer.remaining(); 714 char[] chars = new char[size]; 715 buffer.get(chars, 0, size); 716 return new CharArrayReader(chars); 717 } else if (transferObject instanceof char[]) { 718 return new CharArrayReader((char[])transferObject); 719 } 720 721 InputStream stream = null; 722 723 if (transferObject instanceof InputStream) { 724 stream = (InputStream)transferObject; 725 } else if (transferObject instanceof ByteBuffer) { 726 ByteBuffer buffer = (ByteBuffer)transferObject; 727 int size = buffer.remaining(); 728 byte[] bytes = new byte[size]; 729 buffer.get(bytes, 0, size); 730 stream = new ByteArrayInputStream(bytes); 731 } else if (transferObject instanceof byte[]) { 732 stream = new ByteArrayInputStream((byte[])transferObject); 733 } 734 735 if (stream == null) { 736 throw new IllegalArgumentException ("transfer data is not Reader, String, CharBuffer, char array, InputStream, ByteBuffer, or byte array"); 737 } 738 739 String encoding = getParameter("charset"); 740 return (encoding == null) 741 ? new InputStreamReader(stream) 742 : new InputStreamReader(stream, encoding); 743 } 744 745 749 public String getMimeType() { 750 return (mimeType != null) ? mimeType.toString() : null; 751 } 752 753 761 public Class <?> getRepresentationClass() { 762 return representationClass; 763 } 764 765 772 public String getHumanPresentableName() { 773 return humanPresentableName; 774 } 775 776 780 public String getPrimaryType() { 781 return (mimeType != null) ? mimeType.getPrimaryType() : null; 782 } 783 784 788 public String getSubType() { 789 return (mimeType != null) ? mimeType.getSubType() : null; 790 } 791 792 801 public String getParameter(String paramName) { 802 if (paramName.equals("humanPresentableName")) { 803 return humanPresentableName; 804 } else { 805 return (mimeType != null) 806 ? mimeType.getParameter(paramName) : null; 807 } 808 } 809 810 816 public void setHumanPresentableName(String humanPresentableName) { 817 this.humanPresentableName = humanPresentableName; 818 } 819 820 |