1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
/* DTD.java --
Copyright (C) 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing.text.html.parser;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.BitSet;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
/**
* <p>Representation or the SGML DTD document.
* Provides basis for describing a syntax of the
* HTML documents. The fields of this class are NOT initialized in
* constructor. You need to do this separately before passing this data
* structure to the HTML parser. The subclasses with the fields, pre-
* initialized, for example, for HTML 4.01, can be available only between
* the implementation specific classes
* ( for example, {@link gnu.javax.swing.text.html.parser.HTML_401F }
* in this implementation).</p>
* <p>
* If you need more information about SGML DTD documents,
* the author suggests to read SGML tutorial on
* <a href="http://www.w3.org/TR/WD-html40-970708/intro/sgmltut.html"
* >http://www.w3.org/TR/WD-html40-970708/intro/sgmltut.html</a>.
* We also recommend Goldfarb C.F (1991) <i>The SGML Handbook</i>,
* Oxford University Press, 688 p, ISBN: 0198537379.
* </p>
* <p>
* Warning: the html, head and other tag fields will only be automatically
* assigned if the VM has the correctly implemented reflection mechanism.
* As these fields are not used anywhere in the implementation, not
* exception will be thrown in the opposite case.
* </p>
*
* @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
*/
public class DTD
implements DTDConstants
{
/**
* The version of the persistent data format.
* @specnote This was made <code>final</code> in 1.5.
*/
public static final int FILE_VERSION = 1;
/**
* The table of existing available DTDs.
*/
static Hashtable<String,DTD> dtdHash = new Hashtable<String,DTD>();
/**
* The applet element for this DTD.
*/
public Element applet;
/**
* The base element for this DTD.
*/
public Element base;
/**
* The body element for this DTD.
*/
public Element body;
/**
* The head element for this DTD.
*/
public Element head;
/**
* The html element for this DTD.
*/
public Element html;
/**
* The isindex element of for this DTD.
*/
public Element isindex;
/**
* The meta element for this DTD.
*/
public Element meta;
/**
* The p element for this DTD.
*/
public Element p;
/**
* The param element for this DTD.
*/
public Element param;
/**
* The pcdata for this DTD.
*/
public Element pcdata;
/**
* The title element for this DTD.
*/
public Element title;
/**
* The element for accessing all DTD elements by name.
*/
public Hashtable<String,Element> elementHash =
new Hashtable<String,Element>();
/**
* The entity table for accessing all DTD entities by name.
*/
public Hashtable<Object, Entity> entityHash = new Hashtable<Object, Entity>();
/**
* The name of this DTD.
*/
public String name;
/**
* Contains all elements in this DTD. The
* javax.swing.text.html.parser.Element#index field of all elements
* in this vector is set to the element position in this vector.
*/
public Vector<Element> elements = new Vector<Element>();
/** Create a new DTD with the specified name. */
protected DTD(String a_name)
{
name = a_name;
}
/** Get this DTD by name. The current implementation
* only looks in the internal table of DTD documents. If no corresponding
* entry is found, the new entry is created, placed into
* the table and returned. */
public static DTD getDTD(String name)
throws IOException
{
DTD d = dtdHash.get(name);
if (d == null)
{
d = new DTD(name);
dtdHash.put(d.name, d);
}
return d;
}
/**
* Get the element by the element name. If the element is not yet
* defined, it is newly created and placed into the element table.
* If the element name matches (ingoring case) a public non static
* element field in this class, this field is assigned to the value
* of the newly created element.
*/
public Element getElement(String element_name)
{
return newElement(element_name);
}
/**
* Get the element by the value of its
* {@link javax.swing.text.html.parser.Element#index} field.
*/
public Element getElement(int index)
{
return elements.get(index);
}
/**
* Get the entity with the given identifier.
* @param id that can be returned by
* {@link javax.swing.text.html.parser.Entity#name2type(String an_entity)}
* @return The entity from this DTD or null if there is no entity with
* such id or such entity is not present in the table of this instance.
*/
public Entity getEntity(int id)
{
String name = Entity.mapper.get(id);
if (name != null)
return entityHash.get(name);
else
return null;
}
/**
* Get the named entity by its name.
*/
public Entity getEntity(String entity_name)
{
return entityHash.get(entity_name);
}
/**
* Get the name of this instance of DTD
*/
public String getName()
{
return name;
}
/**
* Creates, adds into the entity table and returns the
* character entity like <code>&lt;</code>
* (means '<code><</code>' );
* @param name The entity name (without heading & and closing ;)
* @param type The entity type
* @param character The entity value (single character)
* @return The created entity
*/
public Entity defEntity(String name, int type, int character)
{
Entity e = newEntity(name, type);
e.data = new char[] { (char) character };
return e;
}
/**
* Define the attributes for the element with the given name.
* If the element is not exist, it is created.
* @param forElement
* @param attributes
*/
public void defineAttributes(String forElement, AttributeList attributes)
{
Element e = elementHash.get(forElement.toLowerCase());
if (e == null)
e = newElement(forElement);
e.atts = attributes;
}
/**
* Defines the element and adds it to the element table. Sets the
* <code>Element.index</code> field to the value, unique for this
* instance of DTD. If the element with the given name already exists,
* replaces all other its settings by the method argument values.
* @param name the name of the element
* @param type the type of the element
* @param headless true if the element needs no starting tag
* (should not occur in HTML).
* @param tailless true if the element needs no ending tag (like
* <code><hr></code>
* @param content the element content
* @param exclusions the set of elements that must not occur inside
* this element. The <code>Element.index</code> value defines which
* bit in this bitset corresponds to that element.
* @param inclusions the set of elements that can occur inside this
* element. the <code>Element.index</code> value defines which
* bit in this bitset corresponds to that element.
* @param attributes the element attributes.
* @return the newly defined element.
*/
public Element defineElement(String name, int type, boolean headless,
boolean tailless, ContentModel content,
BitSet exclusions, BitSet inclusions,
AttributeList attributes
)
{
Element e = newElement(name);
e.type = type;
e.oStart = headless;
e.oEnd = tailless;
e.content = content;
e.exclusions = exclusions;
e.inclusions = inclusions;
e.atts = attributes;
return e;
}
/**
* Creates, intializes and adds to the entity table the new
* entity.
* @param name the name of the entity
* @param type the type of the entity
* @param data the data section of the entity
* @return the created entity
*/
public Entity defineEntity(String name, int type, char[] data)
{
Entity e = newEntity(name, type);
e.data = data;
return e;
}
/** Place this DTD into the DTD table. */
public static void putDTDHash(String name, DTD dtd)
{
dtdHash.put(name, dtd);
}
/**
* <p>Reads DTD from an archived format. This format is not standardized
* and differs between implementations.</p><p> This implementation
* reads and defines all entities and elements using
* ObjectInputStream. The elements and entities can be written into the
* stream in any order. The objects other than elements and entities
* are ignored.</p>
* @param stream A data stream to read from.
* @throws java.io.IOException If one is thrown by the input stream
*/
public void read(DataInputStream stream)
throws java.io.IOException
{
ObjectInputStream oi = new ObjectInputStream(stream);
Object def;
try
{
while (true)
{
def = oi.readObject();
if (def instanceof Element)
{
Element e = (Element) def;
elementHash.put(e.name.toLowerCase(), e);
assignField(e);
}
else if (def instanceof Entity)
{
Entity e = (Entity) def;
entityHash.put(e.name, e);
}
}
}
catch (ClassNotFoundException ex)
{
throw new IOException(ex.getMessage());
}
catch (EOFException ex)
{
// ok EOF
}
}
/**
* Returns the name of this instance of DTD.
*/
public String toString()
{
return name;
}
/**
* Creates and returns new attribute (not an attribute list).
* @param name the name of this attribute
* @param type the type of this attribute (FIXED, IMPLIED or
* REQUIRED from <code>DTDConstants</code>).
* @param modifier the modifier of this attribute
* @param default_value the default value of this attribute
* @param allowed_values the allowed values of this attribute. The multiple
* possible values in this parameter are supposed to be separated by
* '|', same as in SGML DTD <code><!ATTLIST </code>tag. This parameter
* can be null if no list of allowed values is specified.
* @param atts the previous attribute of this element. This is
* placed to the field
* {@link javax.swing.text.html.parser.AttributeList#next },
* creating a linked list.
* @return The attributes.
*/
protected AttributeList defAttributeList(String name, int type, int modifier,
String default_value,
String allowed_values,
AttributeList atts
)
{
AttributeList al = new AttributeList(name);
al.modifier = modifier;
al.value = default_value;
al.next = atts;
if (allowed_values != null)
{
StringTokenizer st = new StringTokenizer(allowed_values, " \t|");
Vector<String> v = new Vector<String>(st.countTokens());
while (st.hasMoreTokens())
v.add(st.nextToken());
al.values = v;
}
return al;
}
/**
* Creates a new content model.
* @param type specifies the BNF operation for this content model.
* The valid operations are documented in the
* {@link javax.swing.text.html.parser.ContentModel#type }.
* @param content the content of this content model
* @param next if the content model is specified by BNF-like
* expression, contains the rest of this expression.
* @return The newly created content model.
*/
protected ContentModel defContentModel(int type, Object content,
ContentModel next
)
{
ContentModel model = new ContentModel();
model.type = type;
model.next = next;
model.content = content;
return model;
}
/**
* Defines a new element and adds it to the element table.
* If the element alredy exists,
* overrides it settings with the specified values.
* @param name the name of the new element
* @param type the type of the element
* @param headless true if the element needs no starting tag
* @param tailless true if the element needs no closing tag
* @param content the element content.
* @param exclusions the elements that must be excluded from the
* content of this element, in all levels of the hierarchy.
* @param inclusions the elements that can be included as the
* content of this element.
* @param attributes the element attributes.
* @return the created or updated element.
*/
protected Element defElement(String name, int type, boolean headless,
boolean tailless, ContentModel content,
String[] exclusions, String[] inclusions,
AttributeList attributes
)
{
// compute the bit sets
BitSet exclude = bitSet(exclusions);
BitSet include = bitSet(inclusions);
Element e =
defineElement(name, type, headless, tailless, content, exclude, include,
attributes
);
return e;
}
/**
* Creates, intializes and adds to the entity table the new
* entity.
* @param name the name of the entity
* @param type the type of the entity
* @param data the data section of the entity
* @return the created entity
*/
protected Entity defEntity(String name, int type, String data)
{
Entity e = newEntity(name, type);
e.data = data.toCharArray();
return e;
}
private void assignField(Element e)
{
String element_name = e.name;
try
{
// Assign the field via reflection.
Field f = getClass().getField(element_name.toLowerCase());
if ((f.getModifiers() & Modifier.PUBLIC) != 0)
if ((f.getModifiers() & Modifier.STATIC) == 0)
if (f.getType().isAssignableFrom(e.getClass()))
f.set(this, e);
}
catch (IllegalAccessException ex)
{
unexpected(ex);
}
catch (NoSuchFieldException ex)
{
// This is ok.
}
// Some virtual machines may still lack the proper
// implementation of reflection. As the tag fields
// are not used anywhere in this implementation,
// (and this class is also rarely used by the end user),
// it may be better not to crash everything by throwing an error
// for each case when the HTML parsing is required.
catch (Throwable t)
{
// This VM has no reflection mechanism implemented!
if (t instanceof OutOfMemoryError)
throw (Error) t;
}
}
/**
* Create the bit set for this array of elements.
* The unknown elements are automatically defined and added
* to the element table.
* @param elements
* @return The bit set.
*/
private BitSet bitSet(String[] elements)
{
BitSet b = new BitSet();
for (int i = 0; i < elements.length; i++)
{
Element e = getElement(elements [ i ]);
if (e == null)
e = newElement(elements [ i ]);
b.set(e.index);
}
return b;
}
/**
* Find the element with the given name in the element table.
* If not find, create a new element with this name and add to the
* table.
* @param name the name of the element
* @return the found or created element.
*/
private Element newElement(String name)
{
Element e = elementHash.get(name.toLowerCase());
if (e == null)
{
e = new Element();
e.name = name;
e.index = elements.size();
elements.add(e);
elementHash.put(e.name.toLowerCase(), e);
assignField(e);
}
return e;
}
/**
* Creates and adds to the element table the entity with an
* unitialized data section. Used internally.
* @param name the name of the entity
* @param type the type of the entity, a bitwise combination
* of GENERAL, PARAMETER, SYSTEM and PUBLIC.
*
* @return the created entity
*/
private Entity newEntity(String name, int type)
{
Entity e = new Entity(name, type, null);
entityHash.put(e.name, e);
return e;
}
private void unexpected(Exception ex)
{
throw new Error("This should never happen, report a bug", ex);
}
}
|