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