KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DCHTest2


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

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

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

68     public void setFile(String JavaDoc filename) {
69     fds = new FileDataSource(filename);
70     System.out.println("DCHTest2: FileDataSource created");
71     if(!fds.getContentType().equals("text/plain")) {
72         System.out.println("Type must be text/plain");
73         System.exit(1);
74     }
75     }
76
77    /**
78     * set the mailcap file in the CMD Map
79     */

80    public void setMailcap(String JavaDoc mailcap) {
81
82        try {
83        cmdMap = new MailcapCommandMap(mailcap);
84        } catch(Exception JavaDoc e){
85        e.printStackTrace();
86        System.exit(1);
87        }
88    }
89
90         
91 }
92
93
94
Popular Tags