1 |
/* DataFlavor.java -- A type of data to transfer via the clipboard. |
/* DataFlavor.java -- A type of data to transfer via the clipboard. |
2 |
Copyright (C) 1999, 2001, 2004 Free Software Foundation, Inc. |
Copyright (C) 1999, 2001, 2004, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
49 |
import java.io.UnsupportedEncodingException; |
import java.io.UnsupportedEncodingException; |
50 |
import java.nio.ByteBuffer; |
import java.nio.ByteBuffer; |
51 |
import java.nio.CharBuffer; |
import java.nio.CharBuffer; |
52 |
|
import java.nio.charset.Charset; |
53 |
import java.rmi.Remote; |
import java.rmi.Remote; |
54 |
|
|
55 |
/** |
/** |
64 |
|
|
65 |
// FIXME: Serialization: Need to write methods for. |
// FIXME: Serialization: Need to write methods for. |
66 |
|
|
67 |
/** |
/** |
68 |
* This is the data flavor used for tranferring plain text. The MIME |
* This is the data flavor used for tranferring plain text. The MIME |
69 |
* type is "text/plain; charset=unicode". The representation class |
* type is "text/plain; charset=unicode". The representation class |
70 |
* is <code>java.io.InputStream</code>. |
* is <code>java.io.InputStream</code>. |
71 |
* |
* |
72 |
* @deprecated The charset unicode is platform specific and InputStream |
* @deprecated The charset unicode is platform specific and InputStream |
73 |
* deals with bytes not chars. Use <code>getRederForText()</code>. |
* deals with bytes not chars. Use <code>getRederForText()</code>. |
74 |
*/ |
*/ |
75 |
public static final DataFlavor plainTextFlavor; |
public static final DataFlavor plainTextFlavor = |
76 |
|
new DataFlavor(java.io.InputStream.class, |
77 |
/** |
"text/plain; charset=unicode", |
78 |
* This is the data flavor used for transferring Java strings. The |
"plain unicode text"); |
|
* MIME type is "application/x-java-serialized-object" and the |
|
|
* representation class is <code>java.lang.String</code>. |
|
|
*/ |
|
|
public static final DataFlavor stringFlavor; |
|
79 |
|
|
80 |
/** |
/** |
81 |
* This is a data flavor used for transferring lists of files. The |
* This is the data flavor used for transferring Java strings. The |
82 |
* representation type is a <code>java.util.List</code>, with each element of |
* MIME type is "application/x-java-serialized-object" and the |
83 |
* the list being a <code>java.io.File</code>. |
* representation class is <code>java.lang.String</code>. |
84 |
*/ |
*/ |
85 |
public static final DataFlavor javaFileListFlavor; |
public static final DataFlavor stringFlavor = |
86 |
|
new DataFlavor(java.lang.String.class, "Java Unicode String"); |
87 |
|
|
88 |
/** |
/** |
89 |
* This is an image flavor used for transferring images. The |
* This is a data flavor used for transferring lists of files. The |
90 |
* representation type is a <code>java.awt.Image</code>. |
* representation type is a <code>java.util.List</code>, with each |
91 |
*/ |
* element of the list being a <code>java.io.File</code>. |
92 |
public static final DataFlavor imageFlavor; |
*/ |
93 |
|
public static final DataFlavor javaFileListFlavor = |
94 |
|
new DataFlavor(java.util.List.class, |
95 |
|
"application/x-java-file-list; class=java.util.List", |
96 |
|
"Java File List"); |
97 |
|
|
98 |
/** |
/** |
99 |
* This is the MIME type used for transferring a serialized object. |
* This is an image flavor used for transferring images. The |
100 |
* The representation class is the type of object be deserialized. |
* representation type is a <code>java.awt.Image</code>. |
101 |
*/ |
*/ |
102 |
public static final String javaSerializedObjectMimeType = |
public static final DataFlavor imageFlavor = |
103 |
"application/x-java-serialized-object"; |
new DataFlavor(java.awt.Image.class, "Java Image"); |
104 |
|
|
105 |
/** |
/** |
106 |
* This is the MIME type used to transfer a Java object reference within |
* This is the MIME type used for transferring a serialized object. |
107 |
* the same JVM. The representation class is the class of the object |
* The representation class is the type of object be deserialized. |
108 |
* being transferred. |
*/ |
109 |
*/ |
public static final String javaSerializedObjectMimeType = |
110 |
public static final String javaJVMLocalObjectMimeType = |
"application/x-java-serialized-object"; |
|
"application/x-java-jvm-local-objectref"; |
|
111 |
|
|
112 |
/** |
/** |
113 |
* This is the MIME type used to transfer a link to a remote object. |
* This is the MIME type used to transfer a Java object reference within |
114 |
* The representation class is the type of object being linked to. |
* the same JVM. The representation class is the class of the object |
115 |
*/ |
* being transferred. |
116 |
public static final String javaRemoteObjectMimeType = |
*/ |
117 |
"application/x-java-remote-object"; |
public static final String javaJVMLocalObjectMimeType = |
118 |
|
"application/x-java-jvm-local-objectref"; |
119 |
|
|
120 |
static |
/** |
121 |
{ |
* This is the MIME type used to transfer a link to a remote object. |
122 |
plainTextFlavor |
* The representation class is the type of object being linked to. |
123 |
= new DataFlavor(java.io.InputStream.class, |
*/ |
124 |
"text/plain; charset=unicode", |
public static final String javaRemoteObjectMimeType = |
125 |
"plain unicode text"); |
"application/x-java-remote-object"; |
|
|
|
|
stringFlavor |
|
|
= new DataFlavor(java.lang.String.class, |
|
|
"Java Unicode String"); |
|
|
|
|
|
javaFileListFlavor |
|
|
= new DataFlavor(java.util.List.class, |
|
|
"application/x-java-file-list; class=java.util.List", |
|
|
"Java File List"); |
|
|
|
|
|
imageFlavor |
|
|
= new DataFlavor(java.awt.Image.class, |
|
|
"Java Image"); |
|
|
} |
|
126 |
|
|
127 |
/*************************************************************************/ |
/*************************************************************************/ |
128 |
|
|
141 |
|
|
142 |
/*************************************************************************/ |
/*************************************************************************/ |
143 |
|
|
|
/* |
|
|
* Static Methods |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* This method attempts to load the named class. The following class |
|
|
* loaders are searched in order: the bootstrap class loader, the |
|
|
* system class loader, the context class loader (if it exists), and |
|
|
* the specified fallback class loader. |
|
|
* |
|
|
* @param className The name of the class to load. |
|
|
* @param classLoader The class loader to use if all others fail, which |
|
|
* may be <code>null</code>. |
|
|
* |
|
|
* @exception ClassNotFoundException If the class cannot be loaded. |
|
|
*/ |
|
|
protected static final Class<?> |
|
|
tryToLoadClass(String className, ClassLoader classLoader) |
|
|
throws ClassNotFoundException |
|
|
{ |
|
|
try |
|
|
{ |
|
|
return(Class.forName(className)); |
|
|
} |
|
|
catch(Exception e) { ; } |
|
|
// Commented out for Java 1.1 |
|
144 |
/* |
/* |
145 |
try |
* Static Methods |
146 |
|
*/ |
147 |
|
|
148 |
|
/** |
149 |
|
* This method attempts to load the named class. The following class |
150 |
|
* loaders are searched in order: the bootstrap class loader, the |
151 |
|
* system class loader, the context class loader (if it exists), and |
152 |
|
* the specified fallback class loader. |
153 |
|
* |
154 |
|
* @param className The name of the class to load. |
155 |
|
* @param classLoader The class loader to use if all others fail, which |
156 |
|
* may be <code>null</code>. |
157 |
|
* |
158 |
|
* @exception ClassNotFoundException If the class cannot be loaded. |
159 |
|
*/ |
160 |
|
protected static final Class<?> tryToLoadClass(String className, |
161 |
|
ClassLoader classLoader) |
162 |
|
throws ClassNotFoundException |
163 |
|
{ |
164 |
|
try |
165 |
|
{ |
166 |
|
return(Class.forName(className)); |
167 |
|
} |
168 |
|
catch(Exception e) { ; } |
169 |
|
// Commented out for Java 1.1 |
170 |
|
/* |
171 |
|
try |
172 |
|
{ |
173 |
|
return(className.getClass().getClassLoader().findClass(className)); |
174 |
|
} |
175 |
|
catch(Exception e) { ; } |
176 |
|
|
177 |
|
try |
178 |
|
{ |
179 |
|
return(ClassLoader.getSystemClassLoader().findClass(className)); |
180 |
|
} |
181 |
|
catch(Exception e) { ; } |
182 |
|
*/ |
183 |
|
|
184 |
|
// FIXME: What is the context class loader? |
185 |
|
/* |
186 |
|
try |
187 |
|
{ |
188 |
|
} |
189 |
|
catch(Exception e) { ; } |
190 |
|
*/ |
191 |
|
|
192 |
|
if (classLoader != null) |
193 |
|
return(classLoader.loadClass(className)); |
194 |
|
else |
195 |
|
throw new ClassNotFoundException(className); |
196 |
|
} |
197 |
|
|
198 |
|
private static Class getRepresentationClassFromMime(String mimeString, |
199 |
|
ClassLoader classLoader) |
200 |
{ |
{ |
201 |
return(className.getClass().getClassLoader().findClass(className)); |
String classname = getParameter("class", mimeString); |
202 |
|
if (classname != null) |
203 |
|
{ |
204 |
|
try |
205 |
|
{ |
206 |
|
return tryToLoadClass(classname, classLoader); |
207 |
|
} |
208 |
|
catch(Exception e) |
209 |
|
{ |
210 |
|
throw new IllegalArgumentException("classname: " + e.getMessage()); |
211 |
|
} |
212 |
|
} |
213 |
|
else |
214 |
|
return java.io.InputStream.class; |
215 |
} |
} |
216 |
catch(Exception e) { ; } |
|
217 |
|
/** |
218 |
|
* Returns the value of the named MIME type parameter, or <code>null</code> |
219 |
|
* if the parameter does not exist. Given the parameter name and the mime |
220 |
|
* string. |
221 |
|
* |
222 |
|
* @param paramName The name of the parameter. |
223 |
|
* @param mimeString The mime string from where the name should be found. |
224 |
|
* |
225 |
|
* @return The value of the parameter or null. |
226 |
|
*/ |
227 |
|
private static String getParameter(String paramName, String mimeString) |
228 |
|
{ |
229 |
|
int idx = mimeString.indexOf(paramName + "="); |
230 |
|
if (idx == -1) |
231 |
|
return(null); |
232 |
|
|
233 |
|
String value = mimeString.substring(idx + paramName.length() + 1); |
234 |
|
|
235 |
|
idx = value.indexOf(" "); |
236 |
|
if (idx == -1) |
237 |
|
return(value); |
238 |
|
else |
239 |
|
return(value.substring(0, idx)); |
240 |
|
} |
241 |
|
|
242 |
|
/** |
243 |
|
* XXX - Currently returns <code>plainTextFlavor</code>. |
244 |
|
*/ |
245 |
|
public static final DataFlavor getTextPlainUnicodeFlavor() |
246 |
|
{ |
247 |
|
return plainTextFlavor; |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
|
* Selects the best supported text flavor on this implementation. |
252 |
|
* Returns <code>null</code> when none of the given flavors is liked. |
253 |
|
* |
254 |
|
* The <code>DataFlavor</code> returned the first data flavor in the |
255 |
|
* array that has either a representation class which is (a subclass of) |
256 |
|
* <code>Reader</code> or <code>String</code>, or has a representation |
257 |
|
* class which is (a subclass of) <code>InputStream</code> and has a |
258 |
|
* primary MIME type of "text" and has an supported encoding. |
259 |
|
*/ |
260 |
|
public static final DataFlavor |
261 |
|
selectBestTextFlavor(DataFlavor[] availableFlavors) |
262 |
|
{ |
263 |
|
for(int i = 0; i < availableFlavors.length; i++) |
264 |
|
{ |
265 |
|
DataFlavor df = availableFlavors[i]; |
266 |
|
Class c = df.representationClass; |
267 |
|
|
268 |
|
// A Reader or String is good. |
269 |
|
if ((Reader.class.isAssignableFrom(c)) |
270 |
|
|| (String.class.isAssignableFrom(c))) |
271 |
|
return df; |
272 |
|
|
273 |
|
// A InputStream is good if the mime primary type is "text" |
274 |
|
if ((InputStream.class.isAssignableFrom(c)) |
275 |
|
&& ("text".equals(df.getPrimaryType()))) |
276 |
|
{ |
277 |
|
String encoding = availableFlavors[i].getParameter("charset"); |
278 |
|
if (encoding == null) |
279 |
|
encoding = "us-ascii"; |
280 |
|
Reader r = null; |
281 |
|
try |
282 |
|
{ |
283 |
|
// Try to construct a dummy reader with the found encoding |
284 |
|
r = new InputStreamReader |
285 |
|
(new ByteArrayInputStream(new byte[0]), encoding); |
286 |
|
} |
287 |
|
catch(UnsupportedEncodingException uee) { /* ignore */ } |
288 |
|
|
289 |
|
if (r != null) |
290 |
|
return df; |
291 |
|
} |
292 |
|
} |
293 |
|
|
294 |
|
// Nothing found |
295 |
|
return null; |
296 |
|
} |
297 |
|
|
|
try |
|
|
{ |
|
|
return(ClassLoader.getSystemClassLoader().findClass(className)); |
|
|
} |
|
|
catch(Exception e) { ; } |
|
|
*/ |
|
298 |
|
|
|
// FIXME: What is the context class loader? |
|
299 |
/* |
/* |
300 |
try |
* Constructors |
301 |
{ |
*/ |
302 |
} |
|
303 |
catch(Exception e) { ; } |
/** |
304 |
*/ |
* Empty public constructor needed for externalization. |
305 |
|
* Should not be used for normal instantiation. |
306 |
if (classLoader != null) |
*/ |
307 |
return(classLoader.loadClass(className)); |
public DataFlavor() |
308 |
else |
{ |
|
throw new ClassNotFoundException(className); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* Empty public constructor needed for externalization. |
|
|
* Should not be used for normal instantiation. |
|
|
*/ |
|
|
public |
|
|
DataFlavor() |
|
|
{ |
|
309 |
mimeType = null; |
mimeType = null; |
310 |
representationClass = null; |
representationClass = null; |
311 |
humanPresentableName = null; |
humanPresentableName = null; |
312 |
} |
} |
313 |
|
|
314 |
/*************************************************************************/ |
/*************************************************************************/ |
315 |
|
|
316 |
/** |
/** |
317 |
* Private constructor. |
* Private constructor. |
318 |
*/ |
*/ |
319 |
private |
private |
320 |
DataFlavor(Class<?> representationClass, |
DataFlavor(Class<?> representationClass, |
321 |
String mimeType, |
String mimeType, |
322 |
String humanPresentableName) |
String humanPresentableName) |
323 |
{ |
{ |
324 |
this.representationClass = representationClass; |
this.representationClass = representationClass; |
325 |
this.mimeType = mimeType; |
this.mimeType = mimeType; |
326 |
if (humanPresentableName != null) |
if (humanPresentableName != null) |
327 |
this.humanPresentableName = humanPresentableName; |
this.humanPresentableName = humanPresentableName; |
328 |
else |
else |
329 |
this.humanPresentableName = mimeType; |
this.humanPresentableName = mimeType; |
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>DataFlavor</code>. The class |
|
|
* and human readable name are specified, the MIME type will be |
|
|
* "application/x-java-serialized-object". If the human readable name |
|
|
* is not specified (<code>null</code>) then the human readable name |
|
|
* will be the same as the MIME type. |
|
|
* |
|
|
* @param representationClass The representation class for this object. |
|
|
* @param humanPresentableName The display name of the object. |
|
|
*/ |
|
|
public |
|
|
DataFlavor(Class<?> representationClass, String humanPresentableName) |
|
|
{ |
|
|
this(representationClass, |
|
|
"application/x-java-serialized-object" |
|
|
+ "; class=" |
|
|
+ representationClass.getName(), |
|
|
humanPresentableName); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>DataFlavor</code> with the |
|
|
* specified MIME type and description. If the MIME type has a |
|
|
* "class=<rep class>" parameter then the representation class will |
|
|
* be the class name specified. Otherwise the class defaults to |
|
|
* <code>java.io.InputStream</code>. If the human readable name |
|
|
* is not specified (<code>null</code>) then the human readable name |
|
|
* will be the same as the MIME type. |
|
|
* |
|
|
* @param mimeType The MIME type for this flavor. |
|
|
* @param humanPresentableName The display name of this flavor. |
|
|
* @param classLoader The class loader for finding classes if the default |
|
|
* class loaders do not work. |
|
|
* |
|
|
* @exception IllegalArgumentException If the representation class |
|
|
* specified cannot be loaded. |
|
|
* @exception ClassNotFoundException If the class is not loaded. |
|
|
*/ |
|
|
public |
|
|
DataFlavor(String mimeType, String humanPresentableName, |
|
|
ClassLoader classLoader) throws ClassNotFoundException |
|
|
{ |
|
|
this(getRepresentationClassFromMime(mimeType, classLoader), |
|
|
mimeType, humanPresentableName); |
|
|
} |
|
|
|
|
|
private static Class |
|
|
getRepresentationClassFromMime(String mimeString, ClassLoader classLoader) |
|
|
{ |
|
|
String classname = getParameter("class", mimeString); |
|
|
if (classname != null) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
return tryToLoadClass(classname, classLoader); |
|
|
} |
|
|
catch(Exception e) |
|
|
{ |
|
|
throw new IllegalArgumentException("classname: " + e.getMessage()); |
|
|
} |
|
330 |
} |
} |
|
else |
|
|
{ |
|
|
return java.io.InputStream.class; |
|
|
} |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>DataFlavor</code> with the |
|
|
* specified MIME type and description. If the MIME type has a |
|
|
* "class=<rep class>" parameter then the representation class will |
|
|
* be the class name specified. Otherwise the class defaults to |
|
|
* <code>java.io.InputStream</code>. If the human readable name |
|
|
* is not specified (<code>null</code>) then the human readable name |
|
|
* will be the same as the MIME type. This is the same as calling |
|
|
* <code>new DataFlavor(mimeType, humanPresentableName, null)</code>. |
|
|
* |
|
|
* @param mimeType The MIME type for this flavor. |
|
|
* @param humanPresentableName The display name of this flavor. |
|
|
* |
|
|
* @exception IllegalArgumentException If the representation class |
|
|
* specified cannot be loaded. |
|
|
*/ |
|
|
public |
|
|
DataFlavor(String mimeType, String humanPresentableName) |
|
|
{ |
|
|
this (getRepresentationClassFromMime (mimeType, null), |
|
|
mimeType, humanPresentableName); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Initializes a new instance of <code>DataFlavor</code> with the specified |
|
|
* MIME type. This type can have a "class=" parameter to specify the |
|
|
* representation class, and then the class must exist or an exception will |
|
|
* be thrown. If there is no "class=" parameter then the representation class |
|
|
* will be <code>java.io.InputStream</code>. This is the same as calling |
|
|
* <code>new DataFlavor(mimeType, null)</code>. |
|
|
* |
|
|
* @param mimeType The MIME type for this flavor. |
|
|
* |
|
|
* @exception IllegalArgumentException If a class is not specified in |
|
|
* the MIME type. |
|
|
* @exception ClassNotFoundException If the class cannot be loaded. |
|
|
*/ |
|
|
public |
|
|
DataFlavor(String mimeType) throws ClassNotFoundException |
|
|
{ |
|
|
this(mimeType, null); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the MIME type of this flavor. |
|
|
* |
|
|
* @return The MIME type for this flavor. |
|
|
*/ |
|
|
public String |
|
|
getMimeType() |
|
|
{ |
|
|
return(mimeType); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the representation class for this flavor. |
|
|
* |
|
|
* @return The representation class for this flavor. |
|
|
*/ |
|
|
public Class<?> |
|
|
getRepresentationClass() |
|
|
{ |
|
|
return(representationClass); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the human presentable name for this flavor. |
|
|
* |
|
|
* @return The human presentable name for this flavor. |
|
|
*/ |
|
|
public String |
|
|
getHumanPresentableName() |
|
|
{ |
|
|
return(humanPresentableName); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the primary MIME type for this flavor. |
|
|
* |
|
|
* @return The primary MIME type for this flavor. |
|
|
*/ |
|
|
public String |
|
|
getPrimaryType() |
|
|
{ |
|
|
int idx = mimeType.indexOf("/"); |
|
|
if (idx == -1) |
|
|
return(mimeType); |
|
|
|
|
|
return(mimeType.substring(0, idx)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the MIME subtype for this flavor. |
|
|
* |
|
|
* @return The MIME subtype for this flavor. |
|
|
*/ |
|
|
public String |
|
|
getSubType() |
|
|
{ |
|
|
int start = mimeType.indexOf("/"); |
|
|
if (start == -1) |
|
|
return ""; |
|
|
|
|
|
int end = mimeType.indexOf(";", start + 1); |
|
|
if (end == -1) |
|
|
return mimeType.substring(start + 1); |
|
|
else |
|
|
return mimeType.substring(start + 1, end); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the value of the named MIME type parameter, or <code>null</code> |
|
|
* if the parameter does not exist. Given the parameter name and the mime |
|
|
* string. |
|
|
* |
|
|
* @param paramName The name of the parameter. |
|
|
* @param mimeString The mime string from where the name should be found. |
|
|
* |
|
|
* @return The value of the parameter or null. |
|
|
*/ |
|
|
private static String |
|
|
getParameter(String paramName, String mimeString) |
|
|
{ |
|
|
int idx = mimeString.indexOf(paramName + "="); |
|
|
if (idx == -1) |
|
|
return(null); |
|
|
|
|
|
String value = mimeString.substring(idx + paramName.length() + 1); |
|
|
|
|
|
idx = value.indexOf(" "); |
|
|
if (idx == -1) |
|
|
return(value); |
|
|
else |
|
|
return(value.substring(0, idx)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the value of the named MIME type parameter, or <code>null</code> |
|
|
* if the parameter does not exist. |
|
|
* |
|
|
* @param paramName The name of the paramter. |
|
|
* |
|
|
* @return The value of the parameter. |
|
|
*/ |
|
|
public String |
|
|
getParameter(String paramName) |
|
|
{ |
|
|
if ("humanPresentableName".equals(paramName)) |
|
|
return getHumanPresentableName(); |
|
|
|
|
|
return getParameter(paramName, mimeType); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Sets the human presentable name to the specified value. |
|
|
* |
|
|
* @param humanPresentableName The new display name. |
|
|
*/ |
|
|
public void |
|
|
setHumanPresentableName(String humanPresentableName) |
|
|
{ |
|
|
this.humanPresentableName = humanPresentableName; |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests the MIME type of this object for equality against the specified |
|
|
* MIME type. Ignores parameters. |
|
|
* |
|
|
* @param mimeType The MIME type to test against. |
|
|
* |
|
|
* @return <code>true</code> if the MIME type is equal to this object's |
|
|
* MIME type (ignoring parameters), <code>false</code> otherwise. |
|
|
* |
|
|
* @exception NullPointerException If mimeType is null. |
|
|
*/ |
|
|
public boolean |
|
|
isMimeTypeEqual(String mimeType) |
|
|
{ |
|
|
String mime = getMimeType(); |
|
|
int i = mime.indexOf(";"); |
|
|
if (i != -1) |
|
|
mime = mime.substring(0, i); |
|
|
|
|
|
i = mimeType.indexOf(";"); |
|
|
if (i != -1) |
|
|
mimeType = mimeType.substring(0, i); |
|
|
|
|
|
return mime.equals(mimeType); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests the MIME type of this object for equality against the specified |
|
|
* data flavor's MIME type |
|
|
* |
|
|
* @param flavor The flavor to test against. |
|
|
* |
|
|
* @return <code>true</code> if the flavor's MIME type is equal to this |
|
|
* object's MIME type, <code>false</code> otherwise. |
|
|
*/ |
|
|
public final boolean |
|
|
isMimeTypeEqual(DataFlavor flavor) |
|
|
{ |
|
|
return(isMimeTypeEqual(flavor.getMimeType())); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests whether or not this flavor represents a serialized object. |
|
|
* |
|
|
* @return <code>true</code> if this flavor represents a serialized |
|
|
* object, <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
isMimeTypeSerializedObject() |
|
|
{ |
|
|
return(mimeType.startsWith(javaSerializedObjectMimeType)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests whether or not this flavor has a representation class of |
|
|
* <code>java.io.InputStream</code>. |
|
|
* |
|
|
* @return <code>true</code> if the representation class of this flavor |
|
|
* is <code>java.io.InputStream</code>, <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
isRepresentationClassInputStream() |
|
|
{ |
|
|
return(representationClass.getName().equals("java.io.InputStream")); |
|
|
} |
|
331 |
|
|
332 |
/*************************************************************************/ |
/*************************************************************************/ |
333 |
|
|
334 |
/** |
/** |
335 |
* Tests whether the representation class for this flavor is |
* Initializes a new instance of <code>DataFlavor</code>. The class |
336 |
* serializable. |
* and human readable name are specified, the MIME type will be |
337 |
* |
* "application/x-java-serialized-object". If the human readable name |
338 |
* @return <code>true</code> if the representation class is serializable, |
* is not specified (<code>null</code>) then the human readable name |
339 |
* <code>false</code> otherwise. |
* will be the same as the MIME type. |
340 |
*/ |
* |
341 |
public boolean |
* @param representationClass The representation class for this object. |
342 |
isRepresentationClassSerializable() |
* @param humanPresentableName The display name of the object. |
343 |
{ |
*/ |
344 |
Class[] interfaces = representationClass.getInterfaces(); |
public |
345 |
|
DataFlavor(Class<?> representationClass, String humanPresentableName) |
|
int i = 0; |
|
|
while (i < interfaces.length) |
|
346 |
{ |
{ |
347 |
if (interfaces[i].getName().equals("java.io.Serializable")) |
this(representationClass, |
348 |
return(true); |
"application/x-java-serialized-object" |
349 |
++i; |
+ "; class=" |
350 |
|
+ representationClass.getName(), |
351 |
|
humanPresentableName); |
352 |
} |
} |
353 |
|
|
354 |
return(false); |
/** |
355 |
} |
* Initializes a new instance of <code>DataFlavor</code> with the |
356 |
|
* specified MIME type and description. If the MIME type has a |
357 |
/*************************************************************************/ |
* "class=<rep class>" parameter then the representation class will |
358 |
|
* be the class name specified. Otherwise the class defaults to |
359 |
/** |
* <code>java.io.InputStream</code>. If the human readable name |
360 |
* Tests whether the representation class for his flavor is remote. |
* is not specified (<code>null</code>) then the human readable name |
361 |
* |
* will be the same as the MIME type. |
362 |
* @return <code>true</code> if the representation class is remote, |
* |
363 |
* <code>false</code> otherwise. |
* @param mimeType The MIME type for this flavor. |
364 |
*/ |
* @param humanPresentableName The display name of this flavor. |
365 |
public boolean |
* @param classLoader The class loader for finding classes if the default |
366 |
isRepresentationClassRemote() |
* class loaders do not work. |
367 |
{ |
* |
368 |
return Remote.class.isAssignableFrom (representationClass); |
* @exception IllegalArgumentException If the representation class |
369 |
} |
* specified cannot be loaded. |
370 |
|
* @exception ClassNotFoundException If the class is not loaded. |
371 |
/*************************************************************************/ |
*/ |
372 |
|
public DataFlavor(String mimeType, String humanPresentableName, |
373 |
/** |
ClassLoader classLoader) |
374 |
* Tests whether or not this flavor represents a serialized object. |
throws ClassNotFoundException |
375 |
* |
{ |
376 |
* @return <code>true</code> if this flavor represents a serialized |
this(getRepresentationClassFromMime(mimeType, classLoader), |
377 |
* object, <code>false</code> otherwise. |
mimeType, humanPresentableName); |
378 |
*/ |
} |
|
public boolean |
|
|
isFlavorSerializedObjectType() |
|
|
{ |
|
|
// FIXME: What is the diff between this and isMimeTypeSerializedObject? |
|
|
return(mimeType.startsWith(javaSerializedObjectMimeType)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests whether or not this flavor represents a remote object. |
|
|
* |
|
|
* @return <code>true</code> if this flavor represents a remote object, |
|
|
* <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
isFlavorRemoteObjectType() |
|
|
{ |
|
|
return(mimeType.startsWith(javaRemoteObjectMimeType)); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Tests whether or not this flavor represents a list of files. |
|
|
* |
|
|
* @return <code>true</code> if this flavor represents a list of files, |
|
|
* <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
isFlavorJavaFileListType() |
|
|
{ |
|
|
if (this.mimeType.equals(javaFileListFlavor.mimeType) && |
|
|
this.representationClass.equals(javaFileListFlavor.representationClass)) |
|
|
return(true); |
|
|
|
|
|
return(false); |
|
|
} |
|
379 |
|
|
380 |
/*************************************************************************/ |
/** |
381 |
|
* Initializes a new instance of <code>DataFlavor</code> with the |
382 |
|
* specified MIME type and description. If the MIME type has a |
383 |
|
* "class=<rep class>" parameter then the representation class will |
384 |
|
* be the class name specified. Otherwise the class defaults to |
385 |
|
* <code>java.io.InputStream</code>. If the human readable name |
386 |
|
* is not specified (<code>null</code>) then the human readable name |
387 |
|
* will be the same as the MIME type. This is the same as calling |
388 |
|
* <code>new DataFlavor(mimeType, humanPresentableName, null)</code>. |
389 |
|
* |
390 |
|
* @param mimeType The MIME type for this flavor. |
391 |
|
* @param humanPresentableName The display name of this flavor. |
392 |
|
* |
393 |
|
* @exception IllegalArgumentException If the representation class |
394 |
|
* specified cannot be loaded. |
395 |
|
*/ |
396 |
|
public DataFlavor(String mimeType, String humanPresentableName) |
397 |
|
{ |
398 |
|
this(getRepresentationClassFromMime (mimeType, null), |
399 |
|
mimeType, humanPresentableName); |
400 |
|
} |
401 |
|
|
402 |
/** |
/** |
403 |
* Returns a copy of this object. |
* Initializes a new instance of <code>DataFlavor</code> with the specified |
404 |
* |
* MIME type. This type can have a "class=" parameter to specify the |
405 |
* @return A copy of this object. |
* representation class, and then the class must exist or an exception will |
406 |
* |
* be thrown. If there is no "class=" parameter then the representation class |
407 |
* @exception CloneNotSupportedException If the object's class does not support |
* will be <code>java.io.InputStream</code>. This is the same as calling |
408 |
* the Cloneable interface. Subclasses that override the clone method can also |
* <code>new DataFlavor(mimeType, null)</code>. |
409 |
* throw this exception to indicate that an instance cannot be cloned. |
* |
410 |
*/ |
* @param mimeType The MIME type for this flavor. |
411 |
public Object clone () throws CloneNotSupportedException |
* |
412 |
{ |
* @exception IllegalArgumentException If a class is not specified in |
413 |
try |
* the MIME type. |
414 |
{ |
* @exception ClassNotFoundException If the class cannot be loaded. |
415 |
return(super.clone()); |
*/ |
416 |
} |
public DataFlavor(String mimeType) throws ClassNotFoundException |
417 |
catch(Exception e) |
{ |
418 |
{ |
this(mimeType, null); |
419 |
return(null); |
} |
|
} |
|
|
} |
|
420 |
|
|
421 |
/*************************************************************************/ |
/*************************************************************************/ |
422 |
|
|
423 |
/** |
/** |
424 |
* This method test the specified <code>DataFlavor</code> for equality |
* Returns the MIME type of this flavor. |
425 |
* against this object. This will be true if the MIME type and |
* |
426 |
* representation type are the equal. |
* @return The MIME type for this flavor. |
427 |
* |
*/ |
428 |
* @param flavor The <code>DataFlavor</code> to test against. |
public String getMimeType() |
429 |
* |
{ |
430 |
* @return <code>true</code> if the flavor is equal to this object, |
return(mimeType); |
431 |
* <code>false</code> otherwise. |
} |
|
*/ |
|
|
public boolean |
|
|
equals(DataFlavor flavor) |
|
|
{ |
|
|
if (flavor == null) |
|
|
return(false); |
|
432 |
|
|
433 |
if (!this.mimeType.toLowerCase().equals(flavor.mimeType.toLowerCase())) |
/** |
434 |
return(false); |
* Returns the representation class for this flavor. |
435 |
|
* |
436 |
|
* @return The representation class for this flavor. |
437 |
|
*/ |
438 |
|
public Class<?> getRepresentationClass() |
439 |
|
{ |
440 |
|
return(representationClass); |
441 |
|
} |
442 |
|
|
443 |
if (!this.representationClass.equals(flavor.representationClass)) |
/** |
444 |
return(false); |
* Returns the human presentable name for this flavor. |
445 |
|
* |
446 |
|
* @return The human presentable name for this flavor. |
447 |
|
*/ |
448 |
|
public String getHumanPresentableName() |
449 |
|
{ |
450 |
|
return(humanPresentableName); |
451 |
|
} |
452 |
|
|
453 |
return(true); |
/** |
454 |
} |
* Returns the primary MIME type for this flavor. |
455 |
|
* |
456 |
|
* @return The primary MIME type for this flavor. |
457 |
|
*/ |
458 |
|
public String getPrimaryType() |
459 |
|
{ |
460 |
|
int idx = mimeType.indexOf("/"); |
461 |
|
if (idx == -1) |
462 |
|
return(mimeType); |
463 |
|
|
464 |
|
return(mimeType.substring(0, idx)); |
465 |
|
} |
466 |
|
|
467 |
/*************************************************************************/ |
/** |
468 |
|
* Returns the MIME subtype for this flavor. |
469 |
|
* |
470 |
|
* @return The MIME subtype for this flavor. |
471 |
|
*/ |
472 |
|
public String getSubType() |
473 |
|
{ |
474 |
|
int start = mimeType.indexOf("/"); |
475 |
|
if (start == -1) |
476 |
|
return ""; |
477 |
|
|
478 |
|
int end = mimeType.indexOf(";", start + 1); |
479 |
|
if (end == -1) |
480 |
|
return mimeType.substring(start + 1); |
481 |
|
else |
482 |
|
return mimeType.substring(start + 1, end); |
483 |
|
} |
484 |
|
|
485 |
/** |
/** |
486 |
* This method test the specified <code>Object</code> for equality |
* Returns the value of the named MIME type parameter, or <code>null</code> |
487 |
* against this object. This will be true if the following conditions |
* if the parameter does not exist. |
488 |
* are met: |
* |
489 |
* <p> |
* @param paramName The name of the paramter. |
490 |
* <ul> |
* |
491 |
* <li>The object is not <code>null</code>.</li> |
* @return The value of the parameter. |
492 |
* <li>The object is an instance of <code>DataFlavor</code>.</li> |
*/ |
493 |
* <li>The object's MIME type and representation class are equal to |
public String getParameter(String paramName) |
494 |
* this object's.</li> |
{ |
495 |
* </ul> |
if ("humanPresentableName".equals(paramName)) |
496 |
* |
return getHumanPresentableName(); |
497 |
* @param obj The <code>Object</code> to test against. |
|
498 |
* |
return getParameter(paramName, mimeType); |
499 |
* @return <code>true</code> if the flavor is equal to this object, |
} |
|
* <code>false</code> otherwise. |
|
|
*/ |
|
|
public boolean |
|
|
equals(Object obj) |
|
|
{ |
|
|
if (!(obj instanceof DataFlavor)) |
|
|
return(false); |
|
500 |
|
|
501 |
return(equals((DataFlavor)obj)); |
/** |
502 |
} |
* Sets the human presentable name to the specified value. |
503 |
|
* |
504 |
|
* @param humanPresentableName The new display name. |
505 |
|
*/ |
506 |
|
public void setHumanPresentableName(String humanPresentableName) |
507 |
|
{ |
508 |
|
this.humanPresentableName = humanPresentableName; |
509 |
|
} |
510 |
|
|
511 |
/*************************************************************************/ |
/** |
512 |
|
* Tests the MIME type of this object for equality against the specified |
513 |
|
* MIME type. Ignores parameters. |
514 |
|
* |
515 |
|
* @param mimeType The MIME type to test against. |
516 |
|
* |
517 |
|
* @return <code>true</code> if the MIME type is equal to this object's |
518 |
|
* MIME type (ignoring parameters), <code>false</code> otherwise. |
519 |
|
* |
520 |
|
* @exception NullPointerException If mimeType is null. |
521 |
|
*/ |
522 |
|
public boolean isMimeTypeEqual(String mimeType) |
523 |
|
{ |
524 |
|
String mime = getMimeType(); |
525 |
|
int i = mime.indexOf(";"); |
526 |
|
if (i != -1) |
527 |
|
mime = mime.substring(0, i); |
528 |
|
|
529 |
|
i = mimeType.indexOf(";"); |
530 |
|
if (i != -1) |
531 |
|
mimeType = mimeType.substring(0, i); |
532 |
|
|
533 |
|
return mime.equals(mimeType); |
534 |
|
} |
535 |
|
|
536 |
/** |
/** |
537 |
* Tests whether or not the specified string is equal to the MIME type |
* Tests the MIME type of this object for equality against the specified |
538 |
* of this object. |
* data flavor's MIME type |
539 |
* |
* |
540 |
* @param str The string to test against. |
* @param flavor The flavor to test against. |
541 |
* |
* |
542 |
* @return <code>true</code> if the string is equal to this object's MIME |
* @return <code>true</code> if the flavor's MIME type is equal to this |
543 |
* type, <code>false</code> otherwise. |
* object's MIME type, <code>false</code> otherwise. |
544 |
* |
*/ |
545 |
* @deprecated Not compatible with <code>hashCode()</code>. |
public final boolean isMimeTypeEqual(DataFlavor flavor) |
546 |
* Use <code>isMimeTypeEqual()</code> |
{ |
547 |
*/ |
return isMimeTypeEqual(flavor.getMimeType()); |
548 |
public boolean |
} |
|
equals(String str) |
|
|
{ |
|
|
return(isMimeTypeEqual(str)); |
|
|
} |
|
549 |
|
|
550 |
/*************************************************************************/ |
/** |
551 |
|
* Tests whether or not this flavor represents a serialized object. |
552 |
|
* |
553 |
|
* @return <code>true</code> if this flavor represents a serialized |
554 |
|
* object, <code>false</code> otherwise. |
555 |
|
*/ |
556 |
|
public boolean isMimeTypeSerializedObject() |
557 |
|
{ |
558 |
|
return mimeType.startsWith(javaSerializedObjectMimeType); |
559 |
|
} |
560 |
|
|
561 |
/** |
/** |
562 |
* Returns the hash code for this data flavor. |
* Tests whether or not this flavor has a representation class of |
563 |
* The hash code is based on the (lower case) mime type and the |
* <code>java.io.InputStream</code>. |
564 |
* representation class. |
* |
565 |
*/ |
* @return <code>true</code> if the representation class of this flavor |
566 |
public int |
* is <code>java.io.InputStream</code>, <code>false</code> otherwise. |
567 |
hashCode() |
*/ |
568 |
{ |
public boolean isRepresentationClassInputStream() |
569 |
return(mimeType.toLowerCase().hashCode()^representationClass.hashCode()); |
{ |
570 |
} |
return representationClass.getName().equals("java.io.InputStream"); |
571 |
|
} |
572 |
|
|
573 |
/*************************************************************************/ |
/** |
574 |
|
* Tests whether the representation class for this flavor is |
575 |
|
* serializable. |
576 |
|
* |
577 |
|
* @return <code>true</code> if the representation class is serializable, |
578 |
|
* <code>false</code> otherwise. |
579 |
|
*/ |
580 |
|
public boolean isRepresentationClassSerializable() |
581 |
|
{ |
582 |
|
Class[] interfaces = representationClass.getInterfaces(); |
583 |
|
|
584 |
|
int i = 0; |
585 |
|
while (i < interfaces.length) |
586 |
|
{ |
587 |
|
if (interfaces[i].getName().equals("java.io.Serializable")) |
588 |
|
return true; |
589 |
|
++i; |
590 |
|
} |
591 |
|
|
592 |
|
return false; |
593 |
|
} |
594 |
|
|
595 |
/** |
/** |
596 |
* Returns <code>true</code> when the given <code>DataFlavor</code> |
* Tests whether the representation class for his flavor is remote. |
597 |
* matches this one. |
* |
598 |
*/ |
* @return <code>true</code> if the representation class is remote, |
599 |
public boolean |
* <code>false</code> otherwise. |
600 |
match(DataFlavor dataFlavor) |
*/ |
601 |
{ |
public boolean isRepresentationClassRemote() |
602 |
// XXX - How is this different from equals? |
{ |
603 |
return(equals(dataFlavor)); |
return Remote.class.isAssignableFrom (representationClass); |
604 |
} |
} |
605 |
|
|
606 |
/*************************************************************************/ |
/** |
607 |
|
* Tests whether or not this flavor represents a serialized object. |
608 |
|
* |
609 |
|
* @return <code>true</code> if this flavor represents a serialized |
610 |
|
* object, <code>false</code> otherwise. |
611 |
|
*/ |
612 |
|
public boolean isFlavorSerializedObjectType() |
613 |
|
{ |
614 |
|
// FIXME: What is the diff between this and isMimeTypeSerializedObject? |
615 |
|
return(mimeType.startsWith(javaSerializedObjectMimeType)); |
616 |
|
} |
617 |
|
|
618 |
/** |
/** |
619 |
* This method exists for backward compatibility. It simply returns |
* Tests whether or not this flavor represents a remote object. |
620 |
* the same name/value pair passed in. |
* |
621 |
* |
* @return <code>true</code> if this flavor represents a remote object, |
622 |
* @param name The parameter name. |
* <code>false</code> otherwise. |
623 |
* @param value The parameter value. |
*/ |
624 |
* |
public boolean isFlavorRemoteObjectType() |
625 |
* @return The name/value pair. |
{ |
626 |
* |
return(mimeType.startsWith(javaRemoteObjectMimeType)); |
627 |
* @deprecated |
} |
|
*/ |
|
|
protected String |
|
|
normalizeMimeTypeParameter(String name, String value) |
|
|
{ |
|
|
return(name + "=" + value); |
|
|
} |
|
628 |
|
|
629 |
/*************************************************************************/ |
/** |
630 |
|
* Tests whether or not this flavor represents a list of files. |
631 |
|
* |
632 |
|
* @return <code>true</code> if this flavor represents a list of files, |
633 |
|
* <code>false</code> otherwise. |
634 |
|
*/ |
635 |
|
public boolean isFlavorJavaFileListType() |
636 |
|
{ |
637 |
|
if (mimeType.equals(javaFileListFlavor.mimeType) |
638 |
|
&& representationClass.equals(javaFileListFlavor.representationClass)) |
639 |
|
return true; |
640 |
|
|
641 |
|
return false ; |
642 |
|
} |
643 |
|
|
644 |
/** |
/** |
645 |
* This method exists for backward compatibility. It simply returns |
* Returns a copy of this object. |
646 |
* the MIME type string unchanged. |
* |
647 |
* |
* @return A copy of this object. |
648 |
* @param type The MIME type. |
* |
649 |
* |
* @exception CloneNotSupportedException If the object's class does not support |
650 |
* @return The MIME type. |
* the Cloneable interface. Subclasses that override the clone method can also |
651 |
* |
* throw this exception to indicate that an instance cannot be cloned. |
652 |
* @deprecated |
*/ |
653 |
*/ |
public Object clone () throws CloneNotSupportedException |
654 |
protected String |
{ |
655 |
normalizeMimeType(String type) |
// FIXME - This cannot be right. |
656 |
{ |
try |
657 |
return(type); |
{ |
658 |
} |
return super.clone(); |
659 |
|
} |
660 |
|
catch(Exception e) |
661 |
|
{ |
662 |
|
return null; |
663 |
|
} |
664 |
|
} |
665 |
|
|
666 |
/*************************************************************************/ |
/** |
667 |
|
* This method test the specified <code>DataFlavor</code> for equality |
668 |
|
* against this object. This will be true if the MIME type and |
669 |
|
* representation type are the equal. |
670 |
|
* |
671 |
|
* @param flavor The <code>DataFlavor</code> to test against. |
672 |
|
* |
673 |
|
* @return <code>true</code> if the flavor is equal to this object, |
674 |
|
* <code>false</code> otherwise. |
675 |
|
*/ |
676 |
|
public boolean equals(DataFlavor flavor) |
677 |
|
{ |
678 |
|
if (flavor == null) |
679 |
|
return false; |
680 |
|
|
681 |
|
if (! this.mimeType.toLowerCase().equals(flavor.mimeType.toLowerCase())) |
682 |
|
return false; |
683 |
|
|
684 |
|
if (! this.representationClass.equals(flavor.representationClass)) |
685 |
|
return false; |
686 |
|
|
687 |
|
return true; |
688 |
|
} |
689 |
|
|
690 |
/** |
/** |
691 |
* Serialize this class. |
* This method test the specified <code>Object</code> for equality |
692 |
* |
* against this object. This will be true if the following conditions |
693 |
* @param stream The <code>ObjectOutput</code> stream to serialize to. |
* are met: |
694 |
* |
* <p> |
695 |
* @exception IOException If an error occurs. |
* <ul> |
696 |
*/ |
* <li>The object is not <code>null</code>.</li> |
697 |
public void |
* <li>The object is an instance of <code>DataFlavor</code>.</li> |
698 |
writeExternal(ObjectOutput stream) throws IOException |
* <li>The object's MIME type and representation class are equal to |
699 |
{ |
* this object's.</li> |
700 |
// FIXME: Implement me |
* </ul> |
701 |
} |
* |
702 |
|
* @param obj The <code>Object</code> to test against. |
703 |
|
* |
704 |
|
* @return <code>true</code> if the flavor is equal to this object, |
705 |
|
* <code>false</code> otherwise. |
706 |
|
*/ |
707 |
|
public boolean equals(Object obj) |
708 |
|
{ |
709 |
|
if (! (obj instanceof DataFlavor)) |
710 |
|
return false; |
711 |
|
|
712 |
|
return equals((DataFlavor) obj); |
713 |
|
} |
714 |
|
|
715 |
/*************************************************************************/ |
/** |
716 |
|
* Tests whether or not the specified string is equal to the MIME type |
717 |
|
* of this object. |
718 |
|
* |
719 |
|
* @param str The string to test against. |
720 |
|
* |
721 |
|
* @return <code>true</code> if the string is equal to this object's MIME |
722 |
|
* type, <code>false</code> otherwise. |
723 |
|
* |
724 |
|
* @deprecated Not compatible with <code>hashCode()</code>. |
725 |
|
* Use <code>isMimeTypeEqual()</code> |
726 |
|
*/ |
727 |
|
public boolean equals(String str) |
728 |
|
{ |
729 |
|
return isMimeTypeEqual(str); |
730 |
|
} |
731 |
|
|
732 |
/** |
/** |
733 |
* De-serialize this class. |
* Returns the hash code for this data flavor. |
734 |
* |
* The hash code is based on the (lower case) mime type and the |
735 |
* @param stream The <code>ObjectInput</code> stream to deserialize from. |
* representation class. |
736 |
* |
*/ |
737 |
* @exception IOException If an error ocurs. |
public int hashCode() |
738 |
* @exception ClassNotFoundException If the class for an object being restored |
{ |
739 |
* cannot be found. |
return mimeType.toLowerCase().hashCode() ^ representationClass.hashCode(); |
740 |
*/ |
} |
|
public void |
|
|
readExternal(ObjectInput stream) throws IOException, ClassNotFoundException |
|
|
{ |
|
|
// FIXME: Implement me |
|
|
} |
|
741 |
|
|
742 |
/*************************************************************************/ |
/** |
743 |
|
* Returns <code>true</code> when the given <code>DataFlavor</code> |
744 |
|
* matches this one. |
745 |
|
*/ |
746 |
|
public boolean match(DataFlavor dataFlavor) |
747 |
|
{ |
748 |
|
// XXX - How is this different from equals? |
749 |
|
return equals(dataFlavor); |
750 |
|
} |
751 |
|
|
752 |
/** |
/** |
753 |
* Returns a string representation of this DataFlavor. Including the |
* This method exists for backward compatibility. It simply returns |
754 |
* representation class name, MIME type and human presentable name. |
* the same name/value pair passed in. |
755 |
*/ |
* |
756 |
public String |
* @param name The parameter name. |
757 |
toString() |
* @param value The parameter value. |
758 |
{ |
* |
759 |
return(getClass().getName() |
* @return The name/value pair. |
760 |
+ "[representationClass=" + getRepresentationClass().getName() |
* |
761 |
+ ",mimeType=" + getMimeType() |
* @deprecated |
762 |
+ ",humanPresentableName=" + getHumanPresentableName() |
*/ |
763 |
+ "]"); |
protected String normalizeMimeTypeParameter(String name, String value) |
764 |
} |
{ |
765 |
|
return name + "=" + value; |
766 |
|
} |
767 |
|
|
768 |
/*************************************************************************/ |
/** |
769 |
|
* This method exists for backward compatibility. It simply returns |
770 |
|
* the MIME type string unchanged. |
771 |
|
* |
772 |
|
* @param type The MIME type. |
773 |
|
* |
774 |
|
* @return The MIME type. |
775 |
|
* |
776 |
|
* @deprecated |
777 |
|
*/ |
778 |
|
protected String normalizeMimeType(String type) |
779 |
|
{ |
780 |
|
return type; |
781 |
|
} |
782 |
|
|
783 |
/** |
/** |
784 |
* XXX - Currently returns <code>plainTextFlavor</code>. |
* Serialize this class. |
785 |
*/ |
* |
786 |
public static final DataFlavor |
* @param stream The <code>ObjectOutput</code> stream to serialize to. |
787 |
getTextPlainUnicodeFlavor() |
* |
788 |
{ |
* @exception IOException If an error occurs. |
789 |
return(plainTextFlavor); |
*/ |
790 |
} |
public void writeExternal(ObjectOutput stream) throws IOException |
791 |
|
{ |
792 |
|
// FIXME: Implement me |
793 |
|
} |
794 |
|
|
|
/*************************************************************************/ |
|
795 |
|
|
796 |
/** |
/** |
797 |
* XXX - Currently returns <code>java.io.InputStream</code>. |
* De-serialize this class. |
798 |
* |
* |
799 |
* @since 1.3 |
* @param stream The <code>ObjectInput</code> stream to deserialize from. |
800 |
*/ |
* |
801 |
public final Class<?> |
* @exception IOException If an error ocurs. |
802 |
getDefaultRepresentationClass() |
* @exception ClassNotFoundException If the class for an object being restored |
803 |
{ |
* cannot be found. |
804 |
return(java.io.InputStream.class); |
*/ |
805 |
} |
public void readExternal(ObjectInput stream) |
806 |
/*************************************************************************/ |
throws IOException, ClassNotFoundException |
807 |
|
{ |
808 |
|
// FIXME: Implement me |
809 |
|
} |
810 |
|
|
811 |
/** |
/** |
812 |
* XXX - Currently returns <code>java.io.InputStream</code>. |
* Returns a string representation of this DataFlavor. Including the |
813 |
*/ |
* representation class name, MIME type and human presentable name. |
814 |
public final String |
*/ |
815 |
getDefaultRepresentationClassAsString() |
public String toString() |
816 |
{ |
{ |
817 |
return(getDefaultRepresentationClass().getName()); |
return (getClass().getName() |
818 |
} |
+ "[representationClass=" + getRepresentationClass().getName() |
819 |
|
+ ",mimeType=" + getMimeType() |
820 |
|
+ ",humanPresentableName=" + getHumanPresentableName() |
821 |
|
+ "]"); |
822 |
|
} |
823 |
|
|
824 |
/*************************************************************************/ |
/** |
825 |
|
* XXX - Currently returns <code>java.io.InputStream</code>. |
826 |
|
* |
827 |
|
* @since 1.3 |
828 |
|
*/ |
829 |
|
public final Class<?> getDefaultRepresentationClass() |
830 |
|
{ |
831 |
|
return java.io.InputStream.class; |
832 |
|
} |
833 |
|
|
834 |
/** |
/** |
835 |
* Selects the best supported text flavor on this implementation. |
* XXX - Currently returns <code>java.io.InputStream</code>. |
836 |
* Returns <code>null</code> when none of the given flavors is liked. |
*/ |
837 |
* |
public final String getDefaultRepresentationClassAsString() |
838 |
* The <code>DataFlavor</code> returned the first data flavor in the |
{ |
839 |
* array that has either a representation class which is (a subclass of) |
return getDefaultRepresentationClass().getName(); |
840 |
* <code>Reader</code> or <code>String</code>, or has a representation |
} |
|
* class which is (a subclass of) <code>InputStream</code> and has a |
|
|
* primary MIME type of "text" and has an supported encoding. |
|
|
*/ |
|
|
public static final DataFlavor |
|
|
selectBestTextFlavor(DataFlavor[] availableFlavors) |
|
|
{ |
|
|
for(int i=0; i<availableFlavors.length; i++) |
|
|
{ |
|
|
DataFlavor df = availableFlavors[i]; |
|
|
Class c = df.representationClass; |
|
841 |
|
|
842 |
// A Reader or String is good. |
/** |
843 |
if ((Reader.class.isAssignableFrom(c)) |
* Creates a <code>Reader</code> for a given <code>Transferable</code>. |
844 |
|| (String.class.isAssignableFrom(c))) |
* |
845 |
{ |
* If the representation class is a (subclass of) <code>Reader</code> |
846 |
return df; |
* then an instance of the representation class is returned. If the |
847 |
} |
* representatation class is a <code>String</code> then a |
848 |
|
* <code>StringReader</code> is returned. And if the representation class |
849 |
// A InputStream is good if the mime primary type is "text" |
* is a (subclass of) <code>InputStream</code> and the primary MIME type |
850 |
if ((InputStream.class.isAssignableFrom(c)) |
* is "text" then a <code>InputStreamReader</code> for the correct charset |
851 |
&& ("text".equals(df.getPrimaryType()))) |
* encoding is returned. |
852 |
|
* |
853 |
|
* @param transferable The <code>Transferable</code> for which a text |
854 |
|
* <code>Reader</code> is requested. |
855 |
|
* |
856 |
|
* @exception IllegalArgumentException If the representation class is not one |
857 |
|
* of the seven listed above or the Transferable has null data. |
858 |
|
* @exception NullPointerException If the Transferable is null. |
859 |
|
* @exception UnsupportedFlavorException when the transferable doesn't |
860 |
|
* support this <code>DataFlavor</code>. Or if the representable class |
861 |
|
* isn't a (subclass of) <code>Reader</code>, <code>String</code>, |
862 |
|
* <code>InputStream</code> and/or the primary MIME type isn't "text". |
863 |
|
* @exception IOException when any IOException occurs. |
864 |
|
* @exception UnsupportedEncodingException if the "charset" isn't supported |
865 |
|
* on this platform. |
866 |
|
*/ |
867 |
|
public Reader getReaderForText(Transferable transferable) |
868 |
|
throws UnsupportedFlavorException, IOException |
869 |
|
{ |
870 |
|
if (!transferable.isDataFlavorSupported(this)) |
871 |
|
throw new UnsupportedFlavorException(this); |
872 |
|
|
873 |
|
if (Reader.class.isAssignableFrom(representationClass)) |
874 |
|
return (Reader)transferable.getTransferData(this); |
875 |
|
|
876 |
|
if (String.class.isAssignableFrom(representationClass)) |
877 |
|
return new StringReader((String)transferable.getTransferData(this)); |
878 |
|
|
879 |
|
if (InputStream.class.isAssignableFrom(representationClass) |
880 |
|
&& "text".equals(getPrimaryType())) |
881 |
{ |
{ |
882 |
String encoding = availableFlavors[i].getParameter("charset"); |
InputStream in = (InputStream)transferable.getTransferData(this); |
883 |
|
String encoding = getParameter("charset"); |
884 |
if (encoding == null) |
if (encoding == null) |
885 |
encoding = "us-ascii"; |
encoding = "us-ascii"; |
886 |
Reader r = null; |
return new InputStreamReader(in, encoding); |
|
try |
|
|
{ |
|
|
// Try to construct a dummy reader with the found encoding |
|
|
r = new InputStreamReader |
|
|
(new ByteArrayInputStream(new byte[0]), encoding); |
|
|
} |
|
|
catch(UnsupportedEncodingException uee) { /* ignore */ } |
|
|
if (r != null) |
|
|
return df; |
|
887 |
} |
} |
888 |
} |
|
889 |
|
throw new UnsupportedFlavorException(this); |
890 |
// Nothing found |
} |
|
return(null); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Creates a <code>Reader</code> for a given <code>Transferable</code>. |
|
|
* |
|
|
* If the representation class is a (subclass of) <code>Reader</code> |
|
|
* then an instance of the representation class is returned. If the |
|
|
* representatation class is a <code>String</code> then a |
|
|
* <code>StringReader</code> is returned. And if the representation class |
|
|
* is a (subclass of) <code>InputStream</code> and the primary MIME type |
|
|
* is "text" then a <code>InputStreamReader</code> for the correct charset |
|
|
* encoding is returned. |
|
|
* |
|
|
* @param transferable The <code>Transferable</code> for which a text |
|
|
* <code>Reader</code> is requested. |
|
|
* |
|
|
* @exception IllegalArgumentException If the representation class is not one |
|
|
* of the seven listed above or the Transferable has null data. |
|
|
* @exception NullPointerException If the Transferable is null. |
|
|
* @exception UnsupportedFlavorException when the transferable doesn't |
|
|
* support this <code>DataFlavor</code>. Or if the representable class |
|
|
* isn't a (subclass of) <code>Reader</code>, <code>String</code>, |
|
|
* <code>InputStream</code> and/or the primary MIME type isn't "text". |
|
|
* @exception IOException when any IOException occurs. |
|
|
* @exception UnsupportedEncodingException if the "charset" isn't supported |
|
|
* on this platform. |
|
|
*/ |
|
|
public Reader getReaderForText(Transferable transferable) |
|
|
throws UnsupportedFlavorException, IOException |
|
|
{ |
|
|
if (!transferable.isDataFlavorSupported(this)) |
|
|
throw new UnsupportedFlavorException(this); |
|
|
|
|
|
if (Reader.class.isAssignableFrom(representationClass)) |
|
|
return((Reader)transferable.getTransferData(this)); |
|
|
|
|
|
if (String.class.isAssignableFrom(representationClass)) |
|
|
return(new StringReader((String)transferable.getTransferData(this))); |
|
|
|
|
|
if (InputStream.class.isAssignableFrom(representationClass) |
|
|
&& "text".equals(getPrimaryType())) |
|
|
{ |
|
|
InputStream in = (InputStream)transferable.getTransferData(this); |
|
|
String encoding = getParameter("charset"); |
|
|
if (encoding == null) |
|
|
encoding = "us-ascii"; |
|
|
return(new InputStreamReader(in, encoding)); |
|
|
} |
|
|
|
|
|
throw new UnsupportedFlavorException(this); |
|
|
} |
|
891 |
|
|
892 |
/** |
/** |
893 |
* Returns whether the representation class for this DataFlavor is |
* Returns whether the representation class for this DataFlavor is |
895 |
* |
* |
896 |
* @since 1.4 |
* @since 1.4 |
897 |
*/ |
*/ |
898 |
public boolean isRepresentationClassByteBuffer () |
public boolean isRepresentationClassByteBuffer() |
899 |
{ |
{ |
900 |
return ByteBuffer.class.isAssignableFrom (representationClass); |
return ByteBuffer.class.isAssignableFrom(representationClass); |
901 |
} |
} |
902 |
|
|
903 |
/** |
/** |
906 |
* |
* |
907 |
* @since 1.4 |
* @since 1.4 |
908 |
*/ |
*/ |
909 |
public boolean isRepresentationClassCharBuffer () |
public boolean isRepresentationClassCharBuffer() |
910 |
{ |
{ |
911 |
return CharBuffer.class.isAssignableFrom (representationClass); |
return CharBuffer.class.isAssignableFrom(representationClass); |
912 |
} |
} |
913 |
|
|
914 |
/** |
/** |
917 |
* |
* |
918 |
* @since 1.4 |
* @since 1.4 |
919 |
*/ |
*/ |
920 |
public boolean isRepresentationClassReader () |
public boolean isRepresentationClassReader() |
921 |
{ |
{ |
922 |
return Reader.class.isAssignableFrom (representationClass); |
return Reader.class.isAssignableFrom(representationClass); |
923 |
|
} |
924 |
|
|
925 |
|
/** |
926 |
|
* Returns whether this <code>DataFlavor</code> is a valid text flavor for |
927 |
|
* this implementation of the Java platform. Only flavors equivalent to |
928 |
|
* <code>DataFlavor.stringFlavor</code> and <code>DataFlavor</code>s with |
929 |
|
* a primary MIME type of "text" can be valid text flavors. |
930 |
|
* <p> |
931 |
|
* If this flavor supports the charset parameter, it must be equivalent to |
932 |
|
* <code>DataFlavor.stringFlavor</code>, or its representation must be |
933 |
|
* <code>java.io.Reader</code>, <code>java.lang.String</code>, |
934 |
|
* <code>java.nio.CharBuffer</code>, <code>java.io.InputStream</code> or |
935 |
|
* <code>java.nio.ByteBuffer</code>, |
936 |
|
* If the representation is <code>java.io.InputStream</code> or |
937 |
|
* <code>java.nio.ByteBuffer</code>, then this flavor's <code>charset</code> |
938 |
|
* parameter must be supported by this implementation of the Java platform. |
939 |
|
* If a charset is not specified, then the platform default charset, which |
940 |
|
* is always supported, is assumed. |
941 |
|
* <p> |
942 |
|
* If this flavor does not support the charset parameter, its |
943 |
|
* representation must be <code>java.io.InputStream</code>, |
944 |
|
* <code>java.nio.ByteBuffer</code>. |
945 |
|
* <p> |
946 |
|
* See <code>selectBestTextFlavor</code> for a list of text flavors which |
947 |
|
* support the charset parameter. |
948 |
|
* |
949 |
|
* @return <code>true</code> if this <code>DataFlavor</code> is a valid |
950 |
|
* text flavor as described above; <code>false</code> otherwise |
951 |
|
* @see #selectBestTextFlavor |
952 |
|
* @since 1.4 |
953 |
|
*/ |
954 |
|
public boolean isFlavorTextType() { |
955 |
|
// FIXME: I'm not 100% sure if this implementation does the same like sun's does |
956 |
|
if(equals(DataFlavor.stringFlavor) || getPrimaryType().equals("text")) |
957 |
|
{ |
958 |
|
String charset = getParameter("charset"); |
959 |
|
Class c = getRepresentationClass(); |
960 |
|
if(charset != null) |
961 |
|
{ |
962 |
|
if(Reader.class.isAssignableFrom(c) |
963 |
|
|| CharBuffer.class.isAssignableFrom(c) |
964 |
|
|| String.class.isAssignableFrom(c)) |
965 |
|
{ |
966 |
|
return true; |
967 |
|
} |
968 |
|
else if(InputStream.class.isAssignableFrom(c) |
969 |
|
|| ByteBuffer.class.isAssignableFrom(c)) |
970 |
|
{ |
971 |
|
return Charset.isSupported(charset); |
972 |
|
} |
973 |
|
} |
974 |
|
else if(InputStream.class.isAssignableFrom(c) |
975 |
|
|| ByteBuffer.class.isAssignableFrom(c)) |
976 |
|
{ |
977 |
|
return true; |
978 |
|
} |
979 |
|
} |
980 |
|
return false; |
981 |
} |
} |
|
|
|
982 |
} // class DataFlavor |
} // class DataFlavor |
983 |
|
|