/** * 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; } }
/** * 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/<other>" * </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/<other>" 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/<other>" 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/<other>" 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>, * <all others>. * * <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>, <all others>. * * <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>, <all others>. * * <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; }