2 * @(#)DOMNodeImpl.java 1.11 2000/08/16
6 package net.sourceforge.phpdt.tidy.w3c;
8 import org.w3c.dom.DOMException;
9 import sun.security.krb5.internal.n;
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>
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
32 public class DOMNodeImpl implements org.w3c.dom.Node {
34 protected Node adaptee;
36 protected DOMNodeImpl(Node adaptee)
38 this.adaptee = adaptee;
42 /* --------------------- DOM ---------------------------- */
45 * @see org.w3c.dom.Node#getNodeValue
47 public String getNodeValue() throws DOMException
49 String value = ""; //BAK 10/10/2000 replaced null
50 if (adaptee.type == Node.TextNode ||
51 adaptee.type == Node.CDATATag ||
52 adaptee.type == Node.CommentTag ||
53 adaptee.type == Node.ProcInsTag)
56 if (adaptee.textarray != null && adaptee.start < adaptee.end)
58 value = Lexer.getString(adaptee.textarray,
60 adaptee.end - adaptee.start);
67 * @see org.w3c.dom.Node#setNodeValue
69 public void setNodeValue(String nodeValue) throws DOMException
71 if (adaptee.type == Node.TextNode ||
72 adaptee.type == Node.CDATATag ||
73 adaptee.type == Node.CommentTag ||
74 adaptee.type == Node.ProcInsTag)
76 byte[] textarray = Lexer.getBytes(nodeValue);
77 adaptee.textarray = textarray;
79 adaptee.end = textarray.length;
84 * @see org.w3c.dom.Node#getNodeName
86 public String getNodeName()
88 return adaptee.element;
92 * @see org.w3c.dom.Node#getNodeType
94 public short getNodeType()
97 switch (adaptee.type) {
99 result = org.w3c.dom.Node.DOCUMENT_NODE;
101 case Node.DocTypeTag:
102 result = org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
104 case Node.CommentTag:
105 result = org.w3c.dom.Node.COMMENT_NODE;
107 case Node.ProcInsTag:
108 result = org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
111 result = org.w3c.dom.Node.TEXT_NODE;
114 result = org.w3c.dom.Node.CDATA_SECTION_NODE;
117 case Node.StartEndTag:
118 result = org.w3c.dom.Node.ELEMENT_NODE;
125 * @see org.w3c.dom.Node#getParentNode
127 public org.w3c.dom.Node getParentNode()
129 if (adaptee.parent != null)
130 return adaptee.parent.getAdapter();
136 * @see org.w3c.dom.Node#getChildNodes
138 public org.w3c.dom.NodeList getChildNodes()
140 return new DOMNodeListImpl(adaptee);
144 * @see org.w3c.dom.Node#getFirstChild
146 public org.w3c.dom.Node getFirstChild()
148 if (adaptee.content != null)
149 return adaptee.content.getAdapter();
155 * @see org.w3c.dom.Node#getLastChild
157 public org.w3c.dom.Node getLastChild()
159 if (adaptee.last != null)
160 return adaptee.last.getAdapter();
166 * @see org.w3c.dom.Node#getPreviousSibling
168 public org.w3c.dom.Node getPreviousSibling()
170 if (adaptee.prev != null)
171 return adaptee.prev.getAdapter();
177 * @see org.w3c.dom.Node#getNextSibling
179 public org.w3c.dom.Node getNextSibling()
181 if (adaptee.next != null)
182 return adaptee.next.getAdapter();
188 * @see org.w3c.dom.Node#getAttributes
190 public org.w3c.dom.NamedNodeMap getAttributes()
192 return new DOMAttrMapImpl(adaptee.attributes);
196 * @see org.w3c.dom.Node#getOwnerDocument
198 public org.w3c.dom.Document getOwnerDocument()
203 if (node != null && node.type == Node.RootNode)
206 for (node = this.adaptee;
207 node != null && node.type != Node.RootNode; node = node.parent);
210 return (org.w3c.dom.Document)node.getAdapter();
216 * @see org.w3c.dom.Node#insertBefore
218 public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild,
219 org.w3c.dom.Node refChild)
222 // TODO - handle newChild already in tree
224 if (newChild == null)
226 if (!(newChild instanceof DOMNodeImpl)) {
227 throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR,
228 "newChild not instanceof DOMNodeImpl");
230 DOMNodeImpl newCh = (DOMNodeImpl)newChild;
232 if (this.adaptee.type == Node.RootNode) {
233 if (newCh.adaptee.type != Node.DocTypeTag &&
234 newCh.adaptee.type != Node.ProcInsTag) {
235 throw new DOMExceptionImpl(DOMException.HIERARCHY_REQUEST_ERR,
236 "newChild cannot be a child of this node");
238 } else if (this.adaptee.type == Node.StartTag) {
239 if (newCh.adaptee.type != Node.StartTag &&
240 newCh.adaptee.type != Node.StartEndTag &&
241 newCh.adaptee.type != Node.CommentTag &&
242 newCh.adaptee.type != Node.TextNode &&
243 newCh.adaptee.type != Node.CDATATag) {
244 throw new DOMExceptionImpl(DOMException.HIERARCHY_REQUEST_ERR,
245 "newChild cannot be a child of this node");
248 if (refChild == null) {
249 Node.insertNodeAtEnd(this.adaptee, newCh.adaptee);
250 if (this.adaptee.type == Node.StartEndTag) {
251 this.adaptee.setType(Node.StartTag);
254 Node ref = this.adaptee.content;
255 while (ref != null) {
256 if (ref.getAdapter() == refChild) break;
260 throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
261 "refChild not found");
263 Node.insertNodeBeforeElement(ref, newCh.adaptee);
269 * @see org.w3c.dom.Node#replaceChild
271 public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild,
272 org.w3c.dom.Node oldChild)
275 // TODO - handle newChild already in tree
277 if (newChild == null)
279 if (!(newChild instanceof DOMNodeImpl)) {
280 throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR,
281 "newChild not instanceof DOMNodeImpl");
283 DOMNodeImpl newCh = (DOMNodeImpl)newChild;
285 if (this.adaptee.type == Node.RootNode) {
286 if (newCh.adaptee.type != Node.DocTypeTag &&
287 newCh.adaptee.type != Node.ProcInsTag) {
288 throw new DOMExceptionImpl(DOMException.HIERARCHY_REQUEST_ERR,
289 "newChild cannot be a child of this node");
291 } else if (this.adaptee.type == Node.StartTag) {
292 if (newCh.adaptee.type != Node.StartTag &&
293 newCh.adaptee.type != Node.StartEndTag &&
294 newCh.adaptee.type != Node.CommentTag &&
295 newCh.adaptee.type != Node.TextNode &&
296 newCh.adaptee.type != Node.CDATATag) {
297 throw new DOMExceptionImpl(DOMException.HIERARCHY_REQUEST_ERR,
298 "newChild cannot be a child of this node");
301 if (oldChild == null) {
302 throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
303 "oldChild not found");
306 Node ref = this.adaptee.content;
307 while (ref != null) {
308 if (ref.getAdapter() == oldChild) break;
312 throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
313 "oldChild not found");
315 newCh.adaptee.next = ref.next;
316 newCh.adaptee.prev = ref.prev;
317 newCh.adaptee.last = ref.last;
318 newCh.adaptee.parent = ref.parent;
319 newCh.adaptee.content = ref.content;
320 if (ref.parent != null) {
321 if (ref.parent.content == ref)
322 ref.parent.content = newCh.adaptee;
323 if (ref.parent.last == ref)
324 ref.parent.last = newCh.adaptee;
326 if (ref.prev != null) {
327 ref.prev.next = newCh.adaptee;
329 if (ref.next != null) {
330 ref.next.prev = newCh.adaptee;
332 for (n = ref.content; n != null; n = n.next) {
334 n.parent = newCh.adaptee;
341 * @see org.w3c.dom.Node#removeChild
343 public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild)
346 if (oldChild == null)
349 Node ref = this.adaptee.content;
350 while (ref != null) {
351 if (ref.getAdapter() == oldChild) break;
355 throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
356 "refChild not found");
358 Node.discardElement(ref);
360 if (this.adaptee.content == null
361 && this.adaptee.type == Node.StartTag) {
362 this.adaptee.setType(Node.StartEndTag);
369 * @see org.w3c.dom.Node#appendChild
371 public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild)
374 // TODO - handle newChild already in tree
376 if (newChild == null)
378 if (!(newChild instanceof DOMNodeImpl)) {
379 throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR,
380 "newChild not instanceof DOMNodeImpl");
382 DOMNodeImpl newCh = (DOMNodeImpl)newChild;
384 if (this.adaptee.type == Node.RootNode) {
385 if (newCh.adaptee.type != Node.DocTypeTag &&
386 newCh.adaptee.type != Node.ProcInsTag) {
387 throw new DOMExceptionImpl(DOMException.HIERARCHY_REQUEST_ERR,
388 "newChild cannot be a child of this node");
390 } else if (this.adaptee.type == Node.StartTag) {
391 if (newCh.adaptee.type != Node.StartTag &&
392 newCh.adaptee.type != Node.StartEndTag &&
393 newCh.adaptee.type != Node.CommentTag &&
394 newCh.adaptee.type != Node.TextNode &&
395 newCh.adaptee.type != Node.CDATATag) {
396 throw new DOMExceptionImpl(DOMException.HIERARCHY_REQUEST_ERR,
397 "newChild cannot be a child of this node");
400 Node.insertNodeAtEnd(this.adaptee, newCh.adaptee);
402 if (this.adaptee.type == Node.StartEndTag) {
403 this.adaptee.setType(Node.StartTag);
410 * @see org.w3c.dom.Node#hasChildNodes
412 public boolean hasChildNodes()
414 return (adaptee.content != null);
418 * @see org.w3c.dom.Node#cloneNode
420 public org.w3c.dom.Node cloneNode(boolean deep)
422 Node node = adaptee.cloneNode(deep);
424 return node.getAdapter();
428 * DOM2 - not implemented.
430 public void normalize()
435 * DOM2 - not implemented.
437 public boolean supports(String feature, String version)
439 return isSupported(feature, version);
443 * DOM2 - not implemented.
445 public String getNamespaceURI()
451 * DOM2 - not implemented.
453 public String getPrefix()
459 * DOM2 - not implemented.
461 public void setPrefix(String prefix)
467 * DOM2 - not implemented.
469 public String getLocalName()
475 * DOM2 - not implemented.
477 public boolean isSupported(String feature,String version) {
482 * DOM2 - @see org.w3c.dom.Node#hasAttributes
483 * contributed by dlp@users.sourceforge.net
485 public boolean hasAttributes()
487 return adaptee.attributes != null;