Why isn't there a protocol error on OSX trying to use the dataflavor that this portable file has?

Reporting this error when trying to copy and paste an image from amazon, although we are checking for the portable object support on a specific file, it fails when trying to use this data attribute to get the url. Ive not seen thsi errors before and recently switched to Java 1.8.0_20, so I assume the problems lie in Java 8?

 java.net.MalformedURLException: no protocol: <?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>http://ecx.images-amazon.com/images/I/41NTPXC8EHL.jpg</string>
        <string></string>
    </array>
    </plist>
    at java.net.URL.<init>(URL.java:586)
    at java.net.URL.<init>(URL.java:483)
    at java.net.URL.<init>(URL.java:432)
    at sun.lwawt.macosx.CDataTransferer.translateBytes(CDataTransferer.java:135)
    at sun.awt.datatransfer.ClipboardTransferable$DataFactory.getTransferData(ClipboardTransferable.java:71)
    at sun.awt.datatransfer.ClipboardTransferable.getTransferData(ClipboardTransferable.java:168)

      

Extracting the code

public static DataFlavor imageUrlFlavor =  new DataFlavor("application/x-java-url;class=java.net.URL");
public void getImage(Transferable trans)

            if(trans.isDataFlavorSupported(FileDropTarget.imageUrlFlavor))
            {
                imageUrl  = (URL) trans.getTransferData(FileDropTarget.imageUrlFlavor);
            }

      

+3


source to share


2 answers


Unsatisfactory answer You can list everything DataFlavor

placed on the clipboard trans.getDataFlavors () ; in general, the copy places several varieties, such as text and text text.

Alternative



public static DataFlavor imageUrlFlavor = 
    new DataFlavor(URL.class, "application/x-java-url");

      

probably doesn't work either.

0


source


OS X seems to wrap the copied data in plist / xml, so it breaks your parsing. Are you copying images directly from your browser or from another application?

I tried to copy images and urls from Chrome and Safari to a simple swing app on my mac (OS X 10.9.5, Java 8u20) and I always got text / plain or images / x-java images mimetypes, never app / x -java-url or xml-wrapped.

 Transferable clipData = clipboard.getContents(clipboard);
 if (clipData != null) {
     try {
           for (DataFlavor f : clipData.getTransferDataFlavors()) {
              System.out.println("f = " + f);
           }

           if (clipData.isDataFlavorSupported(imageUrlFlavor)) {
               URL url = (URL) clipData.getTransferData(imageUrlFlavor);

               System.out.println("Got url:");
               System.out.println(url);
           }

      } catch (UnsupportedFlavorException ufe) {
          System.err.println("Flavor unsupported: " + ufe);
      } catch (IOException ioe) {
          System.err.println("Data not available: " + ioe);
      }
 }   

      



I also tried to do it using drag'n'drop and got the same result:

public void drop(DropTargetDropEvent dtde) {
    System.out.println("drop");
    dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    Transferable trans = dtde.getTransferable();
    boolean gotData = false;
    // try for application/x-java-url flavor
    try {
        if (trans.isDataFlavorSupported(urlFlavor)) {
            URL url = (URL) trans.getTransferData(urlFlavor);
            System.out.println ("got an url: " + url);
            statusField.setText(url.toString());
            statusField.setCaretPosition(0);
            gotData = true;
        } else if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String s =
                    (String) trans.getTransferData(DataFlavor.stringFlavor);
            System.out.println("Got text: " + s);
            statusField.setText(s);
            gotData = true;
        } else {
            dumpDataFlavors(trans);

        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.out.println("gotData is " + gotData);
        dtde.dropComplete(gotData);
    }
}

      

0


source







All Articles