author | dmarkov |
Thu, 16 Jan 2014 20:51:18 +0400 | |
changeset 23262 | 41f2413bba45 |
parent 9035 | 1255eb81cc2f |
child 22567 | 5816a47fa4dd |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7960
diff
changeset
|
2 |
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.text.html.parser; |
|
27 |
||
6862 | 28 |
import sun.awt.AppContext; |
29 |
||
2 | 30 |
import javax.swing.text.html.HTMLEditorKit; |
31 |
import java.io.BufferedInputStream; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.InputStream; |
|
34 |
import java.io.DataInputStream; |
|
35 |
import java.io.ObjectInputStream; |
|
36 |
import java.io.Reader; |
|
37 |
import java.io.Serializable; |
|
38 |
||
39 |
/** |
|
40 |
* Responsible for starting up a new DocumentParser |
|
41 |
* each time its parse method is invoked. Stores a |
|
42 |
* reference to the dtd. |
|
43 |
* |
|
44 |
* @author Sunita Mani |
|
45 |
*/ |
|
46 |
||
47 |
public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable { |
|
48 |
||
6862 | 49 |
private static final Object DTD_KEY = new Object(); |
2 | 50 |
|
7960
440dbbdb5cbd
6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents:
6862
diff
changeset
|
51 |
protected static void setDefaultDTD() { |
440dbbdb5cbd
6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents:
6862
diff
changeset
|
52 |
getDefaultDTD(); |
440dbbdb5cbd
6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents:
6862
diff
changeset
|
53 |
} |
440dbbdb5cbd
6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents:
6862
diff
changeset
|
54 |
|
440dbbdb5cbd
6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents:
6862
diff
changeset
|
55 |
private static synchronized DTD getDefaultDTD() { |
6862 | 56 |
AppContext appContext = AppContext.getAppContext(); |
57 |
||
58 |
DTD dtd = (DTD) appContext.get(DTD_KEY); |
|
59 |
||
2 | 60 |
if (dtd == null) { |
61 |
DTD _dtd = null; |
|
62 |
// (PENDING) Hate having to hard code! |
|
63 |
String nm = "html32"; |
|
64 |
try { |
|
65 |
_dtd = DTD.getDTD(nm); |
|
66 |
} catch (IOException e) { |
|
67 |
// (PENDING) UGLY! |
|
68 |
System.out.println("Throw an exception: could not get default dtd: " + nm); |
|
69 |
} |
|
70 |
dtd = createDTD(_dtd, nm); |
|
6862 | 71 |
|
72 |
appContext.put(DTD_KEY, dtd); |
|
2 | 73 |
} |
7960
440dbbdb5cbd
6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents:
6862
diff
changeset
|
74 |
|
440dbbdb5cbd
6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents:
6862
diff
changeset
|
75 |
return dtd; |
2 | 76 |
} |
77 |
||
78 |
protected static DTD createDTD(DTD dtd, String name) { |
|
79 |
||
80 |
InputStream in = null; |
|
81 |
boolean debug = true; |
|
82 |
try { |
|
83 |
String path = name + ".bdtd"; |
|
84 |
in = getResourceAsStream(path); |
|
85 |
if (in != null) { |
|
86 |
dtd.read(new DataInputStream(new BufferedInputStream(in))); |
|
87 |
dtd.putDTDHash(name, dtd); |
|
88 |
} |
|
89 |
} catch (Exception e) { |
|
90 |
System.out.println(e); |
|
91 |
} |
|
92 |
return dtd; |
|
93 |
} |
|
94 |
||
95 |
||
96 |
public ParserDelegator() { |
|
6862 | 97 |
setDefaultDTD(); |
2 | 98 |
} |
99 |
||
100 |
public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException { |
|
7960
440dbbdb5cbd
6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents:
6862
diff
changeset
|
101 |
new DocumentParser(getDefaultDTD()).parse(r, cb, ignoreCharSet); |
2 | 102 |
} |
103 |
||
104 |
/** |
|
105 |
* Fetch a resource relative to the ParserDelegator classfile. |
|
106 |
* If this is called on 1.2 the loading will occur under the |
|
107 |
* protection of a doPrivileged call to allow the ParserDelegator |
|
108 |
* to function when used in an applet. |
|
109 |
* |
|
110 |
* @param name the name of the resource, relative to the |
|
111 |
* ParserDelegator class. |
|
112 |
* @returns a stream representing the resource |
|
113 |
*/ |
|
114 |
static InputStream getResourceAsStream(String name) { |
|
115 |
try { |
|
116 |
return ResourceLoader.getResourceAsStream(name); |
|
117 |
} catch (Throwable e) { |
|
118 |
// If the class doesn't exist or we have some other |
|
119 |
// problem we just try to call getResourceAsStream directly. |
|
120 |
return ParserDelegator.class.getResourceAsStream(name); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
private void readObject(ObjectInputStream s) |
|
125 |
throws ClassNotFoundException, IOException { |
|
126 |
s.defaultReadObject(); |
|
6862 | 127 |
setDefaultDTD(); |
2 | 128 |
} |
129 |
} |