1 /* DataFlavor.java -- A type of data to transfer via the clipboard.
2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java
.awt
.datatransfer
;
41 import java
.io
.ByteArrayInputStream
;
42 import java
.io
.InputStream
;
43 import java
.io
.InputStreamReader
;
44 import java
.io
.IOException
;
45 import java
.io
.ObjectOutput
;
46 import java
.io
.ObjectInput
;
47 import java
.io
.Reader
;
48 import java
.io
.StringReader
;
49 import java
.io
.UnsupportedEncodingException
;
50 import java
.nio
.ByteBuffer
;
51 import java
.nio
.CharBuffer
;
54 * This class represents a particular data format used for transferring
55 * data via the clipboard.
57 * @author Aaron M. Renn (arenn@urbanophile.com)
59 public class DataFlavor
implements java
.io
.Externalizable
, Cloneable
61 static final long serialVersionUID
= 8367026044764648243L;
63 // FIXME: Serialization: Need to write methods for.
70 * This is the data flavor used for tranferring plain text. The MIME
71 * type is "text/plain; charset=unicode". The representation class
72 * is <code>java.io.InputStream</code>.
74 * @deprecated The charset unicode is platform specific and InputStream
75 * deals with bytes not chars. Use <code>getRederForText()</code>.
77 public static final DataFlavor plainTextFlavor
;
80 * This is the data flavor used for transferring Java strings. The
81 * MIME type is "application/x-java-serialized-object" and the
82 * representation class is <code>java.lang.String</code>.
84 public static final DataFlavor stringFlavor
;
87 * This is a data flavor used for transferring lists of files. The
88 * representation type is a <code>java.util.List</code>, with each element of
89 * the list being a <code>java.io.File</code>.
91 public static final DataFlavor javaFileListFlavor
;
93 public static final DataFlavor imageFlavor
;
96 * This is the MIME type used for transferring a serialized object.
97 * The representation class is the type of object be deserialized.
99 public static final String javaSerializedObjectMimeType
=
100 "application/x-java-serialized-object";
103 * This is the MIME type used to transfer a Java object reference within
104 * the same JVM. The representation class is the class of the object
107 public static final String javaJVMLocalObjectMimeType
=
108 "application/x-java-jvm-local-object";
111 * This is the MIME type used to transfer a link to a remote object.
112 * The representation class is the type of object being linked to.
114 public static final String javaRemoteObjectMimeType
=
115 "application/x-java-remote-object";
120 = new DataFlavor(java
.io
.InputStream
.class,
121 "text/plain; charset=unicode",
122 "plain unicode text");
125 = new DataFlavor(java
.lang
.String
.class,
126 "Java Unicode String");
129 = new DataFlavor(java
.util
.List
.class,
132 // javaFileListFlavor.mimeType = "application/x-java-file-list";
135 = new DataFlavor(java
.awt
.Image
.class,
139 /*************************************************************************/
145 // The MIME type for this flavor
146 private final String mimeType
;
148 // The representation class for this flavor
149 private final Class representationClass
;
151 // The human readable name of this flavor
152 private String humanPresentableName
;
154 /*************************************************************************/
161 * This method attempts to load the named class. The following class
162 * loaders are searched in order: the bootstrap class loader, the
163 * system class loader, the context class loader (if it exists), and
164 * the specified fallback class loader.
166 * @param className The name of the class to load.
167 * @param classLoader The class loader to use if all others fail, which
168 * may be <code>null</code>.
170 * @exception ClassNotFoundException If the class cannot be loaded.
172 protected static final Class
173 tryToLoadClass(String className
, ClassLoader classLoader
)
174 throws ClassNotFoundException
178 return(Class
.forName(className
));
180 catch(Exception e
) { ; }
181 // Commented out for Java 1.1
185 return(className.getClass().getClassLoader().findClass(className));
187 catch(Exception e) { ; }
191 return(ClassLoader.getSystemClassLoader().findClass(className));
193 catch(Exception e) { ; }
196 // FIXME: What is the context class loader?
201 catch(Exception e) { ; }
204 if (classLoader
!= null)
205 return(classLoader
.loadClass(className
));
207 throw new ClassNotFoundException(className
);
210 /*************************************************************************/
217 * Empty public constructor needed for externalization.
218 * Should not be used for normal instantiation.
224 representationClass
= null;
225 humanPresentableName
= null;
228 /*************************************************************************/
231 * Private constructor.
234 DataFlavor(Class representationClass
,
236 String humanPresentableName
)
238 this.representationClass
= representationClass
;
239 this.mimeType
= mimeType
;
240 if (humanPresentableName
!= null)
241 this.humanPresentableName
= humanPresentableName
;
243 this.humanPresentableName
= mimeType
;
246 /*************************************************************************/
249 * Initializes a new instance of <code>DataFlavor</code>. The class
250 * and human readable name are specified, the MIME type will be
251 * "application/x-java-serialized-object". If the human readable name
252 * is not specified (<code>null</code>) then the human readable name
253 * will be the same as the MIME type.
255 * @param representationClass The representation class for this object.
256 * @param humanPresentableName The display name of the object.
259 DataFlavor(Class representationClass
, String humanPresentableName
)
261 this(representationClass
,
262 "application/x-java-serialized-object"
264 + representationClass
.getName(),
265 humanPresentableName
);
268 /*************************************************************************/
271 * Initializes a new instance of <code>DataFlavor</code> with the
272 * specified MIME type and description. If the MIME type has a
273 * "class=<rep class>" parameter then the representation class will
274 * be the class name specified. Otherwise the class defaults to
275 * <code>java.io.InputStream</code>. If the human readable name
276 * is not specified (<code>null</code>) then the human readable name
277 * will be the same as the MIME type.
279 * @param mimeType The MIME type for this flavor.
280 * @param humanPresentableName The display name of this flavor.
281 * @param classLoader The class loader for finding classes if the default
282 * class loaders do not work.
284 * @exception IllegalArgumentException If the representation class
285 * specified cannot be loaded.
286 * @exception ClassNotFoundException If the class is not loaded.
289 DataFlavor(String mimeType
, String humanPresentableName
,
290 ClassLoader classLoader
) throws ClassNotFoundException
292 this(getRepresentationClassFromMime(mimeType
, classLoader
),
293 mimeType
, humanPresentableName
);
297 getRepresentationClassFromMime(String mimeString
, ClassLoader classLoader
)
299 String classname
= getParameter("class", mimeString
);
300 if (classname
!= null)
304 return tryToLoadClass(classname
, classLoader
);
308 throw new IllegalArgumentException("classname: " + e
.getMessage());
313 return java
.io
.InputStream
.class;
317 /*************************************************************************/
320 * Initializes a new instance of <code>DataFlavor</code> with the
321 * specified MIME type and description. If the MIME type has a
322 * "class=<rep class>" parameter then the representation class will
323 * be the class name specified. Otherwise the class defaults to
324 * <code>java.io.InputStream</code>. If the human readable name
325 * is not specified (<code>null</code>) then the human readable name
326 * will be the same as the MIME type. This is the same as calling
327 * <code>new DataFlavor(mimeType, humanPresentableName, null)</code>.
329 * @param mimeType The MIME type for this flavor.
330 * @param humanPresentableName The display name of this flavor.
331 * @param classLoader The class loader for finding classes.
333 * @exception IllegalArgumentException If the representation class
334 * specified cannot be loaded.
337 DataFlavor(String mimeType
, String humanPresentableName
)
339 this (getRepresentationClassFromMime (mimeType
, null), humanPresentableName
);
342 /*************************************************************************/
345 * Initializes a new instance of <code>DataFlavor</code> with the specified
346 * MIME type. This type can have a "class=" parameter to specify the
347 * representation class, and then the class must exist or an exception will
348 * be thrown. If there is no "class=" parameter then the representation class
349 * will be <code>java.io.InputStream</code>. This is the same as calling
350 * <code>new DataFlavor(mimeType, null)</code>.
352 * @param mimeType The MIME type for this flavor.
354 * @exception IllegalArgumentException If a class is not specified in
356 * @exception ClassNotFoundException If the class cannot be loaded.
359 DataFlavor(String mimeType
) throws ClassNotFoundException
361 this(mimeType
, null);
364 /*************************************************************************/
367 * Returns the MIME type of this flavor.
369 * @return The MIME type for this flavor.
377 /*************************************************************************/
380 * Returns the representation class for this flavor.
382 * @return The representation class for this flavor.
385 getRepresentationClass()
387 return(representationClass
);
390 /*************************************************************************/
393 * Returns the human presentable name for this flavor.
395 * @return The human presentable name for this flavor.
398 getHumanPresentableName()
400 return(humanPresentableName
);
403 /*************************************************************************/
406 * Returns the primary MIME type for this flavor.
408 * @return The primary MIME type for this flavor.
413 int idx
= mimeType
.indexOf("/");
417 return(mimeType
.substring(0, idx
));
420 /*************************************************************************/
423 * Returns the MIME subtype for this flavor.
425 * @return The MIME subtype for this flavor.
430 int idx
= mimeType
.indexOf("/");
434 String subtype
= mimeType
.substring(idx
+ 1);
436 idx
= subtype
.indexOf(" ");
440 return(subtype
.substring(0, idx
));
443 /*************************************************************************/
446 * Returns the value of the named MIME type parameter, or <code>null</code>
447 * if the parameter does not exist. Given the parameter name and the mime
450 * @param paramName The name of the parameter.
451 * @param mimeString The mime string from where the name should be found.
453 * @return The value of the parameter or null.
455 private static String
456 getParameter(String paramName
, String mimeString
)
458 int idx
= mimeString
.indexOf(paramName
+ "=");
462 String value
= mimeString
.substring(idx
+ paramName
.length() + 1);
464 idx
= value
.indexOf(" ");
468 return(value
.substring(0, idx
));
471 /*************************************************************************/
474 * Returns the value of the named MIME type parameter, or <code>null</code>
475 * if the parameter does not exist.
477 * @param paramName The name of the paramter.
479 * @return The value of the parameter.
482 getParameter(String paramName
)
484 return getParameter(paramName
, mimeType
);
487 /*************************************************************************/
490 * Sets the human presentable name to the specified value.
492 * @param humanPresentableName The new display name.
495 setHumanPresentableName(String humanPresentableName
)
497 this.humanPresentableName
= humanPresentableName
;
500 /*************************************************************************/
503 * Tests the MIME type of this object for equality against the specified
506 * @param mimeType The MIME type to test against.
508 * @return <code>true</code> if the MIME type is equal to this object's
509 * MIME type, <code>false</code> otherwise.
511 * @exception NullPointerException If mimeType is null.
514 isMimeTypeEqual(String mimeType
)
516 // FIXME: Need to handle default attributes and parameters
518 return(this.mimeType
.equals(mimeType
));
521 /*************************************************************************/
524 * Tests the MIME type of this object for equality against the specified
525 * data flavor's MIME type
527 * @param flavor The flavor to test against.
529 * @return <code>true</code> if the flavor's MIME type is equal to this
530 * object's MIME type, <code>false</code> otherwise.
533 isMimeTypeEqual(DataFlavor flavor
)
535 return(isMimeTypeEqual(flavor
.getMimeType()));
538 /*************************************************************************/
541 * Tests whether or not this flavor represents a serialized object.
543 * @return <code>true</code> if this flavor represents a serialized
544 * object, <code>false</code> otherwise.
547 isMimeTypeSerializedObject()
549 return(mimeType
.startsWith(javaSerializedObjectMimeType
));
552 /*************************************************************************/
555 * Tests whether or not this flavor has a representation class of
556 * <code>java.io.InputStream</code>.
558 * @param <code>true</code> if the representation class of this flavor
559 * is <code>java.io.InputStream</code>, <code>false</code> otherwise.
562 isRepresentationClassInputStream()
564 return(representationClass
.getName().equals("java.io.InputStream"));
567 /*************************************************************************/
570 * Tests whether the representation class for this flavor is
573 * @param <code>true</code> if the representation class is serializable,
574 * <code>false</code> otherwise.
577 isRepresentationClassSerializable()
579 Class
[] interfaces
= representationClass
.getInterfaces();
582 while (i
< interfaces
.length
)
584 if (interfaces
[i
].getName().equals("java.io.Serializable"))
592 /*************************************************************************/
595 * Tests whether the representation class for his flavor is remote.
597 * @return <code>true</code> if the representation class is remote,
598 * <code>false</code> otherwise.
601 isRepresentationClassRemote()
604 throw new RuntimeException("Not implemented");
607 /*************************************************************************/
610 * Tests whether or not this flavor represents a serialized object.
612 * @return <code>true</code> if this flavor represents a serialized
613 * object, <code>false</code> otherwise.
616 isFlavorSerializedObjectType()
618 // FIXME: What is the diff between this and isMimeTypeSerializedObject?
619 return(mimeType
.startsWith(javaSerializedObjectMimeType
));
622 /*************************************************************************/
625 * Tests whether or not this flavor represents a remote object.
627 * @return <code>true</code> if this flavor represents a remote object,
628 * <code>false</code> otherwise.
631 isFlavorRemoteObjectType()
633 return(mimeType
.startsWith(javaRemoteObjectMimeType
));
636 /*************************************************************************/
639 * Tests whether or not this flavor represents a list of files.
641 * @return <code>true</code> if this flavor represents a list of files,
642 * <code>false</code> otherwise.
645 isFlavorJavaFileListType()
647 if (this.mimeType
.equals(javaFileListFlavor
.mimeType
) &&
648 this.representationClass
.equals(javaFileListFlavor
.representationClass
))
654 /*************************************************************************/
657 * Returns a copy of this object.
659 * @return A copy of this object.
661 * @exception CloneNotSupportedException If the object's class does not support
662 * the Cloneable interface. Subclasses that override the clone method can also
663 * throw this exception to indicate that an instance cannot be cloned.
665 public Object
clone () throws CloneNotSupportedException
669 return(super.clone());
677 /*************************************************************************/
680 * This method test the specified <code>DataFlavor</code> for equality
681 * against this object. This will be true if the MIME type and
682 * representation type are the equal.
684 * @param flavor The <code>DataFlavor</code> to test against.
686 * @return <code>true</code> if the flavor is equal to this object,
687 * <code>false</code> otherwise.
690 equals(DataFlavor flavor
)
695 if (!this.mimeType
.toLowerCase().equals(flavor
.mimeType
.toLowerCase()))
698 if (!this.representationClass
.equals(flavor
.representationClass
))
704 /*************************************************************************/
707 * This method test the specified <code>Object</code> for equality
708 * against this object. This will be true if the following conditions
712 * <li>The object is not <code>null</code>.
713 * <li>The object is an instance of <code>DataFlavor</code>.
714 * <li>The object's MIME type and representation class are equal to
718 * @param obj The <code>Object</code> to test against.
720 * @return <code>true</code> if the flavor is equal to this object,
721 * <code>false</code> otherwise.
726 if (!(obj
instanceof DataFlavor
))
729 return(equals((DataFlavor
)obj
));
732 /*************************************************************************/
735 * Tests whether or not the specified string is equal to the MIME type
738 * @param str The string to test against.
740 * @return <code>true</code> if the string is equal to this object's MIME
741 * type, <code>false</code> otherwise.
743 * @deprecated Not compatible with <code>hashCode()</code>.
744 * Use <code>isMimeTypeEqual()</code>
749 return(isMimeTypeEqual(str
));
752 /*************************************************************************/
755 * Returns the hash code for this data flavor.
756 * The hash code is based on the (lower case) mime type and the
757 * representation class.
762 return(mimeType
.toLowerCase().hashCode()^representationClass
.hashCode());
765 /*************************************************************************/
768 * Returns <code>true</code> when the given <code>DataFlavor</code>
772 match(DataFlavor dataFlavor
)
774 // XXX - How is this different from equals?
775 return(equals(dataFlavor
));
778 /*************************************************************************/
781 * This method exists for backward compatibility. It simply returns
782 * the same name/value pair passed in.
784 * @param name The parameter name.
785 * @param value The parameter value.
787 * @return The name/value pair.
792 normalizeMimeTypeParameter(String name
, String value
)
794 return(name
+ "=" + value
);
797 /*************************************************************************/
800 * This method exists for backward compatibility. It simply returns
801 * the MIME type string unchanged.
803 * @param type The MIME type.
805 * @return The MIME type.
810 normalizeMimeType(String type
)
815 /*************************************************************************/
818 * Serialize this class.
820 * @param stream The <code>ObjectOutput</code> stream to serialize to.
822 * @exception IOException If an error occurs.
825 writeExternal(ObjectOutput stream
) throws IOException
827 // FIXME: Implement me
830 /*************************************************************************/
833 * De-serialize this class.
835 * @param stream The <code>ObjectInput</code> stream to deserialize from.
837 * @exception IOException If an error ocurs.
838 * @exception ClassNotFoundException If the class for an object being restored
842 readExternal(ObjectInput stream
) throws IOException
, ClassNotFoundException
844 // FIXME: Implement me
847 /*************************************************************************/
850 * Returns a string representation of this DataFlavor. Including the
851 * representation class name, MIME type and human presentable name.
856 return("DataFlavor[representationClass="
857 + representationClass
.getName()
860 + "humanPresentableName="
861 + humanPresentableName
);
864 /*************************************************************************/
867 * XXX - Currently returns <code>plainTextFlavor</code>.
869 public static final DataFlavor
870 getTextPlainUnicodeFlavor()
872 return(plainTextFlavor
);
875 /*************************************************************************/
878 * XXX - Currently returns <code>java.io.InputStream</code>.
883 getDefaultRepresentationClass()
885 return(java
.io
.InputStream
.class);
887 /*************************************************************************/
890 * XXX - Currently returns <code>java.io.InputStream</code>.
893 getDefaultRepresentationClassAsString()
895 return(getDefaultRepresentationClass().getName());
898 /*************************************************************************/
901 * Selects the best supported text flavor on this implementation.
902 * Returns <code>null</code> when none of the given flavors is liked.
904 * The <code>DataFlavor</code> returned the first data flavor in the
905 * array that has either a representation class which is (a subclass of)
906 * <code>Reader</code> or <code>String</code>, or has a representation
907 * class which is (a subclass of) <code>InputStream</code> and has a
908 * primary MIME type of "text" and has an supported encoding.
910 public static final DataFlavor
911 selectBestTextFlavor(DataFlavor
[] availableFlavors
)
913 for(int i
=0; i
<availableFlavors
.length
; i
++)
915 DataFlavor df
= availableFlavors
[i
];
916 Class c
= df
.representationClass
;
918 // A Reader or String is good.
919 if ((Reader
.class.isAssignableFrom(c
))
920 || (String
.class.isAssignableFrom(c
)))
925 // A InputStream is good if the mime primary type is "text"
926 if ((InputStream
.class.isAssignableFrom(c
))
927 && ("text".equals(df
.getPrimaryType())))
929 String encoding
= availableFlavors
[i
].getParameter("charset");
930 if (encoding
== null)
931 encoding
= "us-ascii";
935 // Try to construct a dummy reader with the found encoding
936 r
= new InputStreamReader
937 (new ByteArrayInputStream(new byte[0]), encoding
);
939 catch(UnsupportedEncodingException uee
) { /* ignore */ }
949 /*************************************************************************/
952 * Creates a <code>Reader</code> for a given <code>Transferable</code>.
954 * If the representation class is a (subclass of) <code>Reader</code>
955 * then an instance of the representation class is returned. If the
956 * representatation class is a <code>String</code> then a
957 * <code>StringReader</code> is returned. And if the representation class
958 * is a (subclass of) <code>InputStream</code> and the primary MIME type
959 * is "text" then a <code>InputStreamReader</code> for the correct charset
960 * encoding is returned.
962 * @param transferable The <code>Transferable</code> for which a text
963 * <code>Reader</code> is requested.
965 * @exception IllegalArgumentException If the representation class is not one
966 * of the seven listed above or the Transferable has null data.
967 * @exception NullPointerException If the Transferable is null.
968 * @exception UnsupportedFlavorException when the transferable doesn't
969 * support this <code>DataFlavor</code>. Or if the representable class
970 * isn't a (subclass of) <code>Reader</code>, <code>String</code>,
971 * <code>InputStream</code> and/or the primary MIME type isn't "text".
972 * @exception IOException when any IOException occurs.
973 * @exception UnsupportedEncodingException if the "charset" isn't supported
976 public Reader
getReaderForText(Transferable transferable
)
977 throws UnsupportedFlavorException
, IOException
979 if (!transferable
.isDataFlavorSupported(this))
980 throw new UnsupportedFlavorException(this);
982 if (Reader
.class.isAssignableFrom(representationClass
))
983 return((Reader
)transferable
.getTransferData(this));
985 if (String
.class.isAssignableFrom(representationClass
))
986 return(new StringReader((String
)transferable
.getTransferData(this)));
988 if (InputStream
.class.isAssignableFrom(representationClass
)
989 && "text".equals(getPrimaryType()))
991 InputStream in
= (InputStream
)transferable
.getTransferData(this);
992 String encoding
= getParameter("charset");
993 if (encoding
== null)
994 encoding
= "us-ascii";
995 return(new InputStreamReader(in
, encoding
));
998 throw new UnsupportedFlavorException(this);
1002 * Returns whether the representation class for this DataFlavor is
1003 * @see java.nio.ByteBuffer or a subclass thereof.
1007 public boolean isRepresentationClassByteBuffer ()
1009 return ByteBuffer
.class.isAssignableFrom (representationClass
);
1013 * Returns whether the representation class for this DataFlavor is
1014 * @see java.nio.CharBuffer or a subclass thereof.
1018 public boolean isRepresentationClassCharBuffer ()
1020 return CharBuffer
.class.isAssignableFrom (representationClass
);
1024 * Returns whether the representation class for this DataFlavor is
1025 * @see java.io.Reader or a subclass thereof.
1029 public boolean isRepresentationClassReader ()
1031 return Reader
.class.isAssignableFrom (representationClass
);
1034 } // class DataFlavor