1 2 /* 3 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package sun.lwawt.macosx; 28 29 import java.awt.*; 30 import java.awt.image.*; 31 32 import java.io.*; 33 import java.net.URI; 34 import java.net.URISyntaxException; 35 import java.net.URL; 36 import java.text.Normalizer; 37 import java.text.Normalizer.Form; 38 import java.util.*; 39 40 import java.awt.datatransfer.*; 41 import sun.awt.datatransfer.*; 42 43 public class CDataTransferer extends DataTransferer { 44 private static final Map<String, Long> predefinedClipboardNameMap; 45 private static final Map<Long, String> predefinedClipboardFormatMap; 46 47 // See SystemFlavorMap, or the flavormap.properties file: 48 // We should define a few more types in flavormap.properties, it's rather slim now. 49 private static final String[] predefinedClipboardNames = { 50 "", 51 "STRING", 52 "FILE_NAME", 53 "TIFF", 54 "RICH_TEXT", 55 "HTML", 56 "PDF", 57 "URL", 58 "PNG", 59 "JFIF", 60 "XPICT" 61 }; 62 63 static { 64 Map<String, Long> nameMap = new HashMap<>(predefinedClipboardNames.length, 1.0f); 65 Map<Long, String> formatMap = new HashMap<>(predefinedClipboardNames.length, 1.0f); 66 for (int i = 1; i < predefinedClipboardNames.length; i++) { 67 nameMap.put(predefinedClipboardNames[i], (long) i); 68 formatMap.put((long) i, predefinedClipboardNames[i]); 69 } 70 predefinedClipboardNameMap = Collections.synchronizedMap(nameMap); 71 predefinedClipboardFormatMap = Collections.synchronizedMap(formatMap); 72 } 73 74 public static final int CF_UNSUPPORTED = 0; 75 public static final int CF_STRING = 1; 76 public static final int CF_FILE = 2; 77 public static final int CF_TIFF = 3; 78 public static final int CF_RICH_TEXT = 4; 79 public static final int CF_HTML = 5; 80 public static final int CF_PDF = 6; 81 public static final int CF_URL = 7; 82 public static final int CF_PNG = 8; 83 public static final int CF_JPEG = 9; 84 public static final int CF_XPICT = 10; 85 86 private CDataTransferer() {} 87 88 private static CDataTransferer fTransferer; 89 90 static synchronized CDataTransferer getInstanceImpl() { 91 if (fTransferer == null) { 92 fTransferer = new CDataTransferer(); 93 } 94 95 return fTransferer; 96 } 97 98 @Override 99 public String getDefaultUnicodeEncoding() { 100 return "utf-16le"; 101 } 102 103 @Override 104 public boolean isLocaleDependentTextFormat(long format) { 105 return format == CF_STRING; 106 } 107 108 @Override 109 public boolean isFileFormat(long format) { 110 return format == CF_FILE; 111 } 112 113 @Override 114 public boolean isImageFormat(long format) { 115 int ifmt = (int)format; 116 switch(ifmt) { 117 case CF_TIFF: 118 case CF_PDF: 119 case CF_PNG: 120 case CF_JPEG: 121 return true; 122 default: 123 return false; 124 } 125 } 126 127 @Override 128 public Object translateBytes(byte[] bytes, DataFlavor flavor, 129 long format, Transferable transferable) throws IOException { 130 131 132 if (format == CF_URL && URL.class.equals(flavor.getRepresentationClass())) { 133 String[] strings = dragQueryFile(bytes); 134 if(strings == null || strings.length == 0) { 135 return null; 136 } 137 return new URL(strings[0]); 138 } else if(isUriListFlavor(flavor)) { 139 // dragQueryFile works fine with files and url, 140 // it parses and extracts values from property list. 141 // maxosx always returns property list for 142 // CF_URL and CF_FILE 143 String[] strings = dragQueryFile(bytes); 144 if(strings == null) { 145 return null; 146 } 147 String separator = System.getProperty("line.separator"); 148 StringBuilder sb = new StringBuilder(); 149 if(strings.length > 0) { 150 sb.append(strings[0]); 151 for(int i = 1; i < strings.length; i++) { 152 sb.append(strings[i]); 153 sb.append(separator); 154 } 155 } 156 bytes = sb.toString().getBytes(); 157 // now we extracted uri from xml, now we should treat it as 158 // regular string that allows to translate data to target represantation 159 // class by base method 160 format = CF_STRING; 161 } else if (format == CF_STRING) { 162 bytes = Normalizer.normalize(new String(bytes, "UTF8"), Form.NFC).getBytes("UTF8"); 163 } 164 165 return super.translateBytes(bytes, flavor, format, transferable); 166 } 167 168 @Override 169 synchronized protected Long getFormatForNativeAsLong(String str) { 170 Long format = predefinedClipboardNameMap.get(str); 171 172 if (format == null) { 173 if (java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance()) { 174 // Do not try to access native system for the unknown format 175 return -1L; 176 } 177 format = registerFormatWithPasteboard(str); 178 predefinedClipboardNameMap.put(str, format); 179 predefinedClipboardFormatMap.put(format, str); 180 } 181 182 return format; 183 } 184 185 /* 186 * Adds type to native mapping NSDictionary. 187 */ 188 private native long registerFormatWithPasteboard(String type); 189 190 // Get registered native format string for an index, return null if unknown: 191 private native String formatForIndex(long index); 192 193 @Override 194 protected String getNativeForFormat(long format) { 195 String returnValue = null; 196 197 // The most common case - just index the array of predefined names: 198 if (format >= 0 && format < predefinedClipboardNames.length) { 199 returnValue = predefinedClipboardNames[(int) format]; 200 } else { 201 Long formatObj = format; 202 returnValue = predefinedClipboardFormatMap.get(formatObj); 203 204 // predefinedClipboardFormatMap may not know this format: 205 if (returnValue == null) { 206 returnValue = formatForIndex(format); 207 208 // Native clipboard may not know this format either: 209 if (returnValue != null) { 210 predefinedClipboardNameMap.put(returnValue, formatObj); 211 predefinedClipboardFormatMap.put(formatObj, returnValue); 212 } 213 } 214 } 215 216 if (returnValue == null) { 217 returnValue = predefinedClipboardNames[CF_UNSUPPORTED]; 218 } 219 220 return returnValue; 221 } 222 223 private final ToolkitThreadBlockedHandler handler = new CToolkitThreadBlockedHandler(); 224 225 @Override 226 public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() { 227 return handler; 228 } 229 230 @Override 231 protected byte[] imageToPlatformBytes(Image image, long format) { 232 return CImage.getCreator().getPlatformImageBytes(image); 233 } 234 235 private static native String[] nativeDragQueryFile(final byte[] bytes); 236 @Override 237 protected String[] dragQueryFile(final byte[] bytes) { 238 if (bytes == null) return null; 239 if (new String(bytes).startsWith("Unsupported type")) return null; 240 return nativeDragQueryFile(bytes); 241 } 242 243 244 @Override 245 protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException { 246 return CImage.getCreator().createImageFromPlatformImageBytes(bytes); 247 } 248 249 @Override 250 protected ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList) throws IOException { 251 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 252 for (String file : fileList) { 253 byte[] bytes = file.getBytes(); 254 bos.write(bytes, 0, bytes.length); 255 bos.write(0); 256 } 257 return bos; 258 } 259 260 @Override 261 protected boolean isURIListFormat(long format) { 262 String nat = getNativeForFormat(format); 263 if (nat == null) { 264 return false; 265 } 266 try { 267 DataFlavor df = new DataFlavor(nat); 268 if (isUriListFlavor(df)) { 269 return true; 270 } 271 } catch (Exception e) { 272 // Not a MIME format. 273 } 274 return false; 275 } 276 277 private boolean isUriListFlavor(DataFlavor df) { 278 if (df.getPrimaryType().equals("text") && df.getSubType().equals("uri-list")) { 279 return true; 280 } 281 return false; 282 } 283 } 284 285