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