下面列出了java.awt.datatransfer.DataFlavor#isMimeTypeEqual ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@Override
protected void setContentsNative(Transferable contents) {
// Don't use delayed Clipboard rendering for the Transferable's data.
// If we did that, we would call Transferable.getTransferData on
// the Toolkit thread, which is a security hole.
//
// Get all of the target formats into which the Transferable can be
// translated. Then, for each format, translate the data and post
// it to the Clipboard.
Map <Long, DataFlavor> formatMap = WDataTransferer.getInstance().
getFormatsForTransferable(contents, getDefaultFlavorTable());
openClipboard(this);
try {
for (Long format : formatMap.keySet()) {
DataFlavor flavor = formatMap.get(format);
try {
byte[] bytes = WDataTransferer.getInstance().
translateTransferable(contents, flavor, format);
publishClipboardData(format, bytes);
} catch (IOException e) {
// Fix 4696186: don't print exception if data with
// javaJVMLocalObjectMimeType failed to serialize.
// May remove this if-check when 5078787 is fixed.
if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) &&
e instanceof java.io.NotSerializableException)) {
e.printStackTrace();
}
}
}
} finally {
closeClipboard();
}
}
@Override
protected void setContentsNative(Transferable contents) {
// Don't use delayed Clipboard rendering for the Transferable's data.
// If we did that, we would call Transferable.getTransferData on
// the Toolkit thread, which is a security hole.
//
// Get all of the target formats into which the Transferable can be
// translated. Then, for each format, translate the data and post
// it to the Clipboard.
Map <Long, DataFlavor> formatMap = WDataTransferer.getInstance().
getFormatsForTransferable(contents, getDefaultFlavorTable());
openClipboard(this);
try {
for (Long format : formatMap.keySet()) {
DataFlavor flavor = formatMap.get(format);
try {
byte[] bytes = WDataTransferer.getInstance().
translateTransferable(contents, flavor, format);
publishClipboardData(format, bytes);
} catch (IOException e) {
// Fix 4696186: don't print exception if data with
// javaJVMLocalObjectMimeType failed to serialize.
// May remove this if-check when 5078787 is fixed.
if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) &&
e instanceof java.io.NotSerializableException)) {
e.printStackTrace();
}
}
}
} finally {
closeClipboard();
}
}
@Override
protected void setContentsNative(Transferable contents) {
// Don't use delayed Clipboard rendering for the Transferable's data.
// If we did that, we would call Transferable.getTransferData on
// the Toolkit thread, which is a security hole.
//
// Get all of the target formats into which the Transferable can be
// translated. Then, for each format, translate the data and post
// it to the Clipboard.
Map <Long, DataFlavor> formatMap = WDataTransferer.getInstance().
getFormatsForTransferable(contents, getDefaultFlavorTable());
openClipboard(this);
try {
for (Long format : formatMap.keySet()) {
DataFlavor flavor = formatMap.get(format);
try {
byte[] bytes = WDataTransferer.getInstance().
translateTransferable(contents, flavor, format);
publishClipboardData(format, bytes);
} catch (IOException e) {
// Fix 4696186: don't print exception if data with
// javaJVMLocalObjectMimeType failed to serialize.
// May remove this if-check when 5078787 is fixed.
if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) &&
e instanceof java.io.NotSerializableException)) {
e.printStackTrace();
}
}
}
} finally {
closeClipboard();
}
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<String>) () -> {
StringBuilder allowedFiles = new StringBuilder(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
return AccessController.doPrivileged((PrivilegedExceptionAction<String>) () -> {
StringBuilder allowedFiles = new StringBuilder(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
}
@Override
protected void setContentsNative(Transferable contents) {
// Don't use delayed Clipboard rendering for the Transferable's data.
// If we did that, we would call Transferable.getTransferData on
// the Toolkit thread, which is a security hole.
//
// Get all of the target formats into which the Transferable can be
// translated. Then, for each format, translate the data and post
// it to the Clipboard.
Map <Long, DataFlavor> formatMap = WDataTransferer.getInstance().
getFormatsForTransferable(contents, getDefaultFlavorTable());
openClipboard(this);
try {
for (Long format : formatMap.keySet()) {
DataFlavor flavor = formatMap.get(format);
try {
byte[] bytes = WDataTransferer.getInstance().
translateTransferable(contents, flavor, format);
publishClipboardData(format, bytes);
} catch (IOException e) {
// Fix 4696186: don't print exception if data with
// javaJVMLocalObjectMimeType failed to serialize.
// May remove this if-check when 5078787 is fixed.
if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) &&
e instanceof java.io.NotSerializableException)) {
e.printStackTrace();
}
}
}
} finally {
closeClipboard();
}
}
@Override
protected void setContentsNative(Transferable contents) {
// Don't use delayed Clipboard rendering for the Transferable's data.
// If we did that, we would call Transferable.getTransferData on
// the Toolkit thread, which is a security hole.
//
// Get all of the target formats into which the Transferable can be
// translated. Then, for each format, translate the data and post
// it to the Clipboard.
Map <Long, DataFlavor> formatMap = WDataTransferer.getInstance().
getFormatsForTransferable(contents, getDefaultFlavorTable());
openClipboard(this);
try {
for (Long format : formatMap.keySet()) {
DataFlavor flavor = formatMap.get(format);
try {
byte[] bytes = WDataTransferer.getInstance().
translateTransferable(contents, flavor, format);
publishClipboardData(format, bytes);
} catch (IOException e) {
// Fix 4696186: don't print exception if data with
// javaJVMLocalObjectMimeType failed to serialize.
// May remove this if-check when 5078787 is fixed.
if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) &&
e instanceof java.io.NotSerializableException)) {
e.printStackTrace();
}
}
}
} finally {
closeClipboard();
}
}
public static boolean isFileOrLinkOrText(@Nonnull final DropTargetDragEvent dtde) {
boolean result = false;
for (final DataFlavor flavor : dtde.getCurrentDataFlavors()) {
if (flavor.isFlavorJavaFileListType() || flavor.isFlavorTextType() || flavor.isMimeTypeEqual(MIME_MOZ_URL) || flavor.isMimeTypeEqual(MIME_TEXT_PLAIN) || flavor.isMimeTypeEqual(MIME_TEXT_HTML)) {
result = true;
break;
}
}
return result;
}
@Override
public boolean canImport(TransferSupport support) {
boolean supportedFlavor = false;
for (DataFlavor flavor : support.getDataFlavors()) {
if (flavor.isMimeTypeEqual(DataTransferable.FLAVORS[0])) {
supportedFlavor = true;
break;
}
}
return supportedFlavor;
}
protected void setContentsNative(Transferable contents) {
// Don't use delayed Clipboard rendering for the Transferable's data.
// If we did that, we would call Transferable.getTransferData on
// the Toolkit thread, which is a security hole.
//
// Get all of the target formats into which the Transferable can be
// translated. Then, for each format, translate the data and post
// it to the Clipboard.
Map <Long, DataFlavor> formatMap = WDataTransferer.getInstance().
getFormatsForTransferable(contents, flavorMap);
openClipboard(this);
try {
for (Long format : formatMap.keySet()) {
DataFlavor flavor = formatMap.get(format);
try {
byte[] bytes = WDataTransferer.getInstance().
translateTransferable(contents, flavor, format);
publishClipboardData(format, bytes);
} catch (IOException e) {
// Fix 4696186: don't print exception if data with
// javaJVMLocalObjectMimeType failed to serialize.
// May remove this if-check when 5078787 is fixed.
if (!(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType) &&
e instanceof java.io.NotSerializableException)) {
e.printStackTrace();
}
}
}
} finally {
closeClipboard();
}
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
String ret_val = "";
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
StringBuffer allowedFiles = new StringBuffer(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
}
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
return ret_val;
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
String ret_val = "";
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
StringBuffer allowedFiles = new StringBuffer(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
}
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
return ret_val;
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
String ret_val = "";
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
StringBuffer allowedFiles = new StringBuffer(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
}
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
return ret_val;
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
String ret_val = "";
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
StringBuffer allowedFiles = new StringBuffer(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
}
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
return ret_val;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType);
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
String ret_val = "";
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
StringBuffer allowedFiles = new StringBuffer(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
}
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
return ret_val;
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
String ret_val = "";
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
StringBuffer allowedFiles = new StringBuffer(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
}
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
return ret_val;
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
String ret_val = "";
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
StringBuffer allowedFiles = new StringBuffer(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
}
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
return ret_val;
}
private String removeSuspectedData(DataFlavor flavor, final Transferable contents, final String str)
throws IOException
{
if (null == System.getSecurityManager()
|| !flavor.isMimeTypeEqual("text/uri-list"))
{
return str;
}
String ret_val = "";
final ProtectionDomain userProtectionDomain = getUserProtectionDomain(contents);
try {
ret_val = (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
StringBuffer allowedFiles = new StringBuffer(str.length());
String [] uriArray = str.split("(\\s)+");
for (String fileName : uriArray)
{
File file = new File(fileName);
if (file.exists() &&
!(isFileInWebstartedCache(file) ||
isForbiddenToRead(file, userProtectionDomain)))
{
if (0 != allowedFiles.length())
{
allowedFiles.append("\\r\\n");
}
allowedFiles.append(fileName);
}
}
return allowedFiles.toString();
}
});
} catch (PrivilegedActionException pae) {
throw new IOException(pae.getMessage(), pae);
}
return ret_val;
}