263 * @exception NullPointerException if either <code>primaryType</code>,
264 * <code>subType</code> or <code>representationClass</code> is null
265 */
266 private DataFlavor(String primaryType, String subType, MimeTypeParameterList params, Class representationClass, String humanPresentableName) {
267 super();
268 if (primaryType == null) {
269 throw new NullPointerException("primaryType");
270 }
271 if (subType == null) {
272 throw new NullPointerException("subType");
273 }
274 if (representationClass == null) {
275 throw new NullPointerException("representationClass");
276 }
277
278 if (params == null) params = new MimeTypeParameterList();
279
280 params.set("class", representationClass.getName());
281
282 if (humanPresentableName == null) {
283 humanPresentableName = (String)params.get("humanPresentableName");
284
285 if (humanPresentableName == null)
286 humanPresentableName = primaryType + "/" + subType;
287 }
288
289 try {
290 mimeType = new MimeType(primaryType, subType, params);
291 } catch (MimeTypeParseException mtpe) {
292 throw new IllegalArgumentException("MimeType Parse Exception: " + mtpe.getMessage());
293 }
294
295 this.representationClass = representationClass;
296 this.humanPresentableName = humanPresentableName;
297
298 mimeType.removeParameter("humanPresentableName");
299 }
300
301 /**
302 * Constructs a <code>DataFlavor</code> that represents a Java class.
303 * <p>
654 * flavors will be chosen non-deterministically.
655 *
656 * @param availableFlavors an array of available <code>DataFlavor</code>s
657 * @return the best (highest fidelity) flavor according to the rules
658 * specified above, or <code>null</code>,
659 * if <code>availableFlavors</code> is <code>null</code>,
660 * has zero length, or contains no text flavors
661 * @since 1.3
662 */
663 public static final DataFlavor selectBestTextFlavor(
664 DataFlavor[] availableFlavors) {
665 if (availableFlavors == null || availableFlavors.length == 0) {
666 return null;
667 }
668
669 if (textFlavorComparator == null) {
670 textFlavorComparator = new TextFlavorComparator();
671 }
672
673 DataFlavor bestFlavor =
674 (DataFlavor)Collections.max(Arrays.asList(availableFlavors),
675 textFlavorComparator);
676
677 if (!bestFlavor.isFlavorTextType()) {
678 return null;
679 }
680
681 return bestFlavor;
682 }
683
684 private static Comparator textFlavorComparator;
685
686 static class TextFlavorComparator
687 extends DataTransferer.DataFlavorComparator {
688
689 /**
690 * Compares two <code>DataFlavor</code> objects. Returns a negative
691 * integer, zero, or a positive integer as the first
692 * <code>DataFlavor</code> is worse than, equal to, or better than the
693 * second.
694 * <p>
|
263 * @exception NullPointerException if either <code>primaryType</code>,
264 * <code>subType</code> or <code>representationClass</code> is null
265 */
266 private DataFlavor(String primaryType, String subType, MimeTypeParameterList params, Class representationClass, String humanPresentableName) {
267 super();
268 if (primaryType == null) {
269 throw new NullPointerException("primaryType");
270 }
271 if (subType == null) {
272 throw new NullPointerException("subType");
273 }
274 if (representationClass == null) {
275 throw new NullPointerException("representationClass");
276 }
277
278 if (params == null) params = new MimeTypeParameterList();
279
280 params.set("class", representationClass.getName());
281
282 if (humanPresentableName == null) {
283 humanPresentableName = params.get("humanPresentableName");
284
285 if (humanPresentableName == null)
286 humanPresentableName = primaryType + "/" + subType;
287 }
288
289 try {
290 mimeType = new MimeType(primaryType, subType, params);
291 } catch (MimeTypeParseException mtpe) {
292 throw new IllegalArgumentException("MimeType Parse Exception: " + mtpe.getMessage());
293 }
294
295 this.representationClass = representationClass;
296 this.humanPresentableName = humanPresentableName;
297
298 mimeType.removeParameter("humanPresentableName");
299 }
300
301 /**
302 * Constructs a <code>DataFlavor</code> that represents a Java class.
303 * <p>
654 * flavors will be chosen non-deterministically.
655 *
656 * @param availableFlavors an array of available <code>DataFlavor</code>s
657 * @return the best (highest fidelity) flavor according to the rules
658 * specified above, or <code>null</code>,
659 * if <code>availableFlavors</code> is <code>null</code>,
660 * has zero length, or contains no text flavors
661 * @since 1.3
662 */
663 public static final DataFlavor selectBestTextFlavor(
664 DataFlavor[] availableFlavors) {
665 if (availableFlavors == null || availableFlavors.length == 0) {
666 return null;
667 }
668
669 if (textFlavorComparator == null) {
670 textFlavorComparator = new TextFlavorComparator();
671 }
672
673 DataFlavor bestFlavor =
674 Collections.max(Arrays.asList(availableFlavors),
675 textFlavorComparator);
676
677 if (!bestFlavor.isFlavorTextType()) {
678 return null;
679 }
680
681 return bestFlavor;
682 }
683
684 private static Comparator textFlavorComparator;
685
686 static class TextFlavorComparator
687 extends DataTransferer.DataFlavorComparator {
688
689 /**
690 * Compares two <code>DataFlavor</code> objects. Returns a negative
691 * integer, zero, or a positive integer as the first
692 * <code>DataFlavor</code> is worse than, equal to, or better than the
693 * second.
694 * <p>
|