KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > parser > ParserDelegator


1 /*
2  * @(#)ParserDelegator.java 1.15 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.text.html.parser;
9
10 import javax.swing.text.html.HTMLEditorKit JavaDoc;
11 import java.io.BufferedInputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.DataInputStream JavaDoc;
15 import java.io.ObjectInputStream JavaDoc;
16 import java.io.Reader JavaDoc;
17 import java.io.Serializable JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19
20 /**
21  * Responsible for starting up a new DocumentParser
22  * each time its parse method is invoked. Stores a
23  * reference to the dtd.
24  *
25  * @author Sunita Mani
26  * @version 1.15, 12/19/03
27  */

28
29 public class ParserDelegator extends HTMLEditorKit.Parser JavaDoc implements Serializable JavaDoc {
30
31     private static DTD JavaDoc dtd = null;
32
33     protected static synchronized void setDefaultDTD() {
34         if (dtd == null) {
35         DTD JavaDoc _dtd = null;
36         // (PENDING) Hate having to hard code!
37
String JavaDoc nm = "html32";
38         try {
39         _dtd = DTD.getDTD(nm);
40         } catch (IOException JavaDoc e) {
41         // (PENDING) UGLY!
42
System.out.println("Throw an exception: could not get default dtd: " + nm);
43         }
44         dtd = createDTD(_dtd, nm);
45         }
46     }
47
48     protected static DTD JavaDoc createDTD(DTD JavaDoc dtd, String JavaDoc name) {
49
50     InputStream JavaDoc in = null;
51     boolean debug = true;
52     try {
53         String JavaDoc path = name + ".bdtd";
54         in = getResourceAsStream(path);
55             if (in != null) {
56                 dtd.read(new DataInputStream JavaDoc(new BufferedInputStream JavaDoc(in)));
57                 dtd.putDTDHash(name, dtd);
58         }
59         } catch (Exception JavaDoc e) {
60             System.out.println(e);
61         }
62         return dtd;
63     }
64
65
66     public ParserDelegator() {
67     if (dtd == null) {
68         setDefaultDTD();
69     }
70     }
71
72     public void parse(Reader JavaDoc r, HTMLEditorKit.ParserCallback JavaDoc cb, boolean ignoreCharSet) throws IOException JavaDoc {
73     new DocumentParser JavaDoc(dtd).parse(r, cb, ignoreCharSet);
74     }
75
76     /**
77      * Fetch a resource relative to the ParserDelegator classfile.
78      * If this is called on 1.2 the loading will occur under the
79      * protection of a doPrivileged call to allow the ParserDelegator
80      * to function when used in an applet.
81      *
82      * @param name the name of the resource, relative to the
83      * ParserDelegator class.
84      * @returns a stream representing the resource
85      */

86     static InputStream JavaDoc getResourceAsStream(String JavaDoc name) {
87     try {
88             return ResourceLoader.getResourceAsStream(name);
89     } catch (Throwable JavaDoc e) {
90         // If the class doesn't exist or we have some other
91
// problem we just try to call getResourceAsStream directly.
92
return ParserDelegator JavaDoc.class.getResourceAsStream(name);
93     }
94     }
95
96     private void readObject(ObjectInputStream JavaDoc s)
97     throws ClassNotFoundException JavaDoc, IOException JavaDoc {
98     s.defaultReadObject();
99     if (dtd == null) {
100         setDefaultDTD();
101     }
102     }
103 }
104
105
106
Popular Tags