KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > parser > Element


1 /*
2  * @(#)Element.java 1.9 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.text.html.parser;
9
10 import java.util.Hashtable JavaDoc;
11 import java.util.BitSet JavaDoc;
12 import java.io.*;
13
14 /**
15  * An element as described in a DTD using the ELEMENT construct.
16  * This is essentiall the description of a tag. It describes the
17  * type, content model, attributes, attribute types etc. It is used
18  * to correctly parse a document by the Parser.
19  *
20  * @see DTD
21  * @see AttributeList
22  * @version 1.9, 12/19/03
23  * @author Arthur van Hoff
24  */

25 public final
26 class Element implements DTDConstants JavaDoc, Serializable {
27     public int index;
28     public String JavaDoc name;
29     public boolean oStart;
30     public boolean oEnd;
31     public BitSet JavaDoc inclusions;
32     public BitSet JavaDoc exclusions;
33     public int type = ANY;
34     public ContentModel JavaDoc content;
35     public AttributeList JavaDoc atts;
36
37     static int maxIndex = 0;
38
39     /**
40      * A field to store user data. Mostly used to store
41      * style sheets.
42      */

43     public Object JavaDoc data;
44
45     Element() {
46     }
47
48     /**
49      * Create a new element.
50      */

51     Element(String JavaDoc name, int index) {
52     this.name = name;
53     this.index = index;
54     maxIndex = Math.max(maxIndex, index);
55     }
56
57     /**
58      * Get the name of the element.
59      */

60     public String JavaDoc getName() {
61     return name;
62     }
63
64     /**
65      * Return true if the start tag can be omitted.
66      */

67     public boolean omitStart() {
68     return oStart;
69     }
70
71     /**
72      * Return true if the end tag can be omitted.
73      */

74     public boolean omitEnd() {
75     return oEnd;
76     }
77
78     /**
79      * Get type.
80      */

81     public int getType() {
82     return type;
83     }
84
85     /**
86      * Get content model
87      */

88     public ContentModel JavaDoc getContent() {
89     return content;
90     }
91
92     /**
93      * Get the attributes.
94      */

95     public AttributeList JavaDoc getAttributes() {
96     return atts;
97     }
98
99     /**
100      * Get index.
101      */

102     public int getIndex() {
103     return index;
104     }
105
106     /**
107      * Check if empty
108      */

109     public boolean isEmpty() {
110     return type == EMPTY;
111     }
112
113     /**
114      * Convert to a string.
115      */

116     public String JavaDoc toString() {
117     return name;
118     }
119
120     /**
121      * Get an attribute by name.
122      */

123     public AttributeList JavaDoc getAttribute(String JavaDoc name) {
124     for (AttributeList JavaDoc a = atts ; a != null ; a = a.next) {
125         if (a.name.equals(name)) {
126         return a;
127         }
128     }
129     return null;
130     }
131
132     /**
133      * Get an attribute by value.
134      */

135     public AttributeList JavaDoc getAttributeByValue(String JavaDoc name) {
136     for (AttributeList JavaDoc a = atts ; a != null ; a = a.next) {
137         if ((a.values != null) && a.values.contains(name)) {
138         return a;
139         }
140     }
141     return null;
142     }
143
144
145     static Hashtable JavaDoc contentTypes = new Hashtable JavaDoc();
146
147     static {
148     contentTypes.put("CDATA", new Integer JavaDoc(CDATA));
149     contentTypes.put("RCDATA", new Integer JavaDoc(RCDATA));
150     contentTypes.put("EMPTY", new Integer JavaDoc(EMPTY));
151     contentTypes.put("ANY", new Integer JavaDoc(ANY));
152     }
153
154     public static int name2type(String JavaDoc nm) {
155     Integer JavaDoc val = (Integer JavaDoc)contentTypes.get(nm);
156     return (val != null) ? val.intValue() : 0;
157     }
158 }
159
Popular Tags