2
|
1 |
/*
|
|
2 |
* Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.awt.datatransfer;
|
|
27 |
|
|
28 |
import java.awt.Toolkit;
|
|
29 |
import java.io.*;
|
|
30 |
import java.nio.*;
|
|
31 |
import java.util.*;
|
|
32 |
|
|
33 |
import sun.awt.datatransfer.DataTransferer;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* A {@code DataFlavor} provides meta information about data. {@code DataFlavor}
|
|
37 |
* is typically used to access data on the clipboard, or during
|
|
38 |
* a drag and drop operation.
|
|
39 |
* <p>
|
|
40 |
* An instance of {@code DataFlavor} encapsulates a content type as
|
|
41 |
* defined in <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
|
|
42 |
* and <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a>.
|
|
43 |
* A content type is typically referred to as a MIME type.
|
|
44 |
* <p>
|
|
45 |
* A content type consists of a media type (referred
|
|
46 |
* to as the primary type), a subtype, and optional parameters. See
|
|
47 |
* <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
|
|
48 |
* for details on the syntax of a MIME type.
|
|
49 |
* <p>
|
|
50 |
* The JRE data transfer implementation interprets the parameter "class"
|
|
51 |
* of a MIME type as <B>a representation class</b>.
|
|
52 |
* The representation class reflects the class of the object being
|
|
53 |
* transferred. In other words, the representation class is the type of
|
|
54 |
* object returned by {@link Transferable#getTransferData}.
|
|
55 |
* For example, the MIME type of {@link #imageFlavor} is
|
|
56 |
* {@code "image/x-java-image;class=java.awt.Image"},
|
|
57 |
* the primary type is {@code image}, the subtype is
|
|
58 |
* {@code x-java-image}, and the representation class is
|
|
59 |
* {@code java.awt.Image}. When {@code getTransferData} is invoked
|
|
60 |
* with a {@code DataFlavor} of {@code imageFlavor}, an instance of
|
|
61 |
* {@code java.awt.Image} is returned.
|
|
62 |
* It's important to note that {@code DataFlavor} does no error checking
|
|
63 |
* against the representation class. It is up to consumers of
|
|
64 |
* {@code DataFlavor}, such as {@code Transferable}, to honor the representation
|
|
65 |
* class.
|
|
66 |
* <br>
|
|
67 |
* Note, if you do not specify a representation class when
|
|
68 |
* creating a {@code DataFlavor}, the default
|
|
69 |
* representation class is used. See appropriate documentation for
|
|
70 |
* {@code DataFlavor}'s constructors.
|
|
71 |
* <p>
|
|
72 |
* Also, {@code DataFlavor} instances with the "text" primary
|
|
73 |
* MIME type may have a "charset" parameter. Refer to
|
|
74 |
* <a href="http://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a> and
|
|
75 |
* {@link #selectBestTextFlavor} for details on "text" MIME types
|
|
76 |
* and the "charset" parameter.
|
|
77 |
* <p>
|
|
78 |
* Equality of {@code DataFlavors} is determined by the primary type,
|
|
79 |
* subtype, and representation class. Refer to {@link #equals(DataFlavor)} for
|
|
80 |
* details. When determining equality, any optional parameters are ignored.
|
|
81 |
* For example, the following produces two {@code DataFlavors} that
|
|
82 |
* are considered identical:
|
|
83 |
* <pre>
|
|
84 |
* DataFlavor flavor1 = new DataFlavor(Object.class, "X-test/test; class=<java.lang.Object>; foo=bar");
|
|
85 |
* DataFlavor flavor2 = new DataFlavor(Object.class, "X-test/test; class=<java.lang.Object>; x=y");
|
|
86 |
* // The following returns true.
|
|
87 |
* flavor1.equals(flavor2);
|
|
88 |
* </pre>
|
|
89 |
* As mentioned, {@code flavor1} and {@code flavor2} are considered identical.
|
|
90 |
* As such, asking a {@code Transferable} for either {@code DataFlavor} returns
|
|
91 |
* the same results.
|
|
92 |
* <p>
|
|
93 |
* For more information on the using data transfer with Swing see
|
|
94 |
* the <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html">
|
|
95 |
* How to Use Drag and Drop and Data Transfer</a>,
|
|
96 |
* section in <em>Java Tutorial</em>.
|
|
97 |
*
|
|
98 |
* @author Blake Sullivan
|
|
99 |
* @author Laurence P. G. Cable
|
|
100 |
* @author Jeff Dunn
|
|
101 |
*/
|
|
102 |
public class DataFlavor implements Externalizable, Cloneable {
|
|
103 |
|
|
104 |
private static final long serialVersionUID = 8367026044764648243L;
|
|
105 |
private static final Class ioInputStreamClass = java.io.InputStream.class;
|
|
106 |
|
|
107 |
/**
|
|
108 |
* Tries to load a class from: the bootstrap loader, the system loader,
|
|
109 |
* the context loader (if one is present) and finally the loader specified.
|
|
110 |
*
|
|
111 |
* @param className the name of the class to be loaded
|
|
112 |
* @param fallback the fallback loader
|
|
113 |
* @return the class loaded
|
|
114 |
* @exception ClassNotFoundException if class is not found
|
|
115 |
*/
|
|
116 |
protected final static Class<?> tryToLoadClass(String className,
|
|
117 |
ClassLoader fallback)
|
|
118 |
throws ClassNotFoundException
|
|
119 |
{
|
|
120 |
ClassLoader systemClassLoader = (ClassLoader)
|
|
121 |
java.security.AccessController.doPrivileged(
|
|
122 |
new java.security.PrivilegedAction() {
|
|
123 |
public Object run() {
|
|
124 |
ClassLoader cl = Thread.currentThread().
|
|
125 |
getContextClassLoader();
|
|
126 |
return (cl != null)
|
|
127 |
? cl
|
|
128 |
: ClassLoader.getSystemClassLoader();
|
|
129 |
}
|
|
130 |
});
|
|
131 |
|
|
132 |
try {
|
|
133 |
return Class.forName(className, true, systemClassLoader);
|
|
134 |
} catch (ClassNotFoundException e2) {
|
|
135 |
if (fallback != null) {
|
|
136 |
return Class.forName(className, true, fallback);
|
|
137 |
} else {
|
|
138 |
throw new ClassNotFoundException(className);
|
|
139 |
}
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
/*
|
|
144 |
* private initializer
|
|
145 |
*/
|
|
146 |
static private DataFlavor createConstant(Class rc, String prn) {
|
|
147 |
try {
|
|
148 |
return new DataFlavor(rc, prn);
|
|
149 |
} catch (Exception e) {
|
|
150 |
return null;
|
|
151 |
}
|
|
152 |
}
|
|
153 |
|
|
154 |
/*
|
|
155 |
* private initializer
|
|
156 |
*/
|
|
157 |
static private DataFlavor createConstant(String mt, String prn) {
|
|
158 |
try {
|
|
159 |
return new DataFlavor(mt, prn);
|
|
160 |
} catch (Exception e) {
|
|
161 |
return null;
|
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
/**
|
|
166 |
* The <code>DataFlavor</code> representing a Java Unicode String class,
|
|
167 |
* where:
|
|
168 |
* <pre>
|
|
169 |
* representationClass = java.lang.String
|
|
170 |
* mimeType = "application/x-java-serialized-object"
|
|
171 |
* </pre>
|
|
172 |
*/
|
|
173 |
public static final DataFlavor stringFlavor = createConstant(java.lang.String.class, "Unicode String");
|
|
174 |
|
|
175 |
/**
|
|
176 |
* The <code>DataFlavor</code> representing a Java Image class,
|
|
177 |
* where:
|
|
178 |
* <pre>
|
|
179 |
* representationClass = java.awt.Image
|
|
180 |
* mimeType = "image/x-java-image"
|
|
181 |
* </pre>
|
|
182 |
*/
|
|
183 |
public static final DataFlavor imageFlavor = createConstant("image/x-java-image; class=java.awt.Image", "Image");
|
|
184 |
|
|
185 |
/**
|
|
186 |
* The <code>DataFlavor</code> representing plain text with Unicode
|
|
187 |
* encoding, where:
|
|
188 |
* <pre>
|
|
189 |
* representationClass = InputStream
|
|
190 |
* mimeType = "text/plain; charset=unicode"
|
|
191 |
* </pre>
|
|
192 |
* This <code>DataFlavor</code> has been <b>deprecated</b> because
|
|
193 |
* (1) Its representation is an InputStream, an 8-bit based representation,
|
|
194 |
* while Unicode is a 16-bit character set; and (2) The charset "unicode"
|
|
195 |
* is not well-defined. "unicode" implies a particular platform's
|
|
196 |
* implementation of Unicode, not a cross-platform implementation.
|
|
197 |
*
|
|
198 |
* @deprecated as of 1.3. Use <code>DataFlavor.getReaderForText(Transferable)</code>
|
|
199 |
* instead of <code>Transferable.getTransferData(DataFlavor.plainTextFlavor)</code>.
|
|
200 |
*/
|
|
201 |
@Deprecated
|
|
202 |
public static final DataFlavor plainTextFlavor = createConstant("text/plain; charset=unicode; class=java.io.InputStream", "Plain Text");
|
|
203 |
|
|
204 |
/**
|
|
205 |
* A MIME Content-Type of application/x-java-serialized-object represents
|
|
206 |
* a graph of Java object(s) that have been made persistent.
|
|
207 |
*
|
|
208 |
* The representation class associated with this <code>DataFlavor</code>
|
|
209 |
* identifies the Java type of an object returned as a reference
|
|
210 |
* from an invocation <code>java.awt.datatransfer.getTransferData</code>.
|
|
211 |
*/
|
|
212 |
public static final String javaSerializedObjectMimeType = "application/x-java-serialized-object";
|
|
213 |
|
|
214 |
/**
|
|
215 |
* To transfer a list of files to/from Java (and the underlying
|
|
216 |
* platform) a <code>DataFlavor</code> of this type/subtype and
|
|
217 |
* representation class of <code>java.util.List</code> is used.
|
|
218 |
* Each element of the list is required/guaranteed to be of type
|
|
219 |
* <code>java.io.File</code>.
|
|
220 |
*/
|
|
221 |
public static final DataFlavor javaFileListFlavor = createConstant("application/x-java-file-list;class=java.util.List", null);
|
|
222 |
|
|
223 |
/**
|
|
224 |
* To transfer a reference to an arbitrary Java object reference that
|
|
225 |
* has no associated MIME Content-type, across a <code>Transferable</code>
|
|
226 |
* interface WITHIN THE SAME JVM, a <code>DataFlavor</code>
|
|
227 |
* with this type/subtype is used, with a <code>representationClass</code>
|
|
228 |
* equal to the type of the class/interface being passed across the
|
|
229 |
* <code>Transferable</code>.
|
|
230 |
* <p>
|
|
231 |
* The object reference returned from
|
|
232 |
* <code>Transferable.getTransferData</code> for a <code>DataFlavor</code>
|
|
233 |
* with this MIME Content-Type is required to be
|
|
234 |
* an instance of the representation Class of the <code>DataFlavor</code>.
|
|
235 |
*/
|
|
236 |
public static final String javaJVMLocalObjectMimeType = "application/x-java-jvm-local-objectref";
|
|
237 |
|
|
238 |
/**
|
|
239 |
* In order to pass a live link to a Remote object via a Drag and Drop
|
|
240 |
* <code>ACTION_LINK</code> operation a Mime Content Type of
|
|
241 |
* application/x-java-remote-object should be used,
|
|
242 |
* where the representation class of the <code>DataFlavor</code>
|
|
243 |
* represents the type of the <code>Remote</code> interface to be
|
|
244 |
* transferred.
|
|
245 |
*/
|
|
246 |
public static final String javaRemoteObjectMimeType = "application/x-java-remote-object";
|
|
247 |
|
|
248 |
/**
|
|
249 |
* Constructs a new <code>DataFlavor</code>. This constructor is
|
|
250 |
* provided only for the purpose of supporting the
|
|
251 |
* <code>Externalizable</code> interface. It is not
|
|
252 |
* intended for public (client) use.
|
|
253 |
*
|
|
254 |
* @since 1.2
|
|
255 |
*/
|
|
256 |
public DataFlavor() {
|
|
257 |
super();
|
|
258 |
}
|
|
259 |
|
|
260 |
/**
|
|
261 |
* Constructs a fully specified <code>DataFlavor</code>.
|
|
262 |
*
|
|
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>
|
|
304 |
* The returned <code>DataFlavor</code> will have the following
|
|
305 |
* characteristics:
|
|
306 |
* <pre>
|
|
307 |
* representationClass = representationClass
|
|
308 |
* mimeType = application/x-java-serialized-object
|
|
309 |
* </pre>
|
|
310 |
* @param representationClass the class used to transfer data in this flavor
|
|
311 |
* @param humanPresentableName the human-readable string used to identify
|
|
312 |
* this flavor; if this parameter is <code>null</code>
|
|
313 |
* then the value of the the MIME Content Type is used
|
|
314 |
* @exception NullPointerException if <code>representationClass</code> is null
|
|
315 |
*/
|
|
316 |
public DataFlavor(Class<?> representationClass, String humanPresentableName) {
|
|
317 |
this("application", "x-java-serialized-object", null, representationClass, humanPresentableName);
|
|
318 |
if (representationClass == null) {
|
|
319 |
throw new NullPointerException("representationClass");
|
|
320 |
}
|
|
321 |
}
|
|
322 |
|
|
323 |
/**
|
|
324 |
* Constructs a <code>DataFlavor</code> that represents a
|
|
325 |
* <code>MimeType</code>.
|
|
326 |
* <p>
|
|
327 |
* The returned <code>DataFlavor</code> will have the following
|
|
328 |
* characteristics:
|
|
329 |
* <p>
|
|
330 |
* If the <code>mimeType</code> is
|
|
331 |
* "application/x-java-serialized-object; class=<representation class>",
|
|
332 |
* the result is the same as calling
|
|
333 |
* <code>new DataFlavor(Class:forName(<representation class>)</code>.
|
|
334 |
* <p>
|
|
335 |
* Otherwise:
|
|
336 |
* <pre>
|
|
337 |
* representationClass = InputStream
|
|
338 |
* mimeType = mimeType
|
|
339 |
* </pre>
|
|
340 |
* @param mimeType the string used to identify the MIME type for this flavor;
|
|
341 |
* if the the <code>mimeType</code> does not specify a
|
|
342 |
* "class=" parameter, or if the class is not successfully
|
|
343 |
* loaded, then an <code>IllegalArgumentException</code>
|
|
344 |
* is thrown
|
|
345 |
* @param humanPresentableName the human-readable string used to identify
|
|
346 |
* this flavor; if this parameter is <code>null</code>
|
|
347 |
* then the value of the the MIME Content Type is used
|
|
348 |
* @exception IllegalArgumentException if <code>mimeType</code> is
|
|
349 |
* invalid or if the class is not successfully loaded
|
|
350 |
* @exception NullPointerException if <code>mimeType</code> is null
|
|
351 |
*/
|
|
352 |
public DataFlavor(String mimeType, String humanPresentableName) {
|
|
353 |
super();
|
|
354 |
if (mimeType == null) {
|
|
355 |
throw new NullPointerException("mimeType");
|
|
356 |
}
|
|
357 |
try {
|
|
358 |
initialize(mimeType, humanPresentableName, this.getClass().getClassLoader());
|
|
359 |
} catch (MimeTypeParseException mtpe) {
|
|
360 |
throw new IllegalArgumentException("failed to parse:" + mimeType);
|
|
361 |
} catch (ClassNotFoundException cnfe) {
|
|
362 |
throw new IllegalArgumentException("can't find specified class: " + cnfe.getMessage());
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
/**
|
|
367 |
* Constructs a <code>DataFlavor</code> that represents a
|
|
368 |
* <code>MimeType</code>.
|
|
369 |
* <p>
|
|
370 |
* The returned <code>DataFlavor</code> will have the following
|
|
371 |
* characteristics:
|
|
372 |
* <p>
|
|
373 |
* If the mimeType is
|
|
374 |
* "application/x-java-serialized-object; class=<representation class>",
|
|
375 |
* the result is the same as calling
|
|
376 |
* <code>new DataFlavor(Class:forName(<representation class>)</code>.
|
|
377 |
* <p>
|
|
378 |
* Otherwise:
|
|
379 |
* <pre>
|
|
380 |
* representationClass = InputStream
|
|
381 |
* mimeType = mimeType
|
|
382 |
* </pre>
|
|
383 |
* @param mimeType the string used to identify the MIME type for this flavor
|
|
384 |
* @param humanPresentableName the human-readable string used to
|
|
385 |
* identify this flavor
|
|
386 |
* @param classLoader the class loader to use
|
|
387 |
* @exception ClassNotFoundException if the class is not loaded
|
|
388 |
* @exception IllegalArgumentException if <code>mimeType</code> is
|
|
389 |
* invalid
|
|
390 |
* @exception NullPointerException if <code>mimeType</code> is null
|
|
391 |
*/
|
|
392 |
public DataFlavor(String mimeType, String humanPresentableName, ClassLoader classLoader) throws ClassNotFoundException {
|
|
393 |
super();
|
|
394 |
if (mimeType == null) {
|
|
395 |
throw new NullPointerException("mimeType");
|
|
396 |
}
|
|
397 |
try {
|
|
398 |
initialize(mimeType, humanPresentableName, classLoader);
|
|
399 |
} catch (MimeTypeParseException mtpe) {
|
|
400 |
throw new IllegalArgumentException("failed to parse:" + mimeType);
|
|
401 |
}
|
|
402 |
}
|
|
403 |
|
|
404 |
/**
|
|
405 |
* Constructs a <code>DataFlavor</code> from a <code>mimeType</code> string.
|
|
406 |
* The string can specify a "class=<fully specified Java class name>"
|
|
407 |
* parameter to create a <code>DataFlavor</code> with the desired
|
|
408 |
* representation class. If the string does not contain "class=" parameter,
|
|
409 |
* <code>java.io.InputStream</code> is used as default.
|
|
410 |
*
|
|
411 |
* @param mimeType the string used to identify the MIME type for this flavor;
|
|
412 |
* if the class specified by "class=" parameter is not
|
|
413 |
* successfully loaded, then an
|
|
414 |
* <code>ClassNotFoundException</code> is thrown
|
|
415 |
* @exception ClassNotFoundException if the class is not loaded
|
|
416 |
* @exception IllegalArgumentException if <code>mimeType</code> is
|
|
417 |
* invalid
|
|
418 |
* @exception NullPointerException if <code>mimeType</code> is null
|
|
419 |
*/
|
|
420 |
public DataFlavor(String mimeType) throws ClassNotFoundException {
|
|
421 |
super();
|
|
422 |
if (mimeType == null) {
|
|
423 |
throw new NullPointerException("mimeType");
|
|
424 |
}
|
|
425 |
try {
|
|
426 |
initialize(mimeType, null, this.getClass().getClassLoader());
|
|
427 |
} catch (MimeTypeParseException mtpe) {
|
|
428 |
throw new IllegalArgumentException("failed to parse:" + mimeType);
|
|
429 |
}
|
|
430 |
}
|
|
431 |
|
|
432 |
/**
|
|
433 |
* Common initialization code called from various constructors.
|
|
434 |
*
|
|
435 |
* @param mimeType the MIME Content Type (must have a class= param)
|
|
436 |
* @param humanPresentableName the human Presentable Name or
|
|
437 |
* <code>null</code>
|
|
438 |
* @param classLoader the fallback class loader to resolve against
|
|
439 |
*
|
|
440 |
* @throws MimeTypeParseException
|
|
441 |
* @throws ClassNotFoundException
|
|
442 |
* @throws NullPointerException if <code>mimeType</code> is null
|
|
443 |
*
|
|
444 |
* @see tryToLoadClass
|
|
445 |
*/
|
|
446 |
private void initialize(String mimeType, String humanPresentableName, ClassLoader classLoader) throws MimeTypeParseException, ClassNotFoundException {
|
|
447 |
if (mimeType == null) {
|
|
448 |
throw new NullPointerException("mimeType");
|
|
449 |
}
|
|
450 |
|
|
451 |
this.mimeType = new MimeType(mimeType); // throws
|
|
452 |
|
|
453 |
String rcn = getParameter("class");
|
|
454 |
|
|
455 |
if (rcn == null) {
|
|
456 |
if ("application/x-java-serialized-object".equals(this.mimeType.getBaseType()))
|
|
457 |
|
|
458 |
throw new IllegalArgumentException("no representation class specified for:" + mimeType);
|
|
459 |
else
|
|
460 |
representationClass = java.io.InputStream.class; // default
|
|
461 |
} else { // got a class name
|
|
462 |
representationClass = DataFlavor.tryToLoadClass(rcn, classLoader);
|
|
463 |
}
|
|
464 |
|
|
465 |
this.mimeType.setParameter("class", representationClass.getName());
|
|
466 |
|
|
467 |
if (humanPresentableName == null) {
|
|
468 |
humanPresentableName = this.mimeType.getParameter("humanPresentableName");
|
|
469 |
if (humanPresentableName == null)
|
|
470 |
humanPresentableName = this.mimeType.getPrimaryType() + "/" + this.mimeType.getSubType();
|
|
471 |
}
|
|
472 |
|
|
473 |
this.humanPresentableName = humanPresentableName; // set it.
|
|
474 |
|
|
475 |
this.mimeType.removeParameter("humanPresentableName"); // just in case
|
|
476 |
}
|
|
477 |
|
|
478 |
/**
|
|
479 |
* String representation of this <code>DataFlavor</code> and its
|
|
480 |
* parameters. The resulting <code>String</code> contains the name of
|
|
481 |
* the <code>DataFlavor</code> class, this flavor's MIME type, and its
|
|
482 |
* representation class. If this flavor has a primary MIME type of "text",
|
|
483 |
* supports the charset parameter, and has an encoded representation, the
|
|
484 |
* flavor's charset is also included. See <code>selectBestTextFlavor</code>
|
|
485 |
* for a list of text flavors which support the charset parameter.
|
|
486 |
*
|
|
487 |
* @return string representation of this <code>DataFlavor</code>
|
|
488 |
* @see #selectBestTextFlavor
|
|
489 |
*/
|
|
490 |
public String toString() {
|
|
491 |
String string = getClass().getName();
|
|
492 |
string += "["+paramString()+"]";
|
|
493 |
return string;
|
|
494 |
}
|
|
495 |
|
|
496 |
private String paramString() {
|
|
497 |
String params = "";
|
|
498 |
params += "mimetype=";
|
|
499 |
if (mimeType == null) {
|
|
500 |
params += "null";
|
|
501 |
} else {
|
|
502 |
params += mimeType.getBaseType();
|
|
503 |
}
|
|
504 |
params += ";representationclass=";
|
|
505 |
if (representationClass == null) {
|
|
506 |
params += "null";
|
|
507 |
} else {
|
|
508 |
params += representationClass.getName();
|
|
509 |
}
|
|
510 |
if (DataTransferer.isFlavorCharsetTextType(this) &&
|
|
511 |
(isRepresentationClassInputStream() ||
|
|
512 |
isRepresentationClassByteBuffer() ||
|
|
513 |
DataTransferer.byteArrayClass.equals(representationClass)))
|
|
514 |
{
|
|
515 |
params += ";charset=" + DataTransferer.getTextCharset(this);
|
|
516 |
}
|
|
517 |
return params;
|
|
518 |
}
|
|
519 |
|
|
520 |
/**
|
|
521 |
* Returns a <code>DataFlavor</code> representing plain text with Unicode
|
|
522 |
* encoding, where:
|
|
523 |
* <pre>
|
|
524 |
* representationClass = java.io.InputStream
|
|
525 |
* mimeType = "text/plain;
|
|
526 |
* charset=<platform default Unicode encoding>"
|
|
527 |
* </pre>
|
|
528 |
* Sun's implementation for Microsoft Windows uses the encoding <code>utf-16le</code>.
|
|
529 |
* Sun's implementation for Solaris and Linux uses the encoding
|
|
530 |
* <code>iso-10646-ucs-2</code>.
|
|
531 |
*
|
|
532 |
* @return a <code>DataFlavor</code> representing plain text
|
|
533 |
* with Unicode encoding
|
|
534 |
* @since 1.3
|
|
535 |
*/
|
|
536 |
public static final DataFlavor getTextPlainUnicodeFlavor() {
|
|
537 |
String encoding = null;
|
|
538 |
DataTransferer transferer = DataTransferer.getInstance();
|
|
539 |
if (transferer != null) {
|
|
540 |
encoding = transferer.getDefaultUnicodeEncoding();
|
|
541 |
}
|
|
542 |
return new DataFlavor(
|
|
543 |
"text/plain;charset="+encoding
|
|
544 |
+";class=java.io.InputStream", "Plain Text");
|
|
545 |
}
|
|
546 |
|
|
547 |
/**
|
|
548 |
* Selects the best text <code>DataFlavor</code> from an array of <code>
|
|
549 |
* DataFlavor</code>s. Only <code>DataFlavor.stringFlavor</code>, and
|
|
550 |
* equivalent flavors, and flavors that have a primary MIME type of "text",
|
|
551 |
* are considered for selection.
|
|
552 |
* <p>
|
|
553 |
* Flavors are first sorted by their MIME types in the following order:
|
|
554 |
* <ul>
|
|
555 |
* <li>"text/sgml"
|
|
556 |
* <li>"text/xml"
|
|
557 |
* <li>"text/html"
|
|
558 |
* <li>"text/rtf"
|
|
559 |
* <li>"text/enriched"
|
|
560 |
* <li>"text/richtext"
|
|
561 |
* <li>"text/uri-list"
|
|
562 |
* <li>"text/tab-separated-values"
|
|
563 |
* <li>"text/t140"
|
|
564 |
* <li>"text/rfc822-headers"
|
|
565 |
* <li>"text/parityfec"
|
|
566 |
* <li>"text/directory"
|
|
567 |
* <li>"text/css"
|
|
568 |
* <li>"text/calendar"
|
|
569 |
* <li>"application/x-java-serialized-object"
|
|
570 |
* <li>"text/plain"
|
|
571 |
* <li>"text/<other>"
|
|
572 |
* </ul>
|
|
573 |
* <p>For example, "text/sgml" will be selected over
|
|
574 |
* "text/html", and <code>DataFlavor.stringFlavor</code> will be chosen
|
|
575 |
* over <code>DataFlavor.plainTextFlavor</code>.
|
|
576 |
* <p>
|
|
577 |
* If two or more flavors share the best MIME type in the array, then that
|
|
578 |
* MIME type will be checked to see if it supports the charset parameter.
|
|
579 |
* <p>
|
|
580 |
* The following MIME types support, or are treated as though they support,
|
|
581 |
* the charset parameter:
|
|
582 |
* <ul>
|
|
583 |
* <li>"text/sgml"
|
|
584 |
* <li>"text/xml"
|
|
585 |
* <li>"text/html"
|
|
586 |
* <li>"text/enriched"
|
|
587 |
* <li>"text/richtext"
|
|
588 |
* <li>"text/uri-list"
|
|
589 |
* <li>"text/directory"
|
|
590 |
* <li>"text/css"
|
|
591 |
* <li>"text/calendar"
|
|
592 |
* <li>"application/x-java-serialized-object"
|
|
593 |
* <li>"text/plain"
|
|
594 |
* </ul>
|
|
595 |
* The following MIME types do not support, or are treated as though they
|
|
596 |
* do not support, the charset parameter:
|
|
597 |
* <ul>
|
|
598 |
* <li>"text/rtf"
|
|
599 |
* <li>"text/tab-separated-values"
|
|
600 |
* <li>"text/t140"
|
|
601 |
* <li>"text/rfc822-headers"
|
|
602 |
* <li>"text/parityfec"
|
|
603 |
* </ul>
|
|
604 |
* For "text/<other>" MIME types, the first time the JRE needs to
|
|
605 |
* determine whether the MIME type supports the charset parameter, it will
|
|
606 |
* check whether the parameter is explicitly listed in an arbitrarily
|
|
607 |
* chosen <code>DataFlavor</code> which uses that MIME type. If so, the JRE
|
|
608 |
* will assume from that point on that the MIME type supports the charset
|
|
609 |
* parameter and will not check again. If the parameter is not explicitly
|
|
610 |
* listed, the JRE will assume from that point on that the MIME type does
|
|
611 |
* not support the charset parameter and will not check again. Because
|
|
612 |
* this check is performed on an arbitrarily chosen
|
|
613 |
* <code>DataFlavor</code>, developers must ensure that all
|
|
614 |
* <code>DataFlavor</code>s with a "text/<other>" MIME type specify
|
|
615 |
* the charset parameter if it is supported by that MIME type. Developers
|
|
616 |
* should never rely on the JRE to substitute the platform's default
|
|
617 |
* charset for a "text/<other>" DataFlavor. Failure to adhere to this
|
|
618 |
* restriction will lead to undefined behavior.
|
|
619 |
* <p>
|
|
620 |
* If the best MIME type in the array does not support the charset
|
|
621 |
* parameter, the flavors which share that MIME type will then be sorted by
|
|
622 |
* their representation classes in the following order:
|
|
623 |
* <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
|
|
624 |
* <code>[B</code>, <all others>.
|
|
625 |
* <p>
|
|
626 |
* If two or more flavors share the best representation class, or if no
|
|
627 |
* flavor has one of the three specified representations, then one of those
|
|
628 |
* flavors will be chosen non-deterministically.
|
|
629 |
* <p>
|
|
630 |
* If the best MIME type in the array does support the charset parameter,
|
|
631 |
* the flavors which share that MIME type will then be sorted by their
|
|
632 |
* representation classes in the following order:
|
|
633 |
* <code>java.io.Reader</code>, <code>java.lang.String</code>,
|
|
634 |
* <code>java.nio.CharBuffer</code>, <code>[C</code>, <all others>.
|
|
635 |
* <p>
|
|
636 |
* If two or more flavors share the best representation class, and that
|
|
637 |
* representation is one of the four explicitly listed, then one of those
|
|
638 |
* flavors will be chosen non-deterministically. If, however, no flavor has
|
|
639 |
* one of the four specified representations, the flavors will then be
|
|
640 |
* sorted by their charsets. Unicode charsets, such as "UTF-16", "UTF-8",
|
|
641 |
* "UTF-16BE", "UTF-16LE", and their aliases, are considered best. After
|
|
642 |
* them, the platform default charset and its aliases are selected.
|
|
643 |
* "US-ASCII" and its aliases are worst. All other charsets are chosen in
|
|
644 |
* alphabetical order, but only charsets supported by this implementation
|
|
645 |
* of the Java platform will be considered.
|
|
646 |
* <p>
|
|
647 |
* If two or more flavors share the best charset, the flavors will then
|
|
648 |
* again be sorted by their representation classes in the following order:
|
|
649 |
* <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
|
|
650 |
* <code>[B</code>, <all others>.
|
|
651 |
* <p>
|
|
652 |
* If two or more flavors share the best representation class, or if no
|
|
653 |
* flavor has one of the three specified representations, then one of those
|
|
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>
|
|
695 |
* <code>DataFlavor</code>s are ordered according to the rules outlined
|
|
696 |
* for <code>selectBestTextFlavor</code>.
|
|
697 |
*
|
|
698 |
* @param obj1 the first <code>DataFlavor</code> to be compared
|
|
699 |
* @param obj2 the second <code>DataFlavor</code> to be compared
|
|
700 |
* @return a negative integer, zero, or a positive integer as the first
|
|
701 |
* argument is worse, equal to, or better than the second
|
|
702 |
* @throws ClassCastException if either of the arguments is not an
|
|
703 |
* instance of <code>DataFlavor</code>
|
|
704 |
* @throws NullPointerException if either of the arguments is
|
|
705 |
* <code>null</code>
|
|
706 |
*
|
|
707 |
* @see #selectBestTextFlavor
|
|
708 |
*/
|
|
709 |
public int compare(Object obj1, Object obj2) {
|
|
710 |
DataFlavor flavor1 = (DataFlavor)obj1;
|
|
711 |
DataFlavor flavor2 = (DataFlavor)obj2;
|
|
712 |
|
|
713 |
if (flavor1.isFlavorTextType()) {
|
|
714 |
if (flavor2.isFlavorTextType()) {
|
|
715 |
return super.compare(obj1, obj2);
|
|
716 |
} else {
|
|
717 |
return 1;
|
|
718 |
}
|
|
719 |
} else if (flavor2.isFlavorTextType()) {
|
|
720 |
return -1;
|
|
721 |
} else {
|
|
722 |
return 0;
|
|
723 |
}
|
|
724 |
}
|
|
725 |
}
|
|
726 |
|
|
727 |
/**
|
|
728 |
* Gets a Reader for a text flavor, decoded, if necessary, for the expected
|
|
729 |
* charset (encoding). The supported representation classes are
|
|
730 |
* <code>java.io.Reader</code>, <code>java.lang.String</code>,
|
|
731 |
* <code>java.nio.CharBuffer</code>, <code>[C</code>,
|
|
732 |
* <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>,
|
|
733 |
* and <code>[B</code>.
|
|
734 |
* <p>
|
|
735 |
* Because text flavors which do not support the charset parameter are
|
|
736 |
* encoded in a non-standard format, this method should not be called for
|
|
737 |
* such flavors. However, in order to maintain backward-compatibility,
|
|
738 |
* if this method is called for such a flavor, this method will treat the
|
|
739 |
* flavor as though it supports the charset parameter and attempt to
|
|
740 |
* decode it accordingly. See <code>selectBestTextFlavor</code> for a list
|
|
741 |
* of text flavors which do not support the charset parameter.
|
|
742 |
*
|
|
743 |
* @param transferable the <code>Transferable</code> whose data will be
|
|
744 |
* requested in this flavor
|
|
745 |
*
|
|
746 |
* @return a <code>Reader</code> to read the <code>Transferable</code>'s
|
|
747 |
* data
|
|
748 |
*
|
|
749 |
* @exception IllegalArgumentException if the representation class
|
|
750 |
* is not one of the seven listed above
|
|
751 |
* @exception IllegalArgumentException if the <code>Transferable</code>
|
|
752 |
* has <code>null</code> data
|
|
753 |
* @exception NullPointerException if the <code>Transferable</code> is
|
|
754 |
* <code>null</code>
|
|
755 |
* @exception UnsupportedEncodingException if this flavor's representation
|
|
756 |
* is <code>java.io.InputStream</code>,
|
|
757 |
* <code>java.nio.ByteBuffer</code>, or <code>[B</code> and
|
|
758 |
* this flavor's encoding is not supported by this
|
|
759 |
* implementation of the Java platform
|
|
760 |
* @exception UnsupportedFlavorException if the <code>Transferable</code>
|
|
761 |
* does not support this flavor
|
|
762 |
* @exception IOException if the data cannot be read because of an
|
|
763 |
* I/O error
|
|
764 |
* @see #selectBestTextFlavor
|
|
765 |
* @since 1.3
|
|
766 |
*/
|
|
767 |
public Reader getReaderForText(Transferable transferable)
|
|
768 |
throws UnsupportedFlavorException, IOException
|
|
769 |
{
|
|
770 |
Object transferObject = transferable.getTransferData(this);
|
|
771 |
if (transferObject == null) {
|
|
772 |
throw new IllegalArgumentException
|
|
773 |
("getTransferData() returned null");
|
|
774 |
}
|
|
775 |
|
|
776 |
if (transferObject instanceof Reader) {
|
|
777 |
return (Reader)transferObject;
|
|
778 |
} else if (transferObject instanceof String) {
|
|
779 |
return new StringReader((String)transferObject);
|
|
780 |
} else if (transferObject instanceof CharBuffer) {
|
|
781 |
CharBuffer buffer = (CharBuffer)transferObject;
|
|
782 |
int size = buffer.remaining();
|
|
783 |
char[] chars = new char[size];
|
|
784 |
buffer.get(chars, 0, size);
|
|
785 |
return new CharArrayReader(chars);
|
|
786 |
} else if (transferObject instanceof char[]) {
|
|
787 |
return new CharArrayReader((char[])transferObject);
|
|
788 |
}
|
|
789 |
|
|
790 |
InputStream stream = null;
|
|
791 |
|
|
792 |
if (transferObject instanceof InputStream) {
|
|
793 |
stream = (InputStream)transferObject;
|
|
794 |
} else if (transferObject instanceof ByteBuffer) {
|
|
795 |
ByteBuffer buffer = (ByteBuffer)transferObject;
|
|
796 |
int size = buffer.remaining();
|
|
797 |
byte[] bytes = new byte[size];
|
|
798 |
buffer.get(bytes, 0, size);
|
|
799 |
stream = new ByteArrayInputStream(bytes);
|
|
800 |
} else if (transferObject instanceof byte[]) {
|
|
801 |
stream = new ByteArrayInputStream((byte[])transferObject);
|
|
802 |
}
|
|
803 |
|
|
804 |
if (stream == null) {
|
|
805 |
throw new IllegalArgumentException("transfer data is not Reader, String, CharBuffer, char array, InputStream, ByteBuffer, or byte array");
|
|
806 |
}
|
|
807 |
|
|
808 |
String encoding = getParameter("charset");
|
|
809 |
return (encoding == null)
|
|
810 |
? new InputStreamReader(stream)
|
|
811 |
: new InputStreamReader(stream, encoding);
|
|
812 |
}
|
|
813 |
|
|
814 |
/**
|
|
815 |
* Returns the MIME type string for this <code>DataFlavor</code>.
|
|
816 |
* @return the MIME type string for this flavor
|
|
817 |
*/
|
|
818 |
public String getMimeType() {
|
|
819 |
return (mimeType != null) ? mimeType.toString() : null;
|
|
820 |
}
|
|
821 |
|
|
822 |
/**
|
|
823 |
* Returns the <code>Class</code> which objects supporting this
|
|
824 |
* <code>DataFlavor</code> will return when this <code>DataFlavor</code>
|
|
825 |
* is requested.
|
|
826 |
* @return the <code>Class</code> which objects supporting this
|
|
827 |
* <code>DataFlavor</code> will return when this <code>DataFlavor</code>
|
|
828 |
* is requested
|
|
829 |
*/
|
|
830 |
public Class<?> getRepresentationClass() {
|
|
831 |
return representationClass;
|
|
832 |
}
|
|
833 |
|
|
834 |
/**
|
|
835 |
* Returns the human presentable name for the data format that this
|
|
836 |
* <code>DataFlavor</code> represents. This name would be localized
|
|
837 |
* for different countries.
|
|
838 |
* @return the human presentable name for the data format that this
|
|
839 |
* <code>DataFlavor</code> represents
|
|
840 |
*/
|
|
841 |
public String getHumanPresentableName() {
|
|
842 |
return humanPresentableName;
|
|
843 |
}
|
|
844 |
|
|
845 |
/**
|
|
846 |
* Returns the primary MIME type for this <code>DataFlavor</code>.
|
|
847 |
* @return the primary MIME type of this <code>DataFlavor</code>
|
|
848 |
*/
|
|
849 |
public String getPrimaryType() {
|
|
850 |
return (mimeType != null) ? mimeType.getPrimaryType() : null;
|
|
851 |
}
|
|
852 |
|
|
853 |
/**
|
|
854 |
* Returns the sub MIME type of this <code>DataFlavor</code>.
|
|
855 |
* @return the Sub MIME type of this <code>DataFlavor</code>
|
|
856 |
*/
|
|
857 |
public String getSubType() {
|
|
858 |
return (mimeType != null) ? mimeType.getSubType() : null;
|
|
859 |
}
|
|
860 |
|
|
861 |
/**
|
|
862 |
* Returns the human presentable name for this <code>DataFlavor</code>
|
|
863 |
* if <code>paramName</code> equals "humanPresentableName". Otherwise
|
|
864 |
* returns the MIME type value associated with <code>paramName</code>.
|
|
865 |
*
|
|
866 |
* @param paramName the parameter name requested
|
|
867 |
* @return the value of the name parameter, or <code>null</code>
|
|
868 |
* if there is no associated value
|
|
869 |
*/
|
|
870 |
public String getParameter(String paramName) {
|
|
871 |
if (paramName.equals("humanPresentableName")) {
|
|
872 |
return humanPresentableName;
|
|
873 |
} else {
|
|
874 |
return (mimeType != null)
|
|
875 |
? mimeType.getParameter(paramName) : null;
|
|
876 |
}
|
|
877 |
}
|
|
878 |
|
|
879 |
/**
|
|
880 |
* Sets the human presentable name for the data format that this
|
|
881 |
* <code>DataFlavor</code> represents. This name would be localized
|
|
882 |
* for different countries.
|
|
883 |
* @param humanPresentableName the new human presentable name
|
|
884 |
*/
|
|
885 |
public void setHumanPresentableName(String humanPresentableName) {
|
|
886 |
this.humanPresentableName = humanPresentableName;
|
|
887 |
}
|
|
888 |
|
|
889 |
/**
|
|
890 |
* {@inheritDoc}
|
|
891 |
* <p>
|
|
892 |
* The equals comparison for the {@code DataFlavor} class is implemented
|
|
893 |
* as follows: Two <code>DataFlavor</code>s are considered equal if and
|
|
894 |
* only if their MIME primary type and subtype and representation class are
|
|
895 |
* equal. Additionally, if the primary type is "text", the subtype denotes
|
|
896 |
* a text flavor which supports the charset parameter, and the
|
|
897 |
* representation class is not <code>java.io.Reader</code>,
|
|
898 |
* <code>java.lang.String</code>, <code>java.nio.CharBuffer</code>, or
|
|
899 |
* <code>[C</code>, the <code>charset</code> parameter must also be equal.
|
|
900 |
* If a charset is not explicitly specified for one or both
|
|
901 |
* <code>DataFlavor</code>s, the platform default encoding is assumed. See
|
|
902 |
* <code>selectBestTextFlavor</code> for a list of text flavors which
|
|
903 |
* support the charset parameter.
|
|
904 |
*
|
|
905 |
* @param o the <code>Object</code> to compare with <code>this</code>
|
|
906 |
* @return <code>true</code> if <code>that</code> is equivalent to this
|
|
907 |
* <code>DataFlavor</code>; <code>false</code> otherwise
|
|
908 |
* @see #selectBestTextFlavor
|
|
909 |
*/
|
|
910 |
public boolean equals(Object o) {
|
|
911 |
return ((o instanceof DataFlavor) && equals((DataFlavor)o));
|
|
912 |
}
|
|
913 |
|
|
914 |
/**
|
|
915 |
* This method has the same behavior as {@link #equals(Object)}.
|
|
916 |
* The only difference being that it takes a {@code DataFlavor} instance
|
|
917 |
* as a parameter.
|
|
918 |
*
|
|
919 |
* @param that the <code>DataFlavor</code> to compare with
|
|
920 |
* <code>this</code>
|
|
921 |
* @return <code>true</code> if <code>that</code> is equivalent to this
|
|
922 |
* <code>DataFlavor</code>; <code>false</code> otherwise
|
|
923 |
* @see #selectBestTextFlavor
|
|
924 |
*/
|
|
925 |
public boolean equals(DataFlavor that) {
|
|
926 |
if (that == null) {
|
|
927 |
return false;
|
|
928 |
}
|
|
929 |
if (this == that) {
|
|
930 |
return true;
|
|
931 |
}
|
|
932 |
|
|
933 |
if (representationClass == null) {
|
|
934 |
if (that.getRepresentationClass() != null) {
|
|
935 |
return false;
|
|
936 |
}
|
|
937 |
} else {
|
|
938 |
if (!representationClass.equals(that.getRepresentationClass())) {
|
|
939 |
return false;
|
|
940 |
}
|
|
941 |
}
|
|
942 |
|
|
943 |
if (mimeType == null) {
|
|
944 |
if (that.mimeType != null) {
|
|
945 |
return false;
|
|
946 |
}
|
|
947 |
} else {
|
|
948 |
if (!mimeType.match(that.mimeType)) {
|
|
949 |
return false;
|
|
950 |
}
|
|
951 |
|
|
952 |
if ("text".equals(getPrimaryType()) &&
|
|
953 |
DataTransferer.doesSubtypeSupportCharset(this) &&
|
|
954 |
representationClass != null &&
|
|
955 |
!(isRepresentationClassReader() ||
|
|
956 |
String.class.equals(representationClass) ||
|
|
957 |
isRepresentationClassCharBuffer() ||
|
|
958 |
DataTransferer.charArrayClass.equals(representationClass)))
|
|
959 |
{
|
|
960 |
String thisCharset =
|
|
961 |
DataTransferer.canonicalName(getParameter("charset"));
|
|
962 |
String thatCharset =
|
|
963 |
DataTransferer.canonicalName(that.getParameter("charset"));
|
|
964 |
if (thisCharset == null) {
|
|
965 |
if (thatCharset != null) {
|
|
966 |
return false;
|
|
967 |
}
|
|
968 |
} else {
|
|
969 |
if (!thisCharset.equals(thatCharset)) {
|
|
970 |
return false;
|
|
971 |
}
|
|
972 |
}
|
|
973 |
}
|
|
974 |
}
|
|
975 |
|
|
976 |
return true;
|
|
977 |
}
|
|
978 |
|
|
979 |
/**
|
|
980 |
* Compares only the <code>mimeType</code> against the passed in
|
|
981 |
* <code>String</code> and <code>representationClass</code> is
|
|
982 |
* not considered in the comparison.
|
|
983 |
*
|
|
984 |
* If <code>representationClass</code> needs to be compared, then
|
|
985 |
* <code>equals(new DataFlavor(s))</code> may be used.
|
|
986 |
* @deprecated As inconsistent with <code>hashCode()</code> contract,
|
|
987 |
* use <code>isMimeTypeEqual(String)</code> instead.
|
|
988 |
* @param s the {@code mimeType} to compare.
|
|
989 |
* @return true if the String (MimeType) is equal; false otherwise or if
|
|
990 |
* {@code s} is {@code null}
|
|
991 |
*/
|
|
992 |
@Deprecated
|
|
993 |
public boolean equals(String s) {
|
|
994 |
if (s == null || mimeType == null)
|
|
995 |
return false;
|
|
996 |
return isMimeTypeEqual(s);
|
|
997 |
}
|
|
998 |
|
|
999 |
/**
|
|
1000 |
* Returns hash code for this <code>DataFlavor</code>.
|
|
1001 |
* For two equal <code>DataFlavor</code>s, hash codes are equal.
|
|
1002 |
* For the <code>String</code>
|
|
1003 |
* that matches <code>DataFlavor.equals(String)</code>, it is not
|
|
1004 |
* guaranteed that <code>DataFlavor</code>'s hash code is equal
|
|
1005 |
* to the hash code of the <code>String</code>.
|
|
1006 |
*
|
|
1007 |
* @return a hash code for this <code>DataFlavor</code>
|
|
1008 |
*/
|
|
1009 |
public int hashCode() {
|
|
1010 |
int total = 0;
|
|
1011 |
|
|
1012 |
if (representationClass != null) {
|
|
1013 |
total += representationClass.hashCode();
|
|
1014 |
}
|
|
1015 |
|
|
1016 |
if (mimeType != null) {
|
|
1017 |
String primaryType = mimeType.getPrimaryType();
|
|
1018 |
if (primaryType != null) {
|
|
1019 |
total += primaryType.hashCode();
|
|
1020 |
}
|
|
1021 |
|
|
1022 |
// Do not add subType.hashCode() to the total. equals uses
|
|
1023 |
// MimeType.match which reports a match if one or both of the
|
|
1024 |
// subTypes is '*', regardless of the other subType.
|
|
1025 |
|
|
1026 |
if ("text".equals(primaryType) &&
|
|
1027 |
DataTransferer.doesSubtypeSupportCharset(this) &&
|
|
1028 |
representationClass != null &&
|
|
1029 |
!(isRepresentationClassReader() ||
|
|
1030 |
String.class.equals(representationClass) ||
|
|
1031 |
isRepresentationClassCharBuffer() ||
|
|
1032 |
DataTransferer.charArrayClass.equals
|
|
1033 |
(representationClass)))
|
|
1034 |
{
|
|
1035 |
String charset =
|
|
1036 |
DataTransferer.canonicalName(getParameter("charset"));
|
|
1037 |
if (charset != null) {
|
|
1038 |
total += charset.hashCode();
|
|
1039 |
}
|
|
1040 |
}
|
|
1041 |
}
|
|
1042 |
|
|
1043 |
return total;
|
|
1044 |
}
|
|
1045 |
|
|
1046 |
/**
|
|
1047 |
* Identical to {@link #equals(DataFlavor)}.
|
|
1048 |
*
|
|
1049 |
* @param that the <code>DataFlavor</code> to compare with
|
|
1050 |
* <code>this</code>
|
|
1051 |
* @return <code>true</code> if <code>that</code> is equivalent to this
|
|
1052 |
* <code>DataFlavor</code>; <code>false</code> otherwise
|
|
1053 |
* @see #selectBestTextFlavor
|
|
1054 |
* @since 1.3
|
|
1055 |
*/
|
|
1056 |
public boolean match(DataFlavor that) {
|
|
1057 |
return equals(that);
|
|
1058 |
}
|
|
1059 |
|
|
1060 |
/**
|
|
1061 |
* Returns whether the string representation of the MIME type passed in
|
|
1062 |
* is equivalent to the MIME type of this <code>DataFlavor</code>.
|
|
1063 |
* Parameters are not included in the comparison.
|
|
1064 |
*
|
|
1065 |
* @param mimeType the string representation of the MIME type
|
|
1066 |
* @return true if the string representation of the MIME type passed in is
|
|
1067 |
* equivalent to the MIME type of this <code>DataFlavor</code>;
|
|
1068 |
* false otherwise
|
|
1069 |
* @throws NullPointerException if mimeType is <code>null</code>
|
|
1070 |
*/
|
|
1071 |
public boolean isMimeTypeEqual(String mimeType) {
|
|
1072 |
// JCK Test DataFlavor0117: if 'mimeType' is null, throw NPE
|
|
1073 |
if (mimeType == null) {
|
|
1074 |
throw new NullPointerException("mimeType");
|
|
1075 |
}
|
|
1076 |
if (this.mimeType == null) {
|
|
1077 |
return false;
|
|
1078 |
}
|
|
1079 |
try {
|
|
1080 |
return this.mimeType.match(new MimeType(mimeType));
|
|
1081 |
} catch (MimeTypeParseException mtpe) {
|
|
1082 |
return false;
|
|
1083 |
}
|
|
1084 |
}
|
|
1085 |
|
|
1086 |
/**
|
|
1087 |
* Compares the <code>mimeType</code> of two <code>DataFlavor</code>
|
|
1088 |
* objects. No parameters are considered.
|
|
1089 |
*
|
|
1090 |
* @param dataFlavor the <code>DataFlavor</code> to be compared
|
|
1091 |
* @return true if the <code>MimeType</code>s are equal,
|
|
1092 |
* otherwise false
|
|
1093 |
*/
|
|
1094 |
|
|
1095 |
public final boolean isMimeTypeEqual(DataFlavor dataFlavor) {
|
|
1096 |
return isMimeTypeEqual(dataFlavor.mimeType);
|
|
1097 |
}
|
|
1098 |
|
|
1099 |
/**
|
|
1100 |
* Compares the <code>mimeType</code> of two <code>DataFlavor</code>
|
|
1101 |
* objects. No parameters are considered.
|
|
1102 |
*
|
|
1103 |
* @return true if the <code>MimeType</code>s are equal,
|
|
1104 |
* otherwise false
|
|
1105 |
*/
|
|
1106 |
|
|
1107 |
private boolean isMimeTypeEqual(MimeType mtype) {
|
|
1108 |
if (this.mimeType == null) {
|
|
1109 |
return (mtype == null);
|
|
1110 |
}
|
|
1111 |
return mimeType.match(mtype);
|
|
1112 |
}
|
|
1113 |
|
|
1114 |
/**
|
|
1115 |
* Does the <code>DataFlavor</code> represent a serialized object?
|
|
1116 |
*/
|
|
1117 |
|
|
1118 |
public boolean isMimeTypeSerializedObject() {
|
|
1119 |
return isMimeTypeEqual(javaSerializedObjectMimeType);
|
|
1120 |
}
|
|
1121 |
|
|
1122 |
public final Class<?> getDefaultRepresentationClass() {
|
|
1123 |
return ioInputStreamClass;
|
|
1124 |
}
|
|
1125 |
|
|
1126 |
public final String getDefaultRepresentationClassAsString() {
|
|
1127 |
return getDefaultRepresentationClass().getName();
|
|
1128 |
}
|
|
1129 |
|
|
1130 |
/**
|
|
1131 |
* Does the <code>DataFlavor</code> represent a
|
|
1132 |
* <code>java.io.InputStream</code>?
|
|
1133 |
*/
|
|
1134 |
|
|
1135 |
public boolean isRepresentationClassInputStream() {
|
|
1136 |
return ioInputStreamClass.isAssignableFrom(representationClass);
|
|
1137 |
}
|
|
1138 |
|
|
1139 |
/**
|
|
1140 |
* Returns whether the representation class for this
|
|
1141 |
* <code>DataFlavor</code> is <code>java.io.Reader</code> or a subclass
|
|
1142 |
* thereof.
|
|
1143 |
*
|
|
1144 |
* @since 1.4
|
|
1145 |
*/
|
|
1146 |
public boolean isRepresentationClassReader() {
|
|
1147 |
return java.io.Reader.class.isAssignableFrom(representationClass);
|
|
1148 |
}
|
|
1149 |
|
|
1150 |
/**
|
|
1151 |
* Returns whether the representation class for this
|
|
1152 |
* <code>DataFlavor</code> is <code>java.nio.CharBuffer</code> or a
|
|
1153 |
* subclass thereof.
|
|
1154 |
*
|
|
1155 |
* @since 1.4
|
|
1156 |
*/
|
|
1157 |
public boolean isRepresentationClassCharBuffer() {
|
|
1158 |
return java.nio.CharBuffer.class.isAssignableFrom(representationClass);
|
|
1159 |
}
|
|
1160 |
|
|
1161 |
/**
|
|
1162 |
* Returns whether the representation class for this
|
|
1163 |
* <code>DataFlavor</code> is <code>java.nio.ByteBuffer</code> or a
|
|
1164 |
* subclass thereof.
|
|
1165 |
*
|
|
1166 |
* @since 1.4
|
|
1167 |
*/
|
|
1168 |
public boolean isRepresentationClassByteBuffer() {
|
|
1169 |
return java.nio.ByteBuffer.class.isAssignableFrom(representationClass);
|
|
1170 |
}
|
|
1171 |
|
|
1172 |
/**
|
|
1173 |
* Returns true if the representation class can be serialized.
|
|
1174 |
* @return true if the representation class can be serialized
|
|
1175 |
*/
|
|
1176 |
|
|
1177 |
public boolean isRepresentationClassSerializable() {
|
|
1178 |
return java.io.Serializable.class.isAssignableFrom(representationClass);
|
|
1179 |
}
|
|
1180 |
|
|
1181 |
/**
|
|
1182 |
* Returns true if the representation class is <code>Remote</code>.
|
|
1183 |
* @return true if the representation class is <code>Remote</code>
|
|
1184 |
*/
|
|
1185 |
|
|
1186 |
public boolean isRepresentationClassRemote() {
|
|
1187 |
return java.rmi.Remote.class.isAssignableFrom(representationClass);
|
|
1188 |
}
|
|
1189 |
|
|
1190 |
/**
|
|
1191 |
* Returns true if the <code>DataFlavor</code> specified represents
|
|
1192 |
* a serialized object.
|
|
1193 |
* @return true if the <code>DataFlavor</code> specified represents
|
|
1194 |
* a Serialized Object
|
|
1195 |
*/
|
|
1196 |
|
|
1197 |
public boolean isFlavorSerializedObjectType() {
|
|
1198 |
return isRepresentationClassSerializable() && isMimeTypeEqual(javaSerializedObjectMimeType);
|
|
1199 |
}
|
|
1200 |
|
|
1201 |
/**
|
|
1202 |
* Returns true if the <code>DataFlavor</code> specified represents
|
|
1203 |
* a remote object.
|
|
1204 |
* @return true if the <code>DataFlavor</code> specified represents
|
|
1205 |
* a Remote Object
|
|
1206 |
*/
|
|
1207 |
|
|
1208 |
public boolean isFlavorRemoteObjectType() {
|
|
1209 |
return isRepresentationClassRemote()
|
|
1210 |
&& isRepresentationClassSerializable()
|
|
1211 |
&& isMimeTypeEqual(javaRemoteObjectMimeType);
|
|
1212 |
}
|
|
1213 |
|
|
1214 |
|
|
1215 |
/**
|
|
1216 |
* Returns true if the <code>DataFlavor</code> specified represents
|
|
1217 |
* a list of file objects.
|
|
1218 |
* @return true if the <code>DataFlavor</code> specified represents
|
|
1219 |
* a List of File objects
|
|
1220 |
*/
|
|
1221 |
|
|
1222 |
public boolean isFlavorJavaFileListType() {
|
|
1223 |
if (mimeType == null || representationClass == null)
|
|
1224 |
return false;
|
|
1225 |
return java.util.List.class.isAssignableFrom(representationClass) &&
|
|
1226 |
mimeType.match(javaFileListFlavor.mimeType);
|
|
1227 |
|
|
1228 |
}
|
|
1229 |
|
|
1230 |
/**
|
|
1231 |
* Returns whether this <code>DataFlavor</code> is a valid text flavor for
|
|
1232 |
* this implementation of the Java platform. Only flavors equivalent to
|
|
1233 |
* <code>DataFlavor.stringFlavor</code> and <code>DataFlavor</code>s with
|
|
1234 |
* a primary MIME type of "text" can be valid text flavors.
|
|
1235 |
* <p>
|
|
1236 |
* If this flavor supports the charset parameter, it must be equivalent to
|
|
1237 |
* <code>DataFlavor.stringFlavor</code>, or its representation must be
|
|
1238 |
* <code>java.io.Reader</code>, <code>java.lang.String</code>,
|
|
1239 |
* <code>java.nio.CharBuffer</code>, <code>[C</code>,
|
|
1240 |
* <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, or
|
|
1241 |
* <code>[B</code>. If the representation is
|
|
1242 |
* <code>java.io.InputStream</code>, <code>java.nio.ByteBuffer</code>, or
|
|
1243 |
* <code>[B</code>, then this flavor's <code>charset</code> parameter must
|
|
1244 |
* be supported by this implementation of the Java platform. If a charset
|
|
1245 |
* is not specified, then the platform default charset, which is always
|
|
1246 |
* supported, is assumed.
|
|
1247 |
* <p>
|
|
1248 |
* If this flavor does not support the charset parameter, its
|
|
1249 |
* representation must be <code>java.io.InputStream</code>,
|
|
1250 |
* <code>java.nio.ByteBuffer</code>, or <code>[B</code>.
|
|
1251 |
* <p>
|
|
1252 |
* See <code>selectBestTextFlavor</code> for a list of text flavors which
|
|
1253 |
* support the charset parameter.
|
|
1254 |
*
|
|
1255 |
* @return <code>true</code> if this <code>DataFlavor</code> is a valid
|
|
1256 |
* text flavor as described above; <code>false</code> otherwise
|
|
1257 |
* @see #selectBestTextFlavor
|
|
1258 |
* @since 1.4
|
|
1259 |
*/
|
|
1260 |
public boolean isFlavorTextType() {
|
|
1261 |
return (DataTransferer.isFlavorCharsetTextType(this) ||
|
|
1262 |
DataTransferer.isFlavorNoncharsetTextType(this));
|
|
1263 |
}
|
|
1264 |
|
|
1265 |
/**
|
|
1266 |
* Serializes this <code>DataFlavor</code>.
|
|
1267 |
*/
|
|
1268 |
|
|
1269 |
public synchronized void writeExternal(ObjectOutput os) throws IOException {
|
|
1270 |
if (mimeType != null) {
|
|
1271 |
mimeType.setParameter("humanPresentableName", humanPresentableName);
|
|
1272 |
os.writeObject(mimeType);
|
|
1273 |
mimeType.removeParameter("humanPresentableName");
|
|
1274 |
} else {
|
|
1275 |
os.writeObject(null);
|
|
1276 |
}
|
|
1277 |
|
|
1278 |
os.writeObject(representationClass);
|
|
1279 |
}
|
|
1280 |
|
|
1281 |
/**
|
|
1282 |
* Restores this <code>DataFlavor</code> from a Serialized state.
|
|
1283 |
*/
|
|
1284 |
|
|
1285 |
public synchronized void readExternal(ObjectInput is) throws IOException , ClassNotFoundException {
|
|
1286 |
String rcn = null;
|
|
1287 |
mimeType = (MimeType)is.readObject();
|
|
1288 |
|
|
1289 |
if (mimeType != null) {
|
|
1290 |
humanPresentableName =
|
|
1291 |
mimeType.getParameter("humanPresentableName");
|
|
1292 |
mimeType.removeParameter("humanPresentableName");
|
|
1293 |
rcn = mimeType.getParameter("class");
|
|
1294 |
if (rcn == null) {
|
|
1295 |
throw new IOException("no class parameter specified in: " +
|
|
1296 |
mimeType);
|
|
1297 |
}
|
|
1298 |
}
|
|
1299 |
|
|
1300 |
try {
|
|
1301 |
representationClass = (Class)is.readObject();
|
|
1302 |
} catch (OptionalDataException ode) {
|
|
1303 |
if (!ode.eof || ode.length != 0) {
|
|
1304 |
throw ode;
|
|
1305 |
}
|
|
1306 |
// Ensure backward compatibility.
|
|
1307 |
// Old versions didn't write the representation class to the stream.
|
|
1308 |
if (rcn != null) {
|
|
1309 |
representationClass =
|
|
1310 |
DataFlavor.tryToLoadClass(rcn, getClass().getClassLoader());
|
|
1311 |
}
|
|
1312 |
}
|
|
1313 |
}
|
|
1314 |
|
|
1315 |
/**
|
|
1316 |
* Returns a clone of this <code>DataFlavor</code>.
|
|
1317 |
* @return a clone of this <code>DataFlavor</code>
|
|
1318 |
*/
|
|
1319 |
|
|
1320 |
public Object clone() throws CloneNotSupportedException {
|
|
1321 |
Object newObj = super.clone();
|
|
1322 |
if (mimeType != null) {
|
|
1323 |
((DataFlavor)newObj).mimeType = (MimeType)mimeType.clone();
|
|
1324 |
}
|
|
1325 |
return newObj;
|
|
1326 |
} // clone()
|
|
1327 |
|
|
1328 |
/**
|
|
1329 |
* Called on <code>DataFlavor</code> for every MIME Type parameter
|
|
1330 |
* to allow <code>DataFlavor</code> subclasses to handle special
|
|
1331 |
* parameters like the text/plain <code>charset</code>
|
|
1332 |
* parameters, whose values are case insensitive. (MIME type parameter
|
|
1333 |
* values are supposed to be case sensitive.
|
|
1334 |
* <p>
|
|
1335 |
* This method is called for each parameter name/value pair and should
|
|
1336 |
* return the normalized representation of the <code>parameterValue</code>.
|
|
1337 |
*
|
|
1338 |
* This method is never invoked by this implementation from 1.1 onwards.
|
|
1339 |
*
|
|
1340 |
* @deprecated
|
|
1341 |
*/
|
|
1342 |
@Deprecated
|
|
1343 |
protected String normalizeMimeTypeParameter(String parameterName, String parameterValue) {
|
|
1344 |
return parameterValue;
|
|
1345 |
}
|
|
1346 |
|
|
1347 |
/**
|
|
1348 |
* Called for each MIME type string to give <code>DataFlavor</code> subtypes
|
|
1349 |
* the opportunity to change how the normalization of MIME types is
|
|
1350 |
* accomplished. One possible use would be to add default
|
|
1351 |
* parameter/value pairs in cases where none are present in the MIME
|
|
1352 |
* type string passed in.
|
|
1353 |
*
|
|
1354 |
* This method is never invoked by this implementation from 1.1 onwards.
|
|
1355 |
*
|
|
1356 |
* @deprecated
|
|
1357 |
*/
|
|
1358 |
@Deprecated
|
|
1359 |
protected String normalizeMimeType(String mimeType) {
|
|
1360 |
return mimeType;
|
|
1361 |
}
|
|
1362 |
|
|
1363 |
/*
|
|
1364 |
* fields
|
|
1365 |
*/
|
|
1366 |
|
|
1367 |
/* placeholder for caching any platform-specific data for flavor */
|
|
1368 |
|
|
1369 |
transient int atom;
|
|
1370 |
|
|
1371 |
/* Mime Type of DataFlavor */
|
|
1372 |
|
|
1373 |
MimeType mimeType;
|
|
1374 |
|
|
1375 |
private String humanPresentableName;
|
|
1376 |
|
|
1377 |
/** Java class of objects this DataFlavor represents **/
|
|
1378 |
|
|
1379 |
private Class representationClass;
|
|
1380 |
|
|
1381 |
} // class DataFlavor
|