001: /*
002: * @(#)DOMElementImpl.java 1.11 2000/08/16
003: *
004: */
005:
006: package org.w3c.tidy;
007:
008: import org.w3c.dom.DOMException;
009:
010: /**
011: *
012: * DOMElementImpl
013: *
014: * (c) 1998-2000 (W3C) MIT, INRIA, Keio University
015: * See Tidy.java for the copyright notice.
016: * Derived from <a href="http://www.w3.org/People/Raggett/tidy">
017: * HTML Tidy Release 4 Aug 2000</a>
018: *
019: * @author Dave Raggett <dsr@w3.org>
020: * @author Andy Quick <ac.quick@sympatico.ca> (translation to Java)
021: * @version 1.4, 1999/09/04 DOM Support
022: * @version 1.5, 1999/10/23 Tidy Release 27 Sep 1999
023: * @version 1.6, 1999/11/01 Tidy Release 22 Oct 1999
024: * @version 1.7, 1999/12/06 Tidy Release 30 Nov 1999
025: * @version 1.8, 2000/01/22 Tidy Release 13 Jan 2000
026: * @version 1.9, 2000/06/03 Tidy Release 30 Apr 2000
027: * @version 1.10, 2000/07/22 Tidy Release 8 Jul 2000
028: * @version 1.11, 2000/08/16 Tidy Release 4 Aug 2000
029: */
030:
031: public class DOMElementImpl extends DOMNodeImpl implements
032: org.w3c.dom.Element {
033:
034: protected DOMElementImpl(Node adaptee) {
035: super (adaptee);
036: }
037:
038: /* --------------------- DOM ---------------------------- */
039:
040: /**
041: * @see org.w3c.dom.Node#getNodeType
042: */
043: public short getNodeType() {
044: return org.w3c.dom.Node.ELEMENT_NODE;
045: }
046:
047: /**
048: * @see org.w3c.dom.Element#getTagName
049: */
050: public String getTagName() {
051: return super .getNodeName();
052: }
053:
054: /**
055: * @see org.w3c.dom.Element#getAttribute
056: */
057: public String getAttribute(String name) {
058: if (this .adaptee == null)
059: return null;
060:
061: AttVal att = this .adaptee.attributes;
062: while (att != null) {
063: if (att.attribute.equals(name))
064: break;
065: att = att.next;
066: }
067: if (att != null)
068: return att.value;
069: else
070: return "";
071: }
072:
073: /**
074: * @see org.w3c.dom.Element#setAttribute
075: */
076: public void setAttribute(String name, String value)
077: throws DOMException {
078: if (this .adaptee == null)
079: return;
080:
081: AttVal att = this .adaptee.attributes;
082: while (att != null) {
083: if (att.attribute.equals(name))
084: break;
085: att = att.next;
086: }
087: if (att != null) {
088: att.value = value;
089: } else {
090: att = new AttVal(null, null, (int) '"', name, value);
091: att.dict = AttributeTable.getDefaultAttributeTable()
092: .findAttribute(att);
093: if (this .adaptee.attributes == null) {
094: this .adaptee.attributes = att;
095: } else {
096: att.next = this .adaptee.attributes;
097: this .adaptee.attributes = att;
098: }
099: }
100: }
101:
102: /**
103: * @see org.w3c.dom.Element#removeAttribute
104: */
105: public void removeAttribute(String name) throws DOMException {
106: if (this .adaptee == null)
107: return;
108:
109: AttVal att = this .adaptee.attributes;
110: AttVal pre = null;
111: while (att != null) {
112: if (att.attribute.equals(name))
113: break;
114: pre = att;
115: att = att.next;
116: }
117: if (att != null) {
118: if (pre == null) {
119: this .adaptee.attributes = att.next;
120: } else {
121: pre.next = att.next;
122: }
123: }
124: }
125:
126: /**
127: * @see org.w3c.dom.Element#getAttributeNode
128: */
129: public org.w3c.dom.Attr getAttributeNode(String name) {
130: if (this .adaptee == null)
131: return null;
132:
133: AttVal att = this .adaptee.attributes;
134: while (att != null) {
135: if (att.attribute.equals(name))
136: break;
137: att = att.next;
138: }
139: if (att != null)
140: return att.getAdapter();
141: else
142: return null;
143: }
144:
145: /**
146: * @see org.w3c.dom.Element#setAttributeNode
147: */
148: public org.w3c.dom.Attr setAttributeNode(org.w3c.dom.Attr newAttr)
149: throws DOMException {
150: if (newAttr == null)
151: return null;
152: if (!(newAttr instanceof DOMAttrImpl)) {
153: throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR,
154: "newAttr not instanceof DOMAttrImpl");
155: }
156:
157: DOMAttrImpl newatt = (DOMAttrImpl) newAttr;
158: String name = newatt.avAdaptee.attribute;
159: org.w3c.dom.Attr result = null;
160:
161: AttVal att = this .adaptee.attributes;
162: while (att != null) {
163: if (att.attribute.equals(name))
164: break;
165: att = att.next;
166: }
167: if (att != null) {
168: result = att.getAdapter();
169: att.adapter = newAttr;
170: } else {
171: if (this .adaptee.attributes == null) {
172: this .adaptee.attributes = newatt.avAdaptee;
173: } else {
174: newatt.avAdaptee.next = this .adaptee.attributes;
175: this .adaptee.attributes = newatt.avAdaptee;
176: }
177: }
178: return result;
179: }
180:
181: /**
182: * @see org.w3c.dom.Element#removeAttributeNode
183: */
184: public org.w3c.dom.Attr removeAttributeNode(org.w3c.dom.Attr oldAttr)
185: throws DOMException {
186: if (oldAttr == null)
187: return null;
188:
189: org.w3c.dom.Attr result = null;
190: AttVal att = this .adaptee.attributes;
191: AttVal pre = null;
192: while (att != null) {
193: if (att.getAdapter() == oldAttr)
194: break;
195: pre = att;
196: att = att.next;
197: }
198: if (att != null) {
199: if (pre == null) {
200: this .adaptee.attributes = att.next;
201: } else {
202: pre.next = att.next;
203: }
204: result = oldAttr;
205: } else {
206: throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
207: "oldAttr not found");
208: }
209: return result;
210: }
211:
212: /**
213: * @see org.w3c.dom.Element#getElementsByTagName
214: */
215: public org.w3c.dom.NodeList getElementsByTagName(String name) {
216: return new DOMNodeListByTagNameImpl(this .adaptee, name);
217: }
218:
219: /**
220: * @see org.w3c.dom.Element#normalize
221: */
222: public void normalize() {
223: // NOT SUPPORTED
224: }
225:
226: /**
227: * DOM2 - not implemented.
228: */
229: public String getAttributeNS(String namespaceURI, String localName) {
230: return null;
231: }
232:
233: /**
234: * DOM2 - not implemented.
235: * @exception org.w3c.dom.DOMException
236: */
237: public void setAttributeNS(String namespaceURI,
238: String qualifiedName, String value)
239: throws org.w3c.dom.DOMException {
240: }
241:
242: /**
243: * DOM2 - not implemented.
244: * @exception org.w3c.dom.DOMException
245: */
246: public void removeAttributeNS(String namespaceURI, String localName)
247: throws org.w3c.dom.DOMException {
248: }
249:
250: /**
251: * DOM2 - not implemented.
252: */
253: public org.w3c.dom.Attr getAttributeNodeNS(String namespaceURI,
254: String localName) {
255: return null;
256: }
257:
258: /**
259: * DOM2 - not implemented.
260: * @exception org.w3c.dom.DOMException
261: */
262: public org.w3c.dom.Attr setAttributeNodeNS(org.w3c.dom.Attr newAttr)
263: throws org.w3c.dom.DOMException {
264: return null;
265: }
266:
267: /**
268: * DOM2 - not implemented.
269: */
270: public org.w3c.dom.NodeList getElementsByTagNameNS(
271: String namespaceURI, String localName) {
272: return null;
273: }
274:
275: /**
276: * DOM2 - not implemented.
277: */
278: public boolean hasAttribute(String name) {
279: return false;
280: }
281:
282: /**
283: * DOM2 - not implemented.
284: */
285: public boolean hasAttributeNS(String namespaceURI, String localName) {
286: return false;
287: }
288:
289: }
|