Example #1
0
  /**
   * Constructs a fully specified <code>DataFlavor</code>.
   *
   * @exception NullPointerException if either <code>primaryType</code>, <code>subType</code> or
   *     <code>representationClass</code> is null
   */
  private DataFlavor(
      String primaryType,
      String subType,
      MimeTypeParameterList params,
      Class representationClass,
      String humanPresentableName) {
    super();
    if (primaryType == null) {
      throw new NullPointerException("primaryType");
    }
    if (subType == null) {
      throw new NullPointerException("subType");
    }
    if (representationClass == null) {
      throw new NullPointerException("representationClass");
    }

    if (params == null) params = new MimeTypeParameterList();

    params.set("class", representationClass.getName());

    if (humanPresentableName == null) {
      humanPresentableName = (String) params.get("humanPresentableName");

      if (humanPresentableName == null) humanPresentableName = primaryType + "/" + subType;
    }

    try {
      mimeType = new MimeType(primaryType, subType, params);
    } catch (MimeTypeParseException mtpe) {
      throw new IllegalArgumentException("MimeType Parse Exception: " + mtpe.getMessage());
    }

    this.representationClass = representationClass;
    this.humanPresentableName = humanPresentableName;

    mimeType.removeParameter("humanPresentableName");
  }
  /**
   * 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());
          }
        }
      }
    }
  }