jdk/src/share/classes/java/awt/datatransfer/DataFlavor.java
author chegar
Tue, 15 Oct 2013 13:54:01 +0100
changeset 20854 242194ae1563
parent 20851 9f284cf7836b
parent 20455 f6f9a0c2796b
child 21276 f1438e2ce393
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4267
diff changeset
     2
 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4267
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4267
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4267
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4267
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4267
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.datatransfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.awt.datatransfer.DataTransferer;
20803
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
    33
import sun.reflect.misc.ReflectUtil;
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
    34
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
    35
import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A {@code DataFlavor} provides meta information about data. {@code DataFlavor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * is typically used to access data on the clipboard, or during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * a drag and drop operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * An instance of {@code DataFlavor} encapsulates a content type as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * defined in <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * and <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * A content type is typically referred to as a MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * A content type consists of a media type (referred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * to as the primary type), a subtype, and optional parameters. See
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * for details on the syntax of a MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The JRE data transfer implementation interprets the parameter &quot;class&quot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of a MIME type as <B>a representation class</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * The representation class reflects the class of the object being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * transferred. In other words, the representation class is the type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * object returned by {@link Transferable#getTransferData}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * For example, the MIME type of {@link #imageFlavor} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * {@code "image/x-java-image;class=java.awt.Image"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * the primary type is {@code image}, the subtype is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * {@code x-java-image}, and the representation class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * {@code java.awt.Image}. When {@code getTransferData} is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * with a {@code DataFlavor} of {@code imageFlavor}, an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * {@code java.awt.Image} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * It's important to note that {@code DataFlavor} does no error checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * against the representation class. It is up to consumers of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * {@code DataFlavor}, such as {@code Transferable}, to honor the representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Note, if you do not specify a representation class when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * creating a {@code DataFlavor}, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * representation class is used. See appropriate documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * {@code DataFlavor}'s constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Also, {@code DataFlavor} instances with the &quot;text&quot; primary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * MIME type may have a &quot;charset&quot; parameter. Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * {@link #selectBestTextFlavor} for details on &quot;text&quot; MIME types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * and the &quot;charset&quot; parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Equality of {@code DataFlavors} is determined by the primary type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * subtype, and representation class. Refer to {@link #equals(DataFlavor)} for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * details. When determining equality, any optional parameters are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * For example, the following produces two {@code DataFlavors} that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * are considered identical:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *   DataFlavor flavor1 = new DataFlavor(Object.class, &quot;X-test/test; class=&lt;java.lang.Object&gt;; foo=bar&quot;);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   DataFlavor flavor2 = new DataFlavor(Object.class, &quot;X-test/test; class=&lt;java.lang.Object&gt;; x=y&quot;);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   // The following returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   flavor1.equals(flavor2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * As mentioned, {@code flavor1} and {@code flavor2} are considered identical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * As such, asking a {@code Transferable} for either {@code DataFlavor} returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * the same results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * For more information on the using data transfer with Swing see
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20172
diff changeset
    96
 * the <a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * How to Use Drag and Drop and Data Transfer</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * section in <em>Java Tutorial</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @author      Blake Sullivan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @author      Laurence P. G. Cable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @author      Jeff Dunn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
public class DataFlavor implements Externalizable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final long serialVersionUID = 8367026044764648243L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static final Class ioInputStreamClass = java.io.InputStream.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Tries to load a class from: the bootstrap loader, the system loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * the context loader (if one is present) and finally the loader specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param className the name of the class to be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param fallback the fallback loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the class loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception ClassNotFoundException if class is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    protected final static Class<?> tryToLoadClass(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                                   ClassLoader fallback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    {
20803
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   122
        ReflectUtil.checkPackageAccess(className);
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   123
        try {
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   124
            SecurityManager sm = System.getSecurityManager();
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   125
            if (sm != null) {
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   126
                sm.checkPermission(GET_CLASSLOADER_PERMISSION);
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   127
            }
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   128
            ClassLoader loader = ClassLoader.getSystemClassLoader();
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   129
            try {
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   130
                // bootstrap class loader and system class loader if present
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   131
                return Class.forName(className, true, loader);
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   132
            }
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   133
            catch (ClassNotFoundException exception) {
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   134
                // thread context class loader if and only if present
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   135
                loader = Thread.currentThread().getContextClassLoader();
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   136
                if (loader != null) {
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   137
                    try {
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   138
                        return Class.forName(className, true, loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    }
20803
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   140
                    catch (ClassNotFoundException e) {
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   141
                        // fallback to user's class loader
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   142
                    }
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   143
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
20803
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   145
        } catch (SecurityException exception) {
140bc93ee613 8012277: Improve AWT DataFlavor
malenkov
parents: 16705
diff changeset
   146
            // ignore secured class loaders
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
20837
a9837567647e 8021969: The index_AccessAllowed jnlp can not load successfully with exception thrown in the log.
malenkov
parents: 20803
diff changeset
   148
        return Class.forName(className, true, fallback);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * private initializer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    static private DataFlavor createConstant(Class rc, String prn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return new DataFlavor(rc, prn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * private initializer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    static private DataFlavor createConstant(String mt, String prn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return new DataFlavor(mt, prn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   173
    /*
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   174
     * private initializer
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   175
     */
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   176
    static private DataFlavor initHtmlDataFlavor(String htmlFlavorType) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   177
        try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   178
            return new DataFlavor ("text/html; class=java.lang.String;document=" +
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   179
                                       htmlFlavorType + ";charset=Unicode");
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   180
        } catch (Exception e) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   181
            return null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   182
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   183
    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   184
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * The <code>DataFlavor</code> representing a Java Unicode String class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * where:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *     representationClass = java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *     mimeType           = "application/x-java-serialized-object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static final DataFlavor stringFlavor = createConstant(java.lang.String.class, "Unicode String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * The <code>DataFlavor</code> representing a Java Image class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * where:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *     representationClass = java.awt.Image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *     mimeType            = "image/x-java-image"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public static final DataFlavor imageFlavor = createConstant("image/x-java-image; class=java.awt.Image", "Image");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * The <code>DataFlavor</code> representing plain text with Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * encoding, where:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *     representationClass = InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *     mimeType            = "text/plain; charset=unicode"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * This <code>DataFlavor</code> has been <b>deprecated</b> because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * (1) Its representation is an InputStream, an 8-bit based representation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * while Unicode is a 16-bit character set; and (2) The charset "unicode"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * is not well-defined. "unicode" implies a particular platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * implementation of Unicode, not a cross-platform implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @deprecated as of 1.3. Use <code>DataFlavor.getReaderForText(Transferable)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *             instead of <code>Transferable.getTransferData(DataFlavor.plainTextFlavor)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public static final DataFlavor plainTextFlavor = createConstant("text/plain; charset=unicode; class=java.io.InputStream", "Plain Text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * A MIME Content-Type of application/x-java-serialized-object represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * a graph of Java object(s) that have been made persistent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * The representation class associated with this <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * identifies the Java type of an object returned as a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * from an invocation <code>java.awt.datatransfer.getTransferData</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public static final String javaSerializedObjectMimeType = "application/x-java-serialized-object";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * To transfer a list of files to/from Java (and the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * platform) a <code>DataFlavor</code> of this type/subtype and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * representation class of <code>java.util.List</code> is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Each element of the list is required/guaranteed to be of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <code>java.io.File</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static final DataFlavor javaFileListFlavor = createConstant("application/x-java-file-list;class=java.util.List", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * To transfer a reference to an arbitrary Java object reference that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * has no associated MIME Content-type, across a <code>Transferable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * interface WITHIN THE SAME JVM, a <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * with this type/subtype is used, with a <code>representationClass</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * equal to the type of the class/interface being passed across the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <code>Transferable</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * The object reference returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <code>Transferable.getTransferData</code> for a <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * with this MIME Content-Type is required to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * an instance of the representation Class of the <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public static final String javaJVMLocalObjectMimeType = "application/x-java-jvm-local-objectref";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * In order to pass a live link to a Remote object via a Drag and Drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <code>ACTION_LINK</code> operation a Mime Content Type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * application/x-java-remote-object should be used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * where the representation class of the <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * represents the type of the <code>Remote</code> interface to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * transferred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static final String javaRemoteObjectMimeType = "application/x-java-remote-object";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   269
     * Represents a piece of an HTML markup. The markup consists of the part
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   270
     * selected on the source side. Therefore some tags in the markup may be
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   271
     * unpaired. If the flavor is used to represent the data in
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   272
     * a {@link Transferable} instance, no additional changes will be made.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   273
     * This DataFlavor instance represents the same HTML markup as DataFlavor
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   274
     * instances which content MIME type does not contain document parameter
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   275
     * and representation class is the String class.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   276
     * <pre>
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   277
     *     representationClass = String
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   278
     *     mimeType           = "text/html"
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   279
     * </pre>
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   280
     */
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   281
    public static DataFlavor selectionHtmlFlavor = initHtmlDataFlavor("selection");
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   282
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   283
    /**
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   284
     * Represents a piece of an HTML markup. If possible, the markup received
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   285
     * from a native system is supplemented with pair tags to be
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   286
     * a well-formed HTML markup. If the flavor is used to represent the data in
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   287
     * a {@link Transferable} instance, no additional changes will be made.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   288
     * <pre>
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   289
     *     representationClass = String
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   290
     *     mimeType           = "text/html"
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   291
     * </pre>
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   292
     */
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   293
    public static DataFlavor fragmentHtmlFlavor = initHtmlDataFlavor("fragment");
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   294
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   295
    /**
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   296
     * Represents a piece of an HTML markup. If possible, the markup
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   297
     * received from a native system is supplemented with additional
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   298
     * tags to make up a well-formed HTML document. If the flavor is used to
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   299
     * represent the data in a {@link Transferable} instance,
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   300
     * no additional changes will be made.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   301
     * <pre>
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   302
     *     representationClass = String
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   303
     *     mimeType           = "text/html"
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   304
     * </pre>
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   305
     */
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   306
    public static  DataFlavor allHtmlFlavor = initHtmlDataFlavor("all");
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   307
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
   308
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Constructs a new <code>DataFlavor</code>.  This constructor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * provided only for the purpose of supporting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <code>Externalizable</code> interface.  It is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * intended for public (client) use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public DataFlavor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Constructs a fully specified <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @exception NullPointerException if either <code>primaryType</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *            <code>subType</code> or <code>representationClass</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private DataFlavor(String primaryType, String subType, MimeTypeParameterList params, Class representationClass, String humanPresentableName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (primaryType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throw new NullPointerException("primaryType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (subType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            throw new NullPointerException("subType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (representationClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            throw new NullPointerException("representationClass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (params == null) params = new MimeTypeParameterList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        params.set("class", representationClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (humanPresentableName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            humanPresentableName = (String)params.get("humanPresentableName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (humanPresentableName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                humanPresentableName = primaryType + "/" + subType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            mimeType = new MimeType(primaryType, subType, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } catch (MimeTypeParseException mtpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throw new IllegalArgumentException("MimeType Parse Exception: " + mtpe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        this.representationClass  = representationClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        this.humanPresentableName = humanPresentableName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        mimeType.removeParameter("humanPresentableName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Constructs a <code>DataFlavor</code> that represents a Java class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * The returned <code>DataFlavor</code> will have the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * characteristics:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *    representationClass = representationClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *    mimeType            = application/x-java-serialized-object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param representationClass the class used to transfer data in this flavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param humanPresentableName the human-readable string used to identify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *                 this flavor; if this parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *                 then the value of the the MIME Content Type is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @exception NullPointerException if <code>representationClass</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public DataFlavor(Class<?> representationClass, String humanPresentableName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        this("application", "x-java-serialized-object", null, representationClass, humanPresentableName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (representationClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            throw new NullPointerException("representationClass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Constructs a <code>DataFlavor</code> that represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <code>MimeType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * The returned <code>DataFlavor</code> will have the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * characteristics:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * If the <code>mimeType</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * "application/x-java-serialized-object; class=&lt;representation class&gt;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * the result is the same as calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <code>new DataFlavor(Class:forName(&lt;representation class&gt;)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Otherwise:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *     representationClass = InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *     mimeType            = mimeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param mimeType the string used to identify the MIME type for this flavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *                 if the the <code>mimeType</code> does not specify a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *                 "class=" parameter, or if the class is not successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *                 loaded, then an <code>IllegalArgumentException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *                 is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param humanPresentableName the human-readable string used to identify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *                 this flavor; if this parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *                 then the value of the the MIME Content Type is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @exception IllegalArgumentException if <code>mimeType</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *                 invalid or if the class is not successfully loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @exception NullPointerException if <code>mimeType</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public DataFlavor(String mimeType, String humanPresentableName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (mimeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw new NullPointerException("mimeType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            initialize(mimeType, humanPresentableName, this.getClass().getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        } catch (MimeTypeParseException mtpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            throw new IllegalArgumentException("failed to parse:" + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            throw new IllegalArgumentException("can't find specified class: " + cnfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Constructs a <code>DataFlavor</code> that represents a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <code>MimeType</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * The returned <code>DataFlavor</code> will have the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * characteristics:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * If the mimeType is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * "application/x-java-serialized-object; class=&lt;representation class&gt;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * the result is the same as calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <code>new DataFlavor(Class:forName(&lt;representation class&gt;)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Otherwise:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *     representationClass = InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *     mimeType            = mimeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param mimeType the string used to identify the MIME type for this flavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param humanPresentableName the human-readable string used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *          identify this flavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param classLoader the class loader to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @exception ClassNotFoundException if the class is not loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @exception IllegalArgumentException if <code>mimeType</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *                 invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @exception NullPointerException if <code>mimeType</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public DataFlavor(String mimeType, String humanPresentableName, ClassLoader classLoader) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (mimeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            throw new NullPointerException("mimeType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            initialize(mimeType, humanPresentableName, classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } catch (MimeTypeParseException mtpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            throw new IllegalArgumentException("failed to parse:" + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Constructs a <code>DataFlavor</code> from a <code>mimeType</code> string.
20172
f48935a247ec 8025218: [javadoc] some errors in java/awt classes
yan
parents: 16705
diff changeset
   466
     * The string can specify a "class=&lt;fully specified Java class name&gt;"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * parameter to create a <code>DataFlavor</code> with the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * representation class. If the string does not contain "class=" parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * <code>java.io.InputStream</code> is used as default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param mimeType the string used to identify the MIME type for this flavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *                 if the class specified by "class=" parameter is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *                 successfully loaded, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *                 <code>ClassNotFoundException</code> is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @exception ClassNotFoundException if the class is not loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @exception IllegalArgumentException if <code>mimeType</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *                 invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @exception NullPointerException if <code>mimeType</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public DataFlavor(String mimeType) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (mimeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            throw new NullPointerException("mimeType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            initialize(mimeType, null, this.getClass().getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        } catch (MimeTypeParseException mtpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            throw new IllegalArgumentException("failed to parse:" + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    * Common initialization code called from various constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    * @param mimeType the MIME Content Type (must have a class= param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    * @param humanPresentableName the human Presentable Name or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    *                 <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    * @param classLoader the fallback class loader to resolve against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    * @throws MimeTypeParseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    * @throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    * @throws  NullPointerException if <code>mimeType</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    * @see tryToLoadClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    private void initialize(String mimeType, String humanPresentableName, ClassLoader classLoader) throws MimeTypeParseException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (mimeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            throw new NullPointerException("mimeType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        this.mimeType = new MimeType(mimeType); // throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        String rcn = getParameter("class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (rcn == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                throw new IllegalArgumentException("no representation class specified for:" + mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                representationClass = java.io.InputStream.class; // default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        } else { // got a class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            representationClass = DataFlavor.tryToLoadClass(rcn, classLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        this.mimeType.setParameter("class", representationClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (humanPresentableName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            humanPresentableName = this.mimeType.getParameter("humanPresentableName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (humanPresentableName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                humanPresentableName = this.mimeType.getPrimaryType() + "/" + this.mimeType.getSubType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        this.humanPresentableName = humanPresentableName; // set it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        this.mimeType.removeParameter("humanPresentableName"); // just in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * String representation of this <code>DataFlavor</code> and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * parameters. The resulting <code>String</code> contains the name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * the <code>DataFlavor</code> class, this flavor's MIME type, and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * representation class. If this flavor has a primary MIME type of "text",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * supports the charset parameter, and has an encoded representation, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * flavor's charset is also included. See <code>selectBestTextFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * for a list of text flavors which support the charset parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @return  string representation of this <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @see #selectBestTextFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        String string = getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        string += "["+paramString()+"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    private String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        String params = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        params += "mimetype=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (mimeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            params += "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            params += mimeType.getBaseType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        params += ";representationclass=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (representationClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
           params += "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
           params += representationClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (DataTransferer.isFlavorCharsetTextType(this) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            (isRepresentationClassInputStream() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
             isRepresentationClassByteBuffer() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
             DataTransferer.byteArrayClass.equals(representationClass)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            params += ";charset=" + DataTransferer.getTextCharset(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Returns a <code>DataFlavor</code> representing plain text with Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * encoding, where:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *     representationClass = java.io.InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *     mimeType            = "text/plain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *                            charset=&lt;platform default Unicode encoding&gt;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Sun's implementation for Microsoft Windows uses the encoding <code>utf-16le</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Sun's implementation for Solaris and Linux uses the encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <code>iso-10646-ucs-2</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return a <code>DataFlavor</code> representing plain text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *    with Unicode encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public static final DataFlavor getTextPlainUnicodeFlavor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        String encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        DataTransferer transferer = DataTransferer.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (transferer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            encoding = transferer.getDefaultUnicodeEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return new DataFlavor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            "text/plain;charset="+encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            +";class=java.io.InputStream", "Plain Text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Selects the best text <code>DataFlavor</code> from an array of <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * DataFlavor</code>s. Only <code>DataFlavor.stringFlavor</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * equivalent flavors, and flavors that have a primary MIME type of "text",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * are considered for selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * Flavors are first sorted by their MIME types in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * <li>"text/sgml"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <li>"text/xml"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <li>"text/html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <li>"text/rtf"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * <li>"text/enriched"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * <li>"text/richtext"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <li>"text/uri-list"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <li>"text/tab-separated-values"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * <li>"text/t140"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <li>"text/rfc822-headers"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <li>"text/parityfec"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * <li>"text/directory"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * <li>"text/css"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <li>"text/calendar"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <li>"application/x-java-serialized-object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <li>"text/plain"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <li>"text/&lt;other&gt;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <p>For example, "text/sgml" will be selected over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * "text/html", and <code>DataFlavor.stringFlavor</code> will be chosen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * over <code>DataFlavor.plainTextFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * If two or more flavors share the best MIME type in the array, then that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * MIME type will be checked to see if it supports the charset parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * The following MIME types support, or are treated as though they support,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * the charset parameter:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * <li>"text/sgml"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * <li>"text/xml"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * <li>"text/html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <li>"text/enriched"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <li>"text/richtext"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * <li>"text/uri-list"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * <li>"text/directory"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * <li>"text/css"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <li>"text/calendar"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * <li>"application/x-java-serialized-object"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * <li>"text/plain"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * The following MIME types do not support, or are treated as though they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * do not support, the charset parameter:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * <li>"text/rtf"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * <li>"text/tab-separated-values"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <li>"text/t140"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <li>"text/rfc822-headers"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * <li>"text/parityfec"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * For "text/&lt;other&gt;" MIME types, the first time the JRE needs to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * determine whether the MIME type supports the charset parameter, it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * check whether the parameter is explicitly listed in an arbitrarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * chosen <code>DataFlavor</code> which uses that MIME type. If so, the JRE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * will assume from that point on that the MIME type supports the charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * parameter and will not check again. If the parameter is not explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * listed, the JRE will assume from that point on that the MIME type does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * not support the charset parameter and will not check again. Because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * this check is performed on an arbitrarily chosen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * <code>DataFlavor</code>, developers must ensure that all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * <code>DataFlavor</code>s with a "text/&lt;other&gt;" MIME type specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * the charset parameter if it is supported by that MIME type. Developers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * should never rely on the JRE to substitute the platform's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * charset for a "text/&lt;other&gt;" DataFlavor. Failure to adhere to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * restriction will lead to undefined behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * If the best MIME type in the array does not support the charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * parameter, the flavors which share that MIME type will then be sorted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * their representation classes in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <code>[B</code>, &lt;all others&gt;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * If two or more flavors share the best representation class, or if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * flavor has one of the three specified representations, then one of those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * flavors will be chosen non-deterministically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * If the best MIME type in the array does support the charset parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * the flavors which share that MIME type will then be sorted by their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * representation classes in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * <code>java.io.Reader</code>, <code>java.lang.String</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * <code>java.nio.CharBuffer</code>, <code>[C</code>, &lt;all others&gt;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * If two or more flavors share the best representation class, and that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * representation is one of the four explicitly listed, then one of those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * flavors will be chosen non-deterministically. If, however, no flavor has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * one of the four specified representations, the flavors will then be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * sorted by their charsets. Unicode charsets, such as "UTF-16", "UTF-8",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * "UTF-16BE", "UTF-16LE", and their aliases, are considered best. After
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * them, the platform default charset and its aliases are selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * "US-ASCII" and its aliases are worst. All other charsets are chosen in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * alphabetical order, but only charsets supported by this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * of the Java platform will be considered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * If two or more flavors share the best charset, the flavors will then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * again be sorted by their representation classes in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * <code>[B</code>, &lt;all others&gt;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * If two or more flavors share the best representation class, or if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * flavor has one of the three specified representations, then one of those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * flavors will be chosen non-deterministically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @param availableFlavors an array of available <code>DataFlavor</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @return the best (highest fidelity) flavor according to the rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *         specified above, or <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *         if <code>availableFlavors</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *         has zero length, or contains no text flavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    public static final DataFlavor selectBestTextFlavor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                                       DataFlavor[] availableFlavors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (availableFlavors == null || availableFlavors.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (textFlavorComparator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            textFlavorComparator = new TextFlavorComparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        DataFlavor bestFlavor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            (DataFlavor)Collections.max(Arrays.asList(availableFlavors),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                                        textFlavorComparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (!bestFlavor.isFlavorTextType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        return bestFlavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    private static Comparator textFlavorComparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    static class TextFlavorComparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        extends DataTransferer.DataFlavorComparator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         * Compares two <code>DataFlavor</code> objects. Returns a negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         * integer, zero, or a positive integer as the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         * <code>DataFlavor</code> is worse than, equal to, or better than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * second.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         * <code>DataFlavor</code>s are ordered according to the rules outlined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         * for <code>selectBestTextFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * @param obj1 the first <code>DataFlavor</code> to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * @param obj2 the second <code>DataFlavor</code> to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         * @return a negative integer, zero, or a positive integer as the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         *         argument is worse, equal to, or better than the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         * @throws ClassCastException if either of the arguments is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         *         instance of <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         * @throws NullPointerException if either of the arguments is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
         *         <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         * @see #selectBestTextFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        public int compare(Object obj1, Object obj2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            DataFlavor flavor1 = (DataFlavor)obj1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            DataFlavor flavor2 = (DataFlavor)obj2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            if (flavor1.isFlavorTextType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                if (flavor2.isFlavorTextType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    return super.compare(obj1, obj2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            } else if (flavor2.isFlavorTextType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * Gets a Reader for a text flavor, decoded, if necessary, for the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * charset (encoding). The supported representation classes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * <code>java.io.Reader</code>, <code>java.lang.String</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * <code>java.nio.CharBuffer</code>, <code>[C</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * and <code>[B</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * Because text flavors which do not support the charset parameter are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * encoded in a non-standard format, this method should not be called for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * such flavors. However, in order to maintain backward-compatibility,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * if this method is called for such a flavor, this method will treat the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * flavor as though it supports the charset parameter and attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * decode it accordingly. See <code>selectBestTextFlavor</code> for a list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * of text flavors which do not support the charset parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * @param transferable the <code>Transferable</code> whose data will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *        requested in this flavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @return a <code>Reader</code> to read the <code>Transferable</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *         data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @exception IllegalArgumentException if the representation class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *            is not one of the seven listed above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @exception IllegalArgumentException if the <code>Transferable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *            has <code>null</code> data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @exception NullPointerException if the <code>Transferable</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *            <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @exception UnsupportedEncodingException if this flavor's representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *            is <code>java.io.InputStream</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *            <code>java.nio.ByteBuffer</code>, or <code>[B</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *            this flavor's encoding is not supported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *            implementation of the Java platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @exception UnsupportedFlavorException if the <code>Transferable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *            does not support this flavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @exception IOException if the data cannot be read because of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *            I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @see #selectBestTextFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public Reader getReaderForText(Transferable transferable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        throws UnsupportedFlavorException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        Object transferObject = transferable.getTransferData(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (transferObject == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                ("getTransferData() returned null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if (transferObject instanceof Reader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            return (Reader)transferObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        } else if (transferObject instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            return new StringReader((String)transferObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } else if (transferObject instanceof CharBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            CharBuffer buffer = (CharBuffer)transferObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            int size = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            char[] chars = new char[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            buffer.get(chars, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            return new CharArrayReader(chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        } else if (transferObject instanceof char[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            return new CharArrayReader((char[])transferObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        InputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        if (transferObject instanceof InputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            stream = (InputStream)transferObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        } else if (transferObject instanceof ByteBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            ByteBuffer buffer = (ByteBuffer)transferObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            int size = buffer.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            byte[] bytes = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            buffer.get(bytes, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            stream = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        } else if (transferObject instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            stream = new ByteArrayInputStream((byte[])transferObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        if (stream == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            throw new IllegalArgumentException("transfer data is not Reader, String, CharBuffer, char array, InputStream, ByteBuffer, or byte array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        String encoding = getParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        return (encoding == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            ? new InputStreamReader(stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            : new InputStreamReader(stream, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * Returns the MIME type string for this <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @return the MIME type string for this flavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public String getMimeType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        return (mimeType != null) ? mimeType.toString() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * Returns the <code>Class</code> which objects supporting this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * <code>DataFlavor</code> will return when this <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * is requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @return the <code>Class</code> which objects supporting this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * <code>DataFlavor</code> will return when this <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * is requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    public Class<?> getRepresentationClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        return representationClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Returns the human presentable name for the data format that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * <code>DataFlavor</code> represents.  This name would be localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * for different countries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @return the human presentable name for the data format that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *    <code>DataFlavor</code> represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    public String getHumanPresentableName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        return humanPresentableName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * Returns the primary MIME type for this <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * @return the primary MIME type of this <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    public String getPrimaryType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        return (mimeType != null) ? mimeType.getPrimaryType() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * Returns the sub MIME type of this <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @return the Sub MIME type of this <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    public String getSubType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        return (mimeType != null) ? mimeType.getSubType() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Returns the human presentable name for this <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * if <code>paramName</code> equals "humanPresentableName".  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * returns the MIME type value associated with <code>paramName</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @param paramName the parameter name requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @return the value of the name parameter, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *  if there is no associated value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    public String getParameter(String paramName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        if (paramName.equals("humanPresentableName")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            return humanPresentableName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            return (mimeType != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                ? mimeType.getParameter(paramName) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * Sets the human presentable name for the data format that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * <code>DataFlavor</code> represents. This name would be localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * for different countries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @param humanPresentableName the new human presentable name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    public void setHumanPresentableName(String humanPresentableName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        this.humanPresentableName = humanPresentableName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * The equals comparison for the {@code DataFlavor} class is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * as follows: Two <code>DataFlavor</code>s are considered equal if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * only if their MIME primary type and subtype and representation class are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * equal. Additionally, if the primary type is "text", the subtype denotes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * a text flavor which supports the charset parameter, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * representation class is not <code>java.io.Reader</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <code>java.lang.String</code>, <code>java.nio.CharBuffer</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * <code>[C</code>, the <code>charset</code> parameter must also be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * If a charset is not explicitly specified for one or both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * <code>DataFlavor</code>s, the platform default encoding is assumed. See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * <code>selectBestTextFlavor</code> for a list of text flavors which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * support the charset parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @param o the <code>Object</code> to compare with <code>this</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @return <code>true</code> if <code>that</code> is equivalent to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *         <code>DataFlavor</code>; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @see #selectBestTextFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        return ((o instanceof DataFlavor) && equals((DataFlavor)o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * This method has the same behavior as {@link #equals(Object)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * The only difference being that it takes a {@code DataFlavor} instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * as a parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @param that the <code>DataFlavor</code> to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *        <code>this</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @return <code>true</code> if <code>that</code> is equivalent to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *         <code>DataFlavor</code>; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @see #selectBestTextFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    public boolean equals(DataFlavor that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        if (that == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        if (this == that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        if (representationClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (that.getRepresentationClass() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            if (!representationClass.equals(that.getRepresentationClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (mimeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            if (that.mimeType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            if (!mimeType.match(that.mimeType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1012
            if ("text".equals(getPrimaryType())) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1013
                if (DataTransferer.doesSubtypeSupportCharset(this) &&
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1014
                    representationClass != null &&
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1015
                    !(isRepresentationClassReader() ||
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1016
                        String.class.equals(representationClass) ||
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1017
                        isRepresentationClassCharBuffer() ||
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1018
                        DataTransferer.charArrayClass.equals(representationClass)))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1019
                {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1020
                    String thisCharset =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1021
                        DataTransferer.canonicalName(getParameter("charset"));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1022
                    String thatCharset =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1023
                        DataTransferer.canonicalName(that.getParameter("charset"));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1024
                    if (thisCharset == null) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1025
                        if (thatCharset != null) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1026
                            return false;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1027
                        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1028
                    } else {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1029
                        if (!thisCharset.equals(thatCharset)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1030
                            return false;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1031
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1033
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1034
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1035
                if ("html".equals(getSubType()) &&
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1036
                        this.getParameter("document") != null )
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1037
                {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1038
                   if (!this.getParameter("document").
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1039
                            equals(that.getParameter("document")))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 5506
diff changeset
  1040
                    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * Compares only the <code>mimeType</code> against the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * <code>String</code> and <code>representationClass</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * not considered in the comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * If <code>representationClass</code> needs to be compared, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * <code>equals(new DataFlavor(s))</code> may be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @deprecated As inconsistent with <code>hashCode()</code> contract,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     *             use <code>isMimeTypeEqual(String)</code> instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @param s the {@code mimeType} to compare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @return true if the String (MimeType) is equal; false otherwise or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *         {@code s} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    public boolean equals(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        if (s == null || mimeType == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        return isMimeTypeEqual(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * Returns hash code for this <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * For two equal <code>DataFlavor</code>s, hash codes are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * For the <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * that matches <code>DataFlavor.equals(String)</code>, it is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * guaranteed that <code>DataFlavor</code>'s hash code is equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * to the hash code of the <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @return a hash code for this <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        if (representationClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            total += representationClass.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (mimeType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            String primaryType = mimeType.getPrimaryType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            if (primaryType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                total += primaryType.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            // Do not add subType.hashCode() to the total. equals uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            // MimeType.match which reports a match if one or both of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            // subTypes is '*', regardless of the other subType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            if ("text".equals(primaryType) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                DataTransferer.doesSubtypeSupportCharset(this) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                representationClass != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                !(isRepresentationClassReader() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                  String.class.equals(representationClass) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                  isRepresentationClassCharBuffer() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                  DataTransferer.charArrayClass.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                  (representationClass)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                String charset =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                    DataTransferer.canonicalName(getParameter("charset"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                if (charset != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    total += charset.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * Identical to {@link #equals(DataFlavor)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * @param that the <code>DataFlavor</code> to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *        <code>this</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @return <code>true</code> if <code>that</code> is equivalent to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *         <code>DataFlavor</code>; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @see #selectBestTextFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    public boolean match(DataFlavor that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        return equals(that);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Returns whether the string representation of the MIME type passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * is equivalent to the MIME type of this <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * Parameters are not included in the comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * @param mimeType the string representation of the MIME type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * @return true if the string representation of the MIME type passed in is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     *         equivalent to the MIME type of this <code>DataFlavor</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *         false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * @throws NullPointerException if mimeType is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    public boolean isMimeTypeEqual(String mimeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        // JCK Test DataFlavor0117: if 'mimeType' is null, throw NPE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        if (mimeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            throw new NullPointerException("mimeType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        if (this.mimeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            return this.mimeType.match(new MimeType(mimeType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        } catch (MimeTypeParseException mtpe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Compares the <code>mimeType</code> of two <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * objects. No parameters are considered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @param dataFlavor the <code>DataFlavor</code> to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @return true if the <code>MimeType</code>s are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *  otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    public final boolean isMimeTypeEqual(DataFlavor dataFlavor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        return isMimeTypeEqual(dataFlavor.mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * Compares the <code>mimeType</code> of two <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * objects.  No parameters are considered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @return true if the <code>MimeType</code>s are equal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *  otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    private boolean isMimeTypeEqual(MimeType mtype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (this.mimeType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            return (mtype == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        return mimeType.match(mtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    * Does the <code>DataFlavor</code> represent a serialized object?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    public boolean isMimeTypeSerializedObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        return isMimeTypeEqual(javaSerializedObjectMimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    public final Class<?> getDefaultRepresentationClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        return ioInputStreamClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    public final String getDefaultRepresentationClassAsString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        return getDefaultRepresentationClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    * Does the <code>DataFlavor</code> represent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    * <code>java.io.InputStream</code>?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    public boolean isRepresentationClassInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        return ioInputStreamClass.isAssignableFrom(representationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * Returns whether the representation class for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * <code>DataFlavor</code> is <code>java.io.Reader</code> or a subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * thereof.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    public boolean isRepresentationClassReader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        return java.io.Reader.class.isAssignableFrom(representationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * Returns whether the representation class for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * <code>DataFlavor</code> is <code>java.nio.CharBuffer</code> or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * subclass thereof.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    public boolean isRepresentationClassCharBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        return java.nio.CharBuffer.class.isAssignableFrom(representationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * Returns whether the representation class for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * <code>DataFlavor</code> is <code>java.nio.ByteBuffer</code> or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * subclass thereof.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    public boolean isRepresentationClassByteBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        return java.nio.ByteBuffer.class.isAssignableFrom(representationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    * Returns true if the representation class can be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    * @return true if the representation class can be serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    public boolean isRepresentationClassSerializable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        return java.io.Serializable.class.isAssignableFrom(representationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    * Returns true if the representation class is <code>Remote</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    * @return true if the representation class is <code>Remote</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    public boolean isRepresentationClassRemote() {
4267
3dee4217d3dc 6890458: Datatransfer API should not require RMI to be present
alanb
parents: 2
diff changeset
  1258
        return DataTransferer.isRemote(representationClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    * Returns true if the <code>DataFlavor</code> specified represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    * a serialized object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    * @return true if the <code>DataFlavor</code> specified represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    *   a Serialized Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    public boolean isFlavorSerializedObjectType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        return isRepresentationClassSerializable() && isMimeTypeEqual(javaSerializedObjectMimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * Returns true if the <code>DataFlavor</code> specified represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * a remote object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * @return true if the <code>DataFlavor</code> specified represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     *  a Remote Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    public boolean isFlavorRemoteObjectType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        return isRepresentationClassRemote()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            && isRepresentationClassSerializable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            && isMimeTypeEqual(javaRemoteObjectMimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    * Returns true if the <code>DataFlavor</code> specified represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    * a list of file objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    * @return true if the <code>DataFlavor</code> specified represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
    *   a List of File objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
   public boolean isFlavorJavaFileListType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        if (mimeType == null || representationClass == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        return java.util.List.class.isAssignableFrom(representationClass) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
               mimeType.match(javaFileListFlavor.mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * Returns whether this <code>DataFlavor</code> is a valid text flavor for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * this implementation of the Java platform. Only flavors equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * <code>DataFlavor.stringFlavor</code> and <code>DataFlavor</code>s with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * a primary MIME type of "text" can be valid text flavors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * If this flavor supports the charset parameter, it must be equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * <code>DataFlavor.stringFlavor</code>, or its representation must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * <code>java.io.Reader</code>, <code>java.lang.String</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * <code>java.nio.CharBuffer</code>, <code>[C</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * <code>[B</code>. If the representation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * <code>[B</code>, then this flavor's <code>charset</code> parameter must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * be supported by this implementation of the Java platform. If a charset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * is not specified, then the platform default charset, which is always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * supported, is assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * If this flavor does not support the charset parameter, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * representation must be <code>java.io.InputStream</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * <code>java.nio.ByteBuffer</code>, or <code>[B</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * See <code>selectBestTextFlavor</code> for a list of text flavors which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * support the charset parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * @return <code>true</code> if this <code>DataFlavor</code> is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *         text flavor as described above; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * @see #selectBestTextFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    public boolean isFlavorTextType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        return (DataTransferer.isFlavorCharsetTextType(this) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                DataTransferer.isFlavorNoncharsetTextType(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    * Serializes this <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
   public synchronized void writeExternal(ObjectOutput os) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
       if (mimeType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
           mimeType.setParameter("humanPresentableName", humanPresentableName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
           os.writeObject(mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
           mimeType.removeParameter("humanPresentableName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
       } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
           os.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
       os.writeObject(representationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    * Restores this <code>DataFlavor</code> from a Serialized state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
   public synchronized void readExternal(ObjectInput is) throws IOException , ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
       String rcn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        mimeType = (MimeType)is.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        if (mimeType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            humanPresentableName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                mimeType.getParameter("humanPresentableName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            mimeType.removeParameter("humanPresentableName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            rcn = mimeType.getParameter("class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            if (rcn == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                throw new IOException("no class parameter specified in: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                                      mimeType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            representationClass = (Class)is.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        } catch (OptionalDataException ode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            if (!ode.eof || ode.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                throw ode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            // Ensure backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            // Old versions didn't write the representation class to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            if (rcn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                representationClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    DataFlavor.tryToLoadClass(rcn, getClass().getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    * Returns a clone of this <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    * @return a clone of this <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        Object newObj = super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        if (mimeType != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            ((DataFlavor)newObj).mimeType = (MimeType)mimeType.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        return newObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    } // clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    * Called on <code>DataFlavor</code> for every MIME Type parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    * to allow <code>DataFlavor</code> subclasses to handle special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    * parameters like the text/plain <code>charset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    * parameters, whose values are case insensitive.  (MIME type parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    * values are supposed to be case sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    * This method is called for each parameter name/value pair and should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    * return the normalized representation of the <code>parameterValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    * This method is never invoked by this implementation from 1.1 onwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    protected String normalizeMimeTypeParameter(String parameterName, String parameterValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        return parameterValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    * Called for each MIME type string to give <code>DataFlavor</code> subtypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    * the opportunity to change how the normalization of MIME types is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    * accomplished.  One possible use would be to add default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    * parameter/value pairs in cases where none are present in the MIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    * type string passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    * This method is never invoked by this implementation from 1.1 onwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    protected String normalizeMimeType(String mimeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        return mimeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    /* placeholder for caching any platform-specific data for flavor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    transient int       atom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    /* Mime Type of DataFlavor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    MimeType            mimeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    private String      humanPresentableName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    /** Java class of objects this DataFlavor represents **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    private Class       representationClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
} // class DataFlavor