[PJUG Javamail] Java- Finding the source URL on the clipboard from cut-and-paste

Martin Hald martin_hald2000@yahoo.com
Mon, 31 Oct 2005 13:00:36 -0800


This is a multi-part message in MIME format.
--------------050005040107040002050209
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Happy Halloween!

Java question for you all.  I'm working a problem that perhaps one of 
you has already seen.  We are building a Java client that does metadata 
discovery and we have had good luck on finding rich metadata on web 
pages.  My current task is to catch the paste action and perform a 
metadata lookup on the source URL.

I've found in IE when you cut and paste content the URL shows up on the 
clipboard (at least as seen through the Clipboard Spy utility).  I've 
been writing a test Java application that dumps the clipboard contents 
but I have yet to find the source URL. 

Any ideas?

Cheers,

-m

PS I've included my clipboard dump utility for those who want to play 
around with it.

-- 
Martin Hald                          Tel: (503) 744-0647
Eduworks                             Cel: (541) 908-4057
Email: mhald@eduworks.com



--------------050005040107040002050209
Content-Type: text/plain;
 name="Clipboard.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="Clipboard.java"

/**
 * 
 */
package org.pdlib.gui.bin;

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;

/**
 * Clipboard data experiment.
 * 
 * @author Martin Hald
 */
public class Clipboard
{
    /**
     * Dump the windows clipboard contents to the shell.
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit()
                .getSystemClipboard();

        DataFlavor[] flavors = clipboard.getAvailableDataFlavors();
        for (int ii = 0; ii < flavors.length; ii++)
        {
            System.out.println("MimeType:" + flavors[ii].getMimeType());
            Transferable contents = clipboard.getContents(null);
            try
            {
                Object o = contents.getTransferData(flavors[ii]);
                String clazz = o.getClass().getName();
                System.out.println("Java Clipboard Transfer Class Type: "
                        + clazz);
                if (clazz.equals("java.io.InputStreamReader"))
                {
                    InputStreamReader is = (InputStreamReader) o;
                    int c;
                    while ((c = is.read()) != -1)
                    {
                        System.out.print((char) c);
                    }
                }
                else if (clazz.equals("java.lang.String"))
                {
                    System.out.println((String) o);
                }
                else if (clazz.equals("[C"))
                {
                    char[] c = (char[]) o;
                    for (int jj = 0; jj < c.length; jj++)
                    {
                        System.out.print(c[jj]);
                    }
                }
                else if (clazz.equals("[B"))
                {
                    byte[] c = (byte[]) o;
                    for (int jj = 0; jj < c.length; jj++)
                    {
                        System.out.print((char) c[jj]);
                    }
                }
                else if (clazz.equals("java.nio.HeapByteBuffer"))
                {
                    ByteBuffer bb = (ByteBuffer) o;
                    CharBuffer cc = bb.asCharBuffer();
                    for (int kk = 0; kk < cc.length(); kk++)
                    {
                        System.out.print(cc.charAt(kk));
                    }
                }
                else
                {
                    System.out.println("Unable to handle clipboard data type");
                    System.out.println(o);
                }
                System.out
                        .println("\n---------------------------------------------------");
            }
            catch (UnsupportedFlavorException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void transfer()
    {

        String result = "";
        java.awt.datatransfer.Clipboard clipboard = Toolkit.getDefaultToolkit()
                .getSystemClipboard();

        Transferable contents = clipboard.getContents(null);
        boolean hasTransferableText = (contents != null)
                && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
        if (hasTransferableText)
        {
            try
            {
                result = (String) contents
                        .getTransferData(DataFlavor.stringFlavor);
            }
            catch (UnsupportedFlavorException ex)
            {
                // highly unlikely since we are using a standard DataFlavor
                System.out.println(ex);
            }
            catch (IOException ex)
            {
                System.out.println(ex);
            }
        }
        System.out.println(result);
    }
}

--------------050005040107040002050209--