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 839 public boolean equals(Object o) { 840 return ((o instanceof DataFlavor ) && equals((DataFlavor )o)); 841 } 842 843 863 public boolean equals(DataFlavor that) { 864 if (that == null) { 865 return false; 866 } 867 if (this == that) { 868 return true; 869 } 870 871 if (representationClass == null) { 872 if (that.getRepresentationClass() != null) { 873 return false; 874 } 875 } else { 876 if (!representationClass.equals(that.getRepresentationClass())) { 877 return false; 878 } 879 } 880 881 if (mimeType == null) { 882 if (that.mimeType != null) { 883 return false; 884 } 885 } else { 886 if (!mimeType.match(that.mimeType)) { 887 return false; 888 } 889 890 if ("text".equals(getPrimaryType()) && 891 DataTransferer.doesSubtypeSupportCharset(this) && 892 representationClass != null && 893 !(isRepresentationClassReader() || 894 String .class.equals(representationClass) || 895 isRepresentationClassCharBuffer() || 896 DataTransferer.charArrayClass.equals(representationClass))) 897 { 898 String thisCharset = 899 DataTransferer.canonicalName(getParameter("charset")); 900 String thatCharset = 901 DataTransferer.canonicalName(that.getParameter("charset")); 902 if (thisCharset == null) { 903 if (thatCharset != null) { 904 return false; 905 } 906 } else { 907 if (!thisCharset.equals(thatCharset)) { 908 return false; 909 } 910 } 911 } 912 } 913 914 return true; 915 } 916 917 928 @Deprecated 929 public boolean equals(String s) { 930 if (s == null || mimeType == null) 931 return false; 932 return isMimeTypeEqual(s); 933 } 934 935 945 public int hashCode() { 946 int total = 0; 947 948 if (representationClass != null) { 949 total += representationClass.hashCode(); 950 } 951 952 if (mimeType != null) { 953 String primaryType = mimeType.getPrimaryType(); 954 if (primaryType != null) { 955 total += primaryType.hashCode(); 956 } 957 958 962 if ("text".equals(primaryType) && 963 DataTransferer.doesSubtypeSupportCharset(this) && 964 representationClass != null && 965 !(isRepresentationClassReader() || 966 String .class.equals(representationClass) || 967 isRepresentationClassCharBuffer() || 968 DataTransferer.charArrayClass.equals 969 (representationClass))) 970 { 971 String charset = 972 DataTransferer.canonicalName(getParameter("charset")); 973 if (charset != null) { 974 total += charset.hashCode(); 975 } 976 } 977 } 978 979 return total; 980 } 981 982 1002 public boolean match(DataFlavor that) { 1003 return equals(that); 1004 } 1005 1006 1017 public boolean isMimeTypeEqual(String mimeType) { 1018 if (mimeType == null) { 1020 throw new NullPointerException ("mimeType"); 1021 } 1022 if (this.mimeType == null) { 1023 return false; 1024 } 1025 try { 1026 return this.mimeType.match(new MimeType (mimeType)); 1027 } catch (MimeTypeParseException mtpe) { 1028 return false; 1029 } 1030 } 1031 1032 1040 1041 public final boolean isMimeTypeEqual(DataFlavor dataFlavor) { 1042 return isMimeTypeEqual(dataFlavor.mimeType); 1043 } 1044 1045 1052 1053 private boolean isMimeTypeEqual(MimeType mtype) { 1054 if (this.mimeType == null) { 1055 return (mtype == null); 1056 } 1057 return mimeType.match(mtype); 1058 } 1059 1060 1063 1064 public boolean isMimeTypeSerializedObject() { 1065 return isMimeTypeEqual(javaSerializedObjectMimeType); 1066 } 1067 1068 public final Class <?> getDefaultRepresentationClass() { 1069 return ioInputStreamClass; 1070 } 1071 1072 public final String getDefaultRepresentationClassAsString() { 1073 return getDefaultRepresentationClass().getName(); 1074 } 1075 1076 1080 1081 public boolean isRepresentationClassInputStream() { 1082 return ioInputStreamClass.isAssignableFrom(representationClass); 1083 } 1084 1085 1092 public boolean isRepresentationClassReader() { 1093 return java.io.Reader .class.isAssignableFrom(representationClass); 1094 } 1095 1096 1103 public boolean isRepresentationClassCharBuffer() { 1104 return java.nio.CharBuffer .class.isAssignableFrom(representationClass); 1105 } 1106 1107 1114 public boolean isRepresentationClassByteBuffer() { 1115 return java.nio.ByteBuffer .class.isAssignableFrom(representationClass); 1116 } 1117 1118 1122 1123 public boolean isRepresentationClassSerializable() { 1124 return java.io.Serializable .class.isAssignableFrom(representationClass); 1125 } 1126 1127 1131 1132 public boolean isRepresentationClassRemote() { 1133 return java.rmi.Remote .class.isAssignableFrom(representationClass); 1134 } 1135 1136 1142 1143 public boolean isFlavorSerializedObjectType() { 1144 return isRepresentationClassSerializable() && isMimeTypeEqual(javaSerializedObjectMimeType); 1145 } 1146 1147 1153 1154 public boolean isFlavorRemoteObjectType() { 1155 return isRepresentationClassRemote() 1156 && isRepresentationClassSerializable() 1157 && isMimeTypeEqual(javaRemoteObjectMimeType); 1158 } 1159 1160 1161 1167 1168 public boolean isFlavorJavaFileListType() { 1169 if (mimeType == null || representationClass == null) 1170 return false; 1171 return java.util.List .class.isAssignableFrom(representationClass) && 1172 mimeType.match(javaFileListFlavor.mimeType); 1173 1174 } 1175 1176 1206 public boolean isFlavorTextType() { 1207 return (DataTransferer.isFlavorCharsetTextType(this) || 1208 DataTransferer.isFlavorNoncharsetTextType(this)); 1209 } 1210 1211 1214 1215 public synchronized void writeExternal(ObjectOutput os) throws IOException { 1216 if (mimeType != null) { 1217 mimeType.setParameter("humanPresentableName", humanPresentableName); 1218 os.writeObject(mimeType); 1219 mimeType.removeParameter("humanPresentableName"); 1220 } else { 1221 os.writeObject(null); 1222 } 1223 1224 os.writeObject(representationClass); 1225 } 1226 1227 1230 1231 public synchronized void readExternal(ObjectInput is) throws IOException , ClassNotFoundException { 1232 String rcn = null; 1233 mimeType = (MimeType )is.readObject(); 1234 1235 if (mimeType != null) { 1236 humanPresentableName = 1237 mimeType.getParameter("humanPresentableName"); 1238 mimeType.removeParameter("humanPresentableName"); 1239 rcn = mimeType.getParameter("class"); 1240 if (rcn == null) { 1241 throw new IOException("no class parameter specified in: " + 1242 mimeType); 1243 } 1244 } 1245 1246 try { 1247 representationClass = (Class )is.readObject(); 1248 } catch (OptionalDataException ode) { 1249 if (!ode.eof || ode.length != 0) { 1250 throw ode; 1251 } 1252 if (rcn != null) { 1255 representationClass = 1256 DataFlavor.tryToLoadClass(rcn, getClass().getClassLoader()); 1257 } 1258 } 1259 } 1260 1261 1265 1266 public Object clone() throws CloneNotSupportedException { 1267 Object newObj = super.clone(); 1268 if (mimeType != null) { 1269 ((DataFlavor )newObj).mimeType = (MimeType )mimeType.clone(); 1270 } 1271 return newObj; 1272 } 1274 1288 @Deprecated 1289 protected String normalizeMimeTypeParameter(String parameterName, String parameterValue) { 1290 return parameterValue; 1291 } 1292 1293 1304 @Deprecated 1305 protected String normalizeMimeType(String mimeType) { 1306 return mimeType; 1307 } 1308 1309 1312 1313 1314 1315 transient int atom; 1316 1317 1318 1319 MimeType mimeType; 1320 1321 private String humanPresentableName; 1322 1323 1324 1325 private Class representationClass; 1326 1327} | Popular Tags |