3090 lines · 116.7 KB · Java
RawDownload
    public static boolean isEncodingSupported(String encoding) {        if (encoding == null) {            return false;        }        try {            return Charset.isSupported(encoding);        } catch (IllegalCharsetNameException icne) {            return false;        }    }     /**     * Returns {@code true} if the given type is a java.rmi.Remote.     */    public static boolean isRemote(Class<?> type) {        return RMI.isRemote(type);    }     /**     * Returns an Iterator which traverses a SortedSet of Strings which are     * a total order of the standard character sets supported by the JRE. The     * ordering follows the same principles as DataFlavor.selectBestTextFlavor.     * So as to avoid loading all available character converters, optional,     * non-standard, character sets are not included.     */    public static Iterator standardEncodings() {        return StandardEncodingsHolder.standardEncodings.iterator();    }     /**     * Converts a FlavorMap to a FlavorTable.     */    public static FlavorTable adaptFlavorMap(final FlavorMap map) {        if (map instanceof FlavorTable) {            return (FlavorTable)map;        }         return new FlavorTable() {                public Map getNativesForFlavors(DataFlavor[] flavors) {                    return map.getNativesForFlavors(flavors);                }                public Map getFlavorsForNatives(String[] natives) {                    return map.getFlavorsForNatives(natives);                }                public List getNativesForFlavor(DataFlavor flav) {                    Map natives =                        getNativesForFlavors(new DataFlavor[] { flav } );                    String nat = (String)natives.get(flav);                    if (nat != null) {                        List list = new ArrayList(1);                        list.add(nat);                        return list;                    } else {                        return Collections.EMPTY_LIST;                    }                }                public List getFlavorsForNative(String nat) {                    Map flavors =                        getFlavorsForNatives(new String[] { nat } );                    DataFlavor flavor = (DataFlavor)flavors.get(nat);                    if (flavor != null) {                        List list = new ArrayList(1);                        list.add(flavor);                        return list;                    } else {                        return Collections.EMPTY_LIST;                    }                }            };    }     /**     * Returns the default Unicode encoding for the platform. The encoding     * need not be canonical. This method is only used by the archaic function     * DataFlavor.getTextPlainUnicodeFlavor().     */    public abstract String getDefaultUnicodeEncoding();     /**     * This method is called for text flavor mappings established while parsing     * the flavormap.properties file. It stores the "eoln" and "terminators"     * parameters which are not officially part of the MIME type. They are     * MIME parameters specific to the flavormap.properties file format.     */    public void registerTextFlavorProperties(String nat, String charset,                                             String eoln, String terminators) {        Long format = getFormatForNativeAsLong(nat);         textNatives.add(format);        nativeCharsets.put(format, (charset != null && charset.length() != 0)            ? charset : getDefaultTextCharset());        if (eoln != null && eoln.length() != 0 && !eoln.equals("\n")) {            nativeEOLNs.put(format, eoln);        }        if (terminators != null && terminators.length() != 0) {            Integer iTerminators = Integer.valueOf(terminators);            if (iTerminators.intValue() > 0) {                nativeTerminators.put(format, iTerminators);            }        }    }     /**     * Determines whether the native corresponding to the specified long format     * was listed in the flavormap.properties file.     */    protected boolean isTextFormat(long format) {        return textNatives.contains(Long.valueOf(format));    }     protected String getCharsetForTextFormat(Long lFormat) {        return (String)nativeCharsets.get(lFormat);    }     /**     * Specifies whether text imported from the native system in the specified     * format is locale-dependent. If so, when decoding such text,     * 'nativeCharsets' should be ignored, and instead, the Transferable should     * be queried for its javaTextEncodingFlavor data for the correct encoding.     */    public abstract boolean isLocaleDependentTextFormat(long format);     /**     * Determines whether the DataFlavor corresponding to the specified long     * format is DataFlavor.javaFileListFlavor.     */    public abstract boolean isFileFormat(long format);     /**     * Determines whether the DataFlavor corresponding to the specified long     * format is DataFlavor.imageFlavor.     */    public abstract boolean isImageFormat(long format);     /**     * Determines whether the format is a URI list we can convert to     * a DataFlavor.javaFileListFlavor.     */    protected boolean isURIListFormat(long format) {        return false;    }     /**     * Returns a Map whose keys are all of the possible formats into which the     * Transferable's transfer data flavors can be translated. The value of     * each key is the DataFlavor in which the Transferable's data should be     * requested when converting to the format.     * <p>     * The map keys are sorted according to the native formats preference     * order.     */    public SortedMap getFormatsForTransferable(Transferable contents,                                               FlavorTable map) {        DataFlavor[] flavors = contents.getTransferDataFlavors();        if (flavors == null) {            return new TreeMap();        }        return getFormatsForFlavors(flavors, map);    }     /**     * Returns a Map whose keys are all of the possible formats into which data     * in the specified DataFlavor can be translated. The value of each key     * is the DataFlavor in which a Transferable's data should be requested     * when converting to the format.     * <p>     * The map keys are sorted according to the native formats preference     * order.     */    public SortedMap getFormatsForFlavor(DataFlavor flavor, FlavorTable map) {        return getFormatsForFlavors(new DataFlavor[] { flavor },                                    map);    }     /**     * Returns a Map whose keys are all of the possible formats into which data     * in the specified DataFlavors can be translated. The value of each key     * is the DataFlavor in which the Transferable's data should be requested     * when converting to the format.     * <p>     * The map keys are sorted according to the native formats preference     * order.     *     * @param flavors the data flavors     * @param map the FlavorTable which contains mappings between     *            DataFlavors and data formats     * @throws NullPointerException if flavors or map is <code>null</code>     */    public SortedMap getFormatsForFlavors(DataFlavor[] flavors, FlavorTable map) {        Map formatMap = new HashMap(flavors.length);        Map textPlainMap = new HashMap(flavors.length);        // Maps formats to indices that will be used to sort the formats        // according to the preference order.        // Larger index value corresponds to the more preferable format.        Map indexMap = new HashMap(flavors.length);        Map textPlainIndexMap = new HashMap(flavors.length);         int currentIndex = 0;         // Iterate backwards so that preferred DataFlavors are used over        // other DataFlavors. (See javadoc for        // Transferable.getTransferDataFlavors.)        for (int i = flavors.length - 1; i >= 0; i--) {            DataFlavor flavor = flavors[i];            if (flavor == null) continue;             // Don't explicitly test for String, since it is just a special            // case of Serializable            if (flavor.isFlavorTextType() ||                flavor.isFlavorJavaFileListType() ||                DataFlavor.imageFlavor.equals(flavor) ||                flavor.isRepresentationClassSerializable() ||                flavor.isRepresentationClassInputStream() ||                flavor.isRepresentationClassRemote())            {                List natives = map.getNativesForFlavor(flavor);                 currentIndex += natives.size();                 for (Iterator iter = natives.iterator(); iter.hasNext(); ) {                    Long lFormat =                        getFormatForNativeAsLong((String)iter.next());                    Integer index = Integer.valueOf(currentIndex--);                     formatMap.put(lFormat, flavor);                    indexMap.put(lFormat, index);                     // SystemFlavorMap.getNativesForFlavor will return                    // text/plain natives for all text/*. While this is good                    // for a single text/* flavor, we would prefer that                    // text/plain native data come from a text/plain flavor.                    if (("text".equals(flavor.getPrimaryType()) &&                         "plain".equals(flavor.getSubType())) ||                        flavor.equals(DataFlavor.stringFlavor))                    {                        textPlainMap.put(lFormat, flavor);                        textPlainIndexMap.put(lFormat, index);                    }                }                 currentIndex += natives.size();            }        }         formatMap.putAll(textPlainMap);        indexMap.putAll(textPlainIndexMap);         // Sort the map keys according to the formats preference order.        Comparator comparator =            new IndexOrderComparator(indexMap, IndexedComparator.SELECT_WORST);        SortedMap sortedMap = new TreeMap(comparator);        sortedMap.putAll(formatMap);         return sortedMap;    }     /**     * Reduces the Map output for the root function to an array of the     * Map's keys.     */    public long[] getFormatsForTransferableAsArray(Transferable contents,                                                   FlavorTable map) {        return keysToLongArray(getFormatsForTransferable(contents, map));    }    public long[] getFormatsForFlavorAsArray(DataFlavor flavor,                                             FlavorTable map) {        return keysToLongArray(getFormatsForFlavor(flavor, map));    }    public long[] getFormatsForFlavorsAsArray(DataFlavor[] flavors,                                              FlavorTable map) {        return keysToLongArray(getFormatsForFlavors(flavors, map));    }     /**     * Returns a Map whose keys are all of the possible DataFlavors into which     * data in the specified format can be translated. The value of each key     * is the format in which the Clipboard or dropped data should be requested     * when converting to the DataFlavor.     */    public Map getFlavorsForFormat(long format, FlavorTable map) {        return getFlavorsForFormats(new long[] { format }, map);    }     /**     * Returns a Map whose keys are all of the possible DataFlavors into which     * data in the specified formats can be translated. The value of each key     * is the format in which the Clipboard or dropped data should be requested     * when converting to the DataFlavor.     */    public Map getFlavorsForFormats(long[] formats, FlavorTable map) {        Map flavorMap = new HashMap(formats.length);        Set mappingSet = new HashSet(formats.length);        Set flavorSet = new HashSet(formats.length);         // First step: build flavorSet, mappingSet and initial flavorMap        // flavorSet  - the set of all the DataFlavors into which        //              data in the specified formats can be translated;        // mappingSet - the set of all the mappings from the specified formats        //              into any DataFlavor;        // flavorMap  - after this step, this map maps each of the DataFlavors        //              from flavorSet to any of the specified formats.        for (int i = 0; i < formats.length; i++) {            long format = formats[i];            String nat = getNativeForFormat(format);            List flavors = map.getFlavorsForNative(nat);             for (Iterator iter = flavors.iterator(); iter.hasNext(); ) {                DataFlavor flavor = (DataFlavor)iter.next();                 // Don't explicitly test for String, since it is just a special                // case of Serializable                if (flavor.isFlavorTextType() ||                    flavor.isFlavorJavaFileListType() ||                    DataFlavor.imageFlavor.equals(flavor) ||                    flavor.isRepresentationClassSerializable() ||                    flavor.isRepresentationClassInputStream() ||                    flavor.isRepresentationClassRemote())                {                    Long lFormat = Long.valueOf(format);                    Object mapping =                        DataTransferer.createMapping(lFormat, flavor);                    flavorMap.put(flavor, lFormat);                    mappingSet.add(mapping);                    flavorSet.add(flavor);                }            }        }         // Second step: for each DataFlavor try to figure out which of the        // specified formats is the best to translate to this flavor.        // Then map each flavor to the best format.        // For the given flavor, FlavorTable indicates which native will        // best reflect data in the specified flavor to the underlying native        // platform. We assume that this native is the best to translate        // to this flavor.        // Note: FlavorTable allows one-way mappings, so we can occasionally        // map a flavor to the format for which the corresponding        // format-to-flavor mapping doesn't exist. For this reason we have built        // a mappingSet of all format-to-flavor mappings for the specified formats        // and check if the format-to-flavor mapping exists for the        // (flavor,format) pair being added.        for (Iterator flavorIter = flavorSet.iterator();             flavorIter.hasNext(); ) {            DataFlavor flavor = (DataFlavor)flavorIter.next();             List natives = map.getNativesForFlavor(flavor);             for (Iterator nativeIter = natives.iterator();                 nativeIter.hasNext(); ) {                Long lFormat =                    getFormatForNativeAsLong((String)nativeIter.next());                Object mapping = DataTransferer.createMapping(lFormat, flavor);                 if (mappingSet.contains(mapping)) {                    flavorMap.put(flavor, lFormat);                    break;                }            }        }         return flavorMap;    }     /**     * Returns a Set of all DataFlavors for which     * 1) a mapping from at least one of the specified formats exists in the     * specified map and     * 2) the data translation for this mapping can be performed by the data     * transfer subsystem.     *     * @param formats the data formats     * @param map the FlavorTable which contains mappings between     *            DataFlavors and data formats     * @throws NullPointerException if formats or map is <code>null</code>     */    public Set getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) {        Set flavorSet = new HashSet(formats.length);         for (int i = 0; i < formats.length; i++) {            String nat = getNativeForFormat(formats[i]);            List flavors = map.getFlavorsForNative(nat);             for (Iterator iter = flavors.iterator(); iter.hasNext(); ) {                DataFlavor flavor = (DataFlavor)iter.next();                 // Don't explicitly test for String, since it is just a special                // case of Serializable                if (flavor.isFlavorTextType() ||                    flavor.isFlavorJavaFileListType() ||                    DataFlavor.imageFlavor.equals(flavor) ||                    flavor.isRepresentationClassSerializable() ||                    flavor.isRepresentationClassInputStream() ||                    flavor.isRepresentationClassRemote())                {                    flavorSet.add(flavor);                }            }        }         return flavorSet;    }     /**     * Returns an array of all DataFlavors for which     * 1) a mapping from the specified format exists in the specified map and     * 2) the data translation for this mapping can be performed by the data     * transfer subsystem.     * The array will be sorted according to a     * <code>DataFlavorComparator</code> created with the specified     * map as an argument.     *     * @param format the data format     * @param map the FlavorTable which contains mappings between     *            DataFlavors and data formats     * @throws NullPointerException if map is <code>null</code>     */    public DataFlavor[] getFlavorsForFormatAsArray(long format,                                                   FlavorTable map) {        return getFlavorsForFormatsAsArray(new long[] { format }, map);    }     /**     * Returns an array of all DataFlavors for which     * 1) a mapping from at least one of the specified formats exists in the     * specified map and     * 2) the data translation for this mapping can be performed by the data     * transfer subsystem.     * The array will be sorted according to a     * <code>DataFlavorComparator</code> created with the specified     * map as an argument.     *     * @param formats the data formats     * @param map the FlavorTable which contains mappings between     *            DataFlavors and data formats     * @throws NullPointerException if formats or map is <code>null</code>     */    public DataFlavor[] getFlavorsForFormatsAsArray(long[] formats,                                                    FlavorTable map) {        // getFlavorsForFormatsAsSet() is less expensive than        // getFlavorsForFormats().        return setToSortedDataFlavorArray(getFlavorsForFormatsAsSet(formats, map));    }     /**     * Returns an object that represents a mapping between the specified     * key and value. <tt>null</tt> values and the <tt>null</tt> keys are     * permitted. The internal representation of the mapping object is     * irrelevant. The only requrement is that the two mapping objects are equal     * if and only if their keys are equal and their values are equal.     * More formally, the two mapping objects are equal if and only if     * <tt>(value1 == null ? value2 == null : value1.equals(value2))     * && (key1 == null ? key2 == null : key1.equals(key2))</tt>.     */    private static Object createMapping(Object key, Object value) {        // NOTE: Should be updated to use AbstractMap.SimpleEntry as        // soon as it is made public.        return Arrays.asList(new Object[] { key, value });    }     /**     * Looks-up or registers the String native with the native data transfer     * system and returns a long format corresponding to that native.     */    protected abstract Long getFormatForNativeAsLong(String str);     /**     * Looks-up the String native corresponding to the specified long format in     * the native data transfer system.     */    protected abstract String getNativeForFormat(long format);     /* Contains common code for finding the best charset for     * clipboard string encoding/decoding, basing on clipboard     * format and localeTransferable(on decoding, if available)     */    private String getBestCharsetForTextFormat(Long lFormat,        Transferable localeTransferable) throws IOException    {        String charset = null;        if (localeTransferable != null &&            isLocaleDependentTextFormat(lFormat) &&            localeTransferable.isDataFlavorSupported(javaTextEncodingFlavor))        {            try {                charset = new String(                    (byte[])localeTransferable.getTransferData(javaTextEncodingFlavor),                    "UTF-8"                );            } catch (UnsupportedFlavorException cannotHappen) {            }        } else {            charset = getCharsetForTextFormat(lFormat);        }        if (charset == null) {            // Only happens when we have a custom text type.            charset = getDefaultTextCharset();        }        return charset;    } 

Lines 501–1000 of 3090. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t