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         "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         if (format == CF_URL && URL.class.equals(flavor.getRepresentationClass())) {
 132             String[] strings = dragQueryFile(bytes);
 133             if(strings == null || strings.length == 0) {
 134                 return null;
 135             }
 136             return new URL(strings[0]);
 137         } else if(isUriListFlavor(flavor)) {
 138             // dragQueryFile works fine with files and url,
 139             // it parses and extracts values from property list.
 140             // maxosx always returns property list for
 141             // CF_URL and CF_FILE
 142             String[] strings = dragQueryFile(bytes);
 143             if(strings == null) {
 144                 return null;
 145             }
 146             bytes = String.join(System.getProperty("line.separator"),
 147                     strings).getBytes();
 148             // now we extracted uri from xml, now we should treat it as
 149             // regular string that allows to translate data to target represantation
 150             // class by base method
 151             format = CF_STRING;
 152         } else if (format == CF_STRING) {
 153             bytes = Normalizer.normalize(new String(bytes, "UTF8"), Form.NFC).getBytes("UTF8");
 154         }
 155 
 156         return super.translateBytes(bytes, flavor, format, transferable);
 157     }
 158 
 159     @Override
 160     protected synchronized Long getFormatForNativeAsLong(String str) {
 161         Long format = predefinedClipboardNameMap.get(str);
 162 
 163         if (format == null) {
 164             if (java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance()) {
 165                 // Do not try to access native system for the unknown format
 166                 return -1L;
 167             }
 168             format = registerFormatWithPasteboard(str);
 169             predefinedClipboardNameMap.put(str, format);
 170             predefinedClipboardFormatMap.put(format, str);
 171         }
 172 
 173         return format;
 174     }
 175 
 176     /*
 177      * Adds type to native mapping NSDictionary.
 178      */
 179     private native long registerFormatWithPasteboard(String type);
 180 
 181     // Get registered native format string for an index, return null if unknown:
 182     private native String formatForIndex(long index);
 183 
 184     @Override
 185     protected String getNativeForFormat(long format) {
 186         String returnValue = null;
 187 
 188         // The most common case - just index the array of predefined names:
 189         if (format >= 0 && format < predefinedClipboardNames.length) {
 190             returnValue = predefinedClipboardNames[(int) format];
 191         } else {
 192             Long formatObj = format;
 193             returnValue = predefinedClipboardFormatMap.get(formatObj);
 194 
 195             // predefinedClipboardFormatMap may not know this format:
 196             if (returnValue == null) {
 197                 returnValue = formatForIndex(format);
 198 
 199                 // Native clipboard may not know this format either:
 200                 if (returnValue != null) {
 201                     predefinedClipboardNameMap.put(returnValue, formatObj);
 202                     predefinedClipboardFormatMap.put(formatObj, returnValue);
 203                 }
 204             }
 205         }
 206 
 207         if (returnValue == null) {
 208             returnValue = predefinedClipboardNames[CF_UNSUPPORTED];
 209         }
 210 
 211         return returnValue;
 212     }
 213 
 214     private final ToolkitThreadBlockedHandler handler = new CToolkitThreadBlockedHandler();
 215 
 216     @Override
 217     public ToolkitThreadBlockedHandler getToolkitThreadBlockedHandler() {
 218         return handler;
 219     }
 220 
 221     @Override
 222     protected byte[] imageToPlatformBytes(Image image, long format) {
 223         return CImage.getCreator().getPlatformImageBytes(image);
 224     }
 225 
 226     private static native String[] nativeDragQueryFile(final byte[] bytes);
 227     @Override
 228     protected String[] dragQueryFile(final byte[] bytes) {
 229         if (bytes == null) return null;
 230         if (new String(bytes).startsWith("Unsupported type")) return null;
 231         return nativeDragQueryFile(bytes);
 232     }
 233 
 234 
 235     @Override
 236     protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException {
 237         return CImage.getCreator().createImageFromPlatformImageBytes(bytes);
 238     }
 239 
 240     @Override
 241     protected ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList) throws IOException {
 242         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 243         for (String file : fileList) {
 244             byte[] bytes = file.getBytes();
 245             bos.write(bytes, 0, bytes.length);
 246             bos.write(0);
 247         }
 248         return bos;
 249     }
 250 
 251     @Override
 252     protected boolean isURIListFormat(long format) {
 253         String nat = getNativeForFormat(format);
 254         if (nat == null) {
 255             return false;
 256         }
 257         try {
 258             DataFlavor df = new DataFlavor(nat);
 259             if (isUriListFlavor(df)) {
 260                 return true;
 261             }
 262         } catch (Exception e) {
 263             // Not a MIME format.
 264         }
 265         return false;
 266     }
 267 
 268     private boolean isUriListFlavor(DataFlavor df) {
 269         if (df.getPrimaryType().equals("text") && df.getSubType().equals("uri-list")) {
 270             return true;
 271         }
 272         return false;
 273     }
 274 }