< prev index next >

src/java.desktop/macosx/classes/sun/lwawt/macosx/CDataTransferer.java

Print this page




  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",


 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      */




  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 import java.util.regex.*;
  40 
  41 import java.awt.datatransfer.*;
  42 import sun.awt.datatransfer.*;
  43 
  44 public class CDataTransferer extends DataTransferer {
  45     private static final Map<String, Long> predefinedClipboardNameMap;
  46     private static final Map<Long, String> predefinedClipboardFormatMap;
  47 
  48     // See SystemFlavorMap, or the flavormap.properties file:
  49     // We should define a few more types in flavormap.properties, it's rather slim now.
  50     private static final String[] predefinedClipboardNames = {
  51         "",
  52         "STRING",
  53         "FILE_NAME",
  54         "TIFF",
  55         "RICH_TEXT",
  56         "HTML",
  57         "PDF",
  58         "URL",
  59         "PNG",


 113 
 114     @Override
 115     public boolean isImageFormat(long format) {
 116         int ifmt = (int)format;
 117         switch(ifmt) {
 118             case CF_TIFF:
 119             case CF_PDF:
 120             case CF_PNG:
 121             case CF_JPEG:
 122                 return true;
 123             default:
 124                 return false;
 125         }
 126     }
 127 
 128     @Override
 129     public Object translateBytes(byte[] bytes, DataFlavor flavor,
 130                                  long format, Transferable transferable) throws IOException {
 131 
 132         if (format == CF_URL && URL.class.equals(flavor.getRepresentationClass())) {
 133             String charset = Charset.defaultCharset().name();
 134             if (transferable != null && transferable.isDataFlavorSupported(javaTextEncodingFlavor)) {
 135                 try {
 136                     charset = new String((byte[]) transferable.getTransferData(javaTextEncodingFlavor), "UTF-8");
 137                 } catch (UnsupportedFlavorException cannotHappen) {
 138                 }
 139             }
 140 
 141             String xml = new String(bytes, charset);
 142             // macosx pasteboard returns a property list that consists of one URL
 143             // let's extract it.
 144             return new URL(extractURL(xml));
 145         }
 146 
 147         if(isUriListFlavor(flavor) && format == CF_FILE) {
 148             // dragQueryFile works fine with files and url,
 149             // it parses and extracts values from property list.
 150             // maxosx always returns property list for
 151             // CF_URL and CF_FILE
 152             String[] strings = dragQueryFile(bytes);
 153             if(strings == null) {
 154                 return null;
 155             }
 156             bytes = String.join(System.getProperty("line.separator"),
 157                     strings).getBytes();
 158             // now we extracted uri from xml, now we should treat it as
 159             // regular string that allows to translate data to target represantation
 160             // class by base method
 161             format = CF_STRING;
 162         } else if (format == CF_STRING) {
 163             bytes = Normalizer.normalize(new String(bytes, "UTF8"), Form.NFC).getBytes("UTF8");
 164         }  
 165 
 166         return super.translateBytes(bytes, flavor, format, transferable);
 167     }
 168 
 169     private String extractURL(String xml) {
 170        Pattern urlExtractorPattern = Pattern.compile("<string>(.*)</string>");
 171         Matcher matcher = urlExtractorPattern.matcher(xml);
 172         if (matcher.find()) {
 173             return matcher.group(1);
 174         } else {
 175             return null;
 176         }
 177     }
 178 
 179     @Override
 180     protected synchronized Long getFormatForNativeAsLong(String str) {
 181         Long format = predefinedClipboardNameMap.get(str);
 182 
 183         if (format == null) {
 184             if (java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadlessInstance()) {
 185                 // Do not try to access native system for the unknown format
 186                 return -1L;
 187             }
 188             format = registerFormatWithPasteboard(str);
 189             predefinedClipboardNameMap.put(str, format);
 190             predefinedClipboardFormatMap.put(format, str);
 191         }
 192 
 193         return format;
 194     }
 195 
 196     /*
 197      * Adds type to native mapping NSDictionary.
 198      */


< prev index next >