Java Code Examples for java.awt.datatransfer.DataFlavor#getRepresentationClass()

The following examples show how to use java.awt.datatransfer.DataFlavor#getRepresentationClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DnDUtils.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
@Nullable
public static String extractDropLink(@Nonnull final DropTargetDropEvent dtde) throws Exception {
  String foundHtmlLink = null;
  String foundMozLink = null;
  for (final DataFlavor df : dtde.getCurrentDataFlavors()) {
    if (df.getRepresentationClass() == String.class) {
      if (foundHtmlLink == null && df.isMimeTypeEqual(MIME_TEXT_HTML)) {
        final String link = extractHtmlLink(true, (String) dtde.getTransferable().getTransferData(df));
        if (link != null) {
          foundHtmlLink = link;
        }
      }
    } else if (df.getRepresentationClass() == InputStream.class && df.isMimeTypeEqual(MIME_MOZ_URL)) {
      if (foundMozLink == null) {
        final InputStream in = ((InputStream) dtde.getTransferable().getTransferData(df));
        final StringWriter string = new StringWriter();
        IOUtils.copy(in, string, Charset.defaultCharset());
        IOUtils.closeQuietly(in);
        foundMozLink = removeZeroChars(string.toString().split("\\n")[0]).trim();
      }
    }
  }
  return foundMozLink == null ? foundHtmlLink : foundMozLink;
}
 
Example 2
Source File: DataFlavorUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether this flavor is a text type which supports the 'charset'
 * parameter.
 */
public static boolean isFlavorCharsetTextType(DataFlavor flavor) {
    // Although stringFlavor doesn't actually support the charset
    // parameter (because its primary MIME type is not "text"), it should
    // be treated as though it does. stringFlavor is semantically
    // equivalent to "text/plain" data.
    if (DataFlavor.stringFlavor.equals(flavor)) {
        return true;
    }

    if (!"text".equals(flavor.getPrimaryType()) ||
            !doesSubtypeSupportCharset(flavor))
    {
        return false;
    }

    Class<?> rep_class = flavor.getRepresentationClass();

    if (flavor.isRepresentationClassReader() ||
            String.class.equals(rep_class) ||
            flavor.isRepresentationClassCharBuffer() ||
            char[].class.equals(rep_class))
    {
        return true;
    }

    if (!(flavor.isRepresentationClassInputStream() ||
            flavor.isRepresentationClassByteBuffer() ||
            byte[].class.equals(rep_class))) {
        return false;
    }

    String charset = flavor.getParameter("charset");

    // null equals default encoding which is always supported
    return (charset == null) || isEncodingSupported(charset);
}
 
Example 3
Source File: DataTransferer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether this flavor is a text type which supports the
 * 'charset' parameter.
 */
public static boolean isFlavorCharsetTextType(DataFlavor flavor) {
    // Although stringFlavor doesn't actually support the charset
    // parameter (because its primary MIME type is not "text"), it should
    // be treated as though it does. stringFlavor is semantically
    // equivalent to "text/plain" data.
    if (DataFlavor.stringFlavor.equals(flavor)) {
        return true;
    }

    if (!"text".equals(flavor.getPrimaryType()) ||
        !doesSubtypeSupportCharset(flavor))
    {
        return false;
    }

    Class rep_class = flavor.getRepresentationClass();

    if (flavor.isRepresentationClassReader() ||
        String.class.equals(rep_class) ||
        flavor.isRepresentationClassCharBuffer() ||
        char[].class.equals(rep_class))
    {
        return true;
    }

    if (!(flavor.isRepresentationClassInputStream() ||
          flavor.isRepresentationClassByteBuffer() ||
          byte[].class.equals(rep_class))) {
        return false;
    }

    String charset = flavor.getParameter("charset");

    return (charset != null)
        ? DataTransferer.isEncodingSupported(charset)
        : true; // null equals default encoding which is always supported
}
 
Example 4
Source File: DataTransferer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether this flavor is a text type which supports the
 * 'charset' parameter.
 */
public static boolean isFlavorCharsetTextType(DataFlavor flavor) {
    // Although stringFlavor doesn't actually support the charset
    // parameter (because its primary MIME type is not "text"), it should
    // be treated as though it does. stringFlavor is semantically
    // equivalent to "text/plain" data.
    if (DataFlavor.stringFlavor.equals(flavor)) {
        return true;
    }

    if (!"text".equals(flavor.getPrimaryType()) ||
        !doesSubtypeSupportCharset(flavor))
    {
        return false;
    }

    Class rep_class = flavor.getRepresentationClass();

    if (flavor.isRepresentationClassReader() ||
        String.class.equals(rep_class) ||
        flavor.isRepresentationClassCharBuffer() ||
        DataTransferer.charArrayClass.equals(rep_class))
    {
        return true;
    }

    if (!(flavor.isRepresentationClassInputStream() ||
          flavor.isRepresentationClassByteBuffer() ||
          DataTransferer.byteArrayClass.equals(rep_class))) {
        return false;
    }

    String charset = flavor.getParameter("charset");

    return (charset != null)
        ? DataTransferer.isEncodingSupported(charset)
        : true; // null equals default encoding which is always supported
}
 
Example 5
Source File: DataTransferer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether this flavor is a text type which supports the
 * 'charset' parameter.
 */
public static boolean isFlavorCharsetTextType(DataFlavor flavor) {
    // Although stringFlavor doesn't actually support the charset
    // parameter (because its primary MIME type is not "text"), it should
    // be treated as though it does. stringFlavor is semantically
    // equivalent to "text/plain" data.
    if (DataFlavor.stringFlavor.equals(flavor)) {
        return true;
    }

    if (!"text".equals(flavor.getPrimaryType()) ||
        !doesSubtypeSupportCharset(flavor))
    {
        return false;
    }

    Class rep_class = flavor.getRepresentationClass();

    if (flavor.isRepresentationClassReader() ||
        String.class.equals(rep_class) ||
        flavor.isRepresentationClassCharBuffer() ||
        char[].class.equals(rep_class))
    {
        return true;
    }

    if (!(flavor.isRepresentationClassInputStream() ||
          flavor.isRepresentationClassByteBuffer() ||
          byte[].class.equals(rep_class))) {
        return false;
    }

    String charset = flavor.getParameter("charset");

    return (charset != null)
        ? DataTransferer.isEncodingSupported(charset)
        : true; // null equals default encoding which is always supported
}
 
Example 6
Source File: DataTransferer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether this flavor is a text type which supports the
 * 'charset' parameter.
 */
public static boolean isFlavorCharsetTextType(DataFlavor flavor) {
    // Although stringFlavor doesn't actually support the charset
    // parameter (because its primary MIME type is not "text"), it should
    // be treated as though it does. stringFlavor is semantically
    // equivalent to "text/plain" data.
    if (DataFlavor.stringFlavor.equals(flavor)) {
        return true;
    }

    if (!"text".equals(flavor.getPrimaryType()) ||
        !doesSubtypeSupportCharset(flavor))
    {
        return false;
    }

    Class rep_class = flavor.getRepresentationClass();

    if (flavor.isRepresentationClassReader() ||
        String.class.equals(rep_class) ||
        flavor.isRepresentationClassCharBuffer() ||
        char[].class.equals(rep_class))
    {
        return true;
    }

    if (!(flavor.isRepresentationClassInputStream() ||
          flavor.isRepresentationClassByteBuffer() ||
          byte[].class.equals(rep_class))) {
        return false;
    }

    String charset = flavor.getParameter("charset");

    return (charset != null)
        ? DataTransferer.isEncodingSupported(charset)
        : true; // null equals default encoding which is always supported
}
 
Example 7
Source File: DataTransferer.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * We support representations which are exactly of the specified Class,
 * and also arbitrary Objects which have a constructor which takes an
 * instance of the Class as its sole parameter.
 */
private Object constructFlavoredObject(Object arg, DataFlavor flavor,
                                       Class clazz)
    throws IOException
{
    final Class dfrc = flavor.getRepresentationClass();

    if (clazz.equals(dfrc)) {
        return arg; // simple case
    } else {
        Constructor[] constructors = null;

        try {
            constructors = (Constructor[])
                AccessController.doPrivileged(new PrivilegedAction() {
                        public Object run() {
                            return dfrc.getConstructors();
                        }
                    });
        } catch (SecurityException se) {
            throw new IOException(se.getMessage());
        }

        Constructor constructor = null;

        for (int j = 0; j < constructors.length; j++) {
            if (!Modifier.isPublic(constructors[j].getModifiers())) {
                continue;
            }

            Class[] ptypes = constructors[j].getParameterTypes();

            if (ptypes != null && ptypes.length == 1 &&
                clazz.equals(ptypes[0])) {
                constructor = constructors[j];
                break;
            }
        }

        if (constructor == null) {
            throw new IOException("can't find <init>(L"+ clazz +
                                  ";)V for class: " + dfrc.getName());
        }

        try {
            return constructor.newInstance(new Object[] { arg } );
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }
}
 
Example 8
Source File: DataFlavorUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public int compare(DataFlavor flavor1, DataFlavor flavor2) {
    if (flavor1.equals(flavor2)) {
        return 0;
    }

    int comp;

    String primaryType1 = flavor1.getPrimaryType();
    String subType1 = flavor1.getSubType();
    String mimeType1 = primaryType1 + "/" + subType1;
    Class<?> class1 = flavor1.getRepresentationClass();

    String primaryType2 = flavor2.getPrimaryType();
    String subType2 = flavor2.getSubType();
    String mimeType2 = primaryType2 + "/" + subType2;
    Class<?> class2 = flavor2.getRepresentationClass();

    if (flavor1.isFlavorTextType() && flavor2.isFlavorTextType()) {
        // First, compare MIME types
        comp = compareIndices(textTypes, mimeType1, mimeType2, UNKNOWN_OBJECT_LOSES);
        if (comp != 0) {
            return comp;
        }

        // Only need to test one flavor because they both have the
        // same MIME type. Also don't need to worry about accidentally
        // passing stringFlavor because either
        //   1. Both flavors are stringFlavor, in which case the
        //      equality test at the top of the function succeeded.
        //   2. Only one flavor is stringFlavor, in which case the MIME
        //      type comparison returned a non-zero value.
        if (doesSubtypeSupportCharset(flavor1)) {
            // Next, prefer the decoded text representations of Reader,
            // String, CharBuffer, and [C, in that order.
            comp = compareIndices(decodedTextRepresentations, class1,
                    class2, UNKNOWN_OBJECT_LOSES);
            if (comp != 0) {
                return comp;
            }

            // Next, compare charsets
            comp = CharsetComparator.INSTANCE.compare(getTextCharset(flavor1),
                    getTextCharset(flavor2));
            if (comp != 0) {
                return comp;
            }
        }

        // Finally, prefer the encoded text representations of
        // InputStream, ByteBuffer, and [B, in that order.
        comp = compareIndices(encodedTextRepresentations, class1,
                class2, UNKNOWN_OBJECT_LOSES);
        if (comp != 0) {
            return comp;
        }
    } else {
        // First, prefer text types
        if (flavor1.isFlavorTextType()) {
            return 1;
        }

        if (flavor2.isFlavorTextType()) {
            return -1;
        }

        // Next, prefer application types.
        comp = compareIndices(primaryTypes, primaryType1, primaryType2,
                UNKNOWN_OBJECT_LOSES);
        if (comp != 0) {
            return comp;
        }

        // Next, look for application/x-java-* types. Prefer unknown
        // MIME types because if the user provides his own data flavor,
        // it will likely be the most descriptive one.
        comp = compareIndices(exactTypes, mimeType1, mimeType2,
                UNKNOWN_OBJECT_WINS);
        if (comp != 0) {
            return comp;
        }

        // Finally, prefer the representation classes of Remote,
        // Serializable, and InputStream, in that order.
        comp = compareIndices(nonTextRepresentations, class1, class2,
                UNKNOWN_OBJECT_LOSES);
        if (comp != 0) {
            return comp;
        }
    }

    // The flavours are not equal but still not distinguishable.
    // Compare String representations in alphabetical order
    return flavor1.getMimeType().compareTo(flavor2.getMimeType());
}
 
Example 9
Source File: XDataTransferer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 10
Source File: DataTransferer.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * We support representations which are exactly of the specified Class,
 * and also arbitrary Objects which have a constructor which takes an
 * instance of the Class as its sole parameter.
 */
private Object constructFlavoredObject(Object arg, DataFlavor flavor,
                                       Class clazz)
    throws IOException
{
    final Class dfrc = flavor.getRepresentationClass();

    if (clazz.equals(dfrc)) {
        return arg; // simple case
    } else {
        Constructor[] constructors = null;

        try {
            constructors = (Constructor[])
                AccessController.doPrivileged(new PrivilegedAction() {
                        public Object run() {
                            return dfrc.getConstructors();
                        }
                    });
        } catch (SecurityException se) {
            throw new IOException(se.getMessage());
        }

        Constructor constructor = null;

        for (int j = 0; j < constructors.length; j++) {
            if (!Modifier.isPublic(constructors[j].getModifiers())) {
                continue;
            }

            Class[] ptypes = constructors[j].getParameterTypes();

            if (ptypes != null && ptypes.length == 1 &&
                clazz.equals(ptypes[0])) {
                constructor = constructors[j];
                break;
            }
        }

        if (constructor == null) {
            throw new IOException("can't find <init>(L"+ clazz +
                                  ";)V for class: " + dfrc.getName());
        }

        try {
            return constructor.newInstance(new Object[] { arg } );
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }
}
 
Example 11
Source File: XDataTransferer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 12
Source File: DataTransferer.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * We support representations which are exactly of the specified Class,
 * and also arbitrary Objects which have a constructor which takes an
 * instance of the Class as its sole parameter.
 */
private Object constructFlavoredObject(Object arg, DataFlavor flavor,
                                       Class clazz)
    throws IOException
{
    final Class dfrc = flavor.getRepresentationClass();

    if (clazz.equals(dfrc)) {
        return arg; // simple case
    } else {
        Constructor[] constructors = null;

        try {
            constructors = (Constructor[])
                AccessController.doPrivileged(new PrivilegedAction() {
                        public Object run() {
                            return dfrc.getConstructors();
                        }
                    });
        } catch (SecurityException se) {
            throw new IOException(se.getMessage());
        }

        Constructor constructor = null;

        for (int j = 0; j < constructors.length; j++) {
            if (!Modifier.isPublic(constructors[j].getModifiers())) {
                continue;
            }

            Class[] ptypes = constructors[j].getParameterTypes();

            if (ptypes != null && ptypes.length == 1 &&
                clazz.equals(ptypes[0])) {
                constructor = constructors[j];
                break;
            }
        }

        if (constructor == null) {
            throw new IOException("can't find <init>(L"+ clazz +
                                  ";)V for class: " + dfrc.getName());
        }

        try {
            return constructor.newInstance(new Object[] { arg } );
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }
}
 
Example 13
Source File: XDataTransferer.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) {
    LinkedHashSet<String> natives = new LinkedHashSet<>(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byte[].class.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 14
Source File: DataTransferer.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * We support representations which are exactly of the specified Class,
 * and also arbitrary Objects which have a constructor which takes an
 * instance of the Class as its sole parameter.
 */
private Object constructFlavoredObject(Object arg, DataFlavor flavor,
                                       Class clazz)
    throws IOException
{
    final Class dfrc = flavor.getRepresentationClass();

    if (clazz.equals(dfrc)) {
        return arg; // simple case
    } else {
        Constructor[] constructors = null;

        try {
            constructors = (Constructor[])
                AccessController.doPrivileged(new PrivilegedAction() {
                        public Object run() {
                            return dfrc.getConstructors();
                        }
                    });
        } catch (SecurityException se) {
            throw new IOException(se.getMessage());
        }

        Constructor constructor = null;

        for (int j = 0; j < constructors.length; j++) {
            if (!Modifier.isPublic(constructors[j].getModifiers())) {
                continue;
            }

            Class[] ptypes = constructors[j].getParameterTypes();

            if (ptypes != null && ptypes.length == 1 &&
                clazz.equals(ptypes[0])) {
                constructor = constructors[j];
                break;
            }
        }

        if (constructor == null) {
            throw new IOException("can't find <init>(L"+ clazz +
                                  ";)V for class: " + dfrc.getName());
        }

        try {
            return constructor.newInstance(new Object[] { arg } );
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }
}
 
Example 15
Source File: XDataTransferer.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public List getPlatformMappingsForFlavor(DataFlavor df) {
    List natives = new ArrayList(1);

    if (df == null) {
        return natives;
    }

    String charset = df.getParameter("charset");
    String baseType = df.getPrimaryType() + "/" + df.getSubType();
    String mimeType = baseType;

    if (charset != null && DataTransferer.isFlavorCharsetTextType(df)) {
        mimeType += ";charset=" + charset;
    }

    // Add a mapping to the MIME native whenever the representation class
    // doesn't require translation.
    if (df.getRepresentationClass() != null &&
        (df.isRepresentationClassInputStream() ||
         df.isRepresentationClassByteBuffer() ||
         byteArrayClass.equals(df.getRepresentationClass()))) {
        natives.add(mimeType);
    }

    if (DataFlavor.imageFlavor.equals(df)) {
        String[] mimeTypes = ImageIO.getWriterMIMETypes();
        if (mimeTypes != null) {
            for (int i = 0; i < mimeTypes.length; i++) {
                Iterator writers =
                    ImageIO.getImageWritersByMIMEType(mimeTypes[i]);

                while (writers.hasNext()) {
                    ImageWriter imageWriter = (ImageWriter)writers.next();
                    ImageWriterSpi writerSpi =
                        imageWriter.getOriginatingProvider();

                    if (writerSpi != null &&
                        writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
                        natives.add(mimeTypes[i]);
                        break;
                    }
                }
            }
        }
    } else if (DataTransferer.isFlavorCharsetTextType(df)) {
        // stringFlavor is semantically equivalent to the standard
        // "text/plain" MIME type.
        if (DataFlavor.stringFlavor.equals(df)) {
            baseType = "text/plain";
        }

        for (String encoding : DataTransferer.standardEncodings()) {
            if (!encoding.equals(charset)) {
                natives.add(baseType + ";charset=" + encoding);
            }
        }

        // Add a MIME format without specified charset.
        if (!natives.contains(baseType)) {
            natives.add(baseType);
        }
    }

    return natives;
}
 
Example 16
Source File: ClassSymbolNode.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private boolean isProgramSelection(DataFlavor flavor) {
	Class<?> clazz = flavor.getRepresentationClass();
	return SelectionTransferData.class.equals(clazz);
}
 
Example 17
Source File: DataTransferer.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * We support representations which are exactly of the specified Class,
 * and also arbitrary Objects which have a constructor which takes an
 * instance of the Class as its sole parameter.
 */
private Object constructFlavoredObject(Object arg, DataFlavor flavor,
                                       Class clazz)
    throws IOException
{
    final Class dfrc = flavor.getRepresentationClass();

    if (clazz.equals(dfrc)) {
        return arg; // simple case
    } else {
        Constructor[] constructors = null;

        try {
            constructors = (Constructor[])
                AccessController.doPrivileged(new PrivilegedAction() {
                        public Object run() {
                            return dfrc.getConstructors();
                        }
                    });
        } catch (SecurityException se) {
            throw new IOException(se.getMessage());
        }

        Constructor constructor = null;

        for (int j = 0; j < constructors.length; j++) {
            if (!Modifier.isPublic(constructors[j].getModifiers())) {
                continue;
            }

            Class[] ptypes = constructors[j].getParameterTypes();

            if (ptypes != null && ptypes.length == 1 &&
                clazz.equals(ptypes[0])) {
                constructor = constructors[j];
                break;
            }
        }

        if (constructor == null) {
            throw new IOException("can't find <init>(L"+ clazz +
                                  ";)V for class: " + dfrc.getName());
        }

        try {
            return constructor.newInstance(new Object[] { arg } );
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }
}
 
Example 18
Source File: ActivationDataFlavor.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compares the DataFlavor passed in with this DataFlavor; calls
 * the <code>isMimeTypeEqual</code> method.
 *
 * @param dataFlavor        the DataFlavor to compare with
 * @return                  true if the MIME type and representation class
 *                          are the same
 */
public boolean equals(DataFlavor dataFlavor) {
    return (isMimeTypeEqual(dataFlavor) &&
            dataFlavor.getRepresentationClass() == representationClass);
}
 
Example 19
Source File: ActivationDataFlavor.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compares the DataFlavor passed in with this DataFlavor; calls
 * the <code>isMimeTypeEqual</code> method.
 *
 * @param dataFlavor        the DataFlavor to compare with
 * @return                  true if the MIME type and representation class
 *                          are the same
 */
public boolean equals(DataFlavor dataFlavor) {
    return (isMimeTypeEqual(dataFlavor) &&
            dataFlavor.getRepresentationClass() == representationClass);
}
 
Example 20
Source File: ActivationDataFlavor.java    From hottub with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Compares the DataFlavor passed in with this DataFlavor; calls
 * the <code>isMimeTypeEqual</code> method.
 *
 * @param dataFlavor        the DataFlavor to compare with
 * @return                  true if the MIME type and representation class
 *                          are the same
 */
public boolean equals(DataFlavor dataFlavor) {
    return (isMimeTypeEqual(dataFlavor) &&
            dataFlavor.getRepresentationClass() == representationClass);
}