changeset 11004:c5d02f908fb2

8081787: [macosx] MalformedURLException is thrown during reading data for application/x-java-url;class=java.net.URL flavor Reviewed-by: alexsch, serb
author mcherkas
date Thu, 13 Aug 2015 16:25:08 +0300
parents bf450c1eb01a
children 2a68af61c4cb
files src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java
diffstat 1 files changed, 42 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java	Sat Aug 08 03:27:01 2015 +0300
+++ b/src/macosx/classes/sun/lwawt/macosx/CDataTransferer.java	Thu Aug 13 16:25:08 2015 +0300
@@ -27,13 +27,13 @@
 
 import java.awt.*;
 import java.awt.image.*;
-import sun.awt.image.ImageRepresentation;
 
 import java.io.*;
 import java.net.URL;
 import java.text.Normalizer;
 import java.text.Normalizer.Form;
 import java.util.*;
+import java.util.regex.*;
 
 import java.awt.datatransfer.*;
 import sun.awt.datatransfer.*;
@@ -122,26 +122,52 @@
 
     @Override
     public Object translateBytes(byte[] bytes, DataFlavor flavor,
-                                    long format, Transferable transferable) throws IOException {
+                                 long format, Transferable transferable) throws IOException {
 
-            if (format == CF_URL && URL.class.equals(flavor.getRepresentationClass()))
-            {
-                String charset = getDefaultTextCharset();
-                if (transferable != null && transferable.isDataFlavorSupported(javaTextEncodingFlavor)) {
-                    try {
-                        charset = new String((byte[])transferable.getTransferData(javaTextEncodingFlavor), "UTF-8");
-                    } catch (UnsupportedFlavorException cannotHappen) {
-                    }
+
+        if (format == CF_URL && URL.class.equals(flavor.getRepresentationClass())) {
+            String charset = getDefaultTextCharset();
+            if (transferable != null && transferable.isDataFlavorSupported(javaTextEncodingFlavor)) {
+                try {
+                    charset = new String((byte[]) transferable.getTransferData(javaTextEncodingFlavor), "UTF-8");
+                } catch (UnsupportedFlavorException cannotHappen) {
                 }
-
-                return new URL(new String(bytes, charset));
             }
 
-            if (format == CF_STRING) {
-                bytes = Normalizer.normalize(new String(bytes, "UTF8"), Form.NFC).getBytes("UTF8");
-            }
+            String xml = new String(bytes, charset);
+            // macosx pastboard returns a propery list that contins of one URL
+            // let's extract it.
+            return new URL(extractURL(xml));
+        } else if (format == CF_STRING) {
+            bytes = Normalizer.normalize(new String(bytes, "UTF8"), Form.NFC).getBytes("UTF8");
+        }
 
-            return super.translateBytes(bytes, flavor, format, transferable);
+        return super.translateBytes(bytes, flavor, format, transferable);
+    }
+
+    /**
+     * Macosx pastboard returns xml document that contains one URL, for exmple:
+     * <pre>
+     *     {@code
+     *      <?xml version=\"1.0\" encoding=\"UTF-8\"?>
+     *      <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
+     *      <plist version=\"1.0\">
+     *          <array>
+     *              <string>file:///Users/mcherkas/Downloads/Version.jpg</string>
+     *              <string></string>
+     *          </array>
+     *      </plist>
+     *     }
+     * </pre>
+     */
+    private String extractURL(String xml) {
+        Pattern urlExtractorPattern = Pattern.compile("<string>(.*)</string>");
+        Matcher matcher = urlExtractorPattern.matcher(xml);
+        if(matcher.find()){
+            return matcher.group(1);
+        } else {
+            return null;
+        }
     }
 
     @Override