📄 systemflavormap.java
字号:
// 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)flavorToNative.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)flavorToNative.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)flavorToNative.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 <code>List</code> of <code>DataFlavor</code>s 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 <code>DataFlavor</code> whose MIME type is a decoded * version of the native. * * @param nat the native whose corresponding <code>DataFlavor</code>s * should be returned. If <code>null</code> is specified, all * <code>DataFlavor</code>s currently known to the data transfer * subsystem are returned in a non-deterministic order. * @return a <code>java.util.List</code> of <code>DataFlavor</code> * objects into which platform-specific data in the specified, * platform-specific native can be translated * * @see #encodeJavaMIMEType * @since 1.4 */ public synchronized List 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 <code>Map</code> of the specified <code>DataFlavor</code>s to * their most preferred <code>String</code> native. Each native value will * be the same as the first native in the List returned by * <code>getNativesForFlavor</code> for the specified flavor. * <p> * If a specified <code>DataFlavor</code> is previously unknown to the * data transfer subsystem, then invoking this method will establish a * mapping in both directions between the specified <code>DataFlavor</code> * and an encoded version of its MIME type as its native. * * @param flavors an array of <code>DataFlavor</code>s which will be the * key set of the returned <code>Map</code>. If <code>null</code> is * specified, a mapping of all <code>DataFlavor</code>s known to the * data transfer subsystem to their most preferred * <code>String</code> natives will be returned. * @return a <code>java.util.Map</code> of <code>DataFlavor</code>s to * <code>String</code> natives * * @see #getNativesForFlavor * @see #encodeDataFlavor */ public synchronized Map 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 <code>Map</code> of the specified <code>String</code> natives * to their most preferred <code>DataFlavor</code>. Each * <code>DataFlavor</code> value will be the same as the first
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -