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