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