Source code: com/port80/html/tidy/DOMElementImpl.java
1 /*
2 * @(#)DOMElementImpl.java 1.11 2000/08/16
3 *
4 */
5
6 package com.port80.html.tidy;
7
8 import org.w3c.dom.DOMException;
9
10
11 /**
12 *
13 * DOMElementImpl
14 *
15 * (c) 1998-2000 (W3C) MIT, INRIA, Keio University
16 * See Tidy.java for the copyright notice.
17 * Derived from <a href="http://www.w3.org/People/Raggett/tidy">
18 * HTML Tidy Release 4 Aug 2000</a>
19 *
20 * @author Dave Raggett <dsr@w3.org>
21 * @author Andy Quick <ac.quick@sympatico.ca> (translation to Java)
22 * @version 1.4, 1999/09/04 DOM Support
23 * @version 1.5, 1999/10/23 Tidy Release 27 Sep 1999
24 * @version 1.6, 1999/11/01 Tidy Release 22 Oct 1999
25 * @version 1.7, 1999/12/06 Tidy Release 30 Nov 1999
26 * @version 1.8, 2000/01/22 Tidy Release 13 Jan 2000
27 * @version 1.9, 2000/06/03 Tidy Release 30 Apr 2000
28 * @version 1.10, 2000/07/22 Tidy Release 8 Jul 2000
29 * @version 1.11, 2000/08/16 Tidy Release 4 Aug 2000
30 */
31
32 public class DOMElementImpl extends DOMNodeImpl
33 implements org.w3c.dom.Element {
34
35 protected DOMElementImpl(Node adaptee)
36 {
37 super(adaptee);
38 }
39
40
41 /* --------------------- DOM ---------------------------- */
42
43 /**
44 * @see org.w3c.dom.Node#getNodeType
45 */
46 public short getNodeType()
47 {
48 return org.w3c.dom.Node.ELEMENT_NODE;
49 }
50
51 /**
52 * @see org.w3c.dom.Element#getTagName
53 */
54 public String getTagName()
55 {
56 return super.getNodeName();
57 }
58
59 /**
60 * @see org.w3c.dom.Element#getAttribute
61 */
62 public String getAttribute(String name)
63 {
64 if (this.adaptee == null)
65 return null;
66
67 AttVal att = this.adaptee.attributes;
68 while (att != null) {
69 if (att.attribute.equals(name)) break;
70 att = att.next;
71 }
72 if (att != null)
73 return att.value;
74 else
75 return "";
76 }
77
78 /**
79 * @see org.w3c.dom.Element#setAttribute
80 */
81 public void setAttribute(String name,
82 String value)
83 throws DOMException
84 {
85 if (this.adaptee == null)
86 return;
87
88 AttVal att = this.adaptee.attributes;
89 while (att != null) {
90 if (att.attribute.equals(name)) break;
91 att = att.next;
92 }
93 if (att != null) {
94 att.value = value;
95 } else {
96 att = new AttVal(null, null, '"', name, value);
97 att.dict =
98 AttributeTable.getDefaultAttributeTable().findAttribute(att);
99 if (this.adaptee.attributes == null) {
100 this.adaptee.attributes = att;
101 } else {
102 att.next = this.adaptee.attributes;
103 this.adaptee.attributes = att;
104 }
105 }
106 }
107
108 /**
109 * @see org.w3c.dom.Element#removeAttribute
110 */
111 public void removeAttribute(String name)
112 throws DOMException
113 {
114 if (this.adaptee == null)
115 return;
116
117 AttVal att = this.adaptee.attributes;
118 AttVal pre = null;
119 while (att != null) {
120 if (att.attribute.equals(name)) break;
121 pre = att;
122 att = att.next;
123 }
124 if (att != null) {
125 if (pre == null) {
126 this.adaptee.attributes = att.next;
127 } else {
128 pre.next = att.next;
129 }
130 }
131 }
132
133 /**
134 * @see org.w3c.dom.Element#getAttributeNode
135 */
136 public org.w3c.dom.Attr getAttributeNode(String name)
137 {
138 if (this.adaptee == null)
139 return null;
140
141 AttVal att = this.adaptee.attributes;
142 while (att != null) {
143 if (att.attribute.equals(name)) break;
144 att = att.next;
145 }
146 if (att != null)
147 return att.getAdapter();
148 else
149 return null;
150 }
151
152 /**
153 * @see org.w3c.dom.Element#setAttributeNode
154 */
155 public org.w3c.dom.Attr setAttributeNode(org.w3c.dom.Attr newAttr)
156 throws DOMException
157 {
158 if (newAttr == null)
159 return null;
160 if (!(newAttr instanceof DOMAttrImpl)) {
161 throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR,
162 "newAttr not instanceof DOMAttrImpl");
163 }
164
165 DOMAttrImpl newatt = (DOMAttrImpl)newAttr;
166 String name = newatt.avAdaptee.attribute;
167 org.w3c.dom.Attr result = null;
168
169 AttVal att = this.adaptee.attributes;
170 while (att != null) {
171 if (att.attribute.equals(name)) break;
172 att = att.next;
173 }
174 if (att != null) {
175 result = att.getAdapter();
176 att.adapter = newAttr;
177 } else {
178 if (this.adaptee.attributes == null) {
179 this.adaptee.attributes = newatt.avAdaptee;
180 } else {
181 newatt.avAdaptee.next = this.adaptee.attributes;
182 this.adaptee.attributes = newatt.avAdaptee;
183 }
184 }
185 return result;
186 }
187
188 /**
189 * @see org.w3c.dom.Element#removeAttributeNode
190 */
191 public org.w3c.dom.Attr removeAttributeNode(org.w3c.dom.Attr oldAttr)
192 throws DOMException
193 {
194 if (oldAttr == null)
195 return null;
196
197 org.w3c.dom.Attr result = null;
198 AttVal att = this.adaptee.attributes;
199 AttVal pre = null;
200 while (att != null) {
201 if (att.getAdapter() == oldAttr) break;
202 pre = att;
203 att = att.next;
204 }
205 if (att != null) {
206 if (pre == null) {
207 this.adaptee.attributes = att.next;
208 } else {
209 pre.next = att.next;
210 }
211 result = oldAttr;
212 } else {
213 throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
214 "oldAttr not found");
215 }
216 return result;
217 }
218
219 /**
220 * @see org.w3c.dom.Element#getElementsByTagName
221 */
222 public org.w3c.dom.NodeList getElementsByTagName(String name)
223 {
224 return new DOMNodeListByTagNameImpl(this.adaptee, name);
225 }
226
227 /**
228 * @see org.w3c.dom.Element#normalize
229 */
230 public void normalize()
231 {
232 // NOT SUPPORTED
233 }
234
235 /**
236 * DOM2 - not implemented.
237 */
238 public String getAttributeNS(String namespaceURI, String localName)
239 {
240 return null;
241 }
242
243 /**
244 * DOM2 - not implemented.
245 * @exception org.w3c.dom.DOMException
246 */
247 public void setAttributeNS(String namespaceURI,
248 String qualifiedName,
249 String value)
250 throws org.w3c.dom.DOMException
251 {
252 }
253
254 /**
255 * DOM2 - not implemented.
256 * @exception org.w3c.dom.DOMException
257 */
258 public void removeAttributeNS(String namespaceURI, String localName)
259 throws org.w3c.dom.DOMException
260 {
261 }
262
263 /**
264 * DOM2 - not implemented.
265 */
266 public org.w3c.dom.Attr getAttributeNodeNS(String namespaceURI,
267 String localName)
268 {
269 return null;
270 }
271
272 /**
273 * DOM2 - not implemented.
274 * @exception org.w3c.dom.DOMException
275 */
276 public org.w3c.dom.Attr setAttributeNodeNS(org.w3c.dom.Attr newAttr)
277 throws org.w3c.dom.DOMException
278 {
279 return null;
280 }
281
282 /**
283 * DOM2 - not implemented.
284 */
285 public org.w3c.dom.NodeList getElementsByTagNameNS(String namespaceURI,
286 String localName)
287 {
288 return null;
289 }
290
291 /**
292 * DOM2 - not implemented.
293 */
294 public boolean hasAttribute(String name)
295 {
296 return false;
297 }
298
299 /**
300 * DOM2 - not implemented.
301 */
302 public boolean hasAttributeNS(String namespaceURI,
303 String localName)
304 {
305 return false;
306 }
307
308 }