jdk/src/share/classes/java/awt/datatransfer/SystemFlavorMap.java
author pchelko
Fri, 31 Jan 2014 14:20:40 +0400
changeset 23279 16c1ddb7b66a
parent 22281 830790e8640d
child 23588 b0269b21e313
permissions -rw-r--r--
8030093: Generify DataTrasfer classes Reviewed-by: anthony, serb
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: 21598
diff changeset
     2
 * Copyright (c) 1997, 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: 438
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: 438
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: 438
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 438
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 438
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.datatransfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.HashSet;
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
    43
import java.util.LinkedHashSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.awt.datatransfer.DataTransferer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The SystemFlavorMap is a configurable map between "natives" (Strings), which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * correspond to platform-specific data formats, and "flavors" (DataFlavors),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * which correspond to platform-independent MIME types. This mapping is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * by the data transfer subsystem to transfer data between Java and native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * applications, and between Java applications in separate VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public final class SystemFlavorMap implements FlavorMap, FlavorTable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Constant prefix used to tag Java types converted to native platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static String JavaMIME = "JAVA_DATAFLAVOR:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * System singleton which maps a thread's ClassLoader to a SystemFlavorMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
    72
    private static final WeakHashMap<ClassLoader, FlavorMap> flavorMaps = new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Copied from java.util.Properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final String keyValueSeparators = "=: \t\r\n\f";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final String strictKeyValueSeparators = "=:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final String whiteSpaceChars = " \t\r\n\f";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The list of valid, decoded text flavor representation classes, in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * from best to worst.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final String[] UNICODE_TEXT_CLASSES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        "java.io.Reader", "java.lang.String", "java.nio.CharBuffer", "\"[C\""
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The list of valid, encoded text flavor representation classes, in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * from best to worst.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final String[] ENCODED_TEXT_CLASSES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        "java.io.InputStream", "java.nio.ByteBuffer", "\"[B\""
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * A String representing text/plain MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final String TEXT_PLAIN_BASE_TYPE = "text/plain";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   103
     * A String representing text/html MIME type.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   104
     */
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   105
    private static final String HTML_TEXT_BASE_TYPE = "text/html";
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   106
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   107
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * This constant is passed to flavorToNativeLookup() to indicate that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * a native should be synthesized, stored, and returned by encoding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * DataFlavor's MIME type in case if the DataFlavor is not found in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * 'flavorToNative' map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static final boolean SYNTHESIZE_IF_NOT_FOUND = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Maps native Strings to Lists of DataFlavors (or base type Strings for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * text DataFlavors).
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   118
     * Do not use the field directly, use getNativeToFlavor() instead.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   120
    private final Map<String, List<DataFlavor>> nativeToFlavor = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   123
     * Accessor to nativeToFlavor map.  Since we use lazy initialization we must
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   124
     * use this accessor instead of direct access to the field which may not be
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   125
     * initialized yet.  This method will initialize the field if needed.
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   126
     *
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   127
     * @return nativeToFlavor
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   128
     */
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   129
    private Map<String, List<DataFlavor>> getNativeToFlavor() {
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   130
        if (!isMapInitialized) {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   131
            initSystemFlavorMap();
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   132
        }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   133
        return nativeToFlavor;
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   134
    }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   135
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   136
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Maps DataFlavors (or base type Strings for text DataFlavors) to Lists of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * native Strings.
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   139
     * Do not use the field directly, use getFlavorToNative() instead.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   141
    private final Map<DataFlavor, List<String>> flavorToNative = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   144
     * Accessor to flavorToNative map.  Since we use lazy initialization we must
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   145
     * use this accessor instead of direct access to the field which may not be
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   146
     * initialized yet.  This method will initialize the field if needed.
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   147
     *
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   148
     * @return flavorToNative
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   149
     */
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   150
    private synchronized Map<DataFlavor, List<String>> getFlavorToNative() {
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   151
        if (!isMapInitialized) {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   152
            initSystemFlavorMap();
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   153
        }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   154
        return flavorToNative;
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   155
    }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   156
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   157
    /**
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   158
     * Shows if the object has been initialized.
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   159
     */
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   160
    private boolean isMapInitialized = false;
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   161
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   162
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Caches the result of getNativesForFlavor(). Maps DataFlavors to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * SoftReferences which reference Lists of String natives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   166
    private Map<DataFlavor, SoftReference<List<String>>> getNativesForFlavorCache = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Caches the result getFlavorsForNative(). Maps String natives to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * SoftReferences which reference Lists of DataFlavors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   172
    private Map<String, SoftReference<List<DataFlavor>>> getFlavorsForNativeCache = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Dynamic mapping generation used for text mappings should not be applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * to the DataFlavors and String natives for which the mappings have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * explicitly specified with setFlavorsForNative() or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * setNativesForFlavor(). This keeps all such keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private Set disabledMappingGenerationKeys = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Returns the default FlavorMap for this thread's ClassLoader.
22281
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 21598
diff changeset
   184
     * @return the default FlavorMap for this thread's ClassLoader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public static FlavorMap getDefaultFlavorMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        ClassLoader contextClassLoader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (contextClassLoader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            contextClassLoader = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        FlavorMap fm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        synchronized(flavorMaps) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   196
            fm = flavorMaps.get(contextClassLoader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (fm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                fm = new SystemFlavorMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                flavorMaps.put(contextClassLoader, fm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return fm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   206
    private SystemFlavorMap() {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   207
    }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   208
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   210
     * Initializes a SystemFlavorMap by reading flavormap.properties and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * AWT.DnD.flavorMapFileURL.
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   212
     * For thread-safety must be called under lock on this.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   214
    private void initSystemFlavorMap() {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   215
        if (isMapInitialized) {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   216
            return;
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   217
        }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   218
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   219
        isMapInitialized = true;
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   220
        BufferedReader flavormapDotProperties =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            java.security.AccessController.doPrivileged(
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   222
                new java.security.PrivilegedAction<BufferedReader>() {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   223
                    public BufferedReader run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        String fileName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                            File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            "lib" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                            File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                            "flavormap.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                            return new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                (new InputStreamReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                    (new File(fileName).toURI().toURL().openStream(), "ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                            System.err.println("MalformedURLException:" + e + " while loading default flavormap.properties file:" + fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                            System.err.println("IOException:" + e + " while loading default flavormap.properties file:" + fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   243
        BufferedReader flavormapURL =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            java.security.AccessController.doPrivileged(
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   245
                new java.security.PrivilegedAction<BufferedReader>() {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   246
                    public BufferedReader run() {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   247
                        String url = Toolkit.getProperty("AWT.DnD.flavorMapFileURL", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            return new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                (new InputStreamReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                    (new URL(url).openStream(), "ISO-8859-1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            System.err.println("MalformedURLException:" + e + " while reading AWT.DnD.flavorMapFileURL:" + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            System.err.println("IOException:" + e + " while reading AWT.DnD.flavorMapFileURL:" + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (flavormapDotProperties != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                parseAndStoreReader(flavormapDotProperties);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                System.err.println("IOException:" + e + " while parsing default flavormap.properties file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (flavormapURL != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                parseAndStoreReader(flavormapURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                System.err.println("IOException:" + e + " while parsing AWT.DnD.flavorMapFileURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Copied code from java.util.Properties. Parsing the data ourselves is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * only way to handle duplicate keys and values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private void parseAndStoreReader(BufferedReader in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // Get next line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            String line = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (line.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                // Continue lines that end in slashes if they are not comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                char firstChar = line.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                if (firstChar != '#' && firstChar != '!') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    while (continueLine(line)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        String nextLine = in.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        if (nextLine == null) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 109
diff changeset
   301
                            nextLine = "";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        String loppedLine =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                            line.substring(0, line.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        // Advance beyond whitespace on new line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        int startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        for(; startIndex < nextLine.length(); startIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            if (whiteSpaceChars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                    indexOf(nextLine.charAt(startIndex)) == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        nextLine = nextLine.substring(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                                      nextLine.length());
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 109
diff changeset
   316
                        line = loppedLine+nextLine;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    // Find start of key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    int len = line.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    int keyStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    for(; keyStart < len; keyStart++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        if(whiteSpaceChars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                               indexOf(line.charAt(keyStart)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    // Blank lines are ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    if (keyStart == len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    // Find separation between key and value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    int separatorIndex = keyStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    for(; separatorIndex < len; separatorIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        char currentChar = line.charAt(separatorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        if (currentChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                            separatorIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        } else if (keyValueSeparators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                       indexOf(currentChar) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    // Skip over whitespace after key if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    int valueIndex = separatorIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    for (; valueIndex < len; valueIndex++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        if (whiteSpaceChars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                indexOf(line.charAt(valueIndex)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    // Skip over one non whitespace key value separators if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    if (valueIndex < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        if (strictKeyValueSeparators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                indexOf(line.charAt(valueIndex)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                            valueIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    // Skip over white space after other separators if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    while (valueIndex < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        if (whiteSpaceChars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                indexOf(line.charAt(valueIndex)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        valueIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    String key = line.substring(keyStart, separatorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    String value = (separatorIndex < len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        ? line.substring(valueIndex, len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        : "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    // Convert then store key and value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    key = loadConvert(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    value = loadConvert(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        MimeType mime = new MimeType(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        if ("text".equals(mime.getPrimaryType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                            String charset = mime.getParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                            if (DataTransferer.doesSubtypeSupportCharset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                    (mime.getSubType(), charset))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                // We need to store the charset and eoln
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                // parameters, if any, so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                // DataTransferer will have this information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                // for conversion into the native format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                DataTransferer transferer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                    DataTransferer.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                if (transferer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                    transferer.registerTextFlavorProperties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                        (key, charset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                         mime.getParameter("eoln"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                         mime.getParameter("terminators"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                            // But don't store any of these parameters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                            // DataFlavor itself for any text natives (even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                            // non-charset ones). The SystemFlavorMap will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                            // synthesize the appropriate mappings later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                            mime.removeParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                            mime.removeParameter("class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                            mime.removeParameter("eoln");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                            mime.removeParameter("terminators");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                            value = mime.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    } catch (MimeTypeParseException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    DataFlavor flavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        flavor = new DataFlavor(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                            flavor = new DataFlavor(value, (String)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        } catch (Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                            ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   429
                    final LinkedHashSet<DataFlavor> dfs = new LinkedHashSet<>();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   430
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   431
                    dfs.add(flavor);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   432
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    if ("text".equals(flavor.getPrimaryType())) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   434
                        dfs.addAll(convertMimeTypeToDataFlavors(value));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   435
                    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   436
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   437
                    for (DataFlavor df : dfs) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   438
                        store(df, key, getFlavorToNative());
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   439
                        store(key, df, getNativeToFlavor());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Copied from java.util.Properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    private boolean continueLine (String line) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        int slashCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        int index = line.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        while((index >= 0) && (line.charAt(index--) == '\\')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            slashCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        return (slashCount % 2 == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Copied from java.util.Properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private String loadConvert(String theString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        char aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int len = theString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        StringBuilder outBuffer = new StringBuilder(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        for (int x = 0; x < len; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            aChar = theString.charAt(x++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (aChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                aChar = theString.charAt(x++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                if (aChar == 'u') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    // Read the xxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        aChar = theString.charAt(x++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        switch (aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                          case '0': case '1': case '2': case '3': case '4':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                          case '5': case '6': case '7': case '8': case '9': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                             value = (value << 4) + aChar - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                          case 'a': case 'b': case 'c':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                          case 'd': case 'e': case 'f': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                             value = (value << 4) + 10 + aChar - 'a';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                          case 'A': case 'B': case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                          case 'D': case 'E': case 'F': {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                             value = (value << 4) + 10 + aChar - 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                          default: {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                              throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                           "Malformed \\uxxxx encoding.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    outBuffer.append((char)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    if (aChar == 't') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        aChar = '\t';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    } else if (aChar == 'r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                        aChar = '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    } else if (aChar == 'n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        aChar = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    } else if (aChar == 'f') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        aChar = '\f';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    outBuffer.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                outBuffer.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return outBuffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Stores the listed object under the specified hash key in map. Unlike a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * standard map, the listed object will not replace any object already at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * the appropriate Map location, but rather will be appended to a List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * stored in that location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   523
    private <H, L> void store(H hashed, L listed, Map<H, List<L>> map) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   524
        List<L> list = map.get(hashed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (list == null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   526
            list = new ArrayList<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            map.put(hashed, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (!list.contains(listed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            list.add(listed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Semantically equivalent to 'nativeToFlavor.get(nat)'. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * handles the case where 'nat' is not found in 'nativeToFlavor'. In that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * case, a new DataFlavor is synthesized, stored, and returned, if and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * only if the specified native is encoded as a Java MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     */
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   540
    private List<DataFlavor> nativeToFlavorLookup(String nat) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   541
        List<DataFlavor> flavors = getNativeToFlavor().get(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (nat != null && !disabledMappingGenerationKeys.contains(nat)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            DataTransferer transferer = DataTransferer.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            if (transferer != null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   546
                List<DataFlavor> platformFlavors =
23279
16c1ddb7b66a 8030093: Generify DataTrasfer classes
pchelko
parents: 22281
diff changeset
   547
                        transferer.getPlatformMappingsForNative(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                if (!platformFlavors.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    if (flavors != null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   550
                        platformFlavors.removeAll(new HashSet<>(flavors));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        // Prepending the platform-specific mappings ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        // that the flavors added with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        // addFlavorForUnencodedNative() are at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        // list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        platformFlavors.addAll(flavors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    flavors = platformFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (flavors == null && isJavaMIMEType(nat)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            String decoded = decodeJavaMIMEType(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            DataFlavor flavor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                flavor = new DataFlavor(decoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                System.err.println("Exception \"" + e.getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                                   ": " + e.getMessage()  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                                   "\"while constructing DataFlavor for: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                   decoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            if (flavor != null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   576
                flavors = new ArrayList<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   577
                getNativeToFlavor().put(nat, flavors);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                flavors.add(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                getFlavorsForNativeCache.remove(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                getFlavorsForNativeCache.remove(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   582
                List<String> natives = getFlavorToNative().get(flavor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                if (natives == null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   584
                    natives = new ArrayList<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   585
                    getFlavorToNative().put(flavor, natives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                natives.add(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                getNativesForFlavorCache.remove(flavor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                getNativesForFlavorCache.remove(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   593
        return (flavors != null) ? flavors : new ArrayList<>(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Semantically equivalent to 'flavorToNative.get(flav)'. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * handles the case where 'flav' is not found in 'flavorToNative' depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * on the value of passes 'synthesize' parameter. If 'synthesize' is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * SYNTHESIZE_IF_NOT_FOUND a native is synthesized, stored, and returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * encoding the DataFlavor's MIME type. Otherwise an empty List is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * and 'flavorToNative' remains unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   604
    private List<String> flavorToNativeLookup(final DataFlavor flav,
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   605
                                              final boolean synthesize) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   606
        List<String> natives = getFlavorToNative().get(flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (flav != null && !disabledMappingGenerationKeys.contains(flav)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            DataTransferer transferer = DataTransferer.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            if (transferer != null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   611
                List<String> platformNatives =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    transferer.getPlatformMappingsForFlavor(flav);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                if (!platformNatives.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    if (natives != null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   615
                        platformNatives.removeAll(new HashSet<>(natives));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        // Prepend the platform-specific mappings to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                        // that the natives added with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                        // addUnencodedNativeForFlavor() are at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        // list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                        platformNatives.addAll(natives);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    natives = platformNatives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        if (natives == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (synthesize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                String encoded = encodeDataFlavor(flav);
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   630
                natives = new ArrayList<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   631
                getFlavorToNative().put(flav, natives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                natives.add(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                getNativesForFlavorCache.remove(flav);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                getNativesForFlavorCache.remove(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   636
                List<DataFlavor> flavors = getNativeToFlavor().get(encoded);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                if (flavors == null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   638
                    flavors = new ArrayList<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   639
                    getNativeToFlavor().put(encoded, flavors);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                flavors.add(flav);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                getFlavorsForNativeCache.remove(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                getFlavorsForNativeCache.remove(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            } else {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   645
                natives = new ArrayList<>(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return natives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * Returns a <code>List</code> of <code>String</code> natives to which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * specified <code>DataFlavor</code> can be translated by the data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * subsystem. The <code>List</code> will be sorted from best native to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * worst. That is, the first native will best reflect data in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * flavor to the underlying native platform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * If the specified <code>DataFlavor</code> is previously unknown to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * data transfer subsystem and the data transfer subsystem is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * translate this <code>DataFlavor</code> to any existing native, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * invoking this method will establish a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * mapping in both directions between the specified <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * and an encoded version of its MIME type as its native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param flav the <code>DataFlavor</code> whose corresponding natives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *        should be returned. If <code>null</code> is specified, all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *        natives currently known to the data transfer subsystem are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *        returned in a non-deterministic order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @return a <code>java.util.List</code> of <code>java.lang.String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *         objects which are platform-specific representations of platform-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *         specific data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @see #encodeDataFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    public synchronized List<String> getNativesForFlavor(DataFlavor flav) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   678
        List<String> retval = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        // Check cache, even for null flav
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   681
        SoftReference<List<String>> ref = getNativesForFlavorCache.get(flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        if (ref != null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   683
            retval = ref.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            if (retval != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                // Create a copy, because client code can modify the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                // list.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   687
                return new ArrayList<>(retval);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (flav == null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   692
            retval = new ArrayList<>(getNativeToFlavor().keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        } else if (disabledMappingGenerationKeys.contains(flav)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            // In this case we shouldn't synthesize a native for this flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            // since its mappings were explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            retval = flavorToNativeLookup(flav, !SYNTHESIZE_IF_NOT_FOUND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        } else if (DataTransferer.isFlavorCharsetTextType(flav)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            // For text/* flavors, flavor-to-native mappings specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            // flavormap.properties are stored per flavor's base type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            if ("text".equals(flav.getPrimaryType())) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   702
                retval = getAllNativesForType(flav.mimeType.getBaseType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                if (retval != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    // To prevent the List stored in the map from modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    retval = new ArrayList(retval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            // Also include text/plain natives, but don't duplicate Strings
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   710
            List<String> textPlainList = getAllNativesForType(TEXT_PLAIN_BASE_TYPE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (textPlainList != null && !textPlainList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                // To prevent the List stored in the map from modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                // This also guarantees that removeAll() is supported.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   715
                textPlainList = new ArrayList<>(textPlainList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                if (retval != null && !retval.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    // Use HashSet to get constant-time performance for search.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   718
                    textPlainList.removeAll(new HashSet<>(retval));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    retval.addAll(textPlainList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    retval = textPlainList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            if (retval == null || retval.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                retval = flavorToNativeLookup(flav, SYNTHESIZE_IF_NOT_FOUND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                // In this branch it is guaranteed that natives explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                // listed for flav's MIME type were added with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                // addUnencodedNativeForFlavor(), so they have lower priority.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   731
                List<String> explicitList =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    flavorToNativeLookup(flav, !SYNTHESIZE_IF_NOT_FOUND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                // flavorToNativeLookup() never returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                // It can return an empty List, however.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                if (!explicitList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    // To prevent the List stored in the map from modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    // This also guarantees that removeAll() is supported.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   739
                    explicitList = new ArrayList<>(explicitList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    // Use HashSet to get constant-time performance for search.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   741
                    explicitList.removeAll(new HashSet<>(retval));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    retval.addAll(explicitList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        } else if (DataTransferer.isFlavorNoncharsetTextType(flav)) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   746
            retval = getAllNativesForType(flav.mimeType.getBaseType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (retval == null || retval.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                retval = flavorToNativeLookup(flav, SYNTHESIZE_IF_NOT_FOUND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                // In this branch it is guaranteed that natives explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                // listed for flav's MIME type were added with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                // addUnencodedNativeForFlavor(), so they have lower priority.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   754
                List<String> explicitList =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                    flavorToNativeLookup(flav, !SYNTHESIZE_IF_NOT_FOUND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                // flavorToNativeLookup() never returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                // It can return an empty List, however.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                if (!explicitList.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    // To prevent the List stored in the map from modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    // This also guarantees that add/removeAll() are supported.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   762
                    retval = new ArrayList<>(retval);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   763
                    explicitList = new ArrayList<>(explicitList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    // Use HashSet to get constant-time performance for search.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   765
                    explicitList.removeAll(new HashSet<>(retval));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    retval.addAll(explicitList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            retval = flavorToNativeLookup(flav, SYNTHESIZE_IF_NOT_FOUND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   773
        getNativesForFlavorCache.put(flav, new SoftReference<>(retval));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // Create a copy, because client code can modify the returned list.
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   775
        return new ArrayList<>(retval);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * Returns a <code>List</code> of <code>DataFlavor</code>s to which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * specified <code>String</code> native can be translated by the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * transfer subsystem. The <code>List</code> will be sorted from best
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <code>DataFlavor</code> to worst. That is, the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * <code>DataFlavor</code> will best reflect data in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * native to a Java application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * If the specified native is previously unknown to the data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * subsystem, and that native has been properly encoded, then invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * method will establish a mapping in both directions between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * native and a <code>DataFlavor</code> whose MIME type is a decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * version of the native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * If the specified native is not a properly encoded native and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * mappings for this native have not been altered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * <code>setFlavorsForNative</code>, then the contents of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * <code>List</code> is platform dependent, but <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * cannot be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * @param nat the native whose corresponding <code>DataFlavor</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *        should be returned. If <code>null</code> is specified, all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *        <code>DataFlavor</code>s currently known to the data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *        subsystem are returned in a non-deterministic order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @return a <code>java.util.List</code> of <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *         objects into which platform-specific data in the specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *         platform-specific native can be translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @see #encodeJavaMIMEType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    public synchronized List<DataFlavor> getFlavorsForNative(String nat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        // Check cache, even for null nat
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   812
        SoftReference<List<DataFlavor>> ref = getFlavorsForNativeCache.get(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if (ref != null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   814
            List<DataFlavor> retval = ref.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            if (retval != null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   816
                return new ArrayList<>(retval);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   820
        final LinkedHashSet <DataFlavor> returnValue =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   821
            new LinkedHashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        if (nat == null) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   824
            final List<String> natives = getNativesForFlavor(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   826
            for (String n : natives)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   828
                final List<DataFlavor> flavors = getFlavorsForNative(n);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   829
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   830
                for (DataFlavor df : flavors)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   832
                    returnValue.add(df);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        } else {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   836
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   837
            final List<DataFlavor> flavors = nativeToFlavorLookup(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            if (disabledMappingGenerationKeys.contains(nat)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                return flavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   843
            final List<DataFlavor> flavorsAndBaseTypes =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   844
                nativeToFlavorLookup(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   846
            for (DataFlavor df : flavorsAndBaseTypes) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   847
                returnValue.add(df);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   848
                if ("text".equals(df.getPrimaryType())) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   849
                    try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   850
                        returnValue.addAll(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   851
                                convertMimeTypeToDataFlavors(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   852
                                        new MimeType(df.getMimeType()
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   853
                                        ).getBaseType()));
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   854
                    } catch (MimeTypeParseException e) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   855
                        e.printStackTrace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   859
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   860
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   861
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   862
        final List<DataFlavor> arrayList = new ArrayList<>(returnValue);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   863
        getFlavorsForNativeCache.put(nat, new SoftReference<>(arrayList));
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   864
        return new ArrayList<>(arrayList);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   865
    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   866
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   867
    private static Set<DataFlavor> convertMimeTypeToDataFlavors(
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   868
        final String baseType) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   869
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   870
        final Set<DataFlavor> returnValue = new LinkedHashSet<>();
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   871
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   872
        String subType = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   873
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   874
        try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   875
            final MimeType mimeType = new MimeType(baseType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   876
            subType = mimeType.getSubType();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   877
        } catch (MimeTypeParseException mtpe) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   878
            // Cannot happen, since we checked all mappings
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   879
            // on load from flavormap.properties.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   880
            assert(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   883
        if (DataTransferer.doesSubtypeSupportCharset(subType, null)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   884
            if (TEXT_PLAIN_BASE_TYPE.equals(baseType))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   885
            {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   886
                returnValue.add(DataFlavor.stringFlavor);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   887
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   888
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   889
            for (String unicodeClassName : UNICODE_TEXT_CLASSES) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   890
                final String mimeType = baseType + ";charset=Unicode;class=" +
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   891
                                            unicodeClassName;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   892
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   893
                final LinkedHashSet<String> mimeTypes =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   894
                    handleHtmlMimeTypes(baseType, mimeType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   895
                for (String mt : mimeTypes) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   896
                    DataFlavor toAdd = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   897
                    try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   898
                        toAdd = new DataFlavor(mt);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   899
                    } catch (ClassNotFoundException cannotHappen) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   900
                    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   901
                    returnValue.add(toAdd);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   902
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   903
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   904
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   905
            for (String charset : DataTransferer.standardEncodings()) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   906
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   907
                for (String encodedTextClass : ENCODED_TEXT_CLASSES) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   908
                    final String mimeType =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   909
                            baseType + ";charset=" + charset +
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   910
                            ";class=" + encodedTextClass;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   911
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   912
                    final LinkedHashSet<String> mimeTypes =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   913
                        handleHtmlMimeTypes(baseType, mimeType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   914
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   915
                    for (String mt : mimeTypes) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   916
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   917
                        DataFlavor df = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   918
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   919
                        try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   920
                            df = new DataFlavor(mt);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   921
                            // Check for equality to plainTextFlavor so
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   922
                            // that we can ensure that the exact charset of
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   923
                            // plainTextFlavor, not the canonical charset
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   924
                            // or another equivalent charset with a
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   925
                            // different name, is used.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   926
                            if (df.equals(DataFlavor.plainTextFlavor)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   927
                                df = DataFlavor.plainTextFlavor;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   928
                            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   929
                        } catch (ClassNotFoundException cannotHappen) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   930
                        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   931
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   932
                        returnValue.add(df);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   933
                    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   934
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   935
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   936
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   937
            if (TEXT_PLAIN_BASE_TYPE.equals(baseType))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   938
            {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   939
                returnValue.add(DataFlavor.plainTextFlavor);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   940
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   941
        } else {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   942
            // Non-charset text natives should be treated as
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   943
            // opaque, 8-bit data in any of its various
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   944
            // representations.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   945
            for (String encodedTextClassName : ENCODED_TEXT_CLASSES) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   946
                DataFlavor toAdd = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   947
                try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   948
                    toAdd = new DataFlavor(baseType +
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   949
                         ";class=" + encodedTextClassName);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   950
                } catch (ClassNotFoundException cannotHappen) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   951
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   952
                returnValue.add(toAdd);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   953
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   954
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   955
        return returnValue;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   956
    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   957
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   958
    private static final String [] htmlDocumntTypes =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   959
        new String [] {"all", "selection", "fragment"};
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   960
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   961
    private static LinkedHashSet<String> handleHtmlMimeTypes(
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   962
        String baseType, String mimeType) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   963
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   964
        LinkedHashSet<String> returnValues = new LinkedHashSet<>();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   965
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   966
        if (HTML_TEXT_BASE_TYPE.equals(baseType)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   967
            for (String documentType : htmlDocumntTypes) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   968
                returnValues.add(mimeType + ";document=" + documentType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   969
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   970
        } else {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   971
            returnValues.add(mimeType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   972
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   973
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   974
        return returnValues;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Returns a <code>Map</code> of the specified <code>DataFlavor</code>s to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * their most preferred <code>String</code> native. Each native value will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * be the same as the first native in the List returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <code>getNativesForFlavor</code> for the specified flavor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * If a specified <code>DataFlavor</code> is previously unknown to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * data transfer subsystem, then invoking this method will establish a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * mapping in both directions between the specified <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * and an encoded version of its MIME type as its native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * @param flavors an array of <code>DataFlavor</code>s which will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *        key set of the returned <code>Map</code>. If <code>null</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *        specified, a mapping of all <code>DataFlavor</code>s known to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *        data transfer subsystem to their most preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *        <code>String</code> natives will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @return a <code>java.util.Map</code> of <code>DataFlavor</code>s to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     *         <code>String</code> natives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @see #getNativesForFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @see #encodeDataFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    public synchronized Map<DataFlavor,String>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        getNativesForFlavors(DataFlavor[] flavors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        // Use getNativesForFlavor to generate extra natives for text flavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        // and stringFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        if (flavors == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            List flavor_list = getFlavorsForNative(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            flavors = new DataFlavor[flavor_list.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            flavor_list.toArray(flavors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1011
        Map<DataFlavor, String> retval = new HashMap<>(flavors.length, 1.0f);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1012
        for (DataFlavor flavor : flavors) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1013
            List<String> natives = getNativesForFlavor(flavor);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1014
            String nat = (natives.isEmpty()) ? null : natives.get(0);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1015
            retval.put(flavor, nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * Returns a <code>Map</code> of the specified <code>String</code> natives
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * to their most preferred <code>DataFlavor</code>. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * <code>DataFlavor</code> value will be the same as the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * <code>DataFlavor</code> in the List returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * <code>getFlavorsForNative</code> for the specified native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * If a specified native is previously unknown to the data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * subsystem, and that native has been properly encoded, then invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * method will establish a mapping in both directions between the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * native and a <code>DataFlavor</code> whose MIME type is a decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * version of the native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @param natives an array of <code>String</code>s which will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *        key set of the returned <code>Map</code>. If <code>null</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *        specified, a mapping of all supported <code>String</code> natives
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *        to their most preferred <code>DataFlavor</code>s will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *        returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @return a <code>java.util.Map</code> of <code>String</code> natives to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *         <code>DataFlavor</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @see #getFlavorsForNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @see #encodeJavaMIMEType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    public synchronized Map<String,DataFlavor>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        getFlavorsForNatives(String[] natives)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        // Use getFlavorsForNative to generate extra flavors for text natives
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        if (natives == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            List native_list = getNativesForFlavor(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            natives = new String[native_list.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            native_list.toArray(natives);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1056
        Map<String, DataFlavor> retval = new HashMap<>(natives.length, 1.0f);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1057
        for (String aNative : natives) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1058
            List<DataFlavor> flavors = getFlavorsForNative(aNative);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1059
            DataFlavor flav = (flavors.isEmpty())? null : flavors.get(0);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1060
            retval.put(aNative, flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Adds a mapping from the specified <code>DataFlavor</code> (and all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * <code>DataFlavor</code>s equal to the specified <code>DataFlavor</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * to the specified <code>String</code> native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * Unlike <code>getNativesForFlavor</code>, the mapping will only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * established in one direction, and the native will not be encoded. To
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * establish a two-way mapping, call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * <code>addFlavorForUnencodedNative</code> as well. The new mapping will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * be of lower priority than any existing mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * This method has no effect if a mapping from the specified or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * <code>DataFlavor</code> to the specified <code>String</code> native
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * already exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @param flav the <code>DataFlavor</code> key for the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @param nat the <code>String</code> native value for the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @throws NullPointerException if flav or nat is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @see #addFlavorForUnencodedNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    public synchronized void addUnencodedNativeForFlavor(DataFlavor flav,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                                                         String nat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        if (flav == null || nat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            throw new NullPointerException("null arguments not permitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1092
        List<String> natives = getFlavorToNative().get(flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (natives == null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1094
            natives = new ArrayList<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
  1095
            getFlavorToNative().put(flav, natives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        } else if (natives.contains(nat)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        natives.add(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        getNativesForFlavorCache.remove(flav);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        getNativesForFlavorCache.remove(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * Discards the current mappings for the specified <code>DataFlavor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * and all <code>DataFlavor</code>s equal to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * <code>DataFlavor</code>, and creates new mappings to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * specified <code>String</code> natives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * Unlike <code>getNativesForFlavor</code>, the mappings will only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * established in one direction, and the natives will not be encoded. To
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * establish two-way mappings, call <code>setFlavorsForNative</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * as well. The first native in the array will represent the highest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * priority mapping. Subsequent natives will represent mappings of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * decreasing priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * If the array contains several elements that reference equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * <code>String</code> natives, this method will establish new mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * for the first of those elements and ignore the rest of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * It is recommended that client code not reset mappings established by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * data transfer subsystem. This method should only be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * application-level mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @param flav the <code>DataFlavor</code> key for the mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @param natives the <code>String</code> native values for the mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @throws NullPointerException if flav or natives is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     *         or if natives contains <code>null</code> elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * @see #setFlavorsForNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    public synchronized void setNativesForFlavor(DataFlavor flav,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                                                 String[] natives) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        if (flav == null || natives == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            throw new NullPointerException("null arguments not permitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
  1138
        getFlavorToNative().remove(flav);
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1139
        for (String aNative : natives) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1140
            addUnencodedNativeForFlavor(flav, aNative);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        disabledMappingGenerationKeys.add(flav);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        // Clear the cache to handle the case of empty natives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        getNativesForFlavorCache.remove(flav);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        getNativesForFlavorCache.remove(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * Adds a mapping from a single <code>String</code> native to a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * <code>DataFlavor</code>. Unlike <code>getFlavorsForNative</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * mapping will only be established in one direction, and the native will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * not be encoded. To establish a two-way mapping, call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <code>addUnencodedNativeForFlavor</code> as well. The new mapping will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * be of lower priority than any existing mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * This method has no effect if a mapping from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * <code>String</code> native to the specified or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <code>DataFlavor</code> already exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * @param nat the <code>String</code> native key for the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @param flav the <code>DataFlavor</code> value for the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @throws NullPointerException if nat or flav is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @see #addUnencodedNativeForFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    public synchronized void addFlavorForUnencodedNative(String nat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                                                         DataFlavor flav) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        if (nat == null || flav == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            throw new NullPointerException("null arguments not permitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1172
        List<DataFlavor> flavors = getNativeToFlavor().get(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        if (flavors == null) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1174
            flavors = new ArrayList<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
  1175
            getNativeToFlavor().put(nat, flavors);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        } else if (flavors.contains(flav)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        flavors.add(flav);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        getFlavorsForNativeCache.remove(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        getFlavorsForNativeCache.remove(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * Discards the current mappings for the specified <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * native, and creates new mappings to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * <code>DataFlavor</code>s. Unlike <code>getFlavorsForNative</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * mappings will only be established in one direction, and the natives need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * not be encoded. To establish two-way mappings, call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * <code>setNativesForFlavor</code> as well. The first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * <code>DataFlavor</code> in the array will represent the highest priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * mapping. Subsequent <code>DataFlavor</code>s will represent mappings of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * decreasing priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * If the array contains several elements that reference equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * <code>DataFlavor</code>s, this method will establish new mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * for the first of those elements and ignore the rest of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * It is recommended that client code not reset mappings established by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * data transfer subsystem. This method should only be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * application-level mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * @param nat the <code>String</code> native key for the mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * @param flavors the <code>DataFlavor</code> values for the mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * @throws NullPointerException if nat or flavors is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *         or if flavors contains <code>null</code> elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @see #setNativesForFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    public synchronized void setFlavorsForNative(String nat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                                                 DataFlavor[] flavors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        if (nat == null || flavors == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            throw new NullPointerException("null arguments not permitted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
  1217
        getNativeToFlavor().remove(nat);
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1218
        for (DataFlavor flavor : flavors) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1219
            addFlavorForUnencodedNative(nat, flavor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        disabledMappingGenerationKeys.add(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        // Clear the cache to handle the case of empty flavors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        getFlavorsForNativeCache.remove(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        getFlavorsForNativeCache.remove(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * Encodes a MIME type for use as a <code>String</code> native. The format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * of an encoded representation of a MIME type is implementation-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * The only restrictions are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * <li>The encoded representation is <code>null</code> if and only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * MIME type <code>String</code> is <code>null</code>.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * <li>The encoded representations for two non-<code>null</code> MIME type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * <code>String</code>s are equal if and only if these <code>String</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * are equal according to <code>String.equals(Object)</code>.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * <p>
15263
f401cac7510b 8005250: Downgrade normative references to ${java.home}/lib folder from Java client code.
uta
parents: 5506
diff changeset
  1239
     * The reference implementation of this method returns the specified MIME
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * type <code>String</code> prefixed with <code>JAVA_DATAFLAVOR:</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @param mimeType the MIME type to encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * @return the encoded <code>String</code>, or <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *         mimeType is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    public static String encodeJavaMIMEType(String mimeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        return (mimeType != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            ? JavaMIME + mimeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * Encodes a <code>DataFlavor</code> for use as a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * native. The format of an encoded <code>DataFlavor</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * implementation-dependent. The only restrictions are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * <li>The encoded representation is <code>null</code> if and only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * specified <code>DataFlavor</code> is <code>null</code> or its MIME type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * <code>String</code> is <code>null</code>.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * <li>The encoded representations for two non-<code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * <code>DataFlavor</code>s with non-<code>null</code> MIME type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     * <code>String</code>s are equal if and only if the MIME type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * <code>String</code>s of these <code>DataFlavor</code>s are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * according to <code>String.equals(Object)</code>.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * <p>
15263
f401cac7510b 8005250: Downgrade normative references to ${java.home}/lib folder from Java client code.
uta
parents: 5506
diff changeset
  1267
     * The reference implementation of this method returns the MIME type
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * <code>String</code> of the specified <code>DataFlavor</code> prefixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * with <code>JAVA_DATAFLAVOR:</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * @param flav the <code>DataFlavor</code> to encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * @return the encoded <code>String</code>, or <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *         flav is <code>null</code> or has a <code>null</code> MIME type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    public static String encodeDataFlavor(DataFlavor flav) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        return (flav != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            ? SystemFlavorMap.encodeJavaMIMEType(flav.getMimeType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * Returns whether the specified <code>String</code> is an encoded Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * @param str the <code>String</code> to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * @return <code>true</code> if the <code>String</code> is encoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *         <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    public static boolean isJavaMIMEType(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        return (str != null && str.startsWith(JavaMIME, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * Decodes a <code>String</code> native for use as a Java MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * @param nat the <code>String</code> to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @return the decoded Java MIME type, or <code>null</code> if nat is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *         an encoded <code>String</code> native
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    public static String decodeJavaMIMEType(String nat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        return (isJavaMIMEType(nat))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            ? nat.substring(JavaMIME.length(), nat.length()).trim()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * Decodes a <code>String</code> native for use as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * <code>DataFlavor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * @param nat the <code>String</code> to decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * @return the decoded <code>DataFlavor</code>, or <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *         nat is not an encoded <code>String</code> native
22281
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 21598
diff changeset
  1313
     * @throws ClassNotFoundException if the class of the data flavor
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 21598
diff changeset
  1314
     * is not loaded
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    public static DataFlavor decodeDataFlavor(String nat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        String retval_str = SystemFlavorMap.decodeJavaMIMEType(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        return (retval_str != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            ? new DataFlavor(retval_str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    }
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1324
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1325
    private List<String> getAllNativesForType(String type) {
21598
b0de5e2762ed 8028230: Behavior of SystemFlavorMap.getNativesForFlavor differ from that in Java 7
pchelko
parents: 21251
diff changeset
  1326
        Set<String> retval = null;
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1327
        for (DataFlavor dataFlavor : convertMimeTypeToDataFlavors(type)) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1328
            List<String> natives = getFlavorToNative().get(dataFlavor);
21251
bf72e65e6f91 8026262: NPE in SystemFlavorMap.getAllNativesForType - regression in jdk8 b110 by fix of #JDK-8024987
pchelko
parents: 20166
diff changeset
  1329
            if (natives != null && !natives.isEmpty()) {
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1330
                if (retval == null) {
21598
b0de5e2762ed 8028230: Behavior of SystemFlavorMap.getNativesForFlavor differ from that in Java 7
pchelko
parents: 21251
diff changeset
  1331
                    retval = new LinkedHashSet<>();
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1332
                }
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1333
                retval.addAll(natives);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1334
            }
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1335
        }
21598
b0de5e2762ed 8028230: Behavior of SystemFlavorMap.getNativesForFlavor differ from that in Java 7
pchelko
parents: 21251
diff changeset
  1336
        return retval == null ? null : new ArrayList<>(retval);
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1337
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
}