This adds the two missing methods in SystemFlavorMap and implements the
actual mapping by reading in the flavormap.properties file. I suppose
there's more that we should do, but I can't make up what with only the
specs at hand.
2006-12-11 Roman Kennke <[EMAIL PROTECTED]>
* java/awt/datatransfer/SystemFlavorMap.java
(flavorToNativeMap): Make typesafe.
(nativeToFlavorMap): Make typesafe.
(SystemFlavorMap): Read in mapping by reading the flavormap.properties.
(addFlavorForUnencodedNative): Access maps in a typesafe way.
(addUnencodedNativeForFlavor): Access maps in a typesafe way.
(getFlavorsForNative): Implemented.
(getNativesForFlavor): Implemented.
(setupMapping): New helper method.
/Roman
Index: java/awt/datatransfer/SystemFlavorMap.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/datatransfer/SystemFlavorMap.java,v
retrieving revision 1.12
diff -u -1 -5 -r1.12 SystemFlavorMap.java
--- java/awt/datatransfer/SystemFlavorMap.java 10 Dec 2006 20:25:43 -0000 1.12
+++ java/awt/datatransfer/SystemFlavorMap.java 11 Dec 2006 20:24:34 -0000
@@ -26,77 +26,169 @@
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package java.awt.datatransfer;
-import gnu.classpath.NotImplementedException;
-
+import java.awt.Toolkit;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Properties;
import java.util.WeakHashMap;
/**
* This class maps between native platform type names and DataFlavors.
*
* XXX - The current implementation does no mapping at all.
*
* @author Mark Wielaard ([EMAIL PROTECTED])
*
* @since 1.2
*/
public final class SystemFlavorMap implements FlavorMap, FlavorTable
{
/**
* The map which maps the thread's <code>ClassLoaders</code> to
* <code>SystemFlavorMaps</code>.
*/
private static final Map systemFlavorMaps = new WeakHashMap();
/**
* Constant which is used to prefix encode Java MIME types.
*/
private static final String GNU_JAVA_MIME_PREFIX = "gnu.java:";
/**
* This map maps native <code>String</code>s to lists of
* <code>DataFlavor</code>s
*/
- private HashMap nativeToFlavorMap = new HashMap();
+ private HashMap<String,List<DataFlavor>> nativeToFlavorMap =
+ new HashMap<String,List<DataFlavor>>();
/**
* This map maps <code>DataFlavor</code>s to lists of native
* <code>String</code>s
*/
- private HashMap flavorToNativeMap = new HashMap();
+ private HashMap<DataFlavor, List<String>> flavorToNativeMap =
+ new HashMap<DataFlavor, List<String>>();
/**
* Private constructor.
*/
private SystemFlavorMap ()
{
+ AccessController.doPrivileged
+ (new PrivilegedAction<Object>()
+ {
+ public Object run()
+ {
+ try
+ {
+ // Load installed flavormap.properties first.
+ String sep = File.separator;
+ File propsFile =
+ new File(System.getProperty("gnu.classpath.home.url")
+ + sep + "accessibility.properties");
+ InputStream in = new FileInputStream(propsFile);
+ Properties props = new Properties();
+ props.load(in);
+ in.close();
+
+ String augmented = Toolkit.getProperty("AWT.DnD.flavorMapFileURL",
+ null);
+ if (augmented != null)
+ {
+ URL url = new URL(augmented);
+ in = url.openStream();
+ props.load(in);
+ }
+ setupMapping(props);
+ }
+ catch (IOException ex)
+ {
+ // Can't do anything about it.
+ }
+ return null;
+ }
+ });
+ }
+
+ /**
+ * Sets up the mapping from native to mime types and vice versa as specified
+ * in the flavormap.properties file.
+ *
+ * This is package private to avoid an accessor method.
+ *
+ * @param props the properties file
+ */
+ void setupMapping(Properties props)
+ {
+ Enumeration propNames = props.propertyNames();
+ while (propNames.hasMoreElements())
+ {
+ try
+ {
+ String nat = (String) propNames.nextElement();
+ String mime = (String) props.getProperty(nat);
+ // Check valid mime type.
+ MimeType type = new MimeType(mime);
+ DataFlavor flav = new DataFlavor(mime);
+
+ List<DataFlavor> flavs = nativeToFlavorMap.get(nat);
+ if (flavs == null)
+ {
+ flavs = new ArrayList<DataFlavor>();
+ nativeToFlavorMap.put(nat, flavs);
+ }
+ List<String> nats = flavorToNativeMap.get(flav);
+ if (nats == null)
+ {
+ nats = new ArrayList<String>();
+ flavorToNativeMap.put(flav, nats);
+ }
+ flavs.add(flav);
+ nats.add(nat);
+ }
+ catch (ClassNotFoundException ex)
+ {
+ // Skip.
+ }
+ catch (MimeTypeParseException ex)
+ {
+ // Skip.
+ }
+ }
}
/**
* Maps the specified <code>DataFlavor</code> objects to the native
* data type name. The returned <code>Map</code> has keys that are
* the data flavors and values that are strings. The returned map
* may be modified. This can be useful for implementing nested mappings.
*
* @param flavors An array of data flavors to map
* or null for all data flavors.
*
* @return A <code>Map</code> of native data types to data flavors.
*/
public Map<DataFlavor, String> getNativesForFlavors (DataFlavor[] flavors)
{
@@ -251,69 +343,105 @@
/**
* Returns a List of <code>DataFlavors</code> to which the specified
* <code>String</code> native can be translated by the data transfer
* subsystem. The <code>List</code> will be sorted from best
* <code>DataFlavor</code> to worst. That is, the first <code>DataFlavor
* </code> will best reflect data in the specified native to a Java
* application.
* <p>
* If the specified native is previously unknown to the data transfer
* subsystem, and that native has been properly encoded, then invoking
* this method will establish a mapping in both directions between the
* specified native and a DataFlavor whose MIME type is a decoded
* version of the native.
*/
- public List<DataFlavor> getFlavorsForNative (String nat)
- throws NotImplementedException
+ public List<DataFlavor> getFlavorsForNative(String nat)
{
- throw new Error ("Not implemented");
+ List<DataFlavor> ret = new ArrayList<DataFlavor>();
+ if (nat == null)
+ {
+ Collection<List<DataFlavor>> all = nativeToFlavorMap.values();
+ for (List<DataFlavor> list : all)
+ {
+ for (DataFlavor flav : list)
+ {
+ if (! ret.contains(flav))
+ ret.add(flav);
+ }
+ }
+ }
+ else
+ {
+ List<DataFlavor> list = nativeToFlavorMap.get(nat);
+ if (list != null)
+ ret.addAll(list);
+ }
+ return ret;
}
public List<String> getNativesForFlavor (DataFlavor flav)
- throws NotImplementedException
{
- throw new Error ("Not implemented");
+ List<String> ret = new ArrayList<String>();
+ if (flav == null)
+ {
+ Collection<List<String>> all = flavorToNativeMap.values();
+ for (List<String> list : all)
+ {
+ for (String nat : list)
+ {
+ if (! ret.contains(nat))
+ ret.add(nat);
+ }
+ }
+ }
+ else
+ {
+ List<String> list = flavorToNativeMap.get(flav);
+ if (list != null)
+ ret.addAll(list);
+ }
+ return ret;
}
/**
* Adds a mapping from a single <code>String</code> native to a single
* <code>DataFlavor</code>. Unlike <code>getFlavorsForNative</code>, the
* mapping will only be established in one direction, and the native will
* not be encoded. To establish a two-way mapping, call
* <code>addUnencodedNativeForFlavor</code> as well. The new mapping will
* be of lower priority than any existing mapping.
* This method has no effect if a mapping from the specified
* <code>String</code> native to the specified or equal
* <code>DataFlavor</code> already exists.
*
* @param nativeStr the <code>String</code> native key for the mapping
* @param flavor the <code>DataFlavor</code> value for the mapping
* @throws NullPointerException if nat or flav is <code>null</code>
*
* @see #addUnencodedNativeForFlavor
* @since 1.4
*/
public synchronized void addFlavorForUnencodedNative(String nativeStr,
DataFlavor flavor)
{
if ((nativeStr == null) || (flavor == null))
throw new NullPointerException();
- List flavors = (List) nativeToFlavorMap.get(nativeStr);
+ List<DataFlavor> flavors = nativeToFlavorMap.get(nativeStr);
if (flavors == null)
{
- flavors = new ArrayList();
+ flavors = new ArrayList<DataFlavor>();
nativeToFlavorMap.put(nativeStr, flavors);
}
else
{
if (! flavors.contains(flavor))
flavors.add(flavor);
}
}
/**
* Adds a mapping from the specified <code>DataFlavor</code> (and all
* <code>DataFlavor</code>s equal to the specified <code>DataFlavor</code>)
* to the specified <code>String</code> native.
* Unlike <code>getNativesForFlavor</code>, the mapping will only be
* established in one direction, and the native will not be encoded. To
@@ -324,34 +452,34 @@
* <code>DataFlavor</code> to the specified <code>String</code> native
* already exists.
*
* @param flavor the <code>DataFlavor</code> key for the mapping
* @param nativeStr the <code>String</code> native value for the mapping
* @throws NullPointerException if flav or nat is <code>null</code>
*
* @see #addFlavorForUnencodedNative
* @since 1.4
*/
public synchronized void addUnencodedNativeForFlavor(DataFlavor flavor,
String nativeStr)
{
if ((nativeStr == null) || (flavor == null))
throw new NullPointerException();
- List natives = (List) flavorToNativeMap.get(flavor);
+ List<String> natives = flavorToNativeMap.get(flavor);
if (natives == null)
{
- natives = new ArrayList();
+ natives = new ArrayList<String>();
flavorToNativeMap.put(flavor, natives);
}
else
{
if (! natives.contains(nativeStr))
natives.add(nativeStr);
}
}
/**
* Discards the current mappings for the specified <code>DataFlavor</code>
* and all <code>DataFlavor</code>s equal to the specified
* <code>DataFlavor</code>, and creates new mappings to the
* specified <code>String</code> natives.
* Unlike <code>getNativesForFlavor</code>, the mappings will only be