2
|
1 |
/*
|
|
2 |
* Copyright 1998-2002 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.swing.text.html.parser;
|
|
27 |
|
|
28 |
import javax.swing.text.html.HTMLEditorKit;
|
|
29 |
import java.io.BufferedInputStream;
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.io.InputStream;
|
|
32 |
import java.io.DataInputStream;
|
|
33 |
import java.io.ObjectInputStream;
|
|
34 |
import java.io.Reader;
|
|
35 |
import java.io.Serializable;
|
|
36 |
import java.lang.reflect.Method;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Responsible for starting up a new DocumentParser
|
|
40 |
* each time its parse method is invoked. Stores a
|
|
41 |
* reference to the dtd.
|
|
42 |
*
|
|
43 |
* @author Sunita Mani
|
|
44 |
*/
|
|
45 |
|
|
46 |
public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable {
|
|
47 |
|
|
48 |
private static DTD dtd = null;
|
|
49 |
|
|
50 |
protected static synchronized void setDefaultDTD() {
|
|
51 |
if (dtd == null) {
|
|
52 |
DTD _dtd = null;
|
|
53 |
// (PENDING) Hate having to hard code!
|
|
54 |
String nm = "html32";
|
|
55 |
try {
|
|
56 |
_dtd = DTD.getDTD(nm);
|
|
57 |
} catch (IOException e) {
|
|
58 |
// (PENDING) UGLY!
|
|
59 |
System.out.println("Throw an exception: could not get default dtd: " + nm);
|
|
60 |
}
|
|
61 |
dtd = createDTD(_dtd, nm);
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
protected static DTD createDTD(DTD dtd, String name) {
|
|
66 |
|
|
67 |
InputStream in = null;
|
|
68 |
boolean debug = true;
|
|
69 |
try {
|
|
70 |
String path = name + ".bdtd";
|
|
71 |
in = getResourceAsStream(path);
|
|
72 |
if (in != null) {
|
|
73 |
dtd.read(new DataInputStream(new BufferedInputStream(in)));
|
|
74 |
dtd.putDTDHash(name, dtd);
|
|
75 |
}
|
|
76 |
} catch (Exception e) {
|
|
77 |
System.out.println(e);
|
|
78 |
}
|
|
79 |
return dtd;
|
|
80 |
}
|
|
81 |
|
|
82 |
|
|
83 |
public ParserDelegator() {
|
|
84 |
if (dtd == null) {
|
|
85 |
setDefaultDTD();
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException {
|
|
90 |
new DocumentParser(dtd).parse(r, cb, ignoreCharSet);
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Fetch a resource relative to the ParserDelegator classfile.
|
|
95 |
* If this is called on 1.2 the loading will occur under the
|
|
96 |
* protection of a doPrivileged call to allow the ParserDelegator
|
|
97 |
* to function when used in an applet.
|
|
98 |
*
|
|
99 |
* @param name the name of the resource, relative to the
|
|
100 |
* ParserDelegator class.
|
|
101 |
* @returns a stream representing the resource
|
|
102 |
*/
|
|
103 |
static InputStream getResourceAsStream(String name) {
|
|
104 |
try {
|
|
105 |
return ResourceLoader.getResourceAsStream(name);
|
|
106 |
} catch (Throwable e) {
|
|
107 |
// If the class doesn't exist or we have some other
|
|
108 |
// problem we just try to call getResourceAsStream directly.
|
|
109 |
return ParserDelegator.class.getResourceAsStream(name);
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
private void readObject(ObjectInputStream s)
|
|
114 |
throws ClassNotFoundException, IOException {
|
|
115 |
s.defaultReadObject();
|
|
116 |
if (dtd == null) {
|
|
117 |
setDefaultDTD();
|
|
118 |
}
|
|
119 |
}
|
|
120 |
}
|