KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > datatransfer > DataFlavor


1 /*
2  * @(#)DataFlavor.java 1.79 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.datatransfer;
9
10 import java.awt.Toolkit JavaDoc;
11 import java.io.*;
12 import java.nio.*;
13 import java.util.*;
14
15 import sun.awt.datatransfer.DataTransferer;
16
17 /**
18  * Each instance represents the opaque concept of a data format as would
19  * appear on a clipboard, during drag and drop, or in a file system.
20  * <p>
21  * <code>DataFlavor</code> objects are constant and never change once
22  * instantiated.
23  * <p>
24  * For information on using data transfer with Swing, see
25  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html">
26  * How to Use Drag and Drop and Data Transfer</a>,
27  * a section in <em>The Java Tutorial</em>, for more information.
28  *
29  * @version 1.79, 05/18/04
30  * @author Blake Sullivan
31  * @author Laurence P. G. Cable
32  * @author Jeff Dunn
33  */

34 public class DataFlavor implements Externalizable, Cloneable JavaDoc {
35     
36     private static final long serialVersionUID = 8367026044764648243L;
37     private static final Class JavaDoc ioInputStreamClass = java.io.InputStream JavaDoc.class;
38
39     /**
40      * Tries to load a class from: the bootstrap loader, the system loader,
41      * the context loader (if one is present) and finally the loader specified.
42      *
43      * @param className the name of the class to be loaded
44      * @param fallback the fallback loader
45      * @return the class loaded
46      * @exception ClassNotFoundException if class is not found
47      */

48     protected final static Class JavaDoc<?> tryToLoadClass(String JavaDoc className,
49                            ClassLoader JavaDoc fallback)
50         throws ClassNotFoundException JavaDoc
51     {
52     ClassLoader JavaDoc systemClassLoader = (ClassLoader JavaDoc)
53         java.security.AccessController.doPrivileged(
54                 new java.security.PrivilegedAction JavaDoc() {
55             public Object JavaDoc run() {
56             ClassLoader JavaDoc 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 JavaDoc e2) {
67         if (fallback != null) {
68         return Class.forName(className, true, fallback);
69         } else {
70         throw new ClassNotFoundException JavaDoc(className);
71         }
72     }
73     }
74     
75     /*
76      * private initializer
77      */

78     static private DataFlavor JavaDoc createConstant(Class JavaDoc rc, String JavaDoc prn) {
79         try {
80             return new DataFlavor JavaDoc(rc, prn);
81         } catch (Exception JavaDoc e) {
82             return null;
83         }
84     }
85     
86     /*
87      * private initializer
88      */

89     static private DataFlavor JavaDoc createConstant(String JavaDoc mt, String JavaDoc prn) {
90         try {
91             return new DataFlavor JavaDoc(mt, prn);
92         } catch (Exception JavaDoc e) {
93             return null;
94         }
95     }
96     
97     /**
98      * The <code>DataFlavor</code> representing a Java Unicode String class,
99      * where:
100      * <pre>
101      * representationClass = java.lang.String
102      * mimeType = "application/x-java-serialized-object"
103      * </pre>
104      */

105     public static final DataFlavor JavaDoc stringFlavor = createConstant(java.lang.String JavaDoc.class, "Unicode String");
106     
107     /**
108      * The <code>DataFlavor</code> representing a Java Image class,
109      * where:
110      * <pre>
111      * representationClass = java.awt.Image
112      * mimeType = "image/x-java-image"
113      * </pre>
114      */

115     public static final DataFlavor JavaDoc imageFlavor = createConstant("image/x-java-image; class=java.awt.Image", "Image");
116     
117     /**
118      * The <code>DataFlavor</code> representing plain text with Unicode
119      * encoding, where:
120      * <pre>
121      * representationClass = InputStream
122      * mimeType = "text/plain; charset=unicode"
123      * </pre>
124      * This <code>DataFlavor</code> has been <b>deprecated</b> because
125      * (1) Its representation is an InputStream, an 8-bit based representation,
126      * while Unicode is a 16-bit character set; and (2) The charset "unicode"
127      * is not well-defined. "unicode" implies a particular platform's
128      * implementation of Unicode, not a cross-platform implementation.
129      *
130      * @deprecated as of 1.3. Use <code>DataFlavor.getReaderForText(Transferable)</code>
131      * instead of <code>Transferable.getTransferData(DataFlavor.plainTextFlavor)</code>.
132      */

133     @Deprecated JavaDoc
134     public static final DataFlavor JavaDoc plainTextFlavor = createConstant("text/plain; charset=unicode; class=java.io.InputStream", "Plain Text");
135     
136     /**
137      * A MIME Content-Type of application/x-java-serialized-object represents
138      * a graph of Java object(s) that have been made persistent.
139      *
140      * The representation class associated with this <code>DataFlavor</code>
141      * identifies the Java type of an object returned as a reference
142      * from an invocation <code>java.awt.datatransfer.getTransferData</code>.
143      */

144     public static final String JavaDoc javaSerializedObjectMimeType = "application/x-java-serialized-object";
145     
146     /**
147      * To transfer a list of files to/from Java (and the underlying
148      * platform) a <code>DataFlavor</code> of this type/subtype and
149      * representation class of <code>java.util.List</code> is used.
150      * Each element of the list is required/guaranteed to be of type
151      * <code>java.io.File</code>.
152      */

153     public static final DataFlavor JavaDoc javaFileListFlavor = createConstant("application/x-java-file-list;class=java.util.List", null);
154
155     /**
156      * To transfer a reference to an arbitrary Java object reference that
157      * has no associated MIME Content-type, across a <code>Transferable</code>
158      * interface WITHIN THE SAME JVM, a <code>DataFlavor</code>
159      * with this type/subtype is used, with a <code>representationClass</code>
160      * equal to the type of the class/interface being passed across the
161      * <code>Transferable</code>.
162      * <p>
163      * The object reference returned from
164      * <code>Transferable.getTransferData</code> for a <code>DataFlavor</code>
165      * with this MIME Content-Type is required to be
166      * an instance of the representation Class of the <code>DataFlavor</code>.
167      */

168     public static final String JavaDoc javaJVMLocalObjectMimeType = "application/x-java-jvm-local-objectref";
169     
170     /**
171      * In order to pass a live link to a Remote object via a Drag and Drop
172      * <code>ACTION_LINK</code> operation a Mime Content Type of
173      * application/x-java-remote-object should be used,
174      * where the representation class of the <code>DataFlavor</code>
175      * represents the type of the <code>Remote</code> interface to be
176      * transferred.
177      */

178     public static final String JavaDoc javaRemoteObjectMimeType = "application/x-java-remote-object";
179
180     /**
181      * Constructs a new <code>DataFlavor</code>. This constructor is
182      * provided only for the purpose of supporting the
183      * <code>Externalizable</code> interface. It is not
184      * intended for public (client) use.
185      *
186      * @since 1.2
187      */

188     public DataFlavor() {
189         super();
190     }
191
192     /**
193      * Constructs a fully specified <code>DataFlavor</code>.
194      *
195      * @exception NullPointerException if either <code>primaryType</code>,
196      * <code>subType</code> or <code>representationClass</code> is null
197      */

198     private DataFlavor(String JavaDoc primaryType, String JavaDoc subType, MimeTypeParameterList JavaDoc params, Class JavaDoc representationClass, String JavaDoc humanPresentableName) {
199         super();
200         if (primaryType == null) {
201             throw new NullPointerException JavaDoc("primaryType");
202         }
203         if (subType == null) {
204             throw new NullPointerException JavaDoc("subType");
205         }
206         if (representationClass == null) {
207             throw new NullPointerException JavaDoc("representationClass");
208         }
209         
210         if (params == null) params = new MimeTypeParameterList JavaDoc();
211         
212         params.set("class", representationClass.getName());
213         
214         if (humanPresentableName == null) {
215             humanPresentableName = (String JavaDoc)params.get("humanPresentableName");
216             
217             if (humanPresentableName == null)
218                 humanPresentableName = primaryType + "/" + subType;
219         }
220         
221         try {
222             mimeType = new MimeType JavaDoc(primaryType, subType, params);
223         } catch (MimeTypeParseException JavaDoc mtpe) {
224             throw new IllegalArgumentException JavaDoc("MimeType Parse Exception: " + mtpe.getMessage());
225         }
226         
227         this.representationClass = representationClass;
228         this.humanPresentableName = humanPresentableName;
229         
230         mimeType.removeParameter("humanPresentableName");
231     }
232     
233     /**
234      * Constructs a <code>DataFlavor</code> that represents a Java class.
235      * <p>
236      * The returned <code>DataFlavor</code> will have the following
237      * characteristics:
238      * <pre>
239      * representationClass = representationClass
240      * mimeType = application/x-java-serialized-object
241      * </pre>
242      * @param representationClass the class used to transfer data in this flavor
243      * @param humanPresentableName the human-readable string used to identify
244      * this flavor; if this parameter is <code>null</code>
245      * then the value of the the MIME Content Type is used
246      * @exception NullPointerException if <code>representationClass</code> is null
247      */

248     public DataFlavor(Class JavaDoc<?> representationClass, String JavaDoc humanPresentableName) {
249         this("application", "x-java-serialized-object", null, representationClass, humanPresentableName);
250         if (representationClass == null) {
251             throw new NullPointerException JavaDoc("representationClass");
252         }
253     }
254
255     /**
256      * Constructs a <code>DataFlavor</code> that represents a
257      * <code>MimeType</code>.
258      * <p>
259      * The returned <code>DataFlavor</code> will have the following
260      * characteristics:
261      * <p>
262      * If the <code>mimeType</code> is
263      * "application/x-java-serialized-object; class=&lt;representation class&gt;",
264      * the result is the same as calling
265      * <code>new DataFlavor(Class:forName(&lt;representation class&gt;)</code>.
266      * <p>
267      * Otherwise:
268      * <pre>
269      * representationClass = InputStream
270      * mimeType = mimeType
271      * </pre>
272      * @param mimeType the string used to identify the MIME type for this flavor;
273      * if the the <code>mimeType</code> does not specify a
274      * "class=" parameter, or if the class is not successfully
275      * loaded, then an <code>IllegalArgumentException</code>
276      * is thrown
277      * @param humanPresentableName the human-readable string used to identify
278      * this flavor; if this parameter is <code>null</code>
279      * then the value of the the MIME Content Type is used
280      * @exception IllegalArgumentException if <code>mimeType</code> is
281      * invalid or if the class is not successfully loaded
282      * @exception NullPointerException if <code>mimeType</code> is null
283      */

284     public DataFlavor(String JavaDoc mimeType, String JavaDoc humanPresentableName) {
285         super();
286         if (mimeType == null) {
287             throw new NullPointerException JavaDoc("mimeType");
288         }
289         try {
290             initialize(mimeType, humanPresentableName, this.getClass().getClassLoader());
291         } catch (MimeTypeParseException JavaDoc mtpe) {
292             throw new IllegalArgumentException JavaDoc("failed to parse:" + mimeType);
293         } catch (ClassNotFoundException JavaDoc cnfe) {
294             throw new IllegalArgumentException JavaDoc("can't find specified class: " + cnfe.getMessage());
295         }
296     }
297     
298     /**
299      * Constructs a <code>DataFlavor</code> that represents a
300      * <code>MimeType</code>.
301      * <p>
302      * The returned <code>DataFlavor</code> will have the following
303      * characteristics:
304      * <p>
305      * If the mimeType is
306      * "application/x-java-serialized-object; class=&lt;representation class&gt;",
307      * the result is the same as calling
308      * <code>new DataFlavor(Class:forName(&lt;representation class&gt;)</code>.
309      * <p>
310      * Otherwise:
311      * <pre>
312      * representationClass = InputStream
313      * mimeType = mimeType
314      * </pre>
315      * @param mimeType the string used to identify the MIME type for this flavor
316      * @param humanPresentableName the human-readable string used to
317      * identify this flavor
318      * @param classLoader the class loader to use
319      * @exception ClassNotFoundException if the class is not loaded
320      * @exception IllegalArgumentException if <code>mimeType</code> is
321      * invalid
322      * @exception NullPointerException if <code>mimeType</code> is null
323      */

324     public DataFlavor(String JavaDoc mimeType, String JavaDoc humanPresentableName, ClassLoader JavaDoc classLoader) throws ClassNotFoundException JavaDoc {
325     super();
326         if (mimeType == null) {
327             throw new NullPointerException JavaDoc("mimeType");
328         }
329     try {
330             initialize(mimeType, humanPresentableName, classLoader);
331         } catch (MimeTypeParseException JavaDoc mtpe) {
332             throw new IllegalArgumentException JavaDoc("failed to parse:" + mimeType);
333         }
334     }
335     
336     /**
337      * Constructs a <code>DataFlavor</code> from a <code>mimeType</code> string.
338      * The string can specify a "class=<fully specified Java class name>"
339      * parameter to create a <code>DataFlavor</code> with the desired
340      * representation class. If the string does not contain "class=" parameter,
341      * <code>java.io.InputStream</code> is used as default.
342      *
343      * @param mimeType the string used to identify the MIME type for this flavor;
344      * if the class specified by "class=" parameter is not
345      * successfully loaded, then an
346      * <code>ClassNotFoundException</code> is thrown
347      * @exception ClassNotFoundException if the class is not loaded
348      * @exception IllegalArgumentException if <code>mimeType</code> is
349      * invalid
350      * @exception NullPointerException if <code>mimeType</code> is null
351      */

352     public DataFlavor(String JavaDoc mimeType) throws ClassNotFoundException JavaDoc {
353         super();
354         if (mimeType == null) {
355             throw new NullPointerException JavaDoc("mimeType");
356         }
357         try {
358             initialize(mimeType, null, this.getClass().getClassLoader());
359         } catch (MimeTypeParseException JavaDoc mtpe) {
360             throw new IllegalArgumentException JavaDoc("failed to parse:" + mimeType);
361         }
362     }
363
364    /**
365     * Common initialization code called from various constructors.
366     *
367     * @param mimeType the MIME Content Type (must have a class= param)
368     * @param humanPresentableName the human Presentable Name or
369     * <code>null</code>
370     * @param classLoader the fallback class loader to resolve against
371     *
372     * @throws MimeTypeParseException
373     * @throws ClassNotFoundException
374     * @throws NullPointerException if <code>mimeType</code> is null
375     *
376     * @see tryToLoadClass
377     */

378     private void initialize(String JavaDoc mimeType, String JavaDoc humanPresentableName, ClassLoader JavaDoc classLoader) throws MimeTypeParseException JavaDoc, ClassNotFoundException JavaDoc {
379         if (mimeType == null) {
380             throw new NullPointerException JavaDoc("mimeType");
381         }
382         
383         this.mimeType = new MimeType JavaDoc(mimeType); // throws
384

385     String JavaDoc rcn = getParameter("class");
386     
387     if (rcn == null) {
388             if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType()))
389             
390                 throw new IllegalArgumentException JavaDoc("no representation class specified for:" + mimeType);
391             else
392                 representationClass = java.io.InputStream JavaDoc.class; // default
393
} else { // got a class name
394
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; // set it.
406

407         this.mimeType.removeParameter("humanPresentableName"); // just in case
408
}
409
410     /**
411      * String representation of this <code>DataFlavor</code> and its
412      * parameters. The resulting <code>String</code> contains the name of
413      * the <code>DataFlavor</code> class, this flavor's MIME type, and its
414      * representation class. If this flavor has a primary MIME type of "text",
415      * supports the charset parameter, and has an encoded representation, the
416      * flavor's charset is also included. See <code>selectBestTextFlavor</code>
417      * for a list of text flavors which support the charset parameter.
418      *
419      * @return string representation of this <code>DataFlavor</code>
420      * @see #selectBestTextFlavor
421      */

422     public String JavaDoc toString() {
423         String JavaDoc string = getClass().getName();
424         string += "["+paramString()+"]";
425         return string;
426     }
427     
428     private String JavaDoc paramString() {
429         String JavaDoc 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     /**
453      * Returns a <code>DataFlavor</code> representing plain text with Unicode
454      * encoding, where:
455      * <pre>
456      * representationClass = java.io.InputStream
457      * mimeType = "text/plain;
458      * charset=&lt;platform default Unicode encoding&gt;"
459      * </pre>
460      * Sun's implementation for Microsoft Windows uses the encoding <code>utf-16le</code>.
461      * Sun's implementation for Solaris and Linux uses the encoding
462      * <code>iso-10646-ucs-2</code>.
463      *
464      * @return a <code>DataFlavor</code> representing plain text
465      * with Unicode encoding
466      */

467     public static final DataFlavor JavaDoc getTextPlainUnicodeFlavor() {
468     String JavaDoc encoding = null;
469     DataTransferer transferer = DataTransferer.getInstance();
470     if (transferer != null) {
471         encoding = transferer.getDefaultUnicodeEncoding();
472     }
473     return new DataFlavor JavaDoc(
474         "text/plain;charset="+encoding
475         +";class=java.io.InputStream", "Plain Text");
476     }
477
478     /**
479      * Selects the best text <code>DataFlavor</code> from an array of <code>
480      * DataFlavor</code>s. Only <code>DataFlavor.stringFlavor</code>, and
481      * equivalent flavors, and flavors that have a primary MIME type of "text",
482      * are considered for selection.
483      * <p>
484      * Flavors are first sorted by their MIME types in the following order:
485      * <ul>
486      * <li>"text/sgml"
487      * <li>"text/xml"
488      * <li>"text/html"
489      * <li>"text/rtf"
490      * <li>"text/enriched"
491      * <li>"text/richtext"
492      * <li>"text/uri-list"
493      * <li>"text/tab-separated-values"
494      * <li>"text/t140"
495      * <li>"text/rfc822-headers"
496      * <li>"text/parityfec"
497      * <li>"text/directory"
498      * <li>"text/css"
499      * <li>"text/calendar"
500      * <li>"application/x-java-serialized-object"
501      * <li>"text/plain"
502      * <li>"text/&lt;other&gt;"
503      * </ul>
504      * <p>For example, "text/sgml" will be selected over
505      * "text/html", and <code>DataFlavor.stringFlavor</code> will be chosen
506      * over <code>DataFlavor.plainTextFlavor</code>.
507      * <p>
508      * If two or more flavors share the best MIME type in the array, then that
509      * MIME type will be checked to see if it supports the charset parameter.
510      * <p>
511      * The following MIME types support, or are treated as though they support,
512      * the charset parameter:
513      * <ul>
514      * <li>"text/sgml"
515      * <li>"text/xml"
516      * <li>"text/html"
517      * <li>"text/enriched"
518      * <li>"text/richtext"
519      * <li>"text/uri-list"
520      * <li>"text/directory"
521      * <li>"text/css"
522      * <li>"text/calendar"
523      * <li>"application/x-java-serialized-object"
524      * <li>"text/plain"
525      * </ul>
526      * The following MIME types do not support, or are treated as though they
527      * do not support, the charset parameter:
528      * <ul>
529      * <li>"text/rtf"
530      * <li>"text/tab-separated-values"
531      * <li>"text/t140"
532      * <li>"text/rfc822-headers"
533      * <li>"text/parityfec"
534      * </ul>
535      * For "text/&lt;other&gt;" MIME types, the first time the JRE needs to
536      * determine whether the MIME type supports the charset parameter, it will
537      * check whether the parameter is explicitly listed in an arbitrarily
538      * chosen <code>DataFlavor</code> which uses that MIME type. If so, the JRE
539      * will assume from that point on that the MIME type supports the charset
540      * parameter and will not check again. If the parameter is not explicitly
541      * listed, the JRE will assume from that point on that the MIME type does
542      * not support the charset parameter and will not check again. Because
543      * this check is performed on an arbitrarily chosen
544      * <code>DataFlavor</code>, developers must ensure that all
545      * <code>DataFlavor</code>s with a "text/&lt;other&gt;" MIME type specify
546      * the charset parameter if it is supported by that MIME type. Developers
547      * should never rely on the JRE to substitute the platform's default
548      * charset for a "text/&lt;other&gt;" DataFlavor. Failure to adhere to this
549      * restriction will lead to undefined behavior.
550      * <p>
551      * If the best MIME type in the array does not support the charset
552      * parameter, the flavors which share that MIME type will then be sorted by
553      * their representation classes in the following order:
554      * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
555      * <code>[B</code>, &lt;all others&gt;.
556      * <p>
557      * If two or more flavors share the best representation class, or if no
558      * flavor has one of the three specified representations, then one of those
559      * flavors will be chosen non-deterministically.
560      * <p>
561      * If the best MIME type in the array does support the charset parameter,
562      * the flavors which share that MIME type will then be sorted by their
563      * representation classes in the following order:
564      * <code>java.io.Reader</code>, <code>java.lang.String</code>,
565      * <code>java.nio.CharBuffer</code>, <code>[C</code>, &lt;all others&gt;.
566      * <p>
567      * If two or more flavors share the best representation class, and that
568      * representation is one of the four explicitly listed, then one of those
569      * flavors will be chosen non-deterministically. If, however, no flavor has
570      * one of the four specified representations, the flavors will then be
571      * sorted by their charsets. Unicode charsets, such as "UTF-16", "UTF-8",
572      * "UTF-16BE", "UTF-16LE", and their aliases, are considered best. After
573      * them, the platform default charset and its aliases are selected.
574      * "US-ASCII" and its aliases are worst. All other charsets are chosen in
575      * alphabetical order, but only charsets supported by this implementation
576      * of the Java platform will be considered.
577      * <p>
578      * If two or more flavors share the best charset, the flavors will then
579      * again be sorted by their representation classes in the following order:
580      * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
581      * <code>[B</code>, &lt;all others&gt;.
582      * <p>
583      * If two or more flavors share the best representation class, or if no
584      * flavor has one of the three specified representations, then one of those
585      * flavors will be chosen non-deterministically.
586      *
587      * @param availableFlavors an array of available <code>DataFlavor</code>s
588      * @return the best (highest fidelity) flavor according to the rules
589      * specified above, or <code>null</code>,
590      * if <code>availableFlavors</code> is <code>null</code>,
591      * has zero length, or contains no text flavors
592      * @since 1.3
593      */

594     public static final DataFlavor JavaDoc selectBestTextFlavor(
595                                        DataFlavor JavaDoc[] 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 JavaDoc bestFlavor =
605             (DataFlavor JavaDoc)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         /**
621          * Compares two <code>DataFlavor</code> objects. Returns a negative
622      * integer, zero, or a positive integer as the first
623      * <code>DataFlavor</code> is worse than, equal to, or better than the
624      * second.
625          * <p>
626          * <code>DataFlavor</code>s are ordered according to the rules outlined
627      * for <code>selectBestTextFlavor</code>.
628      *
629          * @param obj1 the first <code>DataFlavor</code> to be compared
630          * @param obj2 the second <code>DataFlavor</code> to be compared
631          * @return a negative integer, zero, or a positive integer as the first
632          * argument is worse, equal to, or better than the second
633          * @throws ClassCastException if either of the arguments is not an
634          * instance of <code>DataFlavor</code>
635          * @throws NullPointerException if either of the arguments is
636          * <code>null</code>
637      *
638      * @see #selectBestTextFlavor
639          */

640         public int compare(Object JavaDoc obj1, Object JavaDoc obj2) {
641             DataFlavor JavaDoc flavor1 = (DataFlavor JavaDoc)obj1;
642             DataFlavor JavaDoc flavor2 = (DataFlavor JavaDoc)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     /**
659      * Gets a Reader for a text flavor, decoded, if necessary, for the expected
660      * charset (encoding). The supported representation classes are
661      * <code>java.io.Reader</code>, <code>java.lang.String</code>,
662      * <code>java.nio.CharBuffer</code>, <code>[C</code>,
663      * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
664      * and <code>[B</code>.
665      * <p>
666      * Because text flavors which do not support the charset parameter are
667      * encoded in a non-standard format, this method should not be called for
668      * such flavors. However, in order to maintain backward-compatibility,
669      * if this method is called for such a flavor, this method will treat the
670      * flavor as though it supports the charset parameter and attempt to
671      * decode it accordingly. See <code>selectBestTextFlavor</code> for a list
672      * of text flavors which do not support the charset parameter.
673      *
674      * @param transferable the <code>Transferable</code> whose data will be
675      * requested in this flavor
676      *
677      * @return a <code>Reader</code> to read the <code>Transferable</code>'s
678      * data
679      *
680      * @exception IllegalArgumentException if the representation class
681      * is not one of the seven listed above
682      * @exception IllegalArgumentException if the <code>Transferable</code>
683      * has <code>null</code> data
684      * @exception NullPointerException if the <code>Transferable</code> is
685      * <code>null</code>
686      * @exception UnsupportedEncodingException if this flavor's representation
687      * is <code>java.io.InputStream</code>,
688      * <code>java.nio.ByteBuffer</code>, or <code>[B</code> and
689      * this flavor's encoding is not supported by this
690      * implementation of the Java platform
691      * @exception UnsupportedFlavorException if the <code>Transferable</code>
692      * does not support this flavor
693      * @exception IOException if the data cannot be read because of an
694      * I/O error
695      * @see #selectBestTextFlavor
696      * @since 1.3
697      */

698     public Reader getReaderForText(Transferable JavaDoc transferable)
699     throws UnsupportedFlavorException JavaDoc, IOException
700     {
701     Object JavaDoc transferObject = transferable.getTransferData(this);
702     if (transferObject == null) {
703         throw new IllegalArgumentException JavaDoc
704         ("getTransferData() returned null");
705     }
706
707     if (transferObject instanceof Reader) {
708         return (Reader)transferObject;
709     } else if (transferObject instanceof String JavaDoc) {
710         return new StringReader((String JavaDoc)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 JavaDoc("transfer data is not Reader, String, CharBuffer, char array, InputStream, ByteBuffer, or byte array");
737     }
738
739     String JavaDoc encoding = getParameter("charset");
740     return (encoding == null)
741         ? new InputStreamReader(stream)
742         : new InputStreamReader(stream, encoding);
743     }
744     
745     /**
746      * Returns the MIME type string for this <code>DataFlavor</code>.
747      * @return the MIME type string for this flavor
748      */

749     public String JavaDoc getMimeType() {
750         return (mimeType != null) ? mimeType.toString() : null;
751     }
752     
753     /**
754      * Returns the <code>Class</code> which objects supporting this
755      * <code>DataFlavor</code> will return when this <code>DataFlavor</code>
756      * is requested.
757      * @return the <code>Class</code> which objects supporting this
758      * <code>DataFlavor</code> will return when this <code>DataFlavor</code>
759      * is requested
760      */

761     public Class JavaDoc<?> getRepresentationClass() {
762         return representationClass;
763     }
764     
765     /**
766      * Returns the human presentable name for the data format that this
767      * <code>DataFlavor</code> represents. This name would be localized
768      * for different countries.
769      * @return the human presentable name for the data format that this
770      * <code>DataFlavor</code> represents
771      */

772     public String JavaDoc getHumanPresentableName() {
773         return humanPresentableName;
774     }
775     
776     /**
777      * Returns the primary MIME type for this <code>DataFlavor</code>.
778      * @return the primary MIME type of this <code>DataFlavor</code>
779      */

780     public String JavaDoc getPrimaryType() {
781         return (mimeType != null) ? mimeType.getPrimaryType() : null;
782     }
783     
784     /**
785      * Returns the sub MIME type of this <code>DataFlavor</code>.
786      * @return the Sub MIME type of this <code>DataFlavor</code>
787      */

788     public String JavaDoc getSubType() {
789         return (mimeType != null) ? mimeType.getSubType() : null;
790     }
791     
792     /**
793      * Returns the human presentable name for this <code>DataFlavor</code>
794      * if <code>paramName</code> equals "humanPresentableName". Otherwise
795      * returns the MIME type value associated with <code>paramName</code>.
796      *
797      * @param paramName the parameter name requested
798      * @return the value of the name parameter, or <code>null</code>
799      * if there is no associated value
800      */

801     public String JavaDoc getParameter(String JavaDoc paramName) {
802         if (paramName.equals("humanPresentableName")) {
803             return humanPresentableName;
804         } else {
805             return (mimeType != null)
806                 ? mimeType.getParameter(paramName) : null;
807         }
808     }
809     
810     /**
811      * Sets the human presentable name for the data format that this
812      * <code>DataFlavor</code> represents. This name would be localized
813      * for different countries.
814      * @param humanPresentableName the new human presentable name
815      */

816     public void setHumanPresentableName(String JavaDoc humanPresentableName) {
817         this.humanPresentableName = humanPresentableName;
818     }
819     
820     /**
821      * Tests an arbitrary <code>Object</code> to this <code>DataFlavor</code>
822      * for equality. Two <code>DataFlavor</code>s are considered equal if and
823      * only if their MIME primary type and subtype and representation class are
824      * equal. Additionally, if the primary type is "text", the subtype denotes
825      * a text flavor which supports the charset parameter, and the
826      * representation class is not <code>java.io.Reader</code>,
827      * <code>java.lang.String</code>, <code>java.nio.CharBuffer</code>, or
828      * <code>[C</code>, the <code>charset</code> parameter must also be equal.
829      * If a charset is not explicitly specified for one or both
830      * <code>DataFlavor</code>s, the platform default encoding is assumed. See
831      * <code>selectBestText