1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.activation;
  6. import java.awt.datatransfer.DataFlavor;
  7. import java.io.IOException;
  8. import javax.activation.MimeType;
  9. /**
  10. * The ActivationDataFlavor class is a special subclass of
  11. * <code>java.awt.datatransfer.DataFlavor</code>. It allows the JAF to
  12. * set all three values stored by the DataFlavor class via a new
  13. * constructor. It also contains improved MIME parsing in the <code>equals
  14. * </code> method. Except for the improved parsing, its semantics are
  15. * identical to that of the JDK's DataFlavor class.
  16. */
  17. public class ActivationDataFlavor extends DataFlavor {
  18. /*
  19. * Raison d'etre:
  20. *
  21. * The DataFlavor class included in JDK 1.1 has several limitations
  22. * including piss poor MIME type parsing, and the limitation of
  23. * only supporting serialized objects and InputStreams as
  24. * representation objects. This class 'fixes' that.
  25. */
  26. // I think for now I'll keep copies of all the variables and
  27. // then later I may choose try to better coexist with the base
  28. // class *sigh*
  29. private String mimeType = null;
  30. private MimeType mimeObject = null;
  31. private String humanPresentableName = null;
  32. private Class representationClass = null;
  33. /**
  34. * Construct a DataFlavor that represents an arbitrary
  35. * Java object. This constructor is an extension of the
  36. * JDK's DataFlavor in that it allows the explicit setting
  37. * of all three DataFlavor attributes.
  38. * <p>
  39. * The returned DataFlavor will have the following characteristics:
  40. * <p>
  41. * representationClass = representationClass<br>
  42. * mimeType = mimeType<br>
  43. * humanName = humanName
  44. * <p>
  45. *
  46. * @param representationClass the class used in this DataFlavor
  47. * @param mimeType the MIME type of the data represented by this class
  48. * @param humanPresentableName the human presentable name of the flavor
  49. */
  50. public ActivationDataFlavor(Class representationClass,
  51. String mimeType, String humanPresentableName) {
  52. super(mimeType, humanPresentableName); // need to call super
  53. // init private variables:
  54. this.mimeType = mimeType;
  55. this.humanPresentableName = humanPresentableName;
  56. this.representationClass = representationClass;
  57. }
  58. /**
  59. * Construct a DataFlavor that represents a MimeType.
  60. * <p>
  61. * The returned DataFlavor will have the following characteristics:
  62. * <p>
  63. * If the mimeType is "application/x-java-serialized-object;
  64. * class=", the result is the same as calling new
  65. * DataFlavor(Class.forName()) as above.
  66. * <p>
  67. * otherwise:
  68. * <p>
  69. * representationClass = InputStream<p>
  70. * mimeType = mimeType<p>
  71. *
  72. * @param representationClass the class used in this DataFlavor
  73. * @param humanPresentableName the human presentable name of the flavor
  74. */
  75. public ActivationDataFlavor(Class representationClass,
  76. String humanPresentableName) {
  77. super(representationClass, humanPresentableName);
  78. this.mimeType = super.getMimeType();
  79. this.representationClass = representationClass;
  80. this.humanPresentableName = humanPresentableName;
  81. }
  82. /**
  83. * Construct a DataFlavor that represents a MimeType.
  84. * <p>
  85. * The returned DataFlavor will have the following characteristics:
  86. * <p>
  87. * If the mimeType is "application/x-java-serialized-object; class=",
  88. * the result is the same as calling new DataFlavor(Class.forName()) as
  89. * above, otherwise:
  90. * <p>
  91. * representationClass = InputStream<p>
  92. * mimeType = mimeType
  93. *
  94. * @param mimeType the MIME type of the data represented by this class
  95. * @param humanPresentableName the human presentable name of the flavor
  96. */
  97. public ActivationDataFlavor(String mimeType, String humanPresentableName) {
  98. super(mimeType, humanPresentableName);
  99. this.mimeType = mimeType;
  100. this.representationClass = java.io.InputStream.class;
  101. this.humanPresentableName = humanPresentableName;
  102. }
  103. /**
  104. * Return the MIME type for this DataFlavor.
  105. *
  106. * @return the MIME type
  107. */
  108. public String getMimeType() {
  109. return mimeType;
  110. }
  111. /**
  112. * Return the representation class.
  113. *
  114. * @return the representation class
  115. */
  116. public Class getRepresentationClass() {
  117. return representationClass;
  118. }
  119. /**
  120. * Return the Human Presentable name.
  121. *
  122. * @return the human presentable name
  123. */
  124. public String getHumanPresentableName() {
  125. return humanPresentableName;
  126. }
  127. /**
  128. * Set the human presentable name.
  129. *
  130. * @param humanPresentableName the name to set
  131. */
  132. public void setHumanPresentableName(String humanPresentableName) {
  133. this.humanPresentableName = humanPresentableName;
  134. }
  135. /**
  136. * Compares the DataFlavor passed in with this DataFlavor; calls
  137. * the <code>isMimeTypeEqual</code> method.
  138. *
  139. * @param dataFlavor the DataFlavor to compare with
  140. * @return true if the MIME type and representation class
  141. * are the same
  142. */
  143. public boolean equals(DataFlavor dataFlavor) {
  144. return (isMimeTypeEqual(dataFlavor) &&
  145. dataFlavor.getRepresentationClass() == representationClass);
  146. }
  147. /**
  148. * Is the string representation of the MIME type passed in equivalent
  149. * to the MIME type of this DataFlavor. <p>
  150. *
  151. * ActivationDataFlavor delegates the comparison of MIME types to
  152. * the MimeType class included as part of the JavaBeans Activation
  153. * Framework. This provides a more robust comparison than is normally
  154. * available in the DataFlavor class.
  155. *
  156. * @param mimeType the MIME type
  157. * @return true if the same MIME type
  158. */
  159. public boolean isMimeTypeEqual(String mimeType) {
  160. MimeType mt = null;
  161. try {
  162. if (mimeObject == null)
  163. mimeObject = new MimeType(this.mimeType);
  164. mt = new MimeType(mimeType);
  165. } catch (MimeTypeParseException e) {}
  166. return mimeObject.match(mt);
  167. }
  168. /**
  169. * Called on DataFlavor for every MIME Type parameter to allow DataFlavor
  170. * subclasses to handle special parameters like the text/plain charset
  171. * parameters, whose values are case insensitive. (MIME type parameter
  172. * values are supposed to be case sensitive).
  173. * <p>
  174. * This method is called for each parameter name/value pair and should
  175. * return the normalized representation of the parameterValue.
  176. *
  177. * @param parameterName the parameter name
  178. * @param parameterValue the parameter value
  179. * @return the normalized parameter value
  180. */
  181. protected String normalizeMimeTypeParameter(String parameterName,
  182. String parameterValue) {
  183. return parameterName+"="+parameterValue;
  184. }
  185. /**
  186. * Called for each MIME type string to give DataFlavor subtypes the
  187. * opportunity to change how the normalization of MIME types is
  188. * accomplished.
  189. * One possible use would be to add default parameter/value pairs in cases
  190. * where none are present in the MIME type string passed in.
  191. *
  192. * @param mimeType the MIME type
  193. * @return the normalized MIME type
  194. */
  195. protected String normalizeMimeType(String mimeType) {
  196. return mimeType;
  197. }
  198. }