KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ccil > cowan > tagsoup > Element


1 // This file is part of TagSoup.
2
//
3
// This program is free software; you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation; either version 2 of the License, or
6
// (at your option) any later version. You may also distribute
7
// and/or modify it under version 2.1 of the Academic Free License.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12

13 package org.ccil.cowan.tagsoup;
14
15 /**
16 The internal representation of an actual element (not an element type).
17 An Element has an element type, attributes, and a successor Element
18 for use in constructing stacks and queues of Elements.
19 @see ElementType
20 @see AttributesImpl
21 */

22 public class Element {
23
24
25     private ElementType theType; // type of element
26
private AttributesImpl theAtts; // attributes of element
27
private Element theNext; // successor of element
28
private boolean preclosed; // this element has been preclosed
29

30     /**
31     Return an Element from a specified ElementType.
32     @param type The element type of the newly constructed element
33     @param defaultAttributes True if default attributes are wanted
34     */

35
36     public Element(ElementType type, boolean defaultAttributes) {
37         theType = type;
38         if (defaultAttributes) theAtts = new AttributesImpl(type.atts());
39         else theAtts = new AttributesImpl();
40         theNext = null;
41         preclosed = false;
42         }
43
44     /**
45     Return the element type.
46     @return The element type.
47     */

48
49     public ElementType type() { return theType; }
50
51     /**
52     Return the attributes as an AttributesImpl object.
53     Returning an AttributesImpl makes the attributes mutable.
54     @return The attributes
55     @see AttributesImpl
56     */

57     public AttributesImpl atts() { return theAtts; }
58
59     /**
60     Return the next element in an element stack or queue.
61     @return The next element
62     */

63
64     public Element next() { return theNext; }
65
66     /**
67     Change the next element in an element stack or queue.
68     @param next The new next element
69     */

70
71     public void setNext(Element next) { theNext = next; }
72
73     /**
74     Return the name of the element's type.
75     Convenience method.
76     @return The element type name
77     */

78
79     public String JavaDoc name() { return theType.name(); }
80
81     /**
82     Return the namespace name of the element's type.
83     Convenience method.
84     @return The element type namespace name
85     */

86
87     public String JavaDoc namespace() { return theType.namespace(); }
88
89     /**
90     Return the local name of the element's type.
91     Convenience method.
92     @return The element type local name
93     */

94
95     public String JavaDoc localName() { return theType.localName(); }
96
97     /**
98     Return the content model vector of the element's type.
99     Convenience method.
100     @return The content model vector
101     */

102
103     public int model() { return theType.model(); }
104
105     /**
106     Return the member-of vector of the element's type.
107     Convenience method.
108     @return The member-of vector
109     */

110
111     public int memberOf() { return theType.memberOf(); }
112
113     /**
114     Return the flags vector of the element's type.
115     Convenience method.
116     @return The flags vector
117     */

118
119     public int flags() { return theType.flags(); }
120
121     /**
122     Return the parent element type of the element's type.
123     Convenience method.
124     @return The parent element type
125     */

126
127     public ElementType parent() { return theType.parent(); }
128
129     /**
130     Return true if the type of this element can contain the type of
131     another element.
132     Convenience method.
133     @param other The other element
134     */

135
136     public boolean canContain(Element other) {
137         return theType.canContain(other.theType);
138         }
139
140
141     /**
142     Set an attribute and its value into this element.
143     @param name The attribute name (Qname)
144     @param type The attribute type
145     @param value The attribute value
146     */

147
148     public void setAttribute(String JavaDoc name, String JavaDoc type, String JavaDoc value) {
149         theType.setAttribute(theAtts, name, type, value);
150         }
151
152     /**
153     Make this element anonymous.
154     Remove any <tt>id</tt> or <tt>name</tt> attribute present
155     in the element's attributes.
156     */

157
158     public void anonymize() {
159         for (int i = theAtts.getLength() - 1; i >= 0; i--) {
160             if (theAtts.getType(i).equals("ID") ||
161                 theAtts.getQName(i).equals("name")) {
162                 theAtts.removeAttribute(i);
163                 }
164             }
165         }
166
167     /**
168     Clean the attributes of this element.
169     Attributes with null name (the name was ill-formed)
170     or null value (the attribute was present in the element type but
171     not in this actual element) are removed. Type BOOLEAN is
172     changed to type NMTOKEN at this time.
173     */

174
175     public void clean() {
176         for (int i = theAtts.getLength() - 1; i >= 0; i--) {
177             String JavaDoc name = theAtts.getLocalName(i);
178             if (theAtts.getValue(i) == null || name == null ||
179                     name.length() == 0) {
180                 theAtts.removeAttribute(i);
181                 continue;
182                 }
183             if (theAtts.getType(i).equals("BOOLEAN")) {
184                 theAtts.setType(i, "NMTOKEN");
185                 }
186             }
187         }
188
189     /**
190     Force this element to preclosed status, meaning that an end-tag has
191     been seen but the element cannot yet be closed for structural reasons.
192     */

193
194     public void preclose() {
195         preclosed = true;
196         }
197
198     /**
199     Return true if this element has been preclosed.
200     */

201
202     public boolean isPreclosed() {
203         return preclosed;
204         }
205
206     }
207
Popular Tags