package sun.lwawt.macosx;
import java.awt.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.Normalizer;
import java.text.Normalizer.Form;
import java.util.*;
import java.util.regex.*;
import java.awt.datatransfer.*;
import java.nio.charset.StandardCharsets;
import sun.awt.datatransfer.*;
public class CDataTransferer extends DataTransferer {
private static final Map<String, Long> predefinedClipboardNameMap;
private static final Map<Long, String> predefinedClipboardFormatMap;
private static final String[] predefinedClipboardNames = {
"",
"STRING",
"FILE_NAME",
"TIFF",
"RICH_TEXT",
"HTML",
"PDF",
"URL",
"PNG",
"JFIF",
"XPICT"
};
static {
Map<String, Long> nameMap = new HashMap<>(predefinedClipboardNames.length, 1.0f);
Map<Long, String> formatMap = new HashMap<>(predefinedClipboardNames.length, 1.0f);
for (int i = 1; i < predefinedClipboardNames.length; i++) {
nameMap.put(predefinedClipboardNames[i], (long) i);
formatMap.put((long) i, predefinedClipboardNames[i]);
}
predefinedClipboardNameMap = Collections.synchronizedMap(nameMap);
predefinedClipboardFormatMap = Collections.synchronizedMap(formatMap);
}
public static final int CF_UNSUPPORTED = 0;
public static final int CF_STRING = 1;
public static final int CF_FILE = 2;
public static final int CF_TIFF = 3;
public static final int CF_RICH_TEXT = 4;
public static final int CF_HTML = 5;
public static final int CF_PDF = 6;
public static final int CF_URL = 7;
public static final int CF_PNG = 8;
public static final int CF_JPEG = 9;
public static final int CF_XPICT = 10;
private CDataTransferer() {}
private static CDataTransferer fTransferer;
static synchronized CDataTransferer getInstanceImpl() {
if (fTransferer == null) {
fTransferer = new CDataTransferer();
}
return fTransferer;
}
@Override
public String getDefaultUnicodeEncoding() {
return "utf-16le";
}
@Override
public boolean isLocaleDependentTextFormat(long format) {
return format == CF_STRING;
}
@Override
public boolean isFileFormat(long format) {
return format == CF_FILE;
}
@Override
public boolean isImageFormat(long format) {
int ifmt = (int)format;
switch(ifmt) {
case CF_TIFF:
case CF_PDF:
case CF_PNG:
case CF_JPEG:
return true;
default:
return false;
}
}
@Override
public Object translateBytes(byte[] bytes, DataFlavor flavor,
long format, Transferable transferable) throws IOException {
if (format == CF_URL && URL.class.equals(flavor.getRepresentationClass())) {
String charset = Charset.defaultCharset().name();
if (transferable != null && transferable.isDataFlavorSupported(javaTextEncodingFlavor)) {
try {
charset = new String((byte[]) transferable.getTransferData(javaTextEncodingFlavor), StandardCharsets.UTF_8);
} catch (UnsupportedFlavorException cannotHappen) {
}
}
String xml = new String(bytes, charset);
return new URL(extractURL(xml));
}
if(isUriListFlavor(flavor) && format == CF_FILE) {
String[] strings = dragQueryFile(bytes);
if(strings == null) {
return null;
}
bytes = String.join(System.getProperty("line.separator"),
strings).getBytes();
format = CF_STRING;
} else if (format == CF_STRING) {
bytes = Normalizer.normalize(new String(bytes, "UTF8"), Form.NFC).getBytes("UTF8");
}
return super.translateBytes(bytes, flavor, format, transferable);
}
private String (String xml) {
Pattern urlExtractorPattern = Pattern.compile("<string>(.*)</string>");
Matcher matcher = urlExtractorPattern.matcher(xml);
if (matcher.find()) {
return matcher.group(1);
} else {
return null;
}
}
@Override
protected synchronized Long getFormatForNativeAsLong(String str) {
Long format = predefinedClipboardNameMap.get(str);
if (format == null) {
if (java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance()) {
return -1L;
}
format = registerFormatWithPasteboard(str);
predefinedClipboardNameMap.put(str, format);
predefinedClipboardFormatMap.put(format, str);
}
return format;
}
private native long registerFormatWithPasteboard(String type);
private native String formatForIndex(long index);
@Override
protected String getNativeForFormat(long format) {
String returnValue = null;
if (format >= 0 && format < predefinedClipboardNames.length) {
returnValue = predefinedClipboardNames[(int) format];
} else {
Long formatObj = format;
returnValue = predefinedClipboardFormatMap.get(formatObj);
if (returnValue == null) {
returnValue = formatForIndex(format);
if (returnValue != null) {
predefinedClipboardNameMap.put(returnValue, formatObj);
predefinedClipboardFormatMap.put(formatObj, returnValue);
}
}
}
if (returnValue == null) {
returnValue = predefinedClipboardNames[CF_UNSUPPORTED];
}
return returnValue;
}
private final ToolkitThreadBlockedHandler handler = new CToolkitThreadBlockedHandler();
@Override
public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() {
return handler;
}
@Override
protected byte[] imageToPlatformBytes(Image image, long format) {
return CImage.getCreator().getPlatformImageBytes(image);
}
private static native String[] nativeDragQueryFile(final byte[] bytes);
@Override
protected String[] dragQueryFile(final byte[] bytes) {
if (bytes == null) return null;
if (new String(bytes).startsWith("Unsupported type")) return null;
return nativeDragQueryFile(bytes);
}
@Override
protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException {
return CImage.getCreator().createImageFromPlatformImageBytes(bytes);
}
@Override
protected ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (String file : fileList) {
byte[] bytes = file.getBytes();
bos.write(bytes, 0, bytes.length);
bos.write(0);
}
return bos;
}
@Override
protected boolean isURIListFormat(long format) {
String nat = getNativeForFormat(format);
if (nat == null) {
return false;
}
try {
DataFlavor df = new DataFlavor(nat);
if (isUriListFlavor(df)) {
return true;
}
} catch (Exception e) {
}
return false;
}
private boolean isUriListFlavor(DataFlavor df) {
if (df.getPrimaryType().equals("text") && df.getSubType().equals("uri-list")) {
return true;
}
return false;
}
}