/**
  * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
  */
 public boolean isDataFlavorSupported(DataFlavor flavor) {
   /*
    * System.out.println("isDataFlavorSupported: " + flavor.getMimeType()
    * + " " + flavor.getHumanPresentableName());
    */
   for (Iterator<DataFlavor> i = exportFlavors.iterator(); i.hasNext(); ) {
     DataFlavor supportedFlavor = i.next();
     if (supportedFlavor.getPrimaryType().equals(flavor.getPrimaryType())
         && supportedFlavor.getSubType().equals(flavor.getSubType())
         && flavor
             .getRepresentationClass()
             .isAssignableFrom(supportedFlavor.getRepresentationClass())) {
       return true;
     }
   }
   return false;
 }
 /**
  * @param flavor
  * @param supportedFlavors
  * @return
  */
 public static boolean isFlavorSupported(DataFlavor flavor, DataFlavor[] supportedFlavors) {
   for (int i = 0; i < supportedFlavors.length; i++) {
     if (supportedFlavors[i].getPrimaryType().equals(flavor.getPrimaryType())
         && supportedFlavors[i].getSubType().equals(flavor.getSubType())
         && flavor
             .getRepresentationClass()
             .isAssignableFrom(supportedFlavors[i].getRepresentationClass())) {
       return true;
     }
   }
   return false;
 }
示例#3
0
 public boolean isDataFlavorSupported(DataFlavor flavor) {
   return flavor.equals(DataFlavor.stringFlavor)
       || flavor.getPrimaryType().equals("application")
           && flavor.getSubType().equals("x-java-serialized-object")
           && flavor.getRepresentationClass().isAssignableFrom(obj.getClass());
 }
  /**
   * Returns a <code>List</code> of <code>String</code> natives to which the specified <code>
   * DataFlavor</code> can be translated by the data transfer subsystem. The <code>List</code> will
   * be sorted from best native to worst. That is, the first native will best reflect data in the
   * specified flavor to the underlying native platform.
   *
   * <p>If the specified <code>DataFlavor</code> is previously unknown to the data transfer
   * subsystem and the data transfer subsystem is unable to translate this <code>DataFlavor</code>
   * to any existing native, then invoking this method will establish a mapping in both directions
   * between the specified <code>DataFlavor</code> and an encoded version of its MIME type as its
   * native.
   *
   * @param flav the <code>DataFlavor</code> whose corresponding natives should be returned. If
   *     <code>null</code> is specified, all natives currently known to the data transfer subsystem
   *     are returned in a non-deterministic order.
   * @return a <code>java.util.List</code> of <code>java.lang.String</code> objects which are
   *     platform-specific representations of platform- specific data formats
   * @see #encodeDataFlavor
   * @since 1.4
   */
  public synchronized List<String> getNativesForFlavor(DataFlavor flav) {
    List retval = null;

    // Check cache, even for null flav
    SoftReference ref = (SoftReference) getNativesForFlavorCache.get(flav);
    if (ref != null) {
      retval = (List) ref.get();
      if (retval != null) {
        // Create a copy, because client code can modify the returned
        // list.
        return new ArrayList(retval);
      }
    }

    if (flav == null) {
      retval = new ArrayList(getNativeToFlavor().keySet());
    } else if (disabledMappingGenerationKeys.contains(flav)) {
      // In this case we shouldn't synthesize a native for this flavor,
      // since its mappings were explicitly specified.
      retval = flavorToNativeLookup(flav, !SYNTHESIZE_IF_NOT_FOUND);
    } else if (DataTransferer.isFlavorCharsetTextType(flav)) {

      // For text/* flavors, flavor-to-native mappings specified in
      // flavormap.properties are stored per flavor's base type.
      if ("text".equals(flav.getPrimaryType())) {
        retval = (List) getFlavorToNative().get(flav.mimeType.getBaseType());
        if (retval != null) {
          // To prevent the List stored in the map from modification.
          retval = new ArrayList(retval);
        }
      }

      // Also include text/plain natives, but don't duplicate Strings
      List textPlainList = (List) getFlavorToNative().get(TEXT_PLAIN_BASE_TYPE);

      if (textPlainList != null && !textPlainList.isEmpty()) {
        // To prevent the List stored in the map from modification.
        // This also guarantees that removeAll() is supported.
        textPlainList = new ArrayList(textPlainList);
        if (retval != null && !retval.isEmpty()) {
          // Use HashSet to get constant-time performance for search.
          textPlainList.removeAll(new HashSet(retval));
          retval.addAll(textPlainList);
        } else {
          retval = textPlainList;
        }
      }

      if (retval == null || retval.isEmpty()) {
        retval = flavorToNativeLookup(flav, SYNTHESIZE_IF_NOT_FOUND);
      } else {
        // In this branch it is guaranteed that natives explicitly
        // listed for flav's MIME type were added with
        // addUnencodedNativeForFlavor(), so they have lower priority.
        List explicitList = flavorToNativeLookup(flav, !SYNTHESIZE_IF_NOT_FOUND);

        // flavorToNativeLookup() never returns null.
        // It can return an empty List, however.
        if (!explicitList.isEmpty()) {
          // To prevent the List stored in the map from modification.
          // This also guarantees that removeAll() is supported.
          explicitList = new ArrayList(explicitList);
          // Use HashSet to get constant-time performance for search.
          explicitList.removeAll(new HashSet(retval));
          retval.addAll(explicitList);
        }
      }
    } else if (DataTransferer.isFlavorNoncharsetTextType(flav)) {
      retval = (List) getFlavorToNative().get(flav.mimeType.getBaseType());

      if (retval == null || retval.isEmpty()) {
        retval = flavorToNativeLookup(flav, SYNTHESIZE_IF_NOT_FOUND);
      } else {
        // In this branch it is guaranteed that natives explicitly
        // listed for flav's MIME type were added with
        // addUnencodedNativeForFlavor(), so they have lower priority.
        List explicitList = flavorToNativeLookup(flav, !SYNTHESIZE_IF_NOT_FOUND);

        // flavorToNativeLookup() never returns null.
        // It can return an empty List, however.
        if (!explicitList.isEmpty()) {
          // To prevent the List stored in the map from modification.
          // This also guarantees that add/removeAll() are supported.
          retval = new ArrayList(retval);
          explicitList = new ArrayList(explicitList);
          // Use HashSet to get constant-time performance for search.
          explicitList.removeAll(new HashSet(retval));
          retval.addAll(explicitList);
        }
      }
    } else {
      retval = flavorToNativeLookup(flav, SYNTHESIZE_IF_NOT_FOUND);
    }

    getNativesForFlavorCache.put(flav, new SoftReference(retval));
    // Create a copy, because client code can modify the returned list.
    return new ArrayList(retval);
  }
  /**
   * Copied code from java.util.Properties. Parsing the data ourselves is the only way to handle
   * duplicate keys and values.
   */
  private void parseAndStoreReader(BufferedReader in) throws IOException {
    while (true) {
      // Get next line
      String line = in.readLine();
      if (line == null) {
        return;
      }

      if (line.length() > 0) {
        // Continue lines that end in slashes if they are not comments
        char firstChar = line.charAt(0);
        if (firstChar != '#' && firstChar != '!') {
          while (continueLine(line)) {
            String nextLine = in.readLine();
            if (nextLine == null) {
              nextLine = new String("");
            }
            String loppedLine = line.substring(0, line.length() - 1);
            // Advance beyond whitespace on new line
            int startIndex = 0;
            for (; startIndex < nextLine.length(); startIndex++) {
              if (whiteSpaceChars.indexOf(nextLine.charAt(startIndex)) == -1) {
                break;
              }
            }
            nextLine = nextLine.substring(startIndex, nextLine.length());
            line = new String(loppedLine + nextLine);
          }

          // Find start of key
          int len = line.length();
          int keyStart = 0;
          for (; keyStart < len; keyStart++) {
            if (whiteSpaceChars.indexOf(line.charAt(keyStart)) == -1) {
              break;
            }
          }

          // Blank lines are ignored
          if (keyStart == len) {
            continue;
          }

          // Find separation between key and value
          int separatorIndex = keyStart;
          for (; separatorIndex < len; separatorIndex++) {
            char currentChar = line.charAt(separatorIndex);
            if (currentChar == '\\') {
              separatorIndex++;
            } else if (keyValueSeparators.indexOf(currentChar) != -1) {
              break;
            }
          }

          // Skip over whitespace after key if any
          int valueIndex = separatorIndex;
          for (; valueIndex < len; valueIndex++) {
            if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) {
              break;
            }
          }

          // Skip over one non whitespace key value separators if any
          if (valueIndex < len) {
            if (strictKeyValueSeparators.indexOf(line.charAt(valueIndex)) != -1) {
              valueIndex++;
            }
          }

          // Skip over white space after other separators if any
          while (valueIndex < len) {
            if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1) {
              break;
            }
            valueIndex++;
          }

          String key = line.substring(keyStart, separatorIndex);
          String value = (separatorIndex < len) ? line.substring(valueIndex, len) : "";

          // Convert then store key and value
          key = loadConvert(key);
          value = loadConvert(value);

          try {
            MimeType mime = new MimeType(value);
            if ("text".equals(mime.getPrimaryType())) {
              String charset = mime.getParameter("charset");
              if (DataTransferer.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, mime.getParameter("eoln"), mime.getParameter("terminators"));
                }
              }

              // But don't store any of these parameters in the
              // DataFlavor itself for any text natives (even
              // non-charset ones). The SystemFlavorMap will
              // synthesize the appropriate mappings later.
              mime.removeParameter("charset");
              mime.removeParameter("class");
              mime.removeParameter("eoln");
              mime.removeParameter("terminators");
              value = mime.toString();
            }
          } catch (MimeTypeParseException e) {
            e.printStackTrace();
            continue;
          }

          DataFlavor flavor;
          try {
            flavor = new DataFlavor(value);
          } catch (Exception e) {
            try {
              flavor = new DataFlavor(value, (String) null);
            } catch (Exception ee) {
              ee.printStackTrace();
              continue;
            }
          }

          // For text/* flavors, store mappings in separate maps to
          // enable dynamic mapping generation at a run-time.
          if ("text".equals(flavor.getPrimaryType())) {
            store(value, key, getFlavorToNative());
            store(key, value, getNativeToFlavor());
          } else {
            store(flavor, key, getFlavorToNative());
            store(key, flavor, getNativeToFlavor());
          }
        }
      }
    }
  }
示例#6
0
 @Override
 public boolean isDataFlavorSupported(DataFlavor flavor) {
   boolean b = this.flavor.getPrimaryType().equals(flavor.getPrimaryType());
   return b || flavor.equals(DataFlavor.stringFlavor);
 }