src/java.datatransfer/share/classes/java/awt/datatransfer/SystemFlavorMap.java
author chagedorn
Thu, 12 Sep 2019 12:23:25 +0200
changeset 58104 46e11f978852
parent 47216 71c04702a3d5
permissions -rw-r--r--
8220416: Comparison of klass pointers is not optimized any more Summary: Enables dead klass pointer optimization (since JDK-6964458) again. Reviewed-by: roland, thartmann
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
     2
 * Copyright (c) 1997, 2017, 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.io.BufferedReader;
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
    29
import java.io.IOException;
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
    30
import java.io.InputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStreamReader;
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
    32
import java.lang.ref.SoftReference;
31898
6890dfde0ed1 8062728: [PIT] Test closed/... fails for Windows only
ssadetsky
parents: 29010
diff changeset
    33
import java.security.AccessController;
6890dfde0ed1 8062728: [PIT] Test closed/... fails for Windows only
ssadetsky
parents: 29010
diff changeset
    34
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ArrayList;
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
    36
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.HashSet;
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
    39
import java.util.LinkedHashSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Map;
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
    42
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
    45
import sun.datatransfer.DataFlavorUtil;
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
    46
import sun.datatransfer.DesktopDatatransferService;
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
    47
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The SystemFlavorMap is a configurable map between "natives" (Strings), which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * correspond to platform-specific data formats, and "flavors" (DataFlavors),
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
    51
 * which correspond to platform-independent MIME types. This mapping is used by
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
    52
 * the data transfer subsystem to transfer data between Java and native
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * applications, and between Java applications in separate VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public final class SystemFlavorMap implements FlavorMap, FlavorTable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
    60
     * Constant prefix used to tag Java types converted to native platform type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static String JavaMIME = "JAVA_DATAFLAVOR:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
23905
51af43e32270 8026797: Enhance data transfers
malenkov
parents: 23588
diff changeset
    64
    private static final Object FLAVOR_MAP_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * The list of valid, decoded text flavor representation classes, in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * from best to worst.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final String[] UNICODE_TEXT_CLASSES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        "java.io.Reader", "java.lang.String", "java.nio.CharBuffer", "\"[C\""
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The list of valid, encoded text flavor representation classes, in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * from best to worst.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final String[] ENCODED_TEXT_CLASSES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        "java.io.InputStream", "java.nio.ByteBuffer", "\"[B\""
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * A String representing text/plain MIME type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final String TEXT_PLAIN_BASE_TYPE = "text/plain";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
    88
     * A String representing text/html MIME type.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
    89
     */
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
    90
    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
    91
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
    92
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Maps native Strings to Lists of DataFlavors (or base type Strings for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * text DataFlavors).
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
    95
     * <p>
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
    96
     * Do not use the field directly, use {@link #getNativeToFlavor} instead.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
    98
    private final Map<String, LinkedHashSet<DataFlavor>> nativeToFlavor = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   101
     * Accessor to nativeToFlavor map. Since we use lazy initialization we must
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   102
     * use this accessor instead of direct access to the field which may not be
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   103
     * initialized yet. This method will initialize the field if needed.
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   104
     *
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   105
     * @return nativeToFlavor
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   106
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   107
    private Map<String, LinkedHashSet<DataFlavor>> getNativeToFlavor() {
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   108
        if (!isMapInitialized) {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   109
            initSystemFlavorMap();
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   110
        }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   111
        return nativeToFlavor;
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   112
    }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   113
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   114
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Maps DataFlavors (or base type Strings for text DataFlavors) to Lists of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * native Strings.
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   117
     * <p>
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   118
     * Do not use the field directly, use {@link #getFlavorToNative} instead.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   120
    private final Map<DataFlavor, LinkedHashSet<String>> flavorToNative = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   123
     * Accessor to flavorToNative map. Since we use lazy initialization we must
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   124
     * use this accessor instead of direct access to the field which may not be
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   125
     * initialized yet. This method will initialize the field if needed.
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   126
     *
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   127
     * @return flavorToNative
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   128
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   129
    private synchronized Map<DataFlavor, LinkedHashSet<String>> getFlavorToNative() {
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   130
        if (!isMapInitialized) {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   131
            initSystemFlavorMap();
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   132
        }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   133
        return flavorToNative;
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   134
    }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   135
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   136
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   137
     * Maps a text DataFlavor primary mime-type to the native. Used only to
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   138
     * store standard mappings registered in the {@code flavormap.properties}.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   139
     * <p>
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   140
     * Do not use this field directly, use {@link #getTextTypeToNative} instead.
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   141
     */
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   142
    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
   143
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   144
    /**
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   145
     * Shows if the object has been initialized.
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
    private boolean isMapInitialized = false;
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   148
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   149
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   150
     * An accessor to textTypeToNative map. Since we use lazy initialization we
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   151
     * must use this accessor instead of direct access to the field which may
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   152
     * not be initialized yet. This method will initialize the field if needed.
24529
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
     * @return textTypeToNative
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   155
     */
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   156
    private synchronized Map<String, LinkedHashSet<String>> getTextTypeToNative() {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   157
        if (!isMapInitialized) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   158
            initSystemFlavorMap();
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   159
            // 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
   160
            textTypeToNative = Collections.unmodifiableMap(textTypeToNative);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   161
        }
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   162
        return textTypeToNative;
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
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   165
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * 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
   167
     * SoftReferences which reference LinkedHashSet of String natives.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   169
    private final SoftCache<DataFlavor, String> nativesForFlavorCache = new SoftCache<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * 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
   173
     * SoftReferences which reference LinkedHashSet of DataFlavors.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   175
    private final SoftCache<String, DataFlavor> flavorsForNativeCache = new SoftCache<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Dynamic mapping generation used for text mappings should not be applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * to the DataFlavors and String natives for which the mappings have been
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   180
     * explicitly specified with {@link #setFlavorsForNative} or
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   181
     * {@link #setNativesForFlavor}. This keeps all such keys.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   183
    private Set<Object> disabledMappingGenerationKeys = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns the default FlavorMap for this thread's ClassLoader.
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   187
     *
22281
830790e8640d 8031467: Fix doclint missing issues in java.awt.{dnd[.peer], datatransfer}
darcy
parents: 21598
diff changeset
   188
     * @return the default FlavorMap for this thread's ClassLoader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static FlavorMap getDefaultFlavorMap() {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   191
        return DataFlavorUtil.getDesktopService().getFlavorMap(SystemFlavorMap::new);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   194
    private SystemFlavorMap() {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   195
    }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   196
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   198
     * Initializes a SystemFlavorMap by reading {@code flavormap.properties}.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   199
     * For thread-safety must be called under lock on {@code this}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   201
    private void initSystemFlavorMap() {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   202
        if (isMapInitialized) {
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   203
            return;
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   204
        }
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   205
        isMapInitialized = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
31898
6890dfde0ed1 8062728: [PIT] Test closed/... fails for Windows only
ssadetsky
parents: 29010
diff changeset
   207
        InputStream is = AccessController.doPrivileged(
6890dfde0ed1 8062728: [PIT] Test closed/... fails for Windows only
ssadetsky
parents: 29010
diff changeset
   208
            (PrivilegedAction<InputStream>) () -> {
6890dfde0ed1 8062728: [PIT] Test closed/... fails for Windows only
ssadetsky
parents: 29010
diff changeset
   209
                return SystemFlavorMap.class.getResourceAsStream(
6890dfde0ed1 8062728: [PIT] Test closed/... fails for Windows only
ssadetsky
parents: 29010
diff changeset
   210
                        "/sun/datatransfer/resources/flavormap.properties");
6890dfde0ed1 8062728: [PIT] Test closed/... fails for Windows only
ssadetsky
parents: 29010
diff changeset
   211
            });
25782
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   212
        if (is == null) {
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   213
            throw new InternalError("Default flavor mapping not found");
23905
51af43e32270 8026797: Enhance data transfers
malenkov
parents: 23588
diff changeset
   214
        }
51af43e32270 8026797: Enhance data transfers
malenkov
parents: 23588
diff changeset
   215
25782
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   216
        try (InputStreamReader isr = new InputStreamReader(is);
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   217
             BufferedReader reader = new BufferedReader(isr)) {
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   218
            String line;
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   219
            while ((line = reader.readLine()) != null) {
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   220
                line = line.trim();
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   221
                if (line.startsWith("#") || line.isEmpty()) continue;
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   222
                while (line.endsWith("\\")) {
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   223
                    line = line.substring(0, line.length() - 1) + reader.readLine().trim();
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   224
                }
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   225
                int delimiterPosition = line.indexOf('=');
29010
10d6c5d5ebcf 8062738: Test java/awt/datatransfer/MissedHtmlAndRtfBug/MissedHtmlAndRtfBug fails in Windows
serb
parents: 28987
diff changeset
   226
                String key = line.substring(0, delimiterPosition).replace("\\ ", " ");
25782
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   227
                String[] values = line.substring(delimiterPosition + 1, line.length()).split(",");
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   228
                for (String value : values) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    try {
26009
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   230
                        value = loadConvert(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        MimeType mime = new MimeType(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        if ("text".equals(mime.getPrimaryType())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            String charset = mime.getParameter("charset");
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   234
                            if (DataFlavorUtil.doesSubtypeSupportCharset(mime.getSubType(), charset))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                // We need to store the charset and eoln
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                // parameters, if any, so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                // DataTransferer will have this information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                // for conversion into the native format.
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   240
                                DesktopDatatransferService desktopService =
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   241
                                        DataFlavorUtil.getDesktopService();
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   242
                                if (desktopService.isDesktopPresent()) {
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   243
                                    desktopService.registerTextFlavorProperties(
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   244
                                            key, charset,
25782
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   245
                                            mime.getParameter("eoln"),
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   246
                                            mime.getParameter("terminators"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            // But don't store any of these parameters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                            // DataFlavor itself for any text natives (even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                            // non-charset ones). The SystemFlavorMap will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                            // synthesize the appropriate mappings later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                            mime.removeParameter("charset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                            mime.removeParameter("class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            mime.removeParameter("eoln");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            mime.removeParameter("terminators");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            value = mime.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    } catch (MimeTypeParseException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    DataFlavor flavor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        flavor = new DataFlavor(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        try {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   270
                            flavor = new DataFlavor(value, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        } catch (Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                            ee.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   277
                    final LinkedHashSet<DataFlavor> dfs = new LinkedHashSet<>();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   278
                    dfs.add(flavor);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   279
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if ("text".equals(flavor.getPrimaryType())) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   281
                        dfs.addAll(convertMimeTypeToDataFlavors(value));
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   282
                        store(flavor.mimeType.getBaseType(), key, getTextTypeToNative());
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   283
                    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   284
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   285
                    for (DataFlavor df : dfs) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   286
                        store(df, key, getFlavorToNative());
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   287
                        store(key, df, getNativeToFlavor());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
25782
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   291
        } catch (IOException e) {
5a832643a534 8047336: Read flavormap.properties as resource
pchelko
parents: 24529
diff changeset
   292
            throw new InternalError("Error reading default flavor mapping", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
26009
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   296
    // Copied from java.util.Properties
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   297
    private static String loadConvert(String theString) {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   298
        char aChar;
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   299
        int len = theString.length();
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   300
        StringBuilder outBuffer = new StringBuilder(len);
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   301
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   302
        for (int x = 0; x < len; ) {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   303
            aChar = theString.charAt(x++);
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   304
            if (aChar == '\\') {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   305
                aChar = theString.charAt(x++);
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   306
                if (aChar == 'u') {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   307
                    // Read the xxxx
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   308
                    int value = 0;
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   309
                    for (int i = 0; i < 4; i++) {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   310
                        aChar = theString.charAt(x++);
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   311
                        switch (aChar) {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   312
                            case '0': case '1': case '2': case '3': case '4':
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   313
                            case '5': case '6': case '7': case '8': case '9': {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   314
                                value = (value << 4) + aChar - '0';
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   315
                                break;
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   316
                            }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   317
                            case 'a': case 'b': case 'c':
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   318
                            case 'd': case 'e': case 'f': {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   319
                                value = (value << 4) + 10 + aChar - 'a';
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   320
                                break;
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   321
                            }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   322
                            case 'A': case 'B': case 'C':
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   323
                            case 'D': case 'E': case 'F': {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   324
                                value = (value << 4) + 10 + aChar - 'A';
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   325
                                break;
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   326
                            }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   327
                            default: {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   328
                                throw new IllegalArgumentException(
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   329
                                        "Malformed \\uxxxx encoding.");
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   330
                            }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   331
                        }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   332
                    }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   333
                    outBuffer.append((char)value);
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   334
                } else {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   335
                    if (aChar == 't') {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   336
                        aChar = '\t';
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   337
                    } else if (aChar == 'r') {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   338
                        aChar = '\r';
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   339
                    } else if (aChar == 'n') {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   340
                        aChar = '\n';
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   341
                    } else if (aChar == 'f') {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   342
                        aChar = '\f';
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   343
                    }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   344
                    outBuffer.append(aChar);
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   345
                }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   346
            } else {
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   347
                outBuffer.append(aChar);
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   348
            }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   349
        }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   350
        return outBuffer.toString();
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   351
    }
682a3a6d43a7 8051449: Incorrect parsing of the default flavor mapping
pchelko
parents: 25782
diff changeset
   352
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Stores the listed object under the specified hash key in map. Unlike a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * standard map, the listed object will not replace any object already at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * the appropriate Map location, but rather will be appended to a List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * stored in that location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   359
    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
   360
        LinkedHashSet<L> list = map.get(hashed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (list == null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   362
            list = new LinkedHashSet<>(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            map.put(hashed, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (!list.contains(listed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            list.add(listed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   371
     * Semantically equivalent to 'nativeToFlavor.get(nat)'. This method handles
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   372
     * the case where 'nat' is not found in 'nativeToFlavor'. In that case, a
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   373
     * new DataFlavor is synthesized, stored, and returned, if and only if the
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   374
     * specified native is encoded as a Java MIME type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   376
    private LinkedHashSet<DataFlavor> nativeToFlavorLookup(String nat) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   377
        LinkedHashSet<DataFlavor> flavors = getNativeToFlavor().get(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (nat != null && !disabledMappingGenerationKeys.contains(nat)) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   380
            DesktopDatatransferService desktopService = DataFlavorUtil.getDesktopService();
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   381
            if (desktopService.isDesktopPresent()) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   382
                LinkedHashSet<DataFlavor> platformFlavors =
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   383
                        desktopService.getPlatformMappingsForNative(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                if (!platformFlavors.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    if (flavors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        // Prepending the platform-specific mappings ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        // that the flavors added with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        // addFlavorForUnencodedNative() are at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        // list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        platformFlavors.addAll(flavors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    flavors = platformFlavors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (flavors == null && isJavaMIMEType(nat)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            String decoded = decodeJavaMIMEType(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            DataFlavor flavor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                flavor = new DataFlavor(decoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                System.err.println("Exception \"" + e.getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                   ": " + e.getMessage()  +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                   "\"while constructing DataFlavor for: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                   decoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (flavor != null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   411
                flavors = new LinkedHashSet<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   412
                getNativeToFlavor().put(nat, flavors);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                flavors.add(flavor);
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   414
                flavorsForNativeCache.remove(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   416
                LinkedHashSet<String> natives = getFlavorToNative().get(flavor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                if (natives == null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   418
                    natives = new LinkedHashSet<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   419
                    getFlavorToNative().put(flavor, natives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                natives.add(nat);
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   422
                nativesForFlavorCache.remove(flavor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   426
        return (flavors != null) ? flavors : new LinkedHashSet<>(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Semantically equivalent to 'flavorToNative.get(flav)'. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * handles the case where 'flav' is not found in 'flavorToNative' depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * on the value of passes 'synthesize' parameter. If 'synthesize' is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * SYNTHESIZE_IF_NOT_FOUND a native is synthesized, stored, and returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * encoding the DataFlavor's MIME type. Otherwise an empty List is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * and 'flavorToNative' remains unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   437
    private LinkedHashSet<String> flavorToNativeLookup(final DataFlavor flav,
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   438
                                                       final boolean synthesize) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   439
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   440
        LinkedHashSet<String> natives = getFlavorToNative().get(flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (flav != null && !disabledMappingGenerationKeys.contains(flav)) {
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   443
            DesktopDatatransferService desktopService = DataFlavorUtil.getDesktopService();
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   444
            if (desktopService.isDesktopPresent()) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   445
                LinkedHashSet<String> platformNatives =
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   446
                        desktopService.getPlatformMappingsForFlavor(flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                if (!platformNatives.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    if (natives != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        // Prepend the platform-specific mappings to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        // that the natives added with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        // addUnencodedNativeForFlavor() are at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        // list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        platformNatives.addAll(natives);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    natives = platformNatives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (natives == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (synthesize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                String encoded = encodeDataFlavor(flav);
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   463
                natives = new LinkedHashSet<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   464
                getFlavorToNative().put(flav, natives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                natives.add(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   467
                LinkedHashSet<DataFlavor> flavors = getNativeToFlavor().get(encoded);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                if (flavors == null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   469
                    flavors = new LinkedHashSet<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   470
                    getNativeToFlavor().put(encoded, flavors);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                flavors.add(flav);
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   473
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   474
                nativesForFlavorCache.remove(flav);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   475
                flavorsForNativeCache.remove(encoded);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            } else {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   477
                natives = new LinkedHashSet<>(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   481
        return new LinkedHashSet<>(natives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   485
     * Returns a {@code List} of {@code String} natives to which the specified
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   486
     * {@code DataFlavor} can be translated by the data transfer subsystem. The
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   487
     * {@code List} will be sorted from best native to worst. That is, the first
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   488
     * native will best reflect data in the specified flavor to the underlying
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   489
     * native platform.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * <p>
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   491
     * If the specified {@code DataFlavor} is previously unknown to the data
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   492
     * transfer subsystem and the data transfer subsystem is unable to translate
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   493
     * this {@code DataFlavor} to any existing native, then invoking this method
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   494
     * will establish a mapping in both directions between the specified
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   495
     * {@code DataFlavor} and an encoded version of its MIME type as its native.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   497
     * @param  flav the {@code DataFlavor} whose corresponding natives should be
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   498
     *         returned. If {@code null} is specified, all natives currently
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   499
     *         known to the data transfer subsystem are returned in a
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   500
     *         non-deterministic order.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   501
     * @return a {@code java.util.List} of {@code java.lang.String} objects
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   502
     *         which are platform-specific representations of platform-specific
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   503
     *         data formats
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @see #encodeDataFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   507
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    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
   509
        LinkedHashSet<String> retval = nativesForFlavorCache.check(flav);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   510
        if (retval != null) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   511
            return new ArrayList<>(retval);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        if (flav == null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   515
            retval = new LinkedHashSet<>(getNativeToFlavor().keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        } else if (disabledMappingGenerationKeys.contains(flav)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            // In this case we shouldn't synthesize a native for this flavor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            // since its mappings were explicitly specified.
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   519
            retval = flavorToNativeLookup(flav, false);
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   520
        } else if (DataFlavorUtil.isFlavorCharsetTextType(flav)) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   521
            retval = new LinkedHashSet<>(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            // For text/* flavors, flavor-to-native mappings specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            // flavormap.properties are stored per flavor's base type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            if ("text".equals(flav.getPrimaryType())) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   526
                LinkedHashSet<String> textTypeNatives =
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   527
                        getTextTypeToNative().get(flav.mimeType.getBaseType());
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   528
                if (textTypeNatives != null) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   529
                    retval.addAll(textTypeNatives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            // 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
   534
            LinkedHashSet<String> textTypeNatives =
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   535
                    getTextTypeToNative().get(TEXT_PLAIN_BASE_TYPE);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   536
            if (textTypeNatives != null) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   537
                retval.addAll(textTypeNatives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   540
            if (retval.isEmpty()) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   541
                retval = flavorToNativeLookup(flav, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                // In this branch it is guaranteed that natives explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                // listed for flav's MIME type were added with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                // addUnencodedNativeForFlavor(), so they have lower priority.
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   546
                retval.addAll(flavorToNativeLookup(flav, false));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   548
        } else if (DataFlavorUtil.isFlavorNoncharsetTextType(flav)) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   549
            retval = getTextTypeToNative().get(flav.mimeType.getBaseType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            if (retval == null || retval.isEmpty()) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   552
                retval = flavorToNativeLookup(flav, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                // In this branch it is guaranteed that natives explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                // listed for flav's MIME type were added with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                // addUnencodedNativeForFlavor(), so they have lower priority.
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   557
                retval.addAll(flavorToNativeLookup(flav, false));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        } else {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   560
            retval = flavorToNativeLookup(flav, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   563
        nativesForFlavorCache.put(flav, retval);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // 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
   565
        return new ArrayList<>(retval);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   569
     * Returns a {@code List} of {@code DataFlavor}s to which the specified
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   570
     * {@code String} native can be translated by the data transfer subsystem.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   571
     * The {@code List} will be sorted from best {@code DataFlavor} to worst.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   572
     * That is, the first {@code DataFlavor} will best reflect data in the
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   573
     * specified native to a Java application.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * If the specified native is previously unknown to the data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * subsystem, and that native has been properly encoded, then invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * method will establish a mapping in both directions between the specified
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   578
     * native and a {@code DataFlavor} whose MIME type is a decoded version of
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   579
     * the native.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * <p>
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   581
     * If the specified native is not a properly encoded native and the mappings
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   582
     * for this native have not been altered with {@code setFlavorsForNative},
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   583
     * then the contents of the {@code List} is platform dependent, but
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   584
     * {@code null} cannot be returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   586
     * @param  nat the native whose corresponding {@code DataFlavor}s should be
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   587
     *         returned. If {@code null} is specified, all {@code DataFlavor}s
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   588
     *         currently known to the data transfer subsystem are returned in a
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   589
     *         non-deterministic order.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   590
     * @return a {@code java.util.List} of {@code DataFlavor} objects into which
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   591
     *         platform-specific data in the specified, platform-specific native
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   592
     *         can be translated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @see #encodeJavaMIMEType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   596
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    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
   598
        LinkedHashSet<DataFlavor> returnValue = flavorsForNativeCache.check(nat);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   599
        if (returnValue != null) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   600
            return new ArrayList<>(returnValue);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   601
        } else {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   602
            returnValue = new LinkedHashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (nat == null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   606
            for (String n : getNativesForFlavor(null)) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   607
                returnValue.addAll(getFlavorsForNative(n));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        } else {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   610
            final LinkedHashSet<DataFlavor> flavors = nativeToFlavorLookup(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            if (disabledMappingGenerationKeys.contains(nat)) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   612
                return new ArrayList<>(flavors);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   615
            final LinkedHashSet<DataFlavor> flavorsWithSynthesized =
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   616
                    nativeToFlavorLookup(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   618
            for (DataFlavor df : flavorsWithSynthesized) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   619
                returnValue.add(df);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   620
                if ("text".equals(df.getPrimaryType())) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   621
                    String baseType = df.mimeType.getBaseType();
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   622
                    returnValue.addAll(convertMimeTypeToDataFlavors(baseType));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   625
        }
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   626
        flavorsForNativeCache.put(nat, returnValue);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   627
        return new ArrayList<>(returnValue);
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   628
    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   629
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 26749
diff changeset
   630
    @SuppressWarnings("deprecation")
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   631
    private static Set<DataFlavor> convertMimeTypeToDataFlavors(
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   632
        final String baseType) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   633
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   634
        final Set<DataFlavor> returnValue = new LinkedHashSet<>();
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   635
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   636
        String subType = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   637
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   638
        try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   639
            final MimeType mimeType = new MimeType(baseType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   640
            subType = mimeType.getSubType();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   641
        } catch (MimeTypeParseException mtpe) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   642
            // Cannot happen, since we checked all mappings
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   643
            // on load from flavormap.properties.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   646
        if (DataFlavorUtil.doesSubtypeSupportCharset(subType, null)) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   647
            if (TEXT_PLAIN_BASE_TYPE.equals(baseType))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   648
            {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   649
                returnValue.add(DataFlavor.stringFlavor);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   650
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   651
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   652
            for (String unicodeClassName : UNICODE_TEXT_CLASSES) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   653
                final String mimeType = baseType + ";charset=Unicode;class=" +
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   654
                                            unicodeClassName;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   655
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   656
                final LinkedHashSet<String> mimeTypes =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   657
                    handleHtmlMimeTypes(baseType, mimeType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   658
                for (String mt : mimeTypes) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   659
                    DataFlavor toAdd = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   660
                    try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   661
                        toAdd = new DataFlavor(mt);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   662
                    } catch (ClassNotFoundException cannotHappen) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   663
                    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   664
                    returnValue.add(toAdd);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   665
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   666
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   667
26010
9a3cf8ee0776 8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents: 26009
diff changeset
   668
            for (String charset : DataFlavorUtil.standardEncodings()) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   669
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   670
                for (String encodedTextClass : ENCODED_TEXT_CLASSES) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   671
                    final String mimeType =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   672
                            baseType + ";charset=" + charset +
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   673
                            ";class=" + encodedTextClass;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   674
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   675
                    final LinkedHashSet<String> mimeTypes =
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   676
                        handleHtmlMimeTypes(baseType, mimeType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   677
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   678
                    for (String mt : mimeTypes) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   679
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   680
                        DataFlavor df = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   681
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   682
                        try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   683
                            df = new DataFlavor(mt);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   684
                            // Check for equality to plainTextFlavor so
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   685
                            // 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
   686
                            // plainTextFlavor, not the canonical charset
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   687
                            // or another equivalent charset with a
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   688
                            // different name, is used.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   689
                            if (df.equals(DataFlavor.plainTextFlavor)) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   690
                                df = DataFlavor.plainTextFlavor;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   691
                            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   692
                        } catch (ClassNotFoundException cannotHappen) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   693
                        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   694
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   695
                        returnValue.add(df);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   696
                    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   697
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   698
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   699
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   700
            if (TEXT_PLAIN_BASE_TYPE.equals(baseType))
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   701
            {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   702
                returnValue.add(DataFlavor.plainTextFlavor);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   703
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   704
        } else {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   705
            // Non-charset text natives should be treated as
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   706
            // 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
   707
            // representations.
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   708
            for (String encodedTextClassName : ENCODED_TEXT_CLASSES) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   709
                DataFlavor toAdd = null;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   710
                try {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   711
                    toAdd = new DataFlavor(baseType +
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   712
                         ";class=" + encodedTextClassName);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   713
                } catch (ClassNotFoundException cannotHappen) {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   714
                }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   715
                returnValue.add(toAdd);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   716
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   717
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   718
        return returnValue;
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   719
    }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   720
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 26037
diff changeset
   721
    private static final String [] htmlDocumentTypes =
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   722
            new String [] {"all", "selection", "fragment"};
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   723
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   724
    private static LinkedHashSet<String> handleHtmlMimeTypes(String baseType,
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   725
                                                             String mimeType) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   726
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   727
        LinkedHashSet<String> returnValues = new LinkedHashSet<>();
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   728
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   729
        if (HTML_TEXT_BASE_TYPE.equals(baseType)) {
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 26037
diff changeset
   730
            for (String documentType : htmlDocumentTypes) {
16705
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   731
                returnValues.add(mimeType + ";document=" + documentType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   732
            }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   733
        } else {
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   734
            returnValues.add(mimeType);
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   735
        }
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   736
1caaa379eded 7075105: WIN: Provide a way to format HTML on drop
denis
parents: 15263
diff changeset
   737
        return returnValues;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   741
     * Returns a {@code Map} of the specified {@code DataFlavor}s to their most
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   742
     * preferred {@code String} native. Each native value will be the same as
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   743
     * the first native in the List returned by {@code getNativesForFlavor} for
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   744
     * the specified flavor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * <p>
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   746
     * If a specified {@code DataFlavor} is previously unknown to the data
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   747
     * transfer subsystem, then invoking this method will establish a mapping in
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   748
     * both directions between the specified {@code DataFlavor} and an encoded
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   749
     * version of its MIME type as its native.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   751
     * @param  flavors an array of {@code DataFlavor}s which will be the key set
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   752
     *         of the returned {@code Map}. If {@code null} is specified, a
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   753
     *         mapping of all {@code DataFlavor}s known to the data transfer
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   754
     *         subsystem to their most preferred {@code String} natives will be
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   755
     *         returned.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   756
     * @return a {@code java.util.Map} of {@code DataFlavor}s to {@code String}
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   757
     *         natives
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @see #getNativesForFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @see #encodeDataFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   761
    @Override
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   762
    public synchronized Map<DataFlavor,String> getNativesForFlavors(DataFlavor[] flavors)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        // Use getNativesForFlavor to generate extra natives for text flavors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        // and stringFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if (flavors == null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   768
            List<DataFlavor> flavor_list = getFlavorsForNative(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            flavors = new DataFlavor[flavor_list.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            flavor_list.toArray(flavors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   773
        Map<DataFlavor, String> retval = new HashMap<>(flavors.length, 1.0f);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   774
        for (DataFlavor flavor : flavors) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   775
            List<String> natives = getNativesForFlavor(flavor);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   776
            String nat = (natives.isEmpty()) ? null : natives.get(0);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   777
            retval.put(flavor, nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   784
     * Returns a {@code Map} of the specified {@code String} natives to their
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   785
     * most preferred {@code DataFlavor}. Each {@code DataFlavor} value will be
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   786
     * the same as the first {@code DataFlavor} in the List returned by
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   787
     * {@code getFlavorsForNative} for the specified native.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * If a specified native is previously unknown to the data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * subsystem, and that native has been properly encoded, then invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * method will establish a mapping in both directions between the specified
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   792
     * native and a {@code DataFlavor} whose MIME type is a decoded version of
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   793
     * the native.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   795
     * @param  natives an array of {@code String}s which will be the key set of
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   796
     *         the returned {@code Map}. If {@code null} is specified, a mapping
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   797
     *         of all supported {@code String} natives to their most preferred
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   798
     *         {@code DataFlavor}s will be returned.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   799
     * @return a {@code java.util.Map} of {@code String} natives to
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   800
     *         {@code DataFlavor}s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @see #getFlavorsForNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @see #encodeJavaMIMEType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     */
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   804
    @Override
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   805
    public synchronized Map<String,DataFlavor> getFlavorsForNatives(String[] natives)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        // Use getFlavorsForNative to generate extra flavors for text natives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (natives == null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   809
            List<String> nativesList = getNativesForFlavor(null);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   810
            natives = new String[nativesList.size()];
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   811
            nativesList.toArray(natives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   814
        Map<String, DataFlavor> retval = new HashMap<>(natives.length, 1.0f);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   815
        for (String aNative : natives) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   816
            List<DataFlavor> flavors = getFlavorsForNative(aNative);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   817
            DataFlavor flav = (flavors.isEmpty())? null : flavors.get(0);
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   818
            retval.put(aNative, flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   824
     * Adds a mapping from the specified {@code DataFlavor} (and all
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   825
     * {@code DataFlavor}s equal to the specified {@code DataFlavor}) to the
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   826
     * specified {@code String} native. Unlike {@code getNativesForFlavor}, the
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   827
     * mapping will only be established in one direction, and the native will
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   828
     * not be encoded. To establish a two-way mapping, call
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   829
     * {@code addFlavorForUnencodedNative} as well. The new mapping will be of
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   830
     * lower priority than any existing mapping. This method has no effect if a
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   831
     * mapping from the specified or equal {@code DataFlavor} to the specified
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   832
     * {@code String} native already exists.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   834
     * @param  flav the {@code DataFlavor} key for the mapping
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   835
     * @param  nat the {@code String} native value for the mapping
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   836
     * @throws NullPointerException if flav or nat is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @see #addFlavorForUnencodedNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public synchronized void addUnencodedNativeForFlavor(DataFlavor flav,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                                                         String nat) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   842
        Objects.requireNonNull(nat, "Null native not permitted");
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   843
        Objects.requireNonNull(flav, "Null flavor not permitted");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   845
        LinkedHashSet<String> natives = getFlavorToNative().get(flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (natives == null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   847
            natives = new LinkedHashSet<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   848
            getFlavorToNative().put(flav, natives);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        natives.add(nat);
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   851
        nativesForFlavorCache.remove(flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   855
     * Discards the current mappings for the specified {@code DataFlavor} and
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   856
     * all {@code DataFlavor}s equal to the specified {@code DataFlavor}, and
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   857
     * creates new mappings to the specified {@code String} natives. Unlike
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   858
     * {@code getNativesForFlavor}, the mappings will only be established in one
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   859
     * direction, and the natives will not be encoded. To establish two-way
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   860
     * mappings, call {@code setFlavorsForNative} as well. The first native in
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   861
     * the array will represent the highest priority mapping. Subsequent natives
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   862
     * will represent mappings of decreasing priority.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * If the array contains several elements that reference equal
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   865
     * {@code String} natives, this method will establish new mappings for the
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   866
     * first of those elements and ignore the rest of them.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * It is recommended that client code not reset mappings established by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * data transfer subsystem. This method should only be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * application-level mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   872
     * @param  flav the {@code DataFlavor} key for the mappings
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   873
     * @param  natives the {@code String} native values for the mappings
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   874
     * @throws NullPointerException if flav or natives is {@code null} or if
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   875
     *         natives contains {@code null} elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @see #setFlavorsForNative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    public synchronized void setNativesForFlavor(DataFlavor flav,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                                                 String[] natives) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   881
        Objects.requireNonNull(natives, "Null natives not permitted");
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   882
        Objects.requireNonNull(flav, "Null flavors not permitted");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   884
        getFlavorToNative().remove(flav);
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   885
        for (String aNative : natives) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   886
            addUnencodedNativeForFlavor(flav, aNative);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        disabledMappingGenerationKeys.add(flav);
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   889
        nativesForFlavorCache.remove(flav);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   893
     * Adds a mapping from a single {@code String} native to a single
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   894
     * {@code DataFlavor}. Unlike {@code getFlavorsForNative}, the mapping will
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   895
     * only be established in one direction, and the native will not be encoded.
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   896
     * To establish a two-way mapping, call {@code addUnencodedNativeForFlavor}
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   897
     * as well. The new mapping will be of lower priority than any existing
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   898
     * mapping. This method has no effect if a mapping from the specified
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   899
     * {@code String} native to the specified or equal {@code DataFlavor}
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   900
     * already exists.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   902
     * @param  nat the {@code String} native key for the mapping
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   903
     * @param  flav the {@code DataFlavor} value for the mapping
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   904
     * @throws NullPointerException if {@code nat} or {@code flav} is
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   905
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @see #addUnencodedNativeForFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    public synchronized void addFlavorForUnencodedNative(String nat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                                                         DataFlavor flav) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   911
        Objects.requireNonNull(nat, "Null native not permitted");
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   912
        Objects.requireNonNull(flav, "Null flavor not permitted");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   914
        LinkedHashSet<DataFlavor> flavors = getNativeToFlavor().get(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (flavors == null) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   916
            flavors = new LinkedHashSet<>(1);
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   917
            getNativeToFlavor().put(nat, flavors);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        flavors.add(flav);
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   920
        flavorsForNativeCache.remove(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   924
     * Discards the current mappings for the specified {@code String} native,
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   925
     * and creates new mappings to the specified {@code DataFlavor}s. Unlike
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   926
     * {@code getFlavorsForNative}, the mappings will only be established in one
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   927
     * direction, and the natives need not be encoded. To establish two-way
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   928
     * mappings, call {@code setNativesForFlavor} as well. The first
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   929
     * {@code DataFlavor} in the array will represent the highest priority
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   930
     * mapping. Subsequent {@code DataFlavor}s will represent mappings of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * decreasing priority.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * If the array contains several elements that reference equal
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   934
     * {@code DataFlavor}s, this method will establish new mappings for the
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   935
     * first of those elements and ignore the rest of them.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * It is recommended that client code not reset mappings established by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * data transfer subsystem. This method should only be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * application-level mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   941
     * @param  nat the {@code String} native key for the mappings
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   942
     * @param  flavors the {@code DataFlavor} values for the mappings
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   943
     * @throws NullPointerException if {@code nat} or {@code flavors} is
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   944
     *         {@code null} or if {@code flavors} contains {@code null} elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @see #setNativesForFlavor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    public synchronized void setFlavorsForNative(String nat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                                                 DataFlavor[] flavors) {
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   950
        Objects.requireNonNull(nat, "Null native not permitted");
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   951
        Objects.requireNonNull(flavors, "Null flavors not permitted");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
109
ddfda358e547 6603256: Startup: Defer initialization of DropTarget's flavorMap
son
parents: 2
diff changeset
   953
        getNativeToFlavor().remove(nat);
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   954
        for (DataFlavor flavor : flavors) {
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
   955
            addFlavorForUnencodedNative(nat, flavor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        disabledMappingGenerationKeys.add(nat);
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
   958
        flavorsForNativeCache.remove(nat);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   962
     * Encodes a MIME type for use as a {@code String} native. The format of an
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   963
     * encoded representation of a MIME type is implementation-dependent. The
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   964
     * only restrictions are:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * <ul>
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   966
     * <li>The encoded representation is {@code null} if and only if the MIME
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   967
     *     type {@code String} is {@code null}</li>
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   968
     * <li>The encoded representations for two non-{@code null} MIME type
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   969
     *     {@code String}s are equal if and only if these {@code String}s are
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   970
     *     equal according to {@code String.equals(Object)}</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * </ul>
15263
f401cac7510b 8005250: Downgrade normative references to ${java.home}/lib folder from Java client code.
uta
parents: 5506
diff changeset
   972
     * The reference implementation of this method returns the specified MIME
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   973
     * type {@code String} prefixed with {@code JAVA_DATAFLAVOR:}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   975
     * @param  mimeType the MIME type to encode
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   976
     * @return the encoded {@code String}, or {@code null} if {@code mimeType}
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   977
     *         is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    public static String encodeJavaMIMEType(String mimeType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        return (mimeType != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            ? JavaMIME + mimeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   986
     * Encodes a {@code DataFlavor} for use as a {@code String} native. The
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   987
     * format of an encoded {@code DataFlavor} is implementation-dependent. The
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   988
     * only restrictions are:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * <ul>
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   990
     * <li>The encoded representation is {@code null} if and only if the
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   991
     *     specified {@code DataFlavor} is {@code null} or its MIME type
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   992
     *     {@code String} is {@code null}</li>
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   993
     * <li>The encoded representations for two non-{@code null}
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   994
     *     {@code DataFlavor}s with non-{@code null} MIME type {@code String}s
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   995
     *     are equal if and only if the MIME type {@code String}s of these
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   996
     *     {@code DataFlavor}s are equal according to
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
   997
     *     {@code String.equals(Object)}</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * </ul>
15263
f401cac7510b 8005250: Downgrade normative references to ${java.home}/lib folder from Java client code.
uta
parents: 5506
diff changeset
   999
     * The reference implementation of this method returns the MIME type
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1000
     * {@code String} of the specified {@code DataFlavor} prefixed with
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1001
     * {@code JAVA_DATAFLAVOR:}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1003
     * @param  flav the {@code DataFlavor} to encode
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1004
     * @return the encoded {@code String}, or {@code null} if {@code flav} is
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1005
     *         {@code null} or has a {@code null} MIME type
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    public static String encodeDataFlavor(DataFlavor flav) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        return (flav != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            ? SystemFlavorMap.encodeJavaMIMEType(flav.getMimeType())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1014
     * Returns whether the specified {@code String} is an encoded Java MIME
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1015
     * type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1017
     * @param  str the {@code String} to test
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1018
     * @return {@code true} if the {@code String} is encoded; {@code false}
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1019
     *         otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    public static boolean isJavaMIMEType(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        return (str != null && str.startsWith(JavaMIME, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1026
     * Decodes a {@code String} native for use as a Java MIME type.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1028
     * @param  nat the {@code String} to decode
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1029
     * @return the decoded Java MIME type, or {@code null} if {@code nat} is not
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1030
     *         an encoded {@code String} native
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    public static String decodeJavaMIMEType(String nat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        return (isJavaMIMEType(nat))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            ? nat.substring(JavaMIME.length(), nat.length()).trim()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /**
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1039
     * Decodes a {@code String} native for use as a {@code DataFlavor}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *
45655
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1041
     * @param  nat the {@code String} to decode
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1042
     * @return the decoded {@code DataFlavor}, or {@code null} if {@code nat} is
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1043
     *         not an encoded {@code String} native
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1044
     * @throws ClassNotFoundException if the class of the data flavor is not
02c95aa8a53a 8180889: Cleanup of javadoc in java.datatransfer module
serb
parents: 31898
diff changeset
  1045
     *         loaded
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    public static DataFlavor decodeDataFlavor(String nat)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        String retval_str = SystemFlavorMap.decodeJavaMIMEType(nat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return (retval_str != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            ? new DataFlavor(retval_str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    }
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1055
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1056
    private static final class SoftCache<K, V> {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1057
        Map<K, SoftReference<LinkedHashSet<V>>> cache;
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1058
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1059
        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
  1060
            if (cache == null) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1061
                cache = new HashMap<>(1);
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1062
            }
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1063
            cache.put(key, new SoftReference<>(value));
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1064
        }
24529
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1065
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1066
        public void remove(K key) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1067
            if (cache == null) return;
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1068
            cache.remove(null);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1069
            cache.remove(key);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1070
        }
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1071
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1072
        public LinkedHashSet<V> check(K key) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1073
            if (cache == null) return null;
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1074
            SoftReference<LinkedHashSet<V>> ref = cache.get(key);
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1075
            if (ref != null) {
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1076
                return ref.get();
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1077
            }
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1078
            return null;
c580bcb3aabc 8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order
pchelko
parents: 23905
diff changeset
  1079
        }
20166
77e3750a0df9 8024987: Copy/paste regression since JDK8 b86
pchelko
parents: 16705
diff changeset
  1080
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
}