KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cyberneko > html > HTMLElements


1 /*
2  * (C) Copyright 2002-2005, Andy Clark. All rights reserved.
3  *
4  * This file is distributed under an Apache style license. Please
5  * refer to the LICENSE file for specific details.
6  */

7
8 package org.cyberneko.html;
9
10 /**
11  * Collection of HTML element information.
12  *
13  * @author Andy Clark
14  *
15  * @version $Id: HTMLElements.java,v 1.12 2005/02/14 07:16:59 andyc Exp $
16  */

17 public class HTMLElements {
18
19     //
20
// Constants
21
//
22

23     // element codes
24

25     // NOTE: The element codes *must* start with 0 and increment in
26
// sequence. The parent and closes references depends on
27
// this assumption. -Ac
28

29     public static final short A = 0;
30     public static final short ABBR = A+1;
31     public static final short ACRONYM = ABBR+1;
32     public static final short ADDRESS = ACRONYM+1;
33     public static final short APPLET = ADDRESS+1;
34     public static final short AREA = APPLET+1;
35     public static final short B = AREA+1;
36     public static final short BASE = B+1;
37     public static final short BASEFONT = BASE+1;
38     public static final short BDO = BASEFONT+1;
39     public static final short BGSOUND = BDO+1;
40     public static final short BIG = BGSOUND+1;
41     public static final short BLINK = BIG+1;
42     public static final short BLOCKQUOTE = BLINK+1;
43     public static final short BODY = BLOCKQUOTE+1;
44     public static final short BR = BODY+1;
45     public static final short BUTTON = BR+1;
46     public static final short CAPTION = BUTTON+1;
47     public static final short CENTER = CAPTION+1;
48     public static final short CITE = CENTER+1;
49     public static final short CODE = CITE+1;
50     public static final short COL = CODE+1;
51     public static final short COLGROUP = COL+1;
52     public static final short COMMENT = COLGROUP+1;
53     public static final short DEL = COMMENT+1;
54     public static final short DFN = DEL+1;
55     public static final short DIR = DFN+1;
56     public static final short DIV = DIR+1;
57     public static final short DD = DIV+1;
58     public static final short DL = DD+1;
59     public static final short DT = DL+1;
60     public static final short EM = DT+1;
61     public static final short EMBED = EM+1;
62     public static final short FIELDSET = EMBED+1;
63     public static final short FONT = FIELDSET+1;
64     public static final short FORM = FONT+1;
65     public static final short FRAME = FORM+1;
66     public static final short FRAMESET = FRAME+1;
67     public static final short H1 = FRAMESET+1;
68     public static final short H2 = H1+1;
69     public static final short H3 = H2+1;
70     public static final short H4 = H3+1;
71     public static final short H5 = H4+1;
72     public static final short H6 = H5+1;
73     public static final short HEAD = H6+1;
74     public static final short HR = HEAD+1;
75     public static final short HTML = HR+1;
76     public static final short I = HTML+1;
77     public static final short IFRAME = I+1;
78     public static final short ILAYER = IFRAME+1;
79     public static final short IMG = ILAYER+1;
80     public static final short INPUT = IMG+1;
81     public static final short INS = INPUT+1;
82     public static final short ISINDEX = INS+1;
83     public static final short KBD = ISINDEX+1;
84     public static final short KEYGEN = KBD+1;
85     public static final short LABEL = KEYGEN+1;
86     public static final short LAYER = LABEL+1;
87     public static final short LEGEND = LAYER+1;
88     public static final short LI = LEGEND+1;
89     public static final short LINK = LI+1;
90     public static final short LISTING = LINK+1;
91     public static final short MAP = LISTING+1;
92     public static final short MARQUEE = MAP+1;
93     public static final short MENU = MARQUEE+1;
94     public static final short META = MENU+1;
95     public static final short MULTICOL = META+1;
96     public static final short NEXTID = MULTICOL+1;
97     public static final short NOBR = NEXTID+1;
98     public static final short NOEMBED = NOBR+1;
99     public static final short NOFRAMES = NOEMBED+1;
100     public static final short NOLAYER = NOFRAMES+1;
101     public static final short NOSCRIPT = NOLAYER+1;
102     public static final short OBJECT = NOSCRIPT+1;
103     public static final short OL = OBJECT+1;
104     public static final short OPTION = OL+1;
105     public static final short OPTGROUP = OPTION+1;
106     public static final short P = OPTGROUP+1;
107     public static final short PARAM = P+1;
108     public static final short PLAINTEXT = PARAM+1;
109     public static final short PRE = PLAINTEXT+1;
110     public static final short Q = PRE+1;
111     public static final short RB = Q+1;
112     public static final short RBC = RB+1;
113     public static final short RP = RBC+1;
114     public static final short RT = RP+1;
115     public static final short RTC = RT+1;
116     public static final short RUBY = RTC+1;
117     public static final short S = RUBY+1;
118     public static final short SAMP = S+1;
119     public static final short SCRIPT = SAMP+1;
120     public static final short SELECT = SCRIPT+1;
121     public static final short SMALL = SELECT+1;
122     public static final short SOUND = SMALL+1;
123     public static final short SPACER = SOUND+1;
124     public static final short SPAN = SPACER+1;
125     public static final short STRIKE = SPAN+1;
126     public static final short STRONG = STRIKE+1;
127     public static final short STYLE = STRONG+1;
128     public static final short SUB = STYLE+1;
129     public static final short SUP = SUB+1;
130     public static final short TABLE = SUP+1;
131     public static final short TBODY = TABLE+1;
132     public static final short TD = TBODY+1;
133     public static final short TEXTAREA = TD+1;
134     public static final short TFOOT = TEXTAREA+1;
135     public static final short TH = TFOOT+1;
136     public static final short THEAD = TH+1;
137     public static final short TITLE = THEAD+1;
138     public static final short TR = TITLE+1;
139     public static final short TT = TR+1;
140     public static final short U = TT+1;
141     public static final short UL = U+1;
142     public static final short VAR = UL+1;
143     public static final short WBR = VAR+1;
144     public static final short XML = WBR+1;
145     public static final short XMP = XML+1;
146     public static final short UNKNOWN = XMP+1;
147
148     // information
149

150     /** Element information organized by first letter. */
151     protected static final Element[][] ELEMENTS_ARRAY = new Element[26][];
152
153     /** Element information as a contiguous list. */
154     protected static final ElementList ELEMENTS = new ElementList();
155
156     /** No such element. */
157     public static final Element NO_SUCH_ELEMENT = new Element(UNKNOWN, "", 0, new short[]{HEAD,BODY}/*HTML*/, null);
158
159     //
160
// Static initializer
161
//
162

163     /**
164      * Initializes the element information.
165      * <p>
166      * <strong>Note:</strong>
167      * The <code>getElement</code> method requires that the HTML elements
168      * are added to the list in alphabetical order. If new elements are
169      * added, then they <em>must</em> be inserted in alphabetical order.
170      */

171     static {
172         // <!ENTITY % heading "H1|H2|H3|H4|H5|H6">
173
// <!ENTITY % fontstyle "TT | I | B | BIG | SMALL">
174
// <!ENTITY % phrase "EM | STRONG | DFN | CODE | SAMP | KBD | VAR | CITE | ABBR | ACRONYM" >
175
// <!ENTITY % special "A | IMG | OBJECT | BR | SCRIPT | MAP | Q | SUB | SUP | SPAN | BDO">
176
// <!ENTITY % formctrl "INPUT | SELECT | TEXTAREA | LABEL | BUTTON">
177
// <!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
178
// <!ENTITY % block "P | %heading; | %list; | %preformatted; | DL | DIV | NOSCRIPT | BLOCKQUOTE | FORM | HR | TABLE | FIELDSET | ADDRESS">
179
// <!ENTITY % flow "%block; | %inline;">
180

181         // initialize array of element information
182
ELEMENTS_ARRAY['A'-'A'] = new Element[] {
183             // A - - (%inline;)* -(A)
184
new Element(A, "A", Element.INLINE, BODY, null),
185             // ABBR - - (%inline;)*
186
new Element(ABBR, "ABBR", Element.INLINE, BODY, null),
187             // ACRONYM - - (%inline;)*
188
new Element(ACRONYM, "ACRONYM", Element.INLINE, BODY, null),
189             // ADDRESS - - (%inline;)*
190
new Element(ADDRESS, "ADDRESS", Element.BLOCK, BODY, null),
191             // APPLET
192
new Element(APPLET, "APPLET", 0, BODY, null),
193             // AREA - O EMPTY
194
new Element(AREA, "AREA", Element.EMPTY, MAP, null),
195         };
196         ELEMENTS_ARRAY['B'-'A'] = new Element[] {
197             // B - - (%inline;)*
198
new Element(B, "B", Element.INLINE, BODY, null),
199             // BASE - O EMPTY
200
new Element(BASE, "BASE", Element.EMPTY, HEAD, null),
201             // BASEFONT
202
new Element(BASEFONT, "BASEFONT", 0, HEAD, null),
203             // BDO - - (%inline;)*
204
new Element(BDO, "BDO", Element.INLINE, BODY, null),
205             // BGSOUND
206
new Element(BGSOUND, "BGSOUND", Element.EMPTY, HEAD, null),
207             // BIG - - (%inline;)*
208
new Element(BIG, "BIG", Element.INLINE, BODY, null),
209             // BLINK
210
new Element(BLINK, "BLINK", Element.INLINE, BODY, null),
211             // BLOCKQUOTE - - (%block;|SCRIPT)+
212
new Element(BLOCKQUOTE, "BLOCKQUOTE", Element.BLOCK, BODY, new short[]{P}),
213             // BODY O O (%block;|SCRIPT)+ +(INS|DEL)
214
new Element(BODY, "BODY", 0, HTML, new short[]{HEAD}),
215             // BR - O EMPTY
216
new Element(BR, "BR", Element.EMPTY, BODY, null),
217             // BUTTON - - (%flow;)* -(A|%formctrl;|FORM|FIELDSET)
218
new Element(BUTTON, "BUTTON", 0, BODY, null),
219         };
220         ELEMENTS_ARRAY['C'-'A'] = new Element[] {
221             // CAPTION - - (%inline;)*
222
new Element(CAPTION, "CAPTION", Element.INLINE, TABLE, null),
223             // CENTER,
224
new Element(CENTER, "CENTER", 0, BODY, null),
225             // CITE - - (%inline;)*
226
new Element(CITE, "CITE", Element.INLINE, BODY, null),
227             // CODE - - (%inline;)*
228
new Element(CODE, "CODE", Element.INLINE, BODY, null),
229             // COL - O EMPTY
230
new Element(COL, "COL", Element.EMPTY, TABLE, null),
231             // COLGROUP - O (COL)*
232
new Element(COLGROUP, "COLGROUP", 0, TABLE, new short[]{COL,COLGROUP}),
233             // COMMENT
234
new Element(COMMENT, "COMMENT", Element.SPECIAL, HTML, null),
235         };
236         ELEMENTS_ARRAY['D'-'A'] = new Element[] {
237             // DEL - - (%flow;)*
238
new Element(DEL, "DEL", 0, BODY, null),
239             // DFN - - (%inline;)*
240
new Element(DFN, "DFN", Element.INLINE, BODY, null),
241             // DIR
242
new Element(DIR, "DIR", 0, BODY, null),
243             // DIV - - (%flow;)*
244
new Element(DIV, "DIV", Element.BLOCK, BODY, null),
245             // DD - O (%flow;)*
246
new Element(DD, "DD", 0, DL, new short[]{DT,DD}),
247             // DL - - (DT|DD)+
248
new Element(DL, "DL", Element.BLOCK, BODY, null),
249             // DT - O (%inline;)*
250
new Element(DT, "DT", 0, DL, new short[]{DT,DD}),
251         };
252         ELEMENTS_ARRAY['E'-'A'] = new Element[] {
253             // EM - - (%inline;)*
254
new Element(EM, "EM", Element.INLINE, BODY, null),
255             // EMBED
256
new Element(EMBED, "EMBED", 0, BODY, null),
257         };
258         ELEMENTS_ARRAY['F'-'A'] = new Element[] {
259             // FIELDSET - - (#PCDATA,LEGEND,(%flow;)*)
260
new Element(FIELDSET, "FIELDSET", 0, BODY, null),
261             // FONT
262
new Element(FONT, "FONT", Element.CONTAINER, BODY, null),
263             // FORM - - (%block;|SCRIPT)+ -(FORM)
264
new Element(FORM, "FORM", Element.CONTAINER, new short[]{BODY,TD,P,DIV}, new short[]{FORM,BUTTON}),
265             // FRAME - O EMPTY
266
new Element(FRAME, "FRAME", Element.EMPTY, FRAMESET, null),
267             // FRAMESET - - ((FRAMESET|FRAME)+ & NOFRAMES?)
268
new Element(FRAMESET, "FRAMESET", 0, HTML, null),
269         };
270         ELEMENTS_ARRAY['H'-'A'] = new Element[] {
271             // (H1|H2|H3|H4|H5|H6) - - (%inline;)*
272
new Element(H1, "H1", Element.BLOCK, new short[]{BODY,A}, new short[]{H1,H2,H3,H4,H5,H6,P}),
273             new Element(H2, "H2", Element.BLOCK, new short[]{BODY,A}, new short[]{H1,H2,H3,H4,H5,H6,P}),
274             new Element(H3, "H3", Element.BLOCK, new short[]{BODY,A}, new short[]{H1,H2,H3,H4,H5,H6,P}),
275             new Element(H4, "H4", Element.BLOCK, new short[]{BODY,A}, new short[]{H1,H2,H3,H4,H5,H6,P}),
276             new Element(H5, "H5", Element.BLOCK, new short[]{BODY,A}, new short[]{H1,H2,H3,H4,H5,H6,P}),
277             new Element(H6, "H6", Element.BLOCK, new short[]{BODY,A}, new short[]{H1,H2,H3,H4,H5,H6,P}),
278             // HEAD O O (%head.content;) +(%head.misc;)
279
new Element(HEAD, "HEAD", 0, HTML, null),
280             // HR - O EMPTY
281
new Element(HR, "HR", Element.EMPTY, BODY, new short[]{P}),
282             // HTML O O (%html.content;)
283
new Element(HTML, "HTML", 0, null, null),
284         };
285         ELEMENTS_ARRAY['I'-'A'] = new Element[] {
286             // I - - (%inline;)*
287
new Element(I, "I", Element.INLINE, BODY, null),
288             // IFRAME
289
new Element(IFRAME, "IFRAME", Element.BLOCK, BODY, null),
290             // ILAYER
291
new Element(ILAYER, "ILAYER", Element.BLOCK, BODY, null),
292             // IMG - O EMPTY
293
new Element(IMG, "IMG", Element.EMPTY, BODY, null),
294             // INPUT - O EMPTY
295
new Element(INPUT, "INPUT", Element.EMPTY, BODY, null),
296             // INS - - (%flow;)*
297
new Element(INS, "INS", 0, BODY, null),
298             // ISINDEX
299
new Element(ISINDEX, "ISINDEX", 0, HEAD, null),
300         };
301         ELEMENTS_ARRAY['K'-'A'] = new Element[] {
302             // KBD - - (%inline;)*
303
new Element(KBD, "KBD", Element.INLINE, BODY, null),
304             // KEYGEN
305
new Element(KEYGEN, "KEYGEN", 0, BODY, null),
306         };
307         ELEMENTS_ARRAY['L'-'A'] = new Element[] {
308             // LABEL - - (%inline;)* -(LABEL)
309
new Element(LABEL, "LABEL", 0, BODY, null),
310             // LAYER
311
new Element(LAYER, "LAYER", Element.BLOCK, BODY, null),
312             // LEGEND - - (%inline;)*
313
new Element(LEGEND, "LEGEND", Element.INLINE, FIELDSET, null),
314             // LI - O (%flow;)*
315
new Element(LI, "LI", 0, new short[]{BODY,UL,OL}, new short[]{LI}),
316             // LINK - O EMPTY
317
new Element(LINK, "LINK", Element.EMPTY, HEAD, null),
318             // LISTING
319
new Element(LISTING, "LISTING", 0, BODY, null),
320         };
321         ELEMENTS_ARRAY['M'-'A'] = new Element[] {
322             // MAP - - ((%block;) | AREA)+
323
new Element(MAP, "MAP", Element.INLINE, BODY, null),
324             // MARQUEE
325
new Element(MARQUEE, "MARQUEE", 0, BODY, null),
326             // MENU
327
new Element(MENU, "MENU", 0, BODY, null),
328             // META - O EMPTY
329
new Element(META, "META", Element.EMPTY, HEAD, new short[]{STYLE,TITLE}),
330             // MULTICOL
331
new Element(MULTICOL, "MULTICOL", 0, BODY, null),
332         };
333         ELEMENTS_ARRAY['N'-'A'] = new Element[] {
334             // NEXTID
335
new Element(NEXTID, "NEXTID", Element.EMPTY, BODY, null),
336             // NOBR
337
new Element(NOBR, "NOBR", Element.INLINE, BODY, null),
338             // NOEMBED
339
new Element(NOEMBED, "NOEMBED", 0, BODY, null),
340             // NOFRAMES - - (BODY) -(NOFRAMES)
341
new Element(NOFRAMES, "NOFRAMES", 0, FRAMESET, null),
342             // NOLAYER
343
new Element(NOLAYER, "NOLAYER", 0, BODY, null),
344             // NOSCRIPT - - (%block;)+
345
new Element(NOSCRIPT, "NOSCRIPT", 0, new short[]{HEAD,BODY}, null),
346         };
347         ELEMENTS_ARRAY['O'-'A'] = new Element[] {
348             // OBJECT - - (PARAM | %flow;)*
349
new Element(OBJECT, "OBJECT", 0, BODY, null),
350             // OL - - (LI)+
351
new Element(OL, "OL", Element.BLOCK, BODY, null),
352             // OPTGROUP - - (OPTION)+
353
new Element(OPTGROUP, "OPTGROUP", 0, SELECT, new short[]{OPTION}),
354             // OPTION - O (#PCDATA)
355
new Element(OPTION, "OPTION", 0, SELECT, new short[]{OPTION}),
356         };
357         ELEMENTS_ARRAY['P'-'A'] = new Element[] {
358             // P - O (%inline;)*
359
new Element(P, "P", 0, BODY, new short[]{P}),
360             // PARAM - O EMPTY
361
new Element(PARAM, "PARAM", Element.EMPTY, new short[]{OBJECT,APPLET}, null),
362             // PLAINTEXT
363
new Element(PLAINTEXT, "PLAINTEXT", Element.SPECIAL, BODY, null),
364             // PRE - - (%inline;)* -(%pre.exclusion;)
365
new Element(PRE, "PRE", 0, BODY, null),
366         };
367         ELEMENTS_ARRAY['Q'-'A'] = new Element[] {
368             // Q - - (%inline;)*
369
new Element(Q, "Q", Element.INLINE, BODY, null),
370         };
371         ELEMENTS_ARRAY['R'-'A'] = new Element[] {
372             // RB
373
new Element(RB, "RB", Element.INLINE, RUBY, new short[]{RB}),
374             // RBC
375
new Element(RBC, "RBC", 0, RUBY, null),
376             // RP
377
new Element(RP, "RP", Element.INLINE, RUBY, new short[]{RB}),
378             // RT
379
new Element(RT, "RT", Element.INLINE, RUBY, new short[]{RB,RP}),
380             // RTC
381
new Element(RTC, "RTC", 0, RUBY, new short[]{RBC}),
382             // RUBY
383
new Element(RUBY, "RUBY", 0, BODY, new short[]{RUBY}),
384         };
385         ELEMENTS_ARRAY['S'-'A'] = new Element[] {
386             // S
387
new Element(S, "S", 0, BODY, null),
388             // SAMP - - (%inline;)*
389
new Element(SAMP, "SAMP", Element.INLINE, BODY, null),
390             // SCRIPT - - %Script;
391
new Element(SCRIPT, "SCRIPT", Element.SPECIAL, new short[]{HEAD,BODY}, null),
392             // SELECT - - (OPTGROUP|OPTION)+
393
new Element(SELECT, "SELECT", 0, BODY, new short[]{SELECT}),
394             // SMALL - - (%inline;)*
395
new Element(SMALL, "SMALL", Element.INLINE, BODY, null),
396             // SOUND
397
new Element(SOUND, "SOUND", Element.EMPTY, HEAD, null),
398             // SPACER
399
new Element(SPACER, "SPACER", Element.EMPTY, BODY, null),
400             // SPAN - - (%inline;)*
401
new Element(SPAN, "SPAN", Element.CONTAINER, BODY, null),
402             // STRIKE
403
new Element(STRIKE, "STRIKE", Element.INLINE, BODY, null),
404             // STRONG - - (%inline;)*
405
new Element(STRONG, "STRONG", Element.INLINE, BODY, null),
406             // STYLE - - %StyleSheet;
407
new Element(STYLE, "STYLE", Element.SPECIAL, new short[]{HEAD,BODY}, new short[]{STYLE,TITLE,META}),
408             // SUB - - (%inline;)*
409
new Element(SUB, "SUB", Element.INLINE, BODY, null),
410             // SUP - - (%inline;)*
411
new Element(SUP, "SUP", Element.INLINE, BODY, null),
412         };
413         ELEMENTS_ARRAY['T'-'A'] = new Element[] {
414             // TABLE - - (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)
415
new Element(TABLE, "TABLE", Element.BLOCK|Element.CONTAINER, BODY, null),
416             // TBODY O O (TR)+
417
new Element(TBODY, "TBODY", 0, TABLE, new short[]{THEAD,TD,TH,TR,COLGROUP}),
418             // TD - O (%flow;)*
419
new Element(TD, "TD", 0, TR, TABLE, new short[]{TD,TH}),
420             // TEXTAREA - - (#PCDATA)
421
new Element(TEXTAREA, "TEXTAREA", Element.SPECIAL, BODY, null),
422             // TFOOT - O (TR)+
423
new Element(TFOOT, "TFOOT", 0, TABLE, new short[]{THEAD,TBODY,TD,TH,TR}),
424             // TH - O (%flow;)*
425
new Element(TH, "TH", 0, TR, TABLE, new short[]{TD,TH}),
426             // THEAD - O (TR)+
427
new Element(THEAD, "THEAD", 0, TABLE, new short[]{COLGROUP}),
428             // TITLE - - (#PCDATA) -(%head.misc;)
429
new Element(TITLE, "TITLE", 0, new short[]{HEAD,BODY}, null),
430             // TR - O (TH|TD)+
431
new Element(TR, "TR", Element.BLOCK, TABLE, new short[]{TD,TH,TR,COLGROUP}),
432             // TT - - (%inline;)*
433
new Element(TT, "TT", Element.INLINE, BODY, null),
434         };
435         ELEMENTS_ARRAY['U'-'A'] = new Element[] {
436             // U,
437
new Element(U, "U", Element.INLINE, BODY, null),
438             // UL - - (LI)+
439
new Element(UL, "UL", Element.BLOCK, BODY, null),
440         };
441         ELEMENTS_ARRAY['V'-'A'] = new Element[] {
442             // VAR - - (%inline;)*
443
new Element(VAR, "VAR", Element.INLINE, BODY, null),
444         };
445         ELEMENTS_ARRAY['W'-'A'] = new Element[] {
446             // WBR
447
new Element(WBR, "WBR", Element.EMPTY, BODY, null),
448         };
449         ELEMENTS_ARRAY['X'-'A'] = new Element[] {
450             // XML
451
new Element(XML, "XML", 0, BODY, null),
452             // XMP
453
new Element(XMP, "XMP", Element.SPECIAL, BODY, null),
454         };
455
456         // keep contiguous list of elements for lookups by code
457
for (int i = 0; i < ELEMENTS_ARRAY.length; i++) {
458             Element[] elements = ELEMENTS_ARRAY[i];
459             if (elements != null) {
460                 for (int j = 0; j < elements.length; j++) {
461                     Element element = elements[j];
462                     ELEMENTS.addElement(element);
463                 }
464             }
465         }
466         ELEMENTS.addElement(NO_SUCH_ELEMENT);
467
468         // initialize cross references to parent elements
469
for (int i = 0; i < ELEMENTS.size; i++) {
470             Element element = ELEMENTS.data[i];
471             if (element.parentCodes != null) {
472                 element.parent = new Element[element.parentCodes.length];
473                 for (int j = 0; j < element.parentCodes.length; j++) {
474                     element.parent[j] = ELEMENTS.data[element.parentCodes[j]];
475                 }
476                 element.parentCodes = null;
477             }
478         }
479
480     } // <clinit>()
481

482     //
483
// Public static methods
484
//
485

486     /**
487      * Returns the element information for the specified element code.
488      *
489      * @param code The element code.
490      */

491     public static final Element getElement(short code) {
492         return ELEMENTS.data[code];
493     } // getElement(short):Element
494

495     /**
496      * Returns the element information for the specified element name.
497      *
498      * @param ename The element name.
499      */

500     public static final Element getElement(String JavaDoc ename) {
501         return getElement(ename, NO_SUCH_ELEMENT);
502     } // getElement(String):Element
503

504     /**
505      * Returns the element information for the specified element name.
506      *
507      * @param ename The element name.
508      * @param element The default element to return if not found.
509      */

510     public static final Element getElement(String JavaDoc ename, Element element) {
511
512         if (ename.length() > 0) {
513             int c = ename.charAt(0);
514             if (c >= 'a' && c <= 'z') {
515                 c = 'A' + c - 'a';
516             }
517             if (c >= 'A' && c <= 'Z') {
518                 Element[] elements = ELEMENTS_ARRAY[c - 'A'];
519                 if (elements != null) {
520                     for (int i = 0; i < elements.length; i++) {
521                         Element elem = elements[i];
522                         if (elem.name.equalsIgnoreCase(ename)) {
523                             return elem;
524                         }
525                     }
526                 }
527             }
528         }
529         return element;
530
531     } // getElement(String):Element
532

533     //
534
// Classes
535
//
536

537     /**
538      * Element information.
539      *
540      * @author Andy Clark
541      */

542     public static class Element {
543
544         //
545
// Constants
546
//
547

548         /** Inline element. */
549         public static final int INLINE = 0x01;
550
551         /** Block element. */
552         public static final int BLOCK = 0x02;
553
554         /** Empty element. */
555         public static final int EMPTY = 0x04;
556
557         /** Container element. */
558         public static final int CONTAINER = 0x08;
559
560         /** Special element. */
561         public static final int SPECIAL = 0x10;
562
563         /** Empty array. */
564         private static final short[] EMPTY_ARRAY = new short[0];
565
566         //
567
// Data
568
//
569

570         /** The element code. */
571         public short code;
572
573         /** The element name. */
574         public String JavaDoc name;
575
576         /** Informational flags. */
577         public int flags;
578
579         /** Parent elements. */
580         public short[] parentCodes;
581
582         /** Parent elements. */
583         public Element[] parent;
584
585         /** The bounding element code. */
586         public short bounds;
587
588         /** List of elements this element can close. */
589         public short[] closes;
590
591         //
592
// Constructors
593
//
594

595         /**
596          * Constructs an element object.
597          *
598          * @param code The element code.
599          * @param name The element name.
600          * @param flags Informational flags
601          * @param parent Natural closing parent name.
602          * @param closes List of elements this element can close.
603          */

604         public Element(short code, String JavaDoc name, int flags,
605                        short parent, short[] closes) {
606             this(code, name, flags, new short[]{parent}, (short)-1, closes);
607         } // <init>(short,String,int,short,short[]);
608

609         /**
610          * Constructs an element object.
611          *
612          * @param code The element code.
613          * @param name The element name.
614          * @param flags Informational flags
615          * @param parent Natural closing parent name.
616          * @param closes List of elements this element can close.
617          */

618         public Element(short code, String JavaDoc name, int flags,
619                        short parent, short bounds, short[] closes) {
620             this(code, name, flags, new short[]{parent}, bounds, closes);
621         } // <init>(short,String,int,short,short,short[])
622

623         /**
624          * Constructs an element object.
625          *
626          * @param code The element code.
627          * @param name The element name.
628          * @param flags Informational flags
629          * @param parents Natural closing parent names.
630          * @param closes List of elements this element can close.
631          */

632         public Element(short code, String JavaDoc name, int flags,
633                        short[] parents, short[] closes) {
634             this(code, name, flags, parents, (short)-1, closes);
635         } // <init>(short,String,int,short[],short[])
636

637         /**
638          * Constructs an element object.
639          *
640          * @param code The element code.
641          * @param name The element name.
642          * @param flags Informational flags
643          * @param parents Natural closing parent names.
644          * @param closes List of elements this element can close.
645          */

646         public Element(short code, String JavaDoc name, int flags,
647                        short[] parents, short bounds, short[] closes) {
648             this.code = code;
649             this.name = name;
650             this.flags = flags;
651             this.parentCodes = parents;
652             this.parent = null;
653             this.bounds = bounds;
654             this.closes = closes;
655         } // <init>(short,String,int,short[],short,short[])
656

657         //
658
// Public methods
659
//
660

661         /** Returns true if this element is an inline element. */
662         public final boolean isInline() {
663             return (flags & INLINE) != 0;
664         } // isInline():boolean
665

666         /** Returns true if this element is a block element. */
667         public final boolean isBlock() {
668             return (flags & BLOCK) != 0;
669         } // isBlock():boolean
670

671         /** Returns true if this element is an empty element. */
672         public final boolean isEmpty() {
673             return (flags & EMPTY) != 0;
674         } // isEmpty():boolean
675

676         /** Returns true if this element is a container element. */
677         public final boolean isContainer() {
678             return (flags & CONTAINER) != 0;
679         } // isContainer():boolean
680

681         /**
682          * Returns true if this element is special -- if its content
683          * should be parsed ignoring markup.
684          */

685         public final boolean isSpecial() {
686             return (flags & SPECIAL) != 0;
687         } // isSpecial():boolean
688

689         /**
690          * Returns true if this element can close the specified Element.
691          *
692          * @param tag The element.
693          */

694         public boolean closes(short tag) {
695
696             if (closes != null) {
697                 for (int i = 0; i < closes.length; i++) {
698                     if (closes[i] == tag) {
699                         return true;
700                     }
701                 }
702             }
703             return false;
704
705         } // closes(short):boolean
706

707         //
708
// Object methods
709
//
710

711         /** Returns a hash code for this object. */
712         public int hashCode() {
713             return name.hashCode();
714         } // hashCode():int
715

716         /** Returns true if the objects are equal. */
717         public boolean equals(Object JavaDoc o) {
718             return name.equals(o);
719         } // equals(Object):boolean
720

721     } // class Element
722

723     /** Unsynchronized list of elements. */
724     public static class ElementList {
725
726         //
727
// Data
728
//
729

730         /** The size of the list. */
731         public int size;
732
733         /** The data in the list. */
734         public Element[] data = new Element[120];
735
736         //
737
// Public methods
738
//
739

740         /** Adds an element to list, resizing if necessary. */
741         public void addElement(Element element) {
742             if (size == data.length) {
743                 Element[] newarray = new Element[size + 20];
744                 System.arraycopy(data, 0, newarray, 0, size);
745                 data = newarray;
746             }
747             data[size++] = element;
748         } // addElement(Element)
749

750     } // class Element
751

752 } // class HTMLElements
753
Popular Tags