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