The SystemFlavorMap is a configurable map between "natives" (Strings), which
correspond to platform-specific data formats, and "flavors" (DataFlavors),
which correspond to platform-independent MIME types. This mapping is used
by the data transfer subsystem to transfer data between Java and native
applications, and between Java applications in separate VMs.
In the Sun reference implementation, the default SystemFlavorMap is
initialized by the file jre/lib/flavormap.properties
and the
contents of the URL referenced by the AWT property
AWT.DnD.flavorMapFileURL
. See flavormap.properties
for details.
Method from java.awt.datatransfer.SystemFlavorMap Detail: |
public synchronized void addFlavorForUnencodedNative(String nat,
DataFlavor flav) {
if (nat == null || flav == null) {
throw new NullPointerException("null arguments not permitted");
}
List flavors = (List)getNativeToFlavor().get(nat);
if (flavors == null) {
flavors = new ArrayList(1);
getNativeToFlavor().put(nat, flavors);
} else if (flavors.contains(flav)) {
return;
}
flavors.add(flav);
getFlavorsForNativeCache.remove(nat);
getFlavorsForNativeCache.remove(null);
}
Adds a mapping from a single String native to a single
DataFlavor . Unlike getFlavorsForNative , the
mapping will only be established in one direction, and the native will
not be encoded. To establish a two-way mapping, call
addUnencodedNativeForFlavor 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
String native to the specified or equal
DataFlavor already exists. |
public synchronized void addUnencodedNativeForFlavor(DataFlavor flav,
String nat) {
if (flav == null || nat == null) {
throw new NullPointerException("null arguments not permitted");
}
List natives = (List)getFlavorToNative().get(flav);
if (natives == null) {
natives = new ArrayList(1);
getFlavorToNative().put(flav, natives);
} else if (natives.contains(nat)) {
return;
}
natives.add(nat);
getNativesForFlavorCache.remove(flav);
getNativesForFlavorCache.remove(null);
}
Adds a mapping from the specified DataFlavor (and all
DataFlavor s equal to the specified DataFlavor )
to the specified String native.
Unlike getNativesForFlavor , the mapping will only be
established in one direction, and the native will not be encoded. To
establish a two-way mapping, call
addFlavorForUnencodedNative 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 or equal
DataFlavor to the specified String native
already exists. |
public static DataFlavor decodeDataFlavor(String nat) throws ClassNotFoundException {
String retval_str = SystemFlavorMap.decodeJavaMIMEType(nat);
return (retval_str != null)
? new DataFlavor(retval_str)
: null;
}
Decodes a String native for use as a
DataFlavor . |
public static String decodeJavaMIMEType(String nat) {
return (isJavaMIMEType(nat))
? nat.substring(JavaMIME.length(), nat.length()).trim()
: null;
}
Decodes a String native for use as a Java MIME type. |
public static String encodeDataFlavor(DataFlavor flav) {
return (flav != null)
? SystemFlavorMap.encodeJavaMIMEType(flav.getMimeType())
: null;
}
Encodes a DataFlavor for use as a String
native. The format of an encoded DataFlavor is
implementation-dependent. The only restrictions are:
- The encoded representation is
null if and only if the
specified DataFlavor is null or its MIME type
String is null .
- The encoded representations for two non-
null
DataFlavor s with non-null MIME type
String s are equal if and only if the MIME type
String s of these DataFlavor s are equal
according to String.equals(Object) .
Sun's reference implementation of this method returns the MIME type
String of the specified DataFlavor prefixed
with JAVA_DATAFLAVOR: . |
public static String encodeJavaMIMEType(String mimeType) {
return (mimeType != null)
? JavaMIME + mimeType
: null;
}
Encodes a MIME type for use as a String native. The format
of an encoded representation of a MIME type is implementation-dependent.
The only restrictions are:
- The encoded representation is
null if and only if the
MIME type String is null .
- The encoded representations for two non-
null MIME type
String s are equal if and only if these String s
are equal according to String.equals(Object) .
Sun's reference implementation of this method returns the specified MIME
type String prefixed with JAVA_DATAFLAVOR: . |
public static FlavorMap getDefaultFlavorMap() {
ClassLoader contextClassLoader =
Thread.currentThread().getContextClassLoader();
if (contextClassLoader == null) {
contextClassLoader = ClassLoader.getSystemClassLoader();
}
FlavorMap fm;
synchronized(flavorMaps) {
fm = (FlavorMap)flavorMaps.get(contextClassLoader);
if (fm == null) {
fm = new SystemFlavorMap();
flavorMaps.put(contextClassLoader, fm);
}
}
return fm;
}
Returns the default FlavorMap for this thread's ClassLoader. |
public synchronized List<DataFlavor> getFlavorsForNative(String nat) {
// Check cache, even for null nat
SoftReference ref = (SoftReference)getFlavorsForNativeCache.get(nat);
if (ref != null) {
ArrayList retval = (ArrayList)ref.get();
if (retval != null) {
return (List)retval.clone();
}
}
LinkedList retval = new LinkedList();
if (nat == null) {
List natives = getNativesForFlavor(null);
HashSet dups = new HashSet(natives.size());
for (Iterator natives_iter = natives.iterator();
natives_iter.hasNext(); )
{
List flavors =
getFlavorsForNative((String)natives_iter.next());
for (Iterator flavors_iter = flavors.iterator();
flavors_iter.hasNext(); )
{
Object flavor = flavors_iter.next();
if (dups.add(flavor)) {
retval.add(flavor);
}
}
}
} else {
List flavors = nativeToFlavorLookup(nat);
if (disabledMappingGenerationKeys.contains(nat)) {
return flavors;
}
HashSet dups = new HashSet(flavors.size());
List flavorsAndbaseTypes = nativeToFlavorLookup(nat);
for (Iterator flavorsAndbaseTypes_iter =
flavorsAndbaseTypes.iterator();
flavorsAndbaseTypes_iter.hasNext(); )
{
Object value = flavorsAndbaseTypes_iter.next();
if (value instanceof String) {
String baseType = (String)value;
String subType = null;
try {
MimeType mimeType = new MimeType(baseType);
subType = mimeType.getSubType();
} catch (MimeTypeParseException mtpe) {
// Cannot happen, since we checked all mappings
// on load from flavormap.properties.
assert(false);
}
if (DataTransferer.doesSubtypeSupportCharset(subType,
null)) {
if (TEXT_PLAIN_BASE_TYPE.equals(baseType) &&
dups.add(DataFlavor.stringFlavor))
{
retval.add(DataFlavor.stringFlavor);
}
for (int i = 0; i < UNICODE_TEXT_CLASSES.length; i++) {
DataFlavor toAdd = null;
try {
toAdd = new DataFlavor
(baseType + ";charset=Unicode;class=" +
UNICODE_TEXT_CLASSES[i]);
} catch (ClassNotFoundException cannotHappen) {
}
if (dups.add(toAdd)) {
retval.add(toAdd);
}
}
for (Iterator charset_iter =
DataTransferer.standardEncodings();
charset_iter.hasNext(); )
{
String charset = (String)charset_iter.next();
for (int i = 0; i < ENCODED_TEXT_CLASSES.length;
i++)
{
DataFlavor toAdd = null;
try {
toAdd = new DataFlavor
(baseType + ";charset=" + charset +
";class=" + ENCODED_TEXT_CLASSES[i]);
} catch (ClassNotFoundException cannotHappen) {
}
// Check for equality to plainTextFlavor so
// that we can ensure that the exact charset of
// plainTextFlavor, not the canonical charset
// or another equivalent charset with a
// different name, is used.
if (toAdd.equals(DataFlavor.plainTextFlavor)) {
toAdd = DataFlavor.plainTextFlavor;
}
if (dups.add(toAdd)) {
retval.add(toAdd);
}
}
}
if (TEXT_PLAIN_BASE_TYPE.equals(baseType) &&
dups.add(DataFlavor.plainTextFlavor))
{
retval.add(DataFlavor.plainTextFlavor);
}
} else {
// Non-charset text natives should be treated as
// opaque, 8-bit data in any of its various
// representations.
for (int i = 0; i < ENCODED_TEXT_CLASSES.length; i++) {
DataFlavor toAdd = null;
try {
toAdd = new DataFlavor(baseType +
";class=" + ENCODED_TEXT_CLASSES[i]);
} catch (ClassNotFoundException cannotHappen) {
}
if (dups.add(toAdd)) {
retval.add(toAdd);
}
}
}
} else {
DataFlavor flavor = (DataFlavor)value;
if (dups.add(flavor)) {
retval.add(flavor);
}
}
}
}
ArrayList arrayList = new ArrayList(retval);
getFlavorsForNativeCache.put(nat, new SoftReference(arrayList));
return (List)arrayList.clone();
}
Returns a List of DataFlavor s to which the
specified String native can be translated by the data
transfer subsystem. The List will be sorted from best
DataFlavor to worst. That is, the first
DataFlavor will best reflect data in the specified
native to a Java application.
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.
If the specified native is not a properly encoded native and the
mappings for this native have not been altered with
setFlavorsForNative , then the contents of the
List is platform dependent, but null
cannot be returned. |
public synchronized Map<String, DataFlavor> getFlavorsForNatives(String[] natives) {
// Use getFlavorsForNative to generate extra flavors for text natives
if (natives == null) {
List native_list = getNativesForFlavor(null);
natives = new String[native_list.size()];
native_list.toArray(natives);
}
HashMap retval = new HashMap(natives.length, 1.0f);
for (int i = 0; i < natives.length; i++) {
List flavors = getFlavorsForNative(natives[i]);
DataFlavor flav = (flavors.isEmpty())
? null : (DataFlavor)flavors.get(0);
retval.put(natives[i], flav);
}
return retval;
}
Returns a Map of the specified String natives
to their most preferred DataFlavor . Each
DataFlavor value will be the same as the first
DataFlavor in the List returned by
getFlavorsForNative for the specified native.
If a 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 synchronized List<String> getNativesForFlavor(DataFlavor flav) {
List retval = null;
// Check cache, even for null flav
SoftReference ref = (SoftReference)getNativesForFlavorCache.get(flav);
if (ref != null) {
retval = (List)ref.get();
if (retval != null) {
// Create a copy, because client code can modify the returned
// list.
return new ArrayList(retval);
}
}
if (flav == null) {
retval = new ArrayList(getNativeToFlavor().keySet());
} else if (disabledMappingGenerationKeys.contains(flav)) {
// In this case we shouldn't synthesize a native for this flavor,
// since its mappings were explicitly specified.
retval = flavorToNativeLookup(flav, !SYNTHESIZE_IF_NOT_FOUND);
} else if (DataTransferer.isFlavorCharsetTextType(flav)) {
// For text/* flavors, flavor-to-native mappings specified in
// flavormap.properties are stored per flavor's base type.
if ("text".equals(flav.getPrimaryType())) {
retval = (List)getFlavorToNative().get(flav.mimeType.getBaseType());
if (retval != null) {
// To prevent the List stored in the map from modification.
retval = new ArrayList(retval);
}
}
// Also include text/plain natives, but don't duplicate Strings
List textPlainList = (List)getFlavorToNative().get(TEXT_PLAIN_BASE_TYPE);
if (textPlainList != null && !textPlainList.isEmpty()) {
// To prevent the List stored in the map from modification.
// This also guarantees that removeAll() is supported.
textPlainList = new ArrayList(textPlainList);
if (retval != null && !retval.isEmpty()) {
// Use HashSet to get constant-time performance for search.
textPlainList.removeAll(new HashSet(retval));
retval.addAll(textPlainList);
} else {
retval = textPlainList;
}
}
if (retval == null || retval.isEmpty()) {
retval = flavorToNativeLookup(flav, SYNTHESIZE_IF_NOT_FOUND);
} else {
// In this branch it is guaranteed that natives explicitly
// listed for flav's MIME type were added with
// addUnencodedNativeForFlavor(), so they have lower priority.
List explicitList =
flavorToNativeLookup(flav, !SYNTHESIZE_IF_NOT_FOUND);
// flavorToNativeLookup() never returns null.
// It can return an empty List, however.
if (!explicitList.isEmpty()) {
// To prevent the List stored in the map from modification.
// This also guarantees that removeAll() is supported.
explicitList = new ArrayList(explicitList);
// Use HashSet to get constant-time performance for search.
explicitList.removeAll(new HashSet(retval));
retval.addAll(explicitList);
}
}
} else if (DataTransferer.isFlavorNoncharsetTextType(flav)) {
retval = (List)getFlavorToNative().get(flav.mimeType.getBaseType());
if (retval == null || retval.isEmpty()) {
retval = flavorToNativeLookup(flav, SYNTHESIZE_IF_NOT_FOUND);
} else {
// In this branch it is guaranteed that natives explicitly
// listed for flav's MIME type were added with
// addUnencodedNativeForFlavor(), so they have lower priority.
List explicitList =
flavorToNativeLookup(flav, !SYNTHESIZE_IF_NOT_FOUND);
// flavorToNativeLookup() never returns null.
// It can return an empty List, however.
if (!explicitList.isEmpty()) {
// To prevent the List stored in the map from modification.
// This also guarantees that add/removeAll() are supported.
retval = new ArrayList(retval);
explicitList = new ArrayList(explicitList);
// Use HashSet to get constant-time performance for search.
explicitList.removeAll(new HashSet(retval));
retval.addAll(explicitList);
}
}
} else {
retval = flavorToNativeLookup(flav, SYNTHESIZE_IF_NOT_FOUND);
}
getNativesForFlavorCache.put(flav, new SoftReference(retval));
// Create a copy, because client code can modify the returned list.
return new ArrayList(retval);
}
Returns a List of String natives to which the
specified DataFlavor can be translated by the data transfer
subsystem. The List will be sorted from best native to
worst. That is, the first native will best reflect data in the specified
flavor to the underlying native platform.
If the specified DataFlavor is previously unknown to the
data transfer subsystem and the data transfer subsystem is unable to
translate this DataFlavor to any existing native, then
invoking this method will establish a
mapping in both directions between the specified DataFlavor
and an encoded version of its MIME type as its native. |
public synchronized Map<DataFlavor, String> getNativesForFlavors(DataFlavor[] flavors) {
// Use getNativesForFlavor to generate extra natives for text flavors
// and stringFlavor
if (flavors == null) {
List flavor_list = getFlavorsForNative(null);
flavors = new DataFlavor[flavor_list.size()];
flavor_list.toArray(flavors);
}
HashMap retval = new HashMap(flavors.length, 1.0f);
for (int i = 0; i < flavors.length; i++) {
List natives = getNativesForFlavor(flavors[i]);
String nat = (natives.isEmpty()) ? null : (String)natives.get(0);
retval.put(flavors[i], nat);
}
return retval;
}
Returns a Map of the specified DataFlavor s to
their most preferred String native. Each native value will
be the same as the first native in the List returned by
getNativesForFlavor for the specified flavor.
If a specified DataFlavor is previously unknown to the
data transfer subsystem, then invoking this method will establish a
mapping in both directions between the specified DataFlavor
and an encoded version of its MIME type as its native. |
public static boolean isJavaMIMEType(String str) {
return (str != null && str.startsWith(JavaMIME, 0));
}
Returns whether the specified String is an encoded Java
MIME type. |
public synchronized void setFlavorsForNative(String nat,
DataFlavor[] flavors) {
if (nat == null || flavors == null) {
throw new NullPointerException("null arguments not permitted");
}
getNativeToFlavor().remove(nat);
for (int i = 0; i < flavors.length; i++) {
addFlavorForUnencodedNative(nat, flavors[i]);
}
disabledMappingGenerationKeys.add(nat);
// Clear the cache to handle the case of empty flavors.
getFlavorsForNativeCache.remove(nat);
getFlavorsForNativeCache.remove(null);
}
Discards the current mappings for the specified String
native, and creates new mappings to the specified
DataFlavor s. Unlike getFlavorsForNative , the
mappings will only be established in one direction, and the natives need
not be encoded. To establish two-way mappings, call
setNativesForFlavor as well. The first
DataFlavor in the array will represent the highest priority
mapping. Subsequent DataFlavor s will represent mappings of
decreasing priority.
If the array contains several elements that reference equal
DataFlavor s, this method will establish new mappings
for the first of those elements and ignore the rest of them.
It is recommended that client code not reset mappings established by the
data transfer subsystem. This method should only be used for
application-level mappings. |
public synchronized void setNativesForFlavor(DataFlavor flav,
String[] natives) {
if (flav == null || natives == null) {
throw new NullPointerException("null arguments not permitted");
}
getFlavorToNative().remove(flav);
for (int i = 0; i < natives.length; i++) {
addUnencodedNativeForFlavor(flav, natives[i]);
}
disabledMappingGenerationKeys.add(flav);
// Clear the cache to handle the case of empty natives.
getNativesForFlavorCache.remove(flav);
getNativesForFlavorCache.remove(null);
}
Discards the current mappings for the specified DataFlavor
and all DataFlavor s equal to the specified
DataFlavor , and creates new mappings to the
specified String natives.
Unlike getNativesForFlavor , the mappings will only be
established in one direction, and the natives will not be encoded. To
establish two-way mappings, call setFlavorsForNative
as well. The first native in the array will represent the highest
priority mapping. Subsequent natives will represent mappings of
decreasing priority.
If the array contains several elements that reference equal
String natives, this method will establish new mappings
for the first of those elements and ignore the rest of them.
It is recommended that client code not reset mappings established by the
data transfer subsystem. This method should only be used for
application-level mappings. |