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 
  31 import java.io.*;
  32 import java.net.URI;
  33 import java.net.URISyntaxException;
  34 import java.net.URL;
  35 import java.nio.charset.Charset;
  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     };
  61 
  62     static {
  63         Map<String, Long> nameMap = new HashMap<>(predefinedClipboardNames.length, 1.0f);
  64         Map<Long, String> formatMap = new HashMap<>(predefinedClipboardNames.length, 1.0f);
  65         for (int i = 1; i < predefinedClipboardNames.length; i++) {
  66             nameMap.put(predefinedClipboardNames[i], (long) i);
  67             formatMap.put((long) i, predefinedClipboardNames[i]);
  68         }
  69         predefinedClipboardNameMap = Collections.synchronizedMap(nameMap);
  70         predefinedClipboardFormatMap = Collections.synchronizedMap(formatMap);
  71     }
  72 
  73     public static final int CF_UNSUPPORTED = 0;
  74     public static final int CF_STRING      = 1;
  75     public static final int CF_FILE        = 2;
  76     public static final int CF_TIFF        = 3;
  77     public static final int CF_RICH_TEXT   = 4;
  78     public static final int CF_HTML        = 5;
  79     public static final int CF_PDF         = 6;
  80     public static final int CF_URL         = 7;
  81     public static final int CF_PNG         = 8;
  82     public static final int CF_JPEG        = 9;
  83 
  84     private CDataTransferer() {}
  85 
  86     private static CDataTransferer fTransferer;
  87 
  88     static synchronized CDataTransferer getInstanceImpl() {
  89         if (fTransferer == null) {
  90             fTransferer = new CDataTransferer();
  91         }
  92 
  93         return fTransferer;
  94     }
  95 
  96     @Override
  97     public String getDefaultUnicodeEncoding() {
  98         return "utf-16le";
  99     }
 100 
 101     @Override
 102     public boolean isLocaleDependentTextFormat(long format) {
 103         return format == CF_STRING;
 104     }
 105 
 106     @Override
 107     public boolean isFileFormat(long format) {
 108         return format == CF_FILE;
 109     }
 110 
 111     @Override
 112     public boolean isImageFormat(long format) {
 113         int ifmt = (int)format;
 114         switch(ifmt) {
 115             case CF_TIFF:
 116             case CF_PDF:
 117             case CF_PNG:
 118             case CF_JPEG:
 119                 return true;
 120             default:
 121                 return false;
 122         }
 123     }
 124 
 125     @Override
 126     public Object translateBytes(byte[] bytes, DataFlavor flavor,
 127                                  long format, Transferable transferable) throws IOException {
 128 
 129         if (format == CF_URL && URL.class.equals(flavor.getRepresentationClass())) {
 130            String[] strings = dragQueryFile(bytes);
 131             if(strings.length == 0) {
 132                 return null;
 133             }
 134             return new URL(strings[0]);
 135         } else if(isUriListFlavor(flavor)) {
 136             // dragQueryFile works fine with files and url,
 137             // it parses and extracts values from property list.
 138             // maxosx always returens property list for
 139             // CF_URL and CF_FILE
 140             String[] strings = dragQueryFile(bytes);
 141             bytes = String.join(System.getProperty("line.separator"),
 142                     strings).getBytes();
 143             // now we extracted uri from xml, now we should treat it as
 144             // regular string that allows to translate data to target represantation
 145             // class by base method
 146             format = CF_STRING;
 147         } else if (format == CF_STRING) {
 148             bytes = Normalizer.normalize(new String(bytes, "UTF8"), Form.NFC).getBytes("UTF8");
 149         }
 150 
 151         return super.translateBytes(bytes, flavor, format, transferable);
 152     }
 153 
 154     @Override
 155     synchronized protected Long getFormatForNativeAsLong(String str) {
 156         Long format = predefinedClipboardNameMap.get(str);
 157 
 158         if (format == null) {
 159             if (java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance()) {
 160                 // Do not try to access native system for the unknown format
 161                 return -1L;
 162             }
 163             format = registerFormatWithPasteboard(str);
 164             predefinedClipboardNameMap.put(str, format);
 165             predefinedClipboardFormatMap.put(format, str);
 166         }
 167 
 168         return format;
 169     }
 170 
 171     /*
 172      * Adds type to native mapping NSDictionary.
 173      */
 174     private native long registerFormatWithPasteboard(String type);
 175 
 176     // Get registered native format string for an index, return null if unknown:
 177     private native String formatForIndex(long index);
 178 
 179     @Override
 180     protected String getNativeForFormat(long format) {
 181         String returnValue = null;
 182 
 183         // The most common case - just index the array of predefined names:
 184         if (format >= 0 && format < predefinedClipboardNames.length) {
 185             returnValue = predefinedClipboardNames[(int) format];
 186         } else {
 187             Long formatObj = format;
 188             returnValue = predefinedClipboardFormatMap.get(formatObj);
 189 
 190             // predefinedClipboardFormatMap may not know this format:
 191             if (returnValue == null) {
 192                 returnValue = formatForIndex(format);
 193 
 194                 // Native clipboard may not know this format either:
 195                 if (returnValue != null) {
 196                     predefinedClipboardNameMap.put(returnValue, formatObj);
 197                     predefinedClipboardFormatMap.put(formatObj, returnValue);
 198                 }
 199             }
 200         }
 201 
 202         if (returnValue == null) {
 203             returnValue = predefinedClipboardNames[CF_UNSUPPORTED];
 204         }
 205 
 206         return returnValue;
 207     }
 208 
 209     private final ToolkitThreadBlockedHandler handler = new CToolkitThreadBlockedHandler();
 210 
 211     @Override
 212     public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() {
 213         return handler;
 214     }
 215 
 216     @Override
 217     protected byte[] imageToPlatformBytes(Image image, long format) {
 218         return CImage.getCreator().getPlatformImageBytes(image);
 219     }
 220 
 221     private static native String[] nativeDragQueryFile(final byte[] bytes);
 222     @Override
 223     protected String[] dragQueryFile(final byte[] bytes) {
 224         if (bytes == null) return null;
 225         if (new String(bytes).startsWith("Unsupported type")) return null;
 226         return nativeDragQueryFile(bytes);
 227     }
 228 
 229 
 230     @Override
 231     protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException {
 232         return CImage.getCreator().createImageFromPlatformImageBytes(bytes);
 233     }
 234 
 235     @Override
 236     protected ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList) throws IOException {
 237         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 238         for (String file : fileList) {
 239             byte[] bytes = file.getBytes();
 240             bos.write(bytes, 0, bytes.length);
 241             bos.write(0);
 242         }
 243         return bos;
 244     }
 245 
 246     @Override
 247     protected boolean isURIListFormat(long format) {
 248         String nat = getNativeForFormat(format);
 249         if (nat == null) {
 250             return false;
 251         }
 252         try {
 253             DataFlavor df = new DataFlavor(nat);
 254             if (isUriListFlavor(df)) {
 255                 return true;
 256             }
 257         } catch (Exception e) {
 258             // Not a MIME format.
 259         }
 260         return false;
 261     }
 262 
 263     private boolean isUriListFlavor(DataFlavor df) {
 264         if (df.getPrimaryType().equals("text") && df.getSubType().equals("uri-list")) {
 265             return true;
 266         }
 267         return false;
 268     }
 269 }