1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12 * language governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16 * Sun designates this particular file as subject to the "Classpath" exception
17 * as provided by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the License
19 * Header, with the fields enclosed by brackets [] replaced by your own
20 * identifying information: "Portions Copyrighted [year]
21 * [name of copyright owner]"
22 *
23 * Contributor(s):
24 *
25 * If you wish your version of this file to be governed by only the CDDL or
26 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27 * elects to include this software in this distribution under the [CDDL or GPL
28 * Version 2] license." If you don't indicate a single choice of license, a
29 * recipient has the option to distribute your version of this file under
30 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31 * its licensees as provided above. However, if you add GPL Version 2 code
32 * and therefore, elected the GPL Version 2 license, then the option applies
33 * only if the new code is made subject to such option by the copyright
34 * holder.
35 */
36
37 /*
38 * @(#)ActivationDataFlavor.java 1.16 07/07/13
39 */
40
41 package javax.activation;
42
43 import java.awt.datatransfer.DataFlavor;
44 import java.io.IOException;
45 import javax.activation.MimeType;
46
47 /**
48 * The ActivationDataFlavor class is a special subclass of
49 * <code>java.awt.datatransfer.DataFlavor</code>. It allows the JAF to
50 * set all three values stored by the DataFlavor class via a new
51 * constructor. It also contains improved MIME parsing in the <code>equals
52 * </code> method. Except for the improved parsing, its semantics are
53 * identical to that of the JDK's DataFlavor class.
54 */
55
56 public class ActivationDataFlavor extends DataFlavor {
57
58 /*
59 * Raison d'etre:
60 *
61 * The DataFlavor class included in JDK 1.1 has several limitations
62 * including piss poor MIME type parsing, and the limitation of
63 * only supporting serialized objects and InputStreams as
64 * representation objects. This class 'fixes' that.
65 */
66
67 // I think for now I'll keep copies of all the variables and
68 // then later I may choose try to better coexist with the base
69 // class *sigh*
70 private String mimeType = null;
71 private MimeType mimeObject = null;
72 private String humanPresentableName = null;
73 private Class representationClass = null;
74
75 /**
76 * Construct a DataFlavor that represents an arbitrary
77 * Java object. This constructor is an extension of the
78 * JDK's DataFlavor in that it allows the explicit setting
79 * of all three DataFlavor attributes.
80 * <p>
81 * The returned DataFlavor will have the following characteristics:
82 * <p>
83 * representationClass = representationClass<br>
84 * mimeType = mimeType<br>
85 * humanName = humanName
86 * <p>
87 *
88 * @param representationClass the class used in this DataFlavor
89 * @param mimeType the MIME type of the data represented by this class
90 * @param humanPresentableName the human presentable name of the flavor
91 */
92 public ActivationDataFlavor(Class representationClass,
93 String mimeType, String humanPresentableName) {
94 super(mimeType, humanPresentableName); // need to call super
95
96 // init private variables:
97 this.mimeType = mimeType;
98 this.humanPresentableName = humanPresentableName;
99 this.representationClass = representationClass;
100 }
101
102 /**
103 * Construct a DataFlavor that represents a MimeType.
104 * <p>
105 * The returned DataFlavor will have the following characteristics:
106 * <p>
107 * If the mimeType is "application/x-java-serialized-object;
108 * class=", the result is the same as calling new
109 * DataFlavor(Class.forName()) as above.
110 * <p>
111 * otherwise:
112 * <p>
113 * representationClass = InputStream<p>
114 * mimeType = mimeType<p>
115 *
116 * @param representationClass the class used in this DataFlavor
117 * @param humanPresentableName the human presentable name of the flavor
118 */
119 public ActivationDataFlavor(Class representationClass,
120 String humanPresentableName) {
121 super(representationClass, humanPresentableName);
122 this.mimeType = super.getMimeType();
123 this.representationClass = representationClass;
124 this.humanPresentableName = humanPresentableName;
125 }
126
127 /**
128 * Construct a DataFlavor that represents a MimeType.
129 * <p>
130 * The returned DataFlavor will have the following characteristics:
131 * <p>
132 * If the mimeType is "application/x-java-serialized-object; class=",
133 * the result is the same as calling new DataFlavor(Class.forName()) as
134 * above, otherwise:
135 * <p>
136 * representationClass = InputStream<p>
137 * mimeType = mimeType
138 *
139 * @param mimeType the MIME type of the data represented by this class
140 * @param humanPresentableName the human presentable name of the flavor
141 */
142 public ActivationDataFlavor(String mimeType, String humanPresentableName) {
143 super(mimeType, humanPresentableName);
144 this.mimeType = mimeType;
145 try {
146 this.representationClass = Class.forName("java.io.InputStream");
147 } catch (ClassNotFoundException ex) {
148 // XXX - should never happen, ignore it
149 }
150 this.humanPresentableName = humanPresentableName;
151 }
152
153 /**
154 * Return the MIME type for this DataFlavor.
155 *
156 * @return the MIME type
157 */
158 public String getMimeType() {
159 return mimeType;
160 }
161
162 /**
163 * Return the representation class.
164 *
165 * @return the representation class
166 */
167 public Class getRepresentationClass() {
168 return representationClass;
169 }
170
171 /**
172 * Return the Human Presentable name.
173 *
174 * @return the human presentable name
175 */
176 public String getHumanPresentableName() {
177 return humanPresentableName;
178 }
179
180 /**
181 * Set the human presentable name.
182 *
183 * @param humanPresentableName the name to set
184 */
185 public void setHumanPresentableName(String humanPresentableName) {
186 this.humanPresentableName = humanPresentableName;
187 }
188
189 /**
190 * Compares the DataFlavor passed in with this DataFlavor; calls
191 * the <code>isMimeTypeEqual</code> method.
192 *
193 * @param dataFlavor the DataFlavor to compare with
194 * @return true if the MIME type and representation class
195 * are the same
196 */
197 public boolean equals(DataFlavor dataFlavor) {
198 return (isMimeTypeEqual(dataFlavor) &&
199 dataFlavor.getRepresentationClass() == representationClass);
200 }
201
202 /**
203 * Is the string representation of the MIME type passed in equivalent
204 * to the MIME type of this DataFlavor. <p>
205 *
206 * ActivationDataFlavor delegates the comparison of MIME types to
207 * the MimeType class included as part of the JavaBeans Activation
208 * Framework. This provides a more robust comparison than is normally
209 * available in the DataFlavor class.
210 *
211 * @param mimeType the MIME type
212 * @return true if the same MIME type
213 */
214 public boolean isMimeTypeEqual(String mimeType) {
215 MimeType mt = null;
216 try {
217 if (mimeObject == null)
218 mimeObject = new MimeType(this.mimeType);
219 mt = new MimeType(mimeType);
220 } catch (MimeTypeParseException e) {
221 // something didn't parse, do a crude comparison
222 return this.mimeType.equalsIgnoreCase(mimeType);
223 }
224
225 return mimeObject.match(mt);
226 }
227
228 /**
229 * Called on DataFlavor for every MIME Type parameter to allow DataFlavor
230 * subclasses to handle special parameters like the text/plain charset
231 * parameters, whose values are case insensitive. (MIME type parameter
232 * values are supposed to be case sensitive).
233 * <p>
234 * This method is called for each parameter name/value pair and should
235 * return the normalized representation of the parameterValue.
236 * This method is never invoked by this implementation.
237 *
238 * @param parameterName the parameter name
239 * @param parameterValue the parameter value
240 * @return the normalized parameter value
241 * @deprecated
242 */
243 protected String normalizeMimeTypeParameter(String parameterName,
244 String parameterValue) {
245 return parameterValue;
246 }
247
248 /**
249 * Called for each MIME type string to give DataFlavor subtypes the
250 * opportunity to change how the normalization of MIME types is
251 * accomplished.
252 * One possible use would be to add default parameter/value pairs in cases
253 * where none are present in the MIME type string passed in.
254 * This method is never invoked by this implementation.
255 *
256 * @param mimeType the MIME type
257 * @return the normalized MIME type
258 * @deprecated
259 */
260 protected String normalizeMimeType(String mimeType) {
261 return mimeType;
262 }
263 }