Index: java/awt/datatransfer/DataFlavor.java
===================================================================
RCS file: /cvs/classpath/java/awt/datatransfer/DataFlavor.java,v
retrieving revision 1.4
diff -u -u -r1.4 DataFlavor.java
--- java/awt/datatransfer/DataFlavor.java 2001/01/12 13:48:32 1.4
+++ java/awt/datatransfer/DataFlavor.java 2001/01/14 22:49:53
@@ -20,10 +20,12 @@
package java.awt.datatransfer;
+import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectInput;
-import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
@@ -91,22 +93,21 @@
static
{
- plainTextFlavor = new DataFlavor();
- plainTextFlavor.representationClass = java.io.InputStream.class;
- plainTextFlavor.mimeType = "text/plain; charset=unicode";
- plainTextFlavor.humanPresentableName = "plain unicode text";
-
- stringFlavor = new DataFlavor();
- stringFlavor.representationClass = java.lang.String.class;
- stringFlavor.mimeType = javaSerializedObjectMIMEType
- + "; class=java.lang.String";
- stringFlavor.humanPresentableName = "Java Unicode String";
-
- javaFileListFlavor = new DataFlavor();
- javaFileListFlavor.representationClass = java.util.List.class;
- javaFileListFlavor.mimeType = javaSerializedObjectMIMEType
- + "; class=java.util.list";
+ plainTextFlavor
+ = new DataFlavor(java.io.InputStream.class,
+ "text/plain; charset=unicode",
+ "plain unicode text");
+
+ stringFlavor
+ = new DataFlavor(java.lang.String.class,
+ "Java Unicode String");
+
+ javaFileListFlavor
+ = new DataFlavor(java.util.List.class,
+ "Java File List");
+
// javaFileListFlavor.mimeType = "application/x-java-file-list";
+}
/*************************************************************************/
@@ -192,6 +193,27 @@
public
DataFlavor()
{
+ mimeType = null;
+ representationClass = null;
+ humanPresentableName = null;
+}
+
+/*************************************************************************/
+
+/**
+ * Private constructor.
+ */
+private
+DataFlavor(Class representationClass,
+ String mimeType,
+ String humanPresentableName)
+{
+ this.representationClass = representationClass;
+ this.mimeType = mimeType;
+ if (humanPresentableName != null)
+ this.humanPresentableName = humanPresentableName;
+ else
+ this.humanPresentableName = mimeType;
}
/*************************************************************************/
@@ -209,13 +231,11 @@
public
DataFlavor(Class representationClass, String humanPresentableName)
{
- this.representationClass = representationClass;
- this.humanPresentableName = humanPresentableName;
- mimeType = "application/x-java-serialized-object"
- + "; class="
- + representationClass.getName();
- if (this.humanPresentableName == null)
- this.humanPresentableName = mimeType;
+ this(representationClass,
+ "application/x-java-serialized-object"
+ + "; class="
+ + representationClass.getName(),
+ humanPresentableName);
}
/*************************************************************************/
@@ -241,26 +261,29 @@
DataFlavor(String mimeType, String humanPresentableName,
ClassLoader classLoader) throws ClassNotFoundException
{
- this.mimeType = mimeType;
- if (humanPresentableName != null)
- this.humanPresentableName = humanPresentableName;
- else
- this.humanPresentableName = mimeType;
+ this(getRepresentationClassFromMime(mimeType, classLoader),
+ mimeType, humanPresentableName);
+}
- String classname = getParameter("class");
- if (className == null)
- representationClass = java.io.InputStream.class;
- else
+private static Class
+getRepresentationClassFromMime(String mimeString, ClassLoader classLoader)
+{
+ String classname = getParameter("class", mimeString);
+ if (classname != null)
{
try
{
- representationClass = tryToLoadClass(classname, classLoader);
+ return tryToLoadClass(classname, classLoader);
}
catch(Exception e)
{
throw new IllegalArgumentException("classname: " + e.getMessage());
}
}
+ else
+ {
+ return java.io.InputStream.class;
+ }
}
/*************************************************************************/
@@ -394,20 +417,22 @@
/**
* Returns the value of the named MIME type parameter, or null
- * if the parameter does not exist.
+ * if the parameter does not exist. Given the parameter name and the mime
+ * string.
*
- * @param paramName The name of the paramter.
+ * @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.
+ * @return The value of the parameter or null.
*/
-public String
-getParameter(String paramName)
+private static String
+getParameter(String paramName, String mimeString)
{
- int idx = mimeType.indexOf(paramName + "=");
+ int idx = mimeString.indexOf(paramName + "=");
if (idx == -1)
return(null);
- String value = mimeType.substring(idx + paramName.length() + 2);
+ String value = mimeString.substring(idx + paramName.length() + 2);
idx = value.indexOf(" ");
if (idx == -1)
@@ -417,6 +442,21 @@
}
/*************************************************************************/
+/**
+ * Returns the value of the named MIME type parameter, or null
+ * if the parameter does not exist.
+ *
+ * @param paramName The name of the paramter.
+ *
+ * @return The value of the parameter.
+ */
+public String
+getParameter(String paramName)
+{
+ return getParameter(paramName, mimeType);
+}
+
+/*************************************************************************/
/**
* Sets the human presentable name to the specified value.
@@ -800,6 +840,8 @@
/**
* XXX - Currently returns java.io.InputStream
.
+ *
+ * @since 1.3
*/
public static final Class
getDefaultRepresentationClass()
@@ -814,7 +856,7 @@
public static final String
getDefaultRepresentationClassAsString()
{
- return(getDefaultRepresentationClass.getName());
+ return(getDefaultRepresentationClass().getName());
}
/*************************************************************************/
@@ -836,9 +878,17 @@
{
DataFlavor df = availableFlavors[i];
Class c = df.representationClass;
- if ((c instanceof Reader) || (c instanceof String))
- return df;
- if ((c instanceof InputStream) && ("text".equals(df.getPrimaryType()))
+
+ // A Reader or String is good.
+ if ((Reader.class.isAssignableFrom(c))
+ || (String.class.isAssignableFrom(c)))
+ {
+ return df;
+ }
+
+ // A InputStream is good if the mime primary type is "text"
+ if ((InputStream.class.isAssignableFrom(c))
+ && ("text".equals(df.getPrimaryType())))
{
String encoding = availableFlavors[i].getParameter("charset");
if (encoding == null)
@@ -848,9 +898,9 @@
{
// Try to construct a dummy reader with the found encoding
r = new InputStreamReader
- (new ByteArrayInputStream(new byte[0]), encoding));
+ (new ByteArrayInputStream(new byte[0]), encoding);
}
- catch(UnsupportedEncodingException) { /* ignore */ }
+ catch(UnsupportedEncodingException uee) { /* ignore */ }
if (r != null)
return df;
}
@@ -888,26 +938,26 @@
IOException,
UnsupportedEncodingException
{
- if (!transferable.isDataFlavorSupported(this)
- throw UnsupportedFlavorException(this);
+ if (!transferable.isDataFlavorSupported(this))
+ throw new UnsupportedFlavorException(this);
- if (representationClass instanceof Reader)
+ if (Reader.class.isAssignableFrom(representationClass))
return((Reader)transferable.getTransferData(this));
- if (representationClass instanceof String)
- return(StringReader((String)transferable.getTransferData(this)));
+ if (String.class.isAssignableFrom(representationClass))
+ return(new StringReader((String)transferable.getTransferData(this)));
- if ((representationClass instanceof InputStream)
- && "text".equals(getPrimaryType())
+ 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(InputStreamReader(in, encoding));
+ return(new InputStreamReader(in, encoding));
}
- throw UnsupportedFlavorException(this);
+ throw new UnsupportedFlavorException(this);
}
} // class DataFlavor