KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ODCHTest


1 /*
2  * @(#)ODCHTest.java 1.2 99/12/06
3  *
4  * Copyright 1997-1999 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * This software is the proprietary information of Sun Microsystems, Inc.
7  * Use is subject to license terms.
8  *
9  */

10
11 import java.io.*;
12 import javax.activation.*;
13 import java.awt.datatransfer.*;
14
15 public class ODCHTest {
16     private FileDataSource fds = null;
17     private DataHandler dh = null;
18     private DataContentHandlerFactory dchf = null;
19     private String JavaDoc str;
20     /**
21      * main function
22      */

23     public static void main(String JavaDoc args[]) {
24     ODCHTest test = new ODCHTest();
25     
26     if(args.length != 0) {
27         System.out.println("usage: ODCHTest");
28         System.exit(1);
29     }
30     
31     // first let's get a DataSource
32

33     
34     test.doit();
35     }
36
37     private void doit() {
38     DataFlavor xfer_flavors[];
39     Object JavaDoc content = null;
40
41     str = new String JavaDoc("This is a test");
42
43     // now let's create a DataHandler
44
dh = new DataHandler(str, "text/plain");
45     System.out.println("ODCHTest: DataHandler created with str & text/plain");
46
47     // now lets set a DataContentHandlerFactory
48
dchf = new SimpleDCF("text/plain:PlainDCH\n");
49     System.out.println("ODCHTest: Simple dchf created");
50     
51     // now let's set the dchf in the dh
52
dh.setDataContentHandlerFactory(dchf);
53     System.out.println("ODCHTest: DataContentHandlerFactory set in DataHandler");
54     
55     // get the dataflavors
56
xfer_flavors = dh.getTransferDataFlavors();
57     System.out.println("ODCHTest: dh.getTransferDF returned " +
58                xfer_flavors.length + " data flavors.");
59
60     // get the content:
61
try {
62        content = dh.getContent();
63         } catch (Exception JavaDoc e) { e.printStackTrace(); }
64
65     if(content == null)
66         System.out.println("ODCHTest: no content to be had!!!");
67     else {
68         System.out.println("ODCHTest: got content of the following type: " +
69                    content.getClass().getName());
70         if(content == str)
71         System.out.println("get content works");
72         
73     }
74     }
75         
76 }
77
Popular Tags