Danyel
Danyel

Reputation: 2250

Transferable / URL export: Transfering bookmark data between Java App and Mozilla

I'm writing a Java application and I want to integrate a feature to somewhat modify bookmark data from Mozilla. This sounds very complicated, but it's only a drag and drop issue:

1) I can't really drop more than one bookmark entry from Mozilla library to my Java application, because there are no DataFlavors available. Thus, obviously, I cannot read the raw data from the Transferable I get... Single entries come with 78 DataFlavors. Is there any way to enable multiple bookmark dropping?
I can circumvent this issue by exporting all bookmarks to HTML and importing it from inside my app, but it's not very pleasant.

2) It's also quite hard to figure out how to export any URL to Mozilla bookmarks. For instance, the best behaviour would be that Mozilla recognizes my data the same way it does with hyperlinks. Dropping a link, e.g. Google, from inside the browser into the bookmark library immediately creates an entry with name "Google" and URL "http://www.google.com". My application though is forced to return an implementation of java.io.Reader because Mozilla Firefox library apparently doesn't accept anything else.

TL;DR: Is there any way (via drag and drop) so I can 1) import multiple bookmarks from Mozilla to Java App and 2) export any kind of Transferable data to Mozilla bookmarks library so both name and URL are directly recognized by Firefox?

I hope my question is clear.

Regards and thanks in advance!

Upvotes: 0

Views: 1072

Answers (2)

Danyel
Danyel

Reputation: 2250

As of January 2013, on current versions of Java (7u10) / Firefox (17.0.1) for Windows 7, there doesn't seem a way to do what I want to.

Dragging multiple bookmarks into a Java Application comes without any DataFlavors, so reading from them is impossible.
Dropping a bookmark into Firefox seems impossible too - specifying only text/x-moz-url will make Firefox "accept" the data, but Firefox never attempts to read from it (the methods are never called). Specifying more than only text/x-moz-url (e.g. text/x-moz-url; class=java.io.Reader) leads to Firefox not accepting the Data at all.

Unless someone can provide a stable solution, this question is technically answered:

It's not possible.

Upvotes: 0

VGR
VGR

Reputation: 44404

After much trial and error, I noticed that the type text/x-moz-url is not actually a text type at all, but a binary type. It never has a charset associated with it; it is always UTF-16LE.

The following works for me, at least in x86 Linux, but it does not work with the Windows version of Firefox at all:

static class Bookmark
implements Transferable {

    private final String mimeType;
    private final byte[] bytes;

    Bookmark(String uri,
             String name) {
        String text = uri + "\r\n" + name;
        this.bytes = text.getBytes(StandardCharsets.UTF_16LE);
        this.mimeType = "text/x-moz-url";
    }

    public DataFlavor[] getTransferDataFlavors() {
        try {
            return new DataFlavor[] {
                new DataFlavor(mimeType + "; class=java.io.InputStream"),
                new DataFlavor(mimeType + "; class=java.nio.ByteBuffer"),
                new DataFlavor(mimeType + "; class=\"[B\""),
            };
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.isMimeTypeEqual(mimeType);
    }

    public Object getTransferData(DataFlavor flavor)
    throws IOException,
           UnsupportedFlavorException {

        Class<?> cls = flavor.getRepresentationClass();
        if (cls.equals(byte[].class)) {
            return bytes;
        }
        if (cls.equals(ByteBuffer.class)) {
            return ByteBuffer.wrap(bytes);
        }
        if (cls.equals(InputStream.class)) {
            return new ByteArrayInputStream(bytes);
        }

        throw new UnsupportedFlavorException(flavor);
    }
}

The reason it doesn't work in Windows is that the Windows version of Firefox apparently doesn't use text/x-moz-url. When I drag a bookmark from Firefox and print out the supported DataFlavors, text/x-moz-url is not in the list at all. Only string flavors (including text/html and text/uri-list), and a flavor whose representation class is java.net.URL, are supported, and none of those can hold the bookmark's name except text/html, which as I mentioned above, seems to be a type Firefox can provide but will not accept.

Upvotes: 1

Related Questions