KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DCHTest


1 /*
2  * @(#)DCHTest.java 1.3 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 DCHTest {
16     private FileDataSource fds = null;
17     private DataHandler dh = null;
18     private DataContentHandlerFactory dchf = null;
19     /**
20      * main function
21      */

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

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

74     public void setFile(String JavaDoc filename) {
75     fds = new FileDataSource(filename);
76     System.out.println("DCHTest: FileDataSource created");
77     if(!fds.getContentType().equals("text/plain")) {
78         System.out.println("Type must be text/plain");
79         System.exit(1);
80     }
81     }
82         
83 }
84
85
86
Popular Tags