KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleDCF


1 /*
2  * @(#)SimpleDCF.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 javax.activation.*;
12 import java.util.StringTokenizer JavaDoc;
13 import java.util.Hashtable JavaDoc;
14
15 public class SimpleDCF implements DataContentHandlerFactory {
16     Hashtable JavaDoc entry_hash = new Hashtable JavaDoc();
17     /**
18      * the constructor, takes a list of classes as an argument in the
19      * form:
20      * <mimetype>:<class name>\n
21      *
22      * For Example:
23      *
24      * application/x-wombat:com.womco.WombatDCH
25      * text/plain:com.textco.TextDCH
26      *
27      */

28     public SimpleDCF(String JavaDoc entries) {
29     StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(entries);
30
31     String JavaDoc entry;
32     System.out.println("SimpleDCH: new SimpleDCF being created");
33
34     // parse the string
35
while(tok.hasMoreTokens()) {
36         int colon;
37
38         entry = tok.nextToken();
39         System.out.println("full entry = " + entry);
40
41         // parse out the fields
42
colon = entry.indexOf(':');
43         VectorEntry ve = new VectorEntry(entry.substring(0,colon),
44                          entry.substring(colon + 1,
45                                  entry.length()));
46         System.out.println("adding element = " + ve);
47         entry_hash.put(ve.getMimeType(),ve);
48     }
49     }
50
51     /**
52      * implement the factor interface
53      */

54     public DataContentHandler createDataContentHandler(String JavaDoc mimeType){
55     DataContentHandler dch = null;
56
57     System.out.print("SimpleDCF: trying to create a DCH");
58
59     VectorEntry ve = (VectorEntry)entry_hash.get(mimeType);
60     if(ve != null) {
61         System.out.print("...found token");
62         try {
63         
64         dch = (DataContentHandler)Class.forName(
65                     ve.getClassName()).newInstance();
66         if(dch == null)
67             System.out.println("...FAILED!!!");
68         else
69             System.out.println("...SUCCESS!!!");
70
71         } catch(Exception JavaDoc e) {
72         System.out.println(e);
73         }
74     }
75     return dch;
76     }
77 }
78
79 class VectorEntry {
80     private String JavaDoc mimeType;
81     private String JavaDoc className;
82
83     public VectorEntry(String JavaDoc mimeType, String JavaDoc className) {
84     this.mimeType = mimeType;
85     this.className = className;
86     }
87     
88     public String JavaDoc getMimeType() { return mimeType; }
89     public String JavaDoc getClassName() { return className; }
90     public String JavaDoc toString() {
91     return new String JavaDoc("type: " + mimeType + " class name: " + className);
92     }
93
94 }
95
Popular Tags