Exemplo n.º 1
0
 /**
  * Check to see if this mime type is one of the types seen during initialisation or has been added
  * at some later stage using addKnownMimeType(...)
  *
  * @param mimeType
  * @return true if the mimeType is in the list else false is returned
  * @see #addKnownMimeType(String mimetype)
  */
 public static boolean isMimeTypeKnown(final MimeType mimeType) {
   try {
     Set s = (Set) mimeTypes.get(mimeType.getMediaType());
     if (s == null) {
       return false;
     }
     return s.contains(mimeType.getSubType());
   } catch (MimeException e) {
     return false;
   }
 }
Exemplo n.º 2
0
  /**
   * Returns a <code>List</code> of <code>DataFlavor</code>s to which the specified <code>String
   * </code> native can be translated by the data transfer subsystem. The <code>List</code> will be
   * sorted from best <code>DataFlavor</code> to worst. That is, the first <code>DataFlavor</code>
   * will best reflect data in the specified native to a Java application.
   *
   * <p>If the specified native is previously unknown to the data transfer subsystem, and that
   * native has been properly encoded, then invoking this method will establish a mapping in both
   * directions between the specified native and a <code>DataFlavor</code> whose MIME type is a
   * decoded version of the native.
   *
   * <p>If the specified native is not a properly encoded native and the mappings for this native
   * have not been altered with <code>setFlavorsForNative</code>, then the contents of the <code>
   * List</code> is platform dependent, but <code>null</code> cannot be returned.
   *
   * @param nat the native whose corresponding <code>DataFlavor</code>s should be returned. If
   *     <code>null</code> is specified, all <code>DataFlavor</code>s currently known to the data
   *     transfer subsystem are returned in a non-deterministic order.
   * @return a <code>java.util.List</code> of <code>DataFlavor</code> objects into which
   *     platform-specific data in the specified, platform-specific native can be translated
   * @see #encodeJavaMIMEType
   * @since 1.4
   */
  public synchronized List<DataFlavor> getFlavorsForNative(String nat) {

    // Check cache, even for null nat
    SoftReference ref = (SoftReference) getFlavorsForNativeCache.get(nat);
    if (ref != null) {
      ArrayList retval = (ArrayList) ref.get();
      if (retval != null) {
        return (List) retval.clone();
      }
    }

    LinkedList retval = new LinkedList();

    if (nat == null) {
      List natives = getNativesForFlavor(null);
      HashSet dups = new HashSet(natives.size());

      for (Iterator natives_iter = natives.iterator(); natives_iter.hasNext(); ) {
        List flavors = getFlavorsForNative((String) natives_iter.next());
        for (Iterator flavors_iter = flavors.iterator(); flavors_iter.hasNext(); ) {
          Object flavor = flavors_iter.next();
          if (dups.add(flavor)) {
            retval.add(flavor);
          }
        }
      }
    } else {
      List flavors = nativeToFlavorLookup(nat);

      if (disabledMappingGenerationKeys.contains(nat)) {
        return flavors;
      }

      HashSet dups = new HashSet(flavors.size());

      List flavorsAndbaseTypes = nativeToFlavorLookup(nat);

      for (Iterator flavorsAndbaseTypes_iter = flavorsAndbaseTypes.iterator();
          flavorsAndbaseTypes_iter.hasNext(); ) {
        Object value = flavorsAndbaseTypes_iter.next();
        if (value instanceof String) {
          String baseType = (String) value;
          String subType = null;
          try {
            MimeType mimeType = new MimeType(baseType);
            subType = mimeType.getSubType();
          } catch (MimeTypeParseException mtpe) {
            // Cannot happen, since we checked all mappings
            // on load from flavormap.properties.
            assert (false);
          }
          if (DataTransferer.doesSubtypeSupportCharset(subType, null)) {
            if (TEXT_PLAIN_BASE_TYPE.equals(baseType) && dups.add(DataFlavor.stringFlavor)) {
              retval.add(DataFlavor.stringFlavor);
            }

            for (int i = 0; i < UNICODE_TEXT_CLASSES.length; i++) {
              DataFlavor toAdd = null;
              try {
                toAdd =
                    new DataFlavor(baseType + ";charset=Unicode;class=" + UNICODE_TEXT_CLASSES[i]);
              } catch (ClassNotFoundException cannotHappen) {
              }
              if (dups.add(toAdd)) {
                retval.add(toAdd);
              }
            }

            for (Iterator charset_iter = DataTransferer.standardEncodings();
                charset_iter.hasNext(); ) {
              String charset = (String) charset_iter.next();

              for (int i = 0; i < ENCODED_TEXT_CLASSES.length; i++) {
                DataFlavor toAdd = null;
                try {
                  toAdd =
                      new DataFlavor(
                          baseType + ";charset=" + charset + ";class=" + ENCODED_TEXT_CLASSES[i]);
                } catch (ClassNotFoundException cannotHappen) {
                }

                // Check for equality to plainTextFlavor so
                // that we can ensure that the exact charset of
                // plainTextFlavor, not the canonical charset
                // or another equivalent charset with a
                // different name, is used.
                if (toAdd.equals(DataFlavor.plainTextFlavor)) {
                  toAdd = DataFlavor.plainTextFlavor;
                }

                if (dups.add(toAdd)) {
                  retval.add(toAdd);
                }
              }
            }

            if (TEXT_PLAIN_BASE_TYPE.equals(baseType) && dups.add(DataFlavor.plainTextFlavor)) {
              retval.add(DataFlavor.plainTextFlavor);
            }
          } else {
            // Non-charset text natives should be treated as
            // opaque, 8-bit data in any of its various
            // representations.
            for (int i = 0; i < ENCODED_TEXT_CLASSES.length; i++) {
              DataFlavor toAdd = null;
              try {
                toAdd = new DataFlavor(baseType + ";class=" + ENCODED_TEXT_CLASSES[i]);
              } catch (ClassNotFoundException cannotHappen) {
              }

              if (dups.add(toAdd)) {
                retval.add(toAdd);
              }
            }
          }
        } else {
          DataFlavor flavor = (DataFlavor) value;
          if (dups.add(flavor)) {
            retval.add(flavor);
          }
        }
      }
    }

    ArrayList arrayList = new ArrayList(retval);
    getFlavorsForNativeCache.put(nat, new SoftReference(arrayList));
    return (List) arrayList.clone();
  }
Exemplo n.º 3
0
  /**
   * 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());
          }
        }
      }
    }
  }