Ejemplo n.º 1
0
  /**
   * returns whether the given flavor is a color flavor
   *
   * @param flavor a flavor
   * @return whether the given flavor is a color flavor
   */
  public boolean isColorDataFlavor(DataFlavor flavor) {

    boolean isColorDataFlavor = false;

    if (flavor != null) {

      isColorDataFlavor =
          (flavor.isMimeTypeEqual(colorFlavor) || flavor.isMimeTypeEqual(w3cSVGColorFlavor));
    }

    return isColorDataFlavor;
  }
Ejemplo n.º 2
0
  /**
   * This method has the same behavior as {@link #equals(Object)}. The only difference being that it
   * takes a {@code DataFlavor} instance as a parameter.
   *
   * @param that the <code>DataFlavor</code> to compare with <code>this</code>
   * @return <code>true</code> if <code>that</code> is equivalent to this <code>DataFlavor</code>;
   *     <code>false</code> otherwise
   * @see #selectBestTextFlavor
   */
  public boolean equals(DataFlavor that) {
    if (that == null) {
      return false;
    }
    if (this == that) {
      return true;
    }

    if (representationClass == null) {
      if (that.getRepresentationClass() != null) {
        return false;
      }
    } else {
      if (!representationClass.equals(that.getRepresentationClass())) {
        return false;
      }
    }

    if (mimeType == null) {
      if (that.mimeType != null) {
        return false;
      }
    } else {
      if (!mimeType.match(that.mimeType)) {
        return false;
      }

      if ("text".equals(getPrimaryType())
          && DataTransferer.doesSubtypeSupportCharset(this)
          && representationClass != null
          && !(isRepresentationClassReader()
              || String.class.equals(representationClass)
              || isRepresentationClassCharBuffer()
              || DataTransferer.charArrayClass.equals(representationClass))) {
        String thisCharset = DataTransferer.canonicalName(getParameter("charset"));
        String thatCharset = DataTransferer.canonicalName(that.getParameter("charset"));
        if (thisCharset == null) {
          if (thatCharset != null) {
            return false;
          }
        } else {
          if (!thisCharset.equals(thatCharset)) {
            return false;
          }
        }
      }
    }

    return true;
  }
Ejemplo n.º 3
0
    /**
     * Compares two <code>DataFlavor</code> objects. Returns a negative integer, zero, or a positive
     * integer as the first <code>DataFlavor</code> is worse than, equal to, or better than the
     * second.
     *
     * <p><code>DataFlavor</code>s are ordered according to the rules outlined for <code>
     * selectBestTextFlavor</code>.
     *
     * @param obj1 the first <code>DataFlavor</code> to be compared
     * @param obj2 the second <code>DataFlavor</code> to be compared
     * @return a negative integer, zero, or a positive integer as the first argument is worse, equal
     *     to, or better than the second
     * @throws ClassCastException if either of the arguments is not an instance of <code>DataFlavor
     *     </code>
     * @throws NullPointerException if either of the arguments is <code>null</code>
     * @see #selectBestTextFlavor
     */
    public int compare(Object obj1, Object obj2) {
      DataFlavor flavor1 = (DataFlavor) obj1;
      DataFlavor flavor2 = (DataFlavor) obj2;

      if (flavor1.isFlavorTextType()) {
        if (flavor2.isFlavorTextType()) {
          return super.compare(obj1, obj2);
        } else {
          return 1;
        }
      } else if (flavor2.isFlavorTextType()) {
        return -1;
      } else {
        return 0;
      }
    }
Ejemplo n.º 4
0
  /**
   * Common initialization code called from various constructors.
   *
   * @param mimeType the MIME Content Type (must have a class= param)
   * @param humanPresentableName the human Presentable Name or <code>null</code>
   * @param classLoader the fallback class loader to resolve against
   * @throws MimeTypeParseException
   * @throws ClassNotFoundException
   * @throws NullPointerException if <code>mimeType</code> is null
   * @see tryToLoadClass
   */
  private void initialize(String mimeType, String humanPresentableName, ClassLoader classLoader)
      throws MimeTypeParseException, ClassNotFoundException {
    if (mimeType == null) {
      throw new NullPointerException("mimeType");
    }

    this.mimeType = new MimeType(mimeType); // throws

    String rcn = getParameter("class");

    if (rcn == null) {
      if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType()))
        throw new IllegalArgumentException("no representation class specified for:" + mimeType);
      else representationClass = java.io.InputStream.class; // default
    } else { // got a class name
      representationClass = DataFlavor.tryToLoadClass(rcn, classLoader);
    }

    this.mimeType.setParameter("class", representationClass.getName());

    if (humanPresentableName == null) {
      humanPresentableName = this.mimeType.getParameter("humanPresentableName");
      if (humanPresentableName == null)
        humanPresentableName = this.mimeType.getPrimaryType() + "/" + this.mimeType.getSubType();
    }

    this.humanPresentableName = humanPresentableName; // set it.

    this.mimeType.removeParameter("humanPresentableName"); // just in case
  }
Ejemplo n.º 5
0
  /** Restores this <code>DataFlavor</code> from a Serialized state. */
  public synchronized void readExternal(ObjectInput is) throws IOException, ClassNotFoundException {
    String rcn = null;
    mimeType = (MimeType) is.readObject();

    if (mimeType != null) {
      humanPresentableName = mimeType.getParameter("humanPresentableName");
      mimeType.removeParameter("humanPresentableName");
      rcn = mimeType.getParameter("class");
      if (rcn == null) {
        throw new IOException("no class parameter specified in: " + mimeType);
      }
    }

    try {
      representationClass = (Class) is.readObject();
    } catch (OptionalDataException ode) {
      if (!ode.eof || ode.length != 0) {
        throw ode;
      }
      // Ensure backward compatibility.
      // Old versions didn't write the representation class to the stream.
      if (rcn != null) {
        representationClass = DataFlavor.tryToLoadClass(rcn, getClass().getClassLoader());
      }
    }
  }
Ejemplo n.º 6
0
  /**
   * Selects the best text <code>DataFlavor</code> from an array of <code>
   * DataFlavor</code>s. Only <code>DataFlavor.stringFlavor</code>, and equivalent flavors, and
   * flavors that have a primary MIME type of "text", are considered for selection.
   *
   * <p>Flavors are first sorted by their MIME types in the following order:
   *
   * <ul>
   *   <li>"text/sgml"
   *   <li>"text/xml"
   *   <li>"text/html"
   *   <li>"text/rtf"
   *   <li>"text/enriched"
   *   <li>"text/richtext"
   *   <li>"text/uri-list"
   *   <li>"text/tab-separated-values"
   *   <li>"text/t140"
   *   <li>"text/rfc822-headers"
   *   <li>"text/parityfec"
   *   <li>"text/directory"
   *   <li>"text/css"
   *   <li>"text/calendar"
   *   <li>"application/x-java-serialized-object"
   *   <li>"text/plain"
   *   <li>"text/&lt;other&gt;"
   * </ul>
   *
   * <p>For example, "text/sgml" will be selected over "text/html", and <code>
   * DataFlavor.stringFlavor</code> will be chosen over <code>DataFlavor.plainTextFlavor</code>.
   *
   * <p>If two or more flavors share the best MIME type in the array, then that MIME type will be
   * checked to see if it supports the charset parameter.
   *
   * <p>The following MIME types support, or are treated as though they support, the charset
   * parameter:
   *
   * <ul>
   *   <li>"text/sgml"
   *   <li>"text/xml"
   *   <li>"text/html"
   *   <li>"text/enriched"
   *   <li>"text/richtext"
   *   <li>"text/uri-list"
   *   <li>"text/directory"
   *   <li>"text/css"
   *   <li>"text/calendar"
   *   <li>"application/x-java-serialized-object"
   *   <li>"text/plain"
   * </ul>
   *
   * The following MIME types do not support, or are treated as though they do not support, the
   * charset parameter:
   *
   * <ul>
   *   <li>"text/rtf"
   *   <li>"text/tab-separated-values"
   *   <li>"text/t140"
   *   <li>"text/rfc822-headers"
   *   <li>"text/parityfec"
   * </ul>
   *
   * For "text/&lt;other&gt;" MIME types, the first time the JRE needs to determine whether the MIME
   * type supports the charset parameter, it will check whether the parameter is explicitly listed
   * in an arbitrarily chosen <code>DataFlavor</code> which uses that MIME type. If so, the JRE will
   * assume from that point on that the MIME type supports the charset parameter and will not check
   * again. If the parameter is not explicitly listed, the JRE will assume from that point on that
   * the MIME type does not support the charset parameter and will not check again. Because this
   * check is performed on an arbitrarily chosen <code>DataFlavor</code>, developers must ensure
   * that all <code>DataFlavor</code>s with a "text/&lt;other&gt;" MIME type specify the charset
   * parameter if it is supported by that MIME type. Developers should never rely on the JRE to
   * substitute the platform's default charset for a "text/&lt;other&gt;" DataFlavor. Failure to
   * adhere to this restriction will lead to undefined behavior.
   *
   * <p>If the best MIME type in the array does not support the charset parameter, the flavors which
   * share that MIME type will then be sorted by their representation classes in the following
   * order: <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, <code>[B</code>,
   * &lt;all others&gt;.
   *
   * <p>If two or more flavors share the best representation class, or if no flavor has one of the
   * three specified representations, then one of those flavors will be chosen
   * non-deterministically.
   *
   * <p>If the best MIME type in the array does support the charset parameter, the flavors which
   * share that MIME type will then be sorted by their representation classes in the following
   * order: <code>java.io.Reader</code>, <code>java.lang.String</code>, <code>java.nio.CharBuffer
   * </code>, <code>[C</code>, &lt;all others&gt;.
   *
   * <p>If two or more flavors share the best representation class, and that representation is one
   * of the four explicitly listed, then one of those flavors will be chosen non-deterministically.
   * If, however, no flavor has one of the four specified representations, the flavors will then be
   * sorted by their charsets. Unicode charsets, such as "UTF-16", "UTF-8", "UTF-16BE", "UTF-16LE",
   * and their aliases, are considered best. After them, the platform default charset and its
   * aliases are selected. "US-ASCII" and its aliases are worst. All other charsets are chosen in
   * alphabetical order, but only charsets supported by this implementation of the Java platform
   * will be considered.
   *
   * <p>If two or more flavors share the best charset, the flavors will then again be sorted by
   * their representation classes in the following order: <code>java.io.InputStream</code>, <code>
   * java.nio.ByteBuffer</code>, <code>[B</code>, &lt;all others&gt;.
   *
   * <p>If two or more flavors share the best representation class, or if no flavor has one of the
   * three specified representations, then one of those flavors will be chosen
   * non-deterministically.
   *
   * @param availableFlavors an array of available <code>DataFlavor</code>s
   * @return the best (highest fidelity) flavor according to the rules specified above, or <code>
   *     null</code>, if <code>availableFlavors</code> is <code>null</code>, has zero length, or
   *     contains no text flavors
   * @since 1.3
   */
  public static final DataFlavor selectBestTextFlavor(DataFlavor[] availableFlavors) {
    if (availableFlavors == null || availableFlavors.length == 0) {
      return null;
    }

    if (textFlavorComparator == null) {
      textFlavorComparator = new TextFlavorComparator();
    }

    DataFlavor bestFlavor =
        (DataFlavor) Collections.max(Arrays.asList(availableFlavors), textFlavorComparator);

    if (!bestFlavor.isFlavorTextType()) {
      return null;
    }

    return bestFlavor;
  }