001: /*
002: * @(#)DOMNodeImpl.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: * DOMNodeImpl
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 DOMNodeImpl implements org.w3c.dom.Node {
032:
033: protected Node adaptee;
034:
035: protected DOMNodeImpl(Node adaptee) {
036: this .adaptee = adaptee;
037: }
038:
039: /* --------------------- DOM ---------------------------- */
040:
041: /**
042: * @see org.w3c.dom.Node#getNodeValue
043: */
044: public String getNodeValue() throws DOMException {
045: String value = ""; //BAK 10/10/2000 replaced null
046: if (adaptee.type == Node.TextNode
047: || adaptee.type == Node.CDATATag
048: || adaptee.type == Node.CommentTag
049: || adaptee.type == Node.ProcInsTag) {
050:
051: if (adaptee.textarray != null
052: && adaptee.start < adaptee.end) {
053: value = Lexer.getString(adaptee.textarray,
054: adaptee.start, adaptee.end - adaptee.start);
055: }
056: }
057: return value;
058: }
059:
060: /**
061: * @see org.w3c.dom.Node#setNodeValue
062: */
063: public void setNodeValue(String nodeValue) throws DOMException {
064: if (adaptee.type == Node.TextNode
065: || adaptee.type == Node.CDATATag
066: || adaptee.type == Node.CommentTag
067: || adaptee.type == Node.ProcInsTag) {
068: byte[] textarray = Lexer.getBytes(nodeValue);
069: adaptee.textarray = textarray;
070: adaptee.start = 0;
071: adaptee.end = textarray.length;
072: }
073: }
074:
075: /**
076: * @see org.w3c.dom.Node#getNodeName
077: */
078: public String getNodeName() {
079: return adaptee.element;
080: }
081:
082: /**
083: * @see org.w3c.dom.Node#getNodeType
084: */
085: public short getNodeType() {
086: short result = -1;
087: switch (adaptee.type) {
088: case Node.RootNode:
089: result = org.w3c.dom.Node.DOCUMENT_NODE;
090: break;
091: case Node.DocTypeTag:
092: result = org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
093: break;
094: case Node.CommentTag:
095: result = org.w3c.dom.Node.COMMENT_NODE;
096: break;
097: case Node.ProcInsTag:
098: result = org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
099: break;
100: case Node.TextNode:
101: result = org.w3c.dom.Node.TEXT_NODE;
102: break;
103: case Node.CDATATag:
104: result = org.w3c.dom.Node.CDATA_SECTION_NODE;
105: break;
106: case Node.StartTag:
107: case Node.StartEndTag:
108: result = org.w3c.dom.Node.ELEMENT_NODE;
109: break;
110: }
111: return result;
112: }
113:
114: /**
115: * @see org.w3c.dom.Node#getParentNode
116: */
117: public org.w3c.dom.Node getParentNode() {
118: if (adaptee.parent != null)
119: return adaptee.parent.getAdapter();
120: else
121: return null;
122: }
123:
124: /**
125: * @see org.w3c.dom.Node#getChildNodes
126: */
127: public org.w3c.dom.NodeList getChildNodes() {
128: return new DOMNodeListImpl(adaptee);
129: }
130:
131: /**
132: * @see org.w3c.dom.Node#getFirstChild
133: */
134: public org.w3c.dom.Node getFirstChild() {
135: if (adaptee.content != null)
136: return adaptee.content.getAdapter();
137: else
138: return null;
139: }
140:
141: /**
142: * @see org.w3c.dom.Node#getLastChild
143: */
144: public org.w3c.dom.Node getLastChild() {
145: if (adaptee.last != null)
146: return adaptee.last.getAdapter();
147: else
148: return null;
149: }
150:
151: /**
152: * @see org.w3c.dom.Node#getPreviousSibling
153: */
154: public org.w3c.dom.Node getPreviousSibling() {
155: if (adaptee.prev != null)
156: return adaptee.prev.getAdapter();
157: else
158: return null;
159: }
160:
161: /**
162: * @see org.w3c.dom.Node#getNextSibling
163: */
164: public org.w3c.dom.Node getNextSibling() {
165: if (adaptee.next != null)
166: return adaptee.next.getAdapter();
167: else
168: return null;
169: }
170:
171: /**
172: * @see org.w3c.dom.Node#getAttributes
173: */
174: public org.w3c.dom.NamedNodeMap getAttributes() {
175: return new DOMAttrMapImpl(adaptee.attributes);
176: }
177:
178: /**
179: * @see org.w3c.dom.Node#getOwnerDocument
180: */
181: public org.w3c.dom.Document getOwnerDocument() {
182: Node node;
183:
184: node = this .adaptee;
185: if (node != null && node.type == Node.RootNode)
186: return null;
187:
188: for (node = this .adaptee; node != null
189: && node.type != Node.RootNode; node = node.parent)
190: ;
191:
192: if (node != null)
193: return (org.w3c.dom.Document) node.getAdapter();
194: else
195: return null;
196: }
197:
198: /**
199: * @see org.w3c.dom.Node#insertBefore
200: */
201: public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild,
202: org.w3c.dom.Node refChild) throws DOMException {
203: // TODO - handle newChild already in tree
204:
205: if (newChild == null)
206: return null;
207: if (!(newChild instanceof DOMNodeImpl)) {
208: throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR,
209: "newChild not instanceof DOMNodeImpl");
210: }
211: DOMNodeImpl newCh = (DOMNodeImpl) newChild;
212:
213: if (this .adaptee.type == Node.RootNode) {
214: if (newCh.adaptee.type != Node.DocTypeTag
215: && newCh.adaptee.type != Node.ProcInsTag) {
216: throw new DOMExceptionImpl(
217: DOMException.HIERARCHY_REQUEST_ERR,
218: "newChild cannot be a child of this node");
219: }
220: } else if (this .adaptee.type == Node.StartTag) {
221: if (newCh.adaptee.type != Node.StartTag
222: && newCh.adaptee.type != Node.StartEndTag
223: && newCh.adaptee.type != Node.CommentTag
224: && newCh.adaptee.type != Node.TextNode
225: && newCh.adaptee.type != Node.CDATATag) {
226: throw new DOMExceptionImpl(
227: DOMException.HIERARCHY_REQUEST_ERR,
228: "newChild cannot be a child of this node");
229: }
230: }
231: if (refChild == null) {
232: Node.insertNodeAtEnd(this .adaptee, newCh.adaptee);
233: if (this .adaptee.type == Node.StartEndTag) {
234: this .adaptee.setType(Node.StartTag);
235: }
236: } else {
237: Node ref = this .adaptee.content;
238: while (ref != null) {
239: if (ref.getAdapter() == refChild)
240: break;
241: ref = ref.next;
242: }
243: if (ref == null) {
244: throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
245: "refChild not found");
246: }
247: Node.insertNodeBeforeElement(ref, newCh.adaptee);
248: }
249: return newChild;
250: }
251:
252: /**
253: * @see org.w3c.dom.Node#replaceChild
254: */
255: public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild,
256: org.w3c.dom.Node oldChild) throws DOMException {
257: // TODO - handle newChild already in tree
258:
259: if (newChild == null)
260: return null;
261: if (!(newChild instanceof DOMNodeImpl)) {
262: throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR,
263: "newChild not instanceof DOMNodeImpl");
264: }
265: DOMNodeImpl newCh = (DOMNodeImpl) newChild;
266:
267: if (this .adaptee.type == Node.RootNode) {
268: if (newCh.adaptee.type != Node.DocTypeTag
269: && newCh.adaptee.type != Node.ProcInsTag) {
270: throw new DOMExceptionImpl(
271: DOMException.HIERARCHY_REQUEST_ERR,
272: "newChild cannot be a child of this node");
273: }
274: } else if (this .adaptee.type == Node.StartTag) {
275: if (newCh.adaptee.type != Node.StartTag
276: && newCh.adaptee.type != Node.StartEndTag
277: && newCh.adaptee.type != Node.CommentTag
278: && newCh.adaptee.type != Node.TextNode
279: && newCh.adaptee.type != Node.CDATATag) {
280: throw new DOMExceptionImpl(
281: DOMException.HIERARCHY_REQUEST_ERR,
282: "newChild cannot be a child of this node");
283: }
284: }
285: if (oldChild == null) {
286: throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
287: "oldChild not found");
288: } else {
289: Node n;
290: Node ref = this .adaptee.content;
291: while (ref != null) {
292: if (ref.getAdapter() == oldChild)
293: break;
294: ref = ref.next;
295: }
296: if (ref == null) {
297: throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
298: "oldChild not found");
299: }
300: newCh.adaptee.next = ref.next;
301: newCh.adaptee.prev = ref.prev;
302: newCh.adaptee.last = ref.last;
303: newCh.adaptee.parent = ref.parent;
304: newCh.adaptee.content = ref.content;
305: if (ref.parent != null) {
306: if (ref.parent.content == ref)
307: ref.parent.content = newCh.adaptee;
308: if (ref.parent.last == ref)
309: ref.parent.last = newCh.adaptee;
310: }
311: if (ref.prev != null) {
312: ref.prev.next = newCh.adaptee;
313: }
314: if (ref.next != null) {
315: ref.next.prev = newCh.adaptee;
316: }
317: for (n = ref.content; n != null; n = n.next) {
318: if (n.parent == ref)
319: n.parent = newCh.adaptee;
320: }
321: }
322: return oldChild;
323: }
324:
325: /**
326: * @see org.w3c.dom.Node#removeChild
327: */
328: public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild)
329: throws DOMException {
330: if (oldChild == null)
331: return null;
332:
333: Node ref = this .adaptee.content;
334: while (ref != null) {
335: if (ref.getAdapter() == oldChild)
336: break;
337: ref = ref.next;
338: }
339: if (ref == null) {
340: throw new DOMExceptionImpl(DOMException.NOT_FOUND_ERR,
341: "refChild not found");
342: }
343: Node.discardElement(ref);
344:
345: if (this .adaptee.content == null
346: && this .adaptee.type == Node.StartTag) {
347: this .adaptee.setType(Node.StartEndTag);
348: }
349:
350: return oldChild;
351: }
352:
353: /**
354: * @see org.w3c.dom.Node#appendChild
355: */
356: public org.w3c.dom.Node appendChild(org.w3c.dom.Node newChild)
357: throws DOMException {
358: // TODO - handle newChild already in tree
359:
360: if (newChild == null)
361: return null;
362: if (!(newChild instanceof DOMNodeImpl)) {
363: throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR,
364: "newChild not instanceof DOMNodeImpl");
365: }
366: DOMNodeImpl newCh = (DOMNodeImpl) newChild;
367:
368: if (this .adaptee.type == Node.RootNode) {
369: if (newCh.adaptee.type != Node.DocTypeTag
370: && newCh.adaptee.type != Node.ProcInsTag) {
371: throw new DOMExceptionImpl(
372: DOMException.HIERARCHY_REQUEST_ERR,
373: "newChild cannot be a child of this node");
374: }
375: } else if (this .adaptee.type == Node.StartTag) {
376: if (newCh.adaptee.type != Node.StartTag
377: && newCh.adaptee.type != Node.StartEndTag
378: && newCh.adaptee.type != Node.CommentTag
379: && newCh.adaptee.type != Node.TextNode
380: && newCh.adaptee.type != Node.CDATATag) {
381: throw new DOMExceptionImpl(
382: DOMException.HIERARCHY_REQUEST_ERR,
383: "newChild cannot be a child of this node");
384: }
385: }
386: Node.insertNodeAtEnd(this .adaptee, newCh.adaptee);
387:
388: if (this .adaptee.type == Node.StartEndTag) {
389: this .adaptee.setType(Node.StartTag);
390: }
391:
392: return newChild;
393: }
394:
395: /**
396: * @see org.w3c.dom.Node#hasChildNodes
397: */
398: public boolean hasChildNodes() {
399: return (adaptee.content != null);
400: }
401:
402: /**
403: * @see org.w3c.dom.Node#cloneNode
404: */
405: public org.w3c.dom.Node cloneNode(boolean deep) {
406: Node node = adaptee.cloneNode(deep);
407: node.parent = null;
408: return node.getAdapter();
409: }
410:
411: /**
412: * DOM2 - not implemented.
413: */
414: public void normalize() {
415: }
416:
417: /**
418: * DOM2 - not implemented.
419: */
420: public boolean supports(String feature, String version) {
421: return isSupported(feature, version);
422: }
423:
424: /**
425: * DOM2 - not implemented.
426: */
427: public String getNamespaceURI() {
428: return null;
429: }
430:
431: /**
432: * DOM2 - not implemented.
433: */
434: public String getPrefix() {
435: return null;
436: }
437:
438: /**
439: * DOM2 - not implemented.
440: */
441: public void setPrefix(String prefix) throws DOMException {
442: }
443:
444: /**
445: * DOM2 - not implemented.
446: */
447: public String getLocalName() {
448: return null;
449: }
450:
451: /**
452: * DOM2 - not implemented.
453: */
454: public boolean isSupported(String feature, String version) {
455: return false;
456: }
457:
458: /**
459: * DOM2 - @see org.w3c.dom.Node#hasAttributes
460: * contributed by dlp@users.sourceforge.net
461: */
462: public boolean hasAttributes() {
463: return adaptee.attributes != null;
464: }
465:
466: public org.w3c.dom.Node adoptNode(org.w3c.dom.Node oNode) {
467: throw new UnsupportedOperationException(
468: "org.w3c.tidy.DOMNodeImpl adoptNode() Not implemented");
469: }
470:
471: public short compareDocumentPosition(org.w3c.dom.Node oNode) {
472: throw new UnsupportedOperationException(
473: "org.w3c.tidy.DOMNodeImpl compareDocumentPosition() Not implemented");
474: }
475:
476: public boolean isDefaultNamespace(String sStr1) {
477: throw new UnsupportedOperationException(
478: "org.w3c.tidy.DOMNodeImpl isDefaultNamespace() Not implemented");
479: }
480:
481: public boolean isEqualNode(org.w3c.dom.Node oNode) {
482: throw new UnsupportedOperationException(
483: "org.w3c.tidy.DOMNodeImpl isEqualNode() Not implemented");
484: }
485:
486: public boolean isSameNode(org.w3c.dom.Node oNode) {
487: throw new UnsupportedOperationException(
488: "org.w3c.tidy.DOMNodeImpl isSameNode() Not implemented");
489: }
490:
491: public String lookupPrefix(String sStr1) {
492: throw new UnsupportedOperationException(
493: "org.w3c.tidy.DOMNodeImpl lookupPreffix() Not implemented");
494: }
495:
496: public String lookupNamespaceURI(String sStr1) {
497: throw new UnsupportedOperationException(
498: "org.w3c.tidy.DOMNodeImpl lookupNamespaceURI() Not implemented");
499: }
500:
501: public String getDocumentURI() {
502: throw new UnsupportedOperationException(
503: "org.w3c.tidy.DOMNodeImpl getDocumentURI() Not implemented");
504: }
505:
506: public void setDocumentURI(String sStr1) {
507: throw new UnsupportedOperationException(
508: "org.w3c.tidy.DOMNodeImpl setDocumentURI() Not implemented");
509: }
510:
511: public boolean getStrictErrorChecking() {
512: throw new UnsupportedOperationException(
513: "org.w3c.tidy.DOMNodeImpl getStrictErrorChecking() Not implemented");
514: }
515:
516: public void setStrictErrorChecking(boolean bStrictCheck) {
517: throw new UnsupportedOperationException(
518: "org.w3c.tidy.DOMNodeImpl setStrictErrorChecking() Not implemented");
519: }
520:
521: public boolean getXmlStandalone() {
522: throw new UnsupportedOperationException(
523: "org.w3c.tidy.DOMNodeImpl getXmlStandalone() Not implemented");
524: }
525:
526: public void setXmlStandalone(boolean bXmlStandalone) {
527: throw new UnsupportedOperationException(
528: "org.w3c.tidy.DOMNodeImpl setXmlStandalone() Not implemented");
529: }
530:
531: public Object getFeature(String sStr1, String sStr2) {
532: throw new UnsupportedOperationException(
533: "org.w3c.tidy.DOMNodeImpl getFeature() Not implemented");
534: }
535:
536: public String getInputEncoding() {
537: throw new UnsupportedOperationException(
538: "org.w3c.tidy.DOMNodeImpl getInputEncoding() Not implemented");
539: }
540:
541: public String getXmlEncoding() {
542: throw new UnsupportedOperationException(
543: "org.w3c.tidy.DOMNodeImpl getXmlEncoding() Not implemented");
544: }
545:
546: public String getXmlVersion() {
547: throw new UnsupportedOperationException(
548: "org.w3c.tidy.DOMNodeImpl getXmlVersion() Not implemented");
549: }
550:
551: public void setXmlVersion(String sStr1) {
552: throw new UnsupportedOperationException(
553: "org.w3c.tidy.DOMNodeImpl setXmlVersion() Not implemented");
554: }
555:
556: public Object getUserData(String sStr1) {
557: throw new UnsupportedOperationException(
558: "org.w3c.tidy.DOMNodeImpl getUserData() Not implemented");
559: }
560:
561: public Object setUserData(String sStr1, Object oObj2,
562: org.w3c.dom.UserDataHandler oHndlr) {
563: throw new UnsupportedOperationException(
564: "org.w3c.tidy.DOMNodeImpl setUserData() Not implemented");
565: }
566:
567: public org.w3c.dom.DOMConfiguration getDomConfig() {
568: throw new UnsupportedOperationException(
569: "org.w3c.tidy.DOMNodeImpl getDomConfig() Not implemented");
570: }
571:
572: public void normalizeDocument() {
573: throw new UnsupportedOperationException(
574: "org.w3c.tidy.DOMNodeImpl normalizeDocument() Not implemented");
575: }
576:
577: public org.w3c.dom.Node renameNode(org.w3c.dom.Node oNode,
578: String sStr1, String sStr2) {
579: throw new UnsupportedOperationException(
580: "org.w3c.tidy.DOMNodeImpl renameNode() Not implemented");
581: }
582:
583: public String getBaseURI() {
584: throw new UnsupportedOperationException(
585: "org.w3c.tidy.DOMNodeImpl getBaseURI() Not implemented");
586: }
587:
588: public String getTextContent() {
589: throw new UnsupportedOperationException(
590: "org.w3c.tidy.DOMNodeImpl getTextContent() Not implemented");
591: }
592:
593: public void setTextContent(String sStr1) {
594: throw new UnsupportedOperationException(
595: "org.w3c.tidy.DOMNodeImpl setTextContent() Not implemented");
596: }
597:
598: }
|