--- old/src/java.desktop/macosx/classes/sun/lwawt/macosx/CDataTransferer.java 2015-10-19 10:28:11.000000000 +0300 +++ new/src/java.desktop/macosx/classes/sun/lwawt/macosx/CDataTransferer.java 2015-10-19 10:28:11.000000000 +0300 @@ -1,3 +1,4 @@ + /* * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. @@ -28,12 +29,13 @@ 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 sun.awt.datatransfer.*; @@ -125,51 +127,30 @@ 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), "UTF-8"); - } catch (UnsupportedFlavorException cannotHappen) { - } + String[] strings = dragQueryFile(bytes); + if(strings.length == 0) { + return null; } - String xml = new String(bytes, charset); - // macosx pasteboard returns a property list that consists of one URL - // let's extract it. - return new URL(extractURL(xml)); - } - - if (format == CF_STRING) { + return new URL(strings[0]); + } else if(isUriListFlavor(flavor)) { + // dragQueryFile works fine with files and url, + // it parses and extracts values from property list. + // maxosx always returens property list for + // CF_URL and CF_FILE + String[] strings = dragQueryFile(bytes); + bytes = String.join(System.getProperty("line.separator"), + strings).getBytes(); + // now we extracted uri from xml, now we should treat it as + // regular string that allows to translate data to target represantation + // class by base method + 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); } - /** - * Macosx pasteboard returns xml document that contains one URL, for exmple: - *
-     *     {@code
-     * 
-     * 
-     * 
-     *      
-     *          file:///path_to_file
-     *          
-     *      
-     * 
-     *     }
-     * 
- */ - private String extractURL(String xml) { - Pattern urlExtractorPattern = Pattern.compile("(.*)"); - Matcher matcher = urlExtractorPattern.matcher(xml); - if (matcher.find()) { - return matcher.group(1); - } else { - return null; - } - } - @Override synchronized protected Long getFormatForNativeAsLong(String str) { Long format = predefinedClipboardNameMap.get(str); @@ -245,6 +226,7 @@ return nativeDragQueryFile(bytes); } + @Override protected Image platformImageBytesToImage(byte[] bytes, long format) throws IOException { return CImage.getCreator().createImageFromPlatformImageBytes(bytes); @@ -269,7 +251,7 @@ } try { DataFlavor df = new DataFlavor(nat); - if (df.getPrimaryType().equals("text") && df.getSubType().equals("uri-list")) { + if (isUriListFlavor(df)) { return true; } } catch (Exception e) { @@ -277,4 +259,11 @@ } return false; } + + private boolean isUriListFlavor(DataFlavor df) { + if (df.getPrimaryType().equals("text") && df.getSubType().equals("uri-list")) { + return true; + } + return false; + } }