author | dmarkov |
Thu, 16 Jan 2014 20:51:18 +0400 | |
changeset 23262 | 41f2413bba45 |
parent 6862 | f66eb6b6a6b9 |
child 24494 | 67129b9360d2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
6862 | 2 |
* Copyright (c) 1998, 2010, 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 java.io.PrintStream; |
31 |
import java.io.File; |
|
32 |
import java.io.FileInputStream; |
|
33 |
import java.io.InputStream; |
|
34 |
import java.io.IOException; |
|
35 |
import java.io.FileNotFoundException; |
|
36 |
import java.io.BufferedInputStream; |
|
37 |
import java.io.DataInputStream; |
|
38 |
import java.util.Hashtable; |
|
39 |
import java.util.Vector; |
|
40 |
import java.util.BitSet; |
|
41 |
import java.util.StringTokenizer; |
|
42 |
import java.util.Enumeration; |
|
43 |
import java.util.Properties; |
|
44 |
import java.net.URL; |
|
45 |
||
46 |
/** |
|
47 |
* The representation of an SGML DTD. DTD describes a document |
|
48 |
* syntax and is used in parsing of HTML documents. It contains |
|
49 |
* a list of elements and their attributes as well as a list of |
|
50 |
* entities defined in the DTD. |
|
51 |
* |
|
52 |
* @see Element |
|
53 |
* @see AttributeList |
|
54 |
* @see ContentModel |
|
55 |
* @see Parser |
|
56 |
* @author Arthur van Hoff |
|
57 |
*/ |
|
58 |
public |
|
59 |
class DTD implements DTDConstants { |
|
60 |
public String name; |
|
61 |
public Vector<Element> elements = new Vector<Element>(); |
|
62 |
public Hashtable<String,Element> elementHash |
|
63 |
= new Hashtable<String,Element>(); |
|
64 |
public Hashtable<Object,Entity> entityHash |
|
65 |
= new Hashtable<Object,Entity>(); |
|
66 |
public final Element pcdata = getElement("#pcdata"); |
|
67 |
public final Element html = getElement("html"); |
|
68 |
public final Element meta = getElement("meta"); |
|
69 |
public final Element base = getElement("base"); |
|
70 |
public final Element isindex = getElement("isindex"); |
|
71 |
public final Element head = getElement("head"); |
|
72 |
public final Element body = getElement("body"); |
|
73 |
public final Element applet = getElement("applet"); |
|
74 |
public final Element param = getElement("param"); |
|
75 |
public final Element p = getElement("p"); |
|
76 |
public final Element title = getElement("title"); |
|
77 |
final Element style = getElement("style"); |
|
78 |
final Element link = getElement("link"); |
|
79 |
final Element script = getElement("script"); |
|
80 |
||
81 |
public static final int FILE_VERSION = 1; |
|
82 |
||
83 |
/** |
|
84 |
* Creates a new DTD with the specified name. |
|
85 |
* @param name the name, as a <code>String</code> of the new DTD |
|
86 |
*/ |
|
87 |
protected DTD(String name) { |
|
88 |
this.name = name; |
|
89 |
defEntity("#RE", GENERAL, '\r'); |
|
90 |
defEntity("#RS", GENERAL, '\n'); |
|
91 |
defEntity("#SPACE", GENERAL, ' '); |
|
92 |
defineElement("unknown", EMPTY, false, true, null, null, null, null); |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Gets the name of the DTD. |
|
97 |
* @return the name of the DTD |
|
98 |
*/ |
|
99 |
public String getName() { |
|
100 |
return name; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Gets an entity by name. |
|
105 |
* @return the <code>Entity</code> corresponding to the |
|
106 |
* <code>name</code> <code>String</code> |
|
107 |
*/ |
|
108 |
public Entity getEntity(String name) { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
109 |
return entityHash.get(name); |
2 | 110 |
} |
111 |
||
112 |
/** |
|
113 |
* Gets a character entity. |
|
114 |
* @return the <code>Entity</code> corresponding to the |
|
115 |
* <code>ch</code> character |
|
116 |
*/ |
|
117 |
public Entity getEntity(int ch) { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
118 |
return entityHash.get(Integer.valueOf(ch)); |
2 | 119 |
} |
120 |
||
121 |
/** |
|
122 |
* Returns <code>true</code> if the element is part of the DTD, |
|
123 |
* otherwise returns <code>false</code>. |
|
124 |
* |
|
125 |
* @param name the requested <code>String</code> |
|
126 |
* @return <code>true</code> if <code>name</code> exists as |
|
127 |
* part of the DTD, otherwise returns <code>false</code> |
|
128 |
*/ |
|
129 |
boolean elementExists(String name) { |
|
130 |
return !"unknown".equals(name) && (elementHash.get(name) != null); |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Gets an element by name. A new element is |
|
135 |
* created if the element doesn't exist. |
|
136 |
* |
|
137 |
* @param name the requested <code>String</code> |
|
138 |
* @return the <code>Element</code> corresponding to |
|
139 |
* <code>name</code>, which may be newly created |
|
140 |
*/ |
|
141 |
public Element getElement(String name) { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
142 |
Element e = elementHash.get(name); |
2 | 143 |
if (e == null) { |
144 |
e = new Element(name, elements.size()); |
|
145 |
elements.addElement(e); |
|
146 |
elementHash.put(name, e); |
|
147 |
} |
|
148 |
return e; |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* Gets an element by index. |
|
153 |
* |
|
154 |
* @param index the requested index |
|
155 |
* @return the <code>Element</code> corresponding to |
|
156 |
* <code>index</code> |
|
157 |
*/ |
|
158 |
public Element getElement(int index) { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
159 |
return elements.elementAt(index); |
2 | 160 |
} |
161 |
||
162 |
/** |
|
163 |
* Defines an entity. If the <code>Entity</code> specified |
|
164 |
* by <code>name</code>, <code>type</code>, and <code>data</code> |
|
165 |
* exists, it is returned; otherwise a new <code>Entity</code> |
|
166 |
* is created and is returned. |
|
167 |
* |
|
168 |
* @param name the name of the <code>Entity</code> as a <code>String</code> |
|
169 |
* @param type the type of the <code>Entity</code> |
|
170 |
* @param data the <code>Entity</code>'s data |
|
171 |
* @return the <code>Entity</code> requested or a new <code>Entity</code> |
|
172 |
* if not found |
|
173 |
*/ |
|
174 |
public Entity defineEntity(String name, int type, char data[]) { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
175 |
Entity ent = entityHash.get(name); |
2 | 176 |
if (ent == null) { |
177 |
ent = new Entity(name, type, data); |
|
178 |
entityHash.put(name, ent); |
|
179 |
if (((type & GENERAL) != 0) && (data.length == 1)) { |
|
180 |
switch (type & ~GENERAL) { |
|
181 |
case CDATA: |
|
182 |
case SDATA: |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
183 |
entityHash.put(Integer.valueOf(data[0]), ent); |
2 | 184 |
break; |
185 |
} |
|
186 |
} |
|
187 |
} |
|
188 |
return ent; |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Returns the <code>Element</code> which matches the |
|
193 |
* specified parameters. If one doesn't exist, a new |
|
194 |
* one is created and returned. |
|
195 |
* |
|
196 |
* @param name the name of the <code>Element</code> |
|
197 |
* @param type the type of the <code>Element</code> |
|
198 |
* @param omitStart <code>true</code> if start should be omitted |
|
199 |
* @param omitEnd <code>true</code> if end should be omitted |
|
200 |
* @param content the <code>ContentModel</code> |
|
201 |
* @param atts the <code>AttributeList</code> specifying the |
|
202 |
* <code>Element</code> |
|
203 |
* @return the <code>Element</code> specified |
|
204 |
*/ |
|
205 |
public Element defineElement(String name, int type, |
|
206 |
boolean omitStart, boolean omitEnd, ContentModel content, |
|
207 |
BitSet exclusions, BitSet inclusions, AttributeList atts) { |
|
208 |
Element e = getElement(name); |
|
209 |
e.type = type; |
|
210 |
e.oStart = omitStart; |
|
211 |
e.oEnd = omitEnd; |
|
212 |
e.content = content; |
|
213 |
e.exclusions = exclusions; |
|
214 |
e.inclusions = inclusions; |
|
215 |
e.atts = atts; |
|
216 |
return e; |
|
217 |
} |
|
218 |
||
219 |
/** |
|
220 |
* Defines attributes for an {@code Element}. |
|
221 |
* |
|
222 |
* @param name the name of the <code>Element</code> |
|
223 |
* @param atts the <code>AttributeList</code> specifying the |
|
224 |
* <code>Element</code> |
|
225 |
*/ |
|
226 |
public void defineAttributes(String name, AttributeList atts) { |
|
227 |
Element e = getElement(name); |
|
228 |
e.atts = atts; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Creates and returns a character <code>Entity</code>. |
|
233 |
* @param name the entity's name |
|
234 |
* @return the new character <code>Entity</code> |
|
235 |
*/ |
|
236 |
public Entity defEntity(String name, int type, int ch) { |
|
237 |
char data[] = {(char)ch}; |
|
238 |
return defineEntity(name, type, data); |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* Creates and returns an <code>Entity</code>. |
|
243 |
* @param name the entity's name |
|
244 |
* @return the new <code>Entity</code> |
|
245 |
*/ |
|
246 |
protected Entity defEntity(String name, int type, String str) { |
|
247 |
int len = str.length(); |
|
248 |
char data[] = new char[len]; |
|
249 |
str.getChars(0, len, data, 0); |
|
250 |
return defineEntity(name, type, data); |
|
251 |
} |
|
252 |
||
253 |
/** |
|
254 |
* Creates and returns an <code>Element</code>. |
|
255 |
* @param name the element's name |
|
256 |
* @return the new <code>Element</code> |
|
257 |
*/ |
|
258 |
protected Element defElement(String name, int type, |
|
259 |
boolean omitStart, boolean omitEnd, ContentModel content, |
|
260 |
String[] exclusions, String[] inclusions, AttributeList atts) { |
|
261 |
BitSet excl = null; |
|
262 |
if (exclusions != null && exclusions.length > 0) { |
|
263 |
excl = new BitSet(); |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
264 |
for (String str : exclusions) { |
2 | 265 |
if (str.length() > 0) { |
266 |
excl.set(getElement(str).getIndex()); |
|
267 |
} |
|
268 |
} |
|
269 |
} |
|
270 |
BitSet incl = null; |
|
271 |
if (inclusions != null && inclusions.length > 0) { |
|
272 |
incl = new BitSet(); |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
273 |
for (String str : inclusions) { |
2 | 274 |
if (str.length() > 0) { |
275 |
incl.set(getElement(str).getIndex()); |
|
276 |
} |
|
277 |
} |
|
278 |
} |
|
279 |
return defineElement(name, type, omitStart, omitEnd, content, excl, incl, atts); |
|
280 |
} |
|
281 |
||
282 |
/** |
|
283 |
* Creates and returns an <code>AttributeList</code>. |
|
284 |
* @param name the attribute list's name |
|
285 |
* @return the new <code>AttributeList</code> |
|
286 |
*/ |
|
287 |
protected AttributeList defAttributeList(String name, int type, int modifier, String value, String values, AttributeList atts) { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
288 |
Vector<String> vals = null; |
2 | 289 |
if (values != null) { |
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
290 |
vals = new Vector<String>(); |
2 | 291 |
for (StringTokenizer s = new StringTokenizer(values, "|") ; s.hasMoreTokens() ;) { |
292 |
String str = s.nextToken(); |
|
293 |
if (str.length() > 0) { |
|
294 |
vals.addElement(str); |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
298 |
return new AttributeList(name, type, modifier, value, vals, atts); |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* Creates and returns a new content model. |
|
303 |
* @param type the type of the new content model |
|
304 |
* @return the new <code>ContentModel</code> |
|
305 |
*/ |
|
306 |
protected ContentModel defContentModel(int type, Object obj, ContentModel next) { |
|
307 |
return new ContentModel(type, obj, next); |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Returns a string representation of this DTD. |
|
312 |
* @return the string representation of this DTD |
|
313 |
*/ |
|
314 |
public String toString() { |
|
315 |
return name; |
|
316 |
} |
|
317 |
||
318 |
/** |
|
6862 | 319 |
* The hashtable key of DTDs in AppContext. |
2 | 320 |
*/ |
6862 | 321 |
private static final Object DTD_HASH_KEY = new Object(); |
2 | 322 |
|
6862 | 323 |
public static void putDTDHash(String name, DTD dtd) { |
324 |
getDtdHash().put(name, dtd); |
|
325 |
} |
|
326 |
||
2 | 327 |
/** |
328 |
* Returns a DTD with the specified <code>name</code>. If |
|
329 |
* a DTD with that name doesn't exist, one is created |
|
330 |
* and returned. Any uppercase characters in the name |
|
331 |
* are converted to lowercase. |
|
332 |
* |
|
333 |
* @param name the name of the DTD |
|
334 |
* @return the DTD which corresponds to <code>name</code> |
|
335 |
*/ |
|
336 |
public static DTD getDTD(String name) throws IOException { |
|
337 |
name = name.toLowerCase(); |
|
6862 | 338 |
DTD dtd = getDtdHash().get(name); |
2 | 339 |
if (dtd == null) |
340 |
dtd = new DTD(name); |
|
341 |
||
342 |
return dtd; |
|
343 |
} |
|
344 |
||
6862 | 345 |
private static Hashtable<String, DTD> getDtdHash() { |
346 |
AppContext appContext = AppContext.getAppContext(); |
|
347 |
||
348 |
Hashtable<String, DTD> result = (Hashtable<String, DTD>) appContext.get(DTD_HASH_KEY); |
|
349 |
||
350 |
if (result == null) { |
|
351 |
result = new Hashtable<String, DTD>(); |
|
352 |
||
353 |
appContext.put(DTD_HASH_KEY, result); |
|
354 |
} |
|
355 |
||
356 |
return result; |
|
357 |
} |
|
358 |
||
2 | 359 |
/** |
360 |
* Recreates a DTD from an archived format. |
|
361 |
* @param in the <code>DataInputStream</code> to read from |
|
362 |
*/ |
|
363 |
public void read(DataInputStream in) throws IOException { |
|
364 |
if (in.readInt() != FILE_VERSION) { |
|
365 |
} |
|
366 |
||
367 |
// |
|
368 |
// Read the list of names |
|
369 |
// |
|
370 |
String[] names = new String[in.readShort()]; |
|
371 |
for (int i = 0; i < names.length; i++) { |
|
372 |
names[i] = in.readUTF(); |
|
373 |
} |
|
374 |
||
375 |
||
376 |
// |
|
377 |
// Read the entities |
|
378 |
// |
|
379 |
int num = in.readShort(); |
|
380 |
for (int i = 0; i < num; i++) { |
|
381 |
short nameId = in.readShort(); |
|
382 |
int type = in.readByte(); |
|
383 |
String name = in.readUTF(); |
|
384 |
defEntity(names[nameId], type | GENERAL, name); |
|
385 |
} |
|
386 |
||
387 |
// Read the elements |
|
388 |
// |
|
389 |
num = in.readShort(); |
|
390 |
for (int i = 0; i < num; i++) { |
|
391 |
short nameId = in.readShort(); |
|
392 |
int type = in.readByte(); |
|
393 |
byte flags = in.readByte(); |
|
394 |
ContentModel m = readContentModel(in, names); |
|
395 |
String[] exclusions = readNameArray(in, names); |
|
396 |
String[] inclusions = readNameArray(in, names); |
|
397 |
AttributeList atts = readAttributeList(in, names); |
|
398 |
defElement(names[nameId], type, |
|
399 |
((flags & 0x01) != 0), ((flags & 0x02) != 0), |
|
400 |
m, exclusions, inclusions, atts); |
|
401 |
} |
|
402 |
} |
|
403 |
||
404 |
private ContentModel readContentModel(DataInputStream in, String[] names) |
|
405 |
throws IOException { |
|
406 |
byte flag = in.readByte(); |
|
407 |
switch(flag) { |
|
408 |
case 0: // null |
|
409 |
return null; |
|
410 |
case 1: { // content_c |
|
411 |
int type = in.readByte(); |
|
412 |
ContentModel m = readContentModel(in, names); |
|
413 |
ContentModel next = readContentModel(in, names); |
|
414 |
return defContentModel(type, m, next); |
|
415 |
} |
|
416 |
case 2: { // content_e |
|
417 |
int type = in.readByte(); |
|
418 |
Element el = getElement(names[in.readShort()]); |
|
419 |
ContentModel next = readContentModel(in, names); |
|
420 |
return defContentModel(type, el, next); |
|
421 |
} |
|
422 |
default: |
|
423 |
throw new IOException("bad bdtd"); |
|
424 |
} |
|
425 |
} |
|
426 |
||
427 |
private String[] readNameArray(DataInputStream in, String[] names) |
|
428 |
throws IOException { |
|
429 |
int num = in.readShort(); |
|
430 |
if (num == 0) { |
|
431 |
return null; |
|
432 |
} |
|
433 |
String[] result = new String[num]; |
|
434 |
for (int i = 0; i < num; i++) { |
|
435 |
result[i] = names[in.readShort()]; |
|
436 |
} |
|
437 |
return result; |
|
438 |
} |
|
439 |
||
440 |
||
441 |
private AttributeList readAttributeList(DataInputStream in, String[] names) |
|
442 |
throws IOException { |
|
443 |
AttributeList result = null; |
|
444 |
for (int num = in.readByte(); num > 0; --num) { |
|
445 |
short nameId = in.readShort(); |
|
446 |
int type = in.readByte(); |
|
447 |
int modifier = in.readByte(); |
|
448 |
short valueId = in.readShort(); |
|
449 |
String value = (valueId == -1) ? null : names[valueId]; |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
450 |
Vector<String> values = null; |
2 | 451 |
short numValues = in.readShort(); |
452 |
if (numValues > 0) { |
|
1287
a04aca99c77a
6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents:
438
diff
changeset
|
453 |
values = new Vector<String>(numValues); |
2 | 454 |
for (int i = 0; i < numValues; i++) { |
455 |
values.addElement(names[in.readShort()]); |
|
456 |
} |
|
457 |
} |
|
458 |
result = new AttributeList(names[nameId], type, modifier, value, |
|
459 |
values, result); |
|
460 |
// We reverse the order of the linked list by doing this, but |
|
461 |
// that order isn't important. |
|
462 |
} |
|
463 |
return result; |
|
464 |
} |
|
465 |
||
466 |
} |