jdk/src/java.desktop/share/classes/java/awt/datatransfer/SystemFlavorMap.java
changeset 26037 508779ce6619
parent 26010 9a3cf8ee0776
parent 25859 3317bb8137f4
child 26749 b6598aa90114
--- a/jdk/src/java.desktop/share/classes/java/awt/datatransfer/SystemFlavorMap.java	Mon Aug 18 14:03:21 2014 +0100
+++ b/jdk/src/java.desktop/share/classes/java/awt/datatransfer/SystemFlavorMap.java	Tue Aug 19 10:32:16 2014 -0700
@@ -25,22 +25,15 @@
 
 package java.awt.datatransfer;
 
-import java.awt.Toolkit;
-
-import java.io.BufferedInputStream;
-import java.io.InputStream;
-import java.lang.ref.SoftReference;
+import sun.datatransfer.DataFlavorUtil;
+import sun.datatransfer.DesktopDatatransferService;
 
 import java.io.BufferedReader;
-import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.io.IOException;
-
-import java.net.URL;
-import java.net.MalformedURLException;
-
+import java.lang.ref.SoftReference;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -48,12 +41,8 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Properties;
 import java.util.Set;
 
-import sun.awt.AppContext;
-import sun.awt.datatransfer.DataTransferer;
-
 /**
  * The SystemFlavorMap is a configurable map between "natives" (Strings), which
  * correspond to platform-specific data formats, and "flavors" (DataFlavors),
@@ -74,13 +63,6 @@
     private static final Object FLAVOR_MAP_KEY = new Object();
 
     /**
-     * Copied from java.util.Properties.
-     */
-    private static final String keyValueSeparators = "=: \t\r\n\f";
-    private static final String strictKeyValueSeparators = "=:";
-    private static final String whiteSpaceChars = " \t\r\n\f";
-
-    /**
      * The list of valid, decoded text flavor representation classes, in order
      * from best to worst.
      */
@@ -198,16 +180,11 @@
 
     /**
      * Returns the default FlavorMap for this thread's ClassLoader.
+     *
      * @return the default FlavorMap for this thread's ClassLoader
      */
     public static FlavorMap getDefaultFlavorMap() {
-        AppContext context = AppContext.getAppContext();
-        FlavorMap fm = (FlavorMap) context.get(FLAVOR_MAP_KEY);
-        if (fm == null) {
-            fm = new SystemFlavorMap();
-            context.put(FLAVOR_MAP_KEY, fm);
-        }
-        return fm;
+        return DataFlavorUtil.getDesktopService().getFlavorMap(SystemFlavorMap::new);
     }
 
     private SystemFlavorMap() {
@@ -223,7 +200,7 @@
         }
         isMapInitialized = true;
 
-        InputStream is = SystemFlavorMap.class.getResourceAsStream("/sun/awt/datatransfer/flavormap.properties");
+        InputStream is = SystemFlavorMap.class.getResourceAsStream("/sun/datatransfer/resources/flavormap.properties");
         if (is == null) {
             throw new InternalError("Default flavor mapping not found");
         }
@@ -238,22 +215,25 @@
                     line = line.substring(0, line.length() - 1) + reader.readLine().trim();
                 }
                 int delimiterPosition = line.indexOf('=');
-                String key = line.substring(0, delimiterPosition).replace("\\ ", " ");
+                String key = line.substring(0, delimiterPosition).replaceAll("\\ ", " ");
                 String[] values = line.substring(delimiterPosition + 1, line.length()).split(",");
                 for (String value : values) {
                     try {
+                        value = loadConvert(value);
                         MimeType mime = new MimeType(value);
                         if ("text".equals(mime.getPrimaryType())) {
                             String charset = mime.getParameter("charset");
-                            if (DataTransferer.doesSubtypeSupportCharset(mime.getSubType(), charset))
+                            if (DataFlavorUtil.doesSubtypeSupportCharset(mime.getSubType(), charset))
                             {
                                 // We need to store the charset and eoln
                                 // parameters, if any, so that the
                                 // DataTransferer will have this information
                                 // for conversion into the native format.
-                                DataTransferer transferer = DataTransferer.getInstance();
-                                if (transferer != null) {
-                                    transferer.registerTextFlavorProperties(key, charset,
+                                DesktopDatatransferService desktopService =
+                                        DataFlavorUtil.getDesktopService();
+                                if (desktopService.isDesktopPresent()) {
+                                    desktopService.registerTextFlavorProperties(
+                                            key, charset,
                                             mime.getParameter("eoln"),
                                             mime.getParameter("terminators"));
                                 }
@@ -305,6 +285,63 @@
         }
     }
 
+    // Copied from java.util.Properties
+    private static String loadConvert(String theString) {
+        char aChar;
+        int len = theString.length();
+        StringBuilder outBuffer = new StringBuilder(len);
+
+        for (int x = 0; x < len; ) {
+            aChar = theString.charAt(x++);
+            if (aChar == '\\') {
+                aChar = theString.charAt(x++);
+                if (aChar == 'u') {
+                    // Read the xxxx
+                    int value = 0;
+                    for (int i = 0; i < 4; i++) {
+                        aChar = theString.charAt(x++);
+                        switch (aChar) {
+                            case '0': case '1': case '2': case '3': case '4':
+                            case '5': case '6': case '7': case '8': case '9': {
+                                value = (value << 4) + aChar - '0';
+                                break;
+                            }
+                            case 'a': case 'b': case 'c':
+                            case 'd': case 'e': case 'f': {
+                                value = (value << 4) + 10 + aChar - 'a';
+                                break;
+                            }
+                            case 'A': case 'B': case 'C':
+                            case 'D': case 'E': case 'F': {
+                                value = (value << 4) + 10 + aChar - 'A';
+                                break;
+                            }
+                            default: {
+                                throw new IllegalArgumentException(
+                                        "Malformed \\uxxxx encoding.");
+                            }
+                        }
+                    }
+                    outBuffer.append((char)value);
+                } else {
+                    if (aChar == 't') {
+                        aChar = '\t';
+                    } else if (aChar == 'r') {
+                        aChar = '\r';
+                    } else if (aChar == 'n') {
+                        aChar = '\n';
+                    } else if (aChar == 'f') {
+                        aChar = '\f';
+                    }
+                    outBuffer.append(aChar);
+                }
+            } else {
+                outBuffer.append(aChar);
+            }
+        }
+        return outBuffer.toString();
+    }
+
     /**
      * Stores the listed object under the specified hash key in map. Unlike a
      * standard map, the listed object will not replace any object already at
@@ -332,10 +369,10 @@
         LinkedHashSet<DataFlavor> flavors = getNativeToFlavor().get(nat);
 
         if (nat != null && !disabledMappingGenerationKeys.contains(nat)) {
-            DataTransferer transferer = DataTransferer.getInstance();
-            if (transferer != null) {
+            DesktopDatatransferService desktopService = DataFlavorUtil.getDesktopService();
+            if (desktopService.isDesktopPresent()) {
                 LinkedHashSet<DataFlavor> platformFlavors =
-                        transferer.getPlatformMappingsForNative(nat);
+                        desktopService.getPlatformMappingsForNative(nat);
                 if (!platformFlavors.isEmpty()) {
                     if (flavors != null) {
                         // Prepending the platform-specific mappings ensures
@@ -395,10 +432,10 @@
         LinkedHashSet<String> natives = getFlavorToNative().get(flav);
 
         if (flav != null && !disabledMappingGenerationKeys.contains(flav)) {
-            DataTransferer transferer = DataTransferer.getInstance();
-            if (transferer != null) {
+            DesktopDatatransferService desktopService = DataFlavorUtil.getDesktopService();
+            if (desktopService.isDesktopPresent()) {
                 LinkedHashSet<String> platformNatives =
-                    transferer.getPlatformMappingsForFlavor(flav);
+                        desktopService.getPlatformMappingsForFlavor(flav);
                 if (!platformNatives.isEmpty()) {
                     if (natives != null) {
                         // Prepend the platform-specific mappings to ensure
@@ -474,7 +511,7 @@
             // In this case we shouldn't synthesize a native for this flavor,
             // since its mappings were explicitly specified.
             retval = flavorToNativeLookup(flav, false);
-        } else if (DataTransferer.isFlavorCharsetTextType(flav)) {
+        } else if (DataFlavorUtil.isFlavorCharsetTextType(flav)) {
             retval = new LinkedHashSet<>(0);
 
             // For text/* flavors, flavor-to-native mappings specified in
@@ -502,7 +539,7 @@
                 // addUnencodedNativeForFlavor(), so they have lower priority.
                 retval.addAll(flavorToNativeLookup(flav, false));
             }
-        } else if (DataTransferer.isFlavorNoncharsetTextType(flav)) {
+        } else if (DataFlavorUtil.isFlavorNoncharsetTextType(flav)) {
             retval = getTextTypeToNative().get(flav.mimeType.getBaseType());
 
             if (retval == null || retval.isEmpty()) {
@@ -602,7 +639,7 @@
             // on load from flavormap.properties.
         }
 
-        if (DataTransferer.doesSubtypeSupportCharset(subType, null)) {
+        if (DataFlavorUtil.doesSubtypeSupportCharset(subType, null)) {
             if (TEXT_PLAIN_BASE_TYPE.equals(baseType))
             {
                 returnValue.add(DataFlavor.stringFlavor);
@@ -624,7 +661,7 @@
                 }
             }
 
-            for (String charset : DataTransferer.standardEncodings()) {
+            for (String charset : DataFlavorUtil.standardEncodings()) {
 
                 for (String encodedTextClass : ENCODED_TEXT_CLASSES) {
                     final String mimeType =