KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > syntax > dom > SyntaxNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.text.syntax.dom;
21
22 import org.w3c.dom.*;
23 import org.netbeans.modules.xml.text.syntax.*;
24 import org.netbeans.modules.xml.spi.dom.*;
25 import org.netbeans.editor.*;
26
27 /**
28  * It represents higher level abstraction elements implementing
29  * DOM Node interface.
30  * <p>
31  * Known differences to DOM specs:
32  * <li> <code>getParentNode()</code> may return <code>null</code>
33  * <li> NOT_SUPPORTED_ERR is thrown from <code>get_XXX_By_YYY()</code>
34  * <li> implements <code>equals</code>ity at DOM Node level
35  * <p>
36  * Instances are produced by {@link XMLSyntaxSupport}.
37  *
38  * @author Petr Kuzel
39  *
40  * @version 1.0
41  */

42
43 public abstract class SyntaxNode extends SyntaxElement implements Node {
44
45     /** Creates new SyntaxNode */
46     public SyntaxNode(XMLSyntaxSupport support, TokenItem first, int to) {
47         super( support, first, to);
48     }
49
50     /**
51      * Default implementation returning first previous <code>SyntaxNode</code>
52      * or <code>null</code>. It is <code>StartTag</code> aware.
53      */

54     public Node getPreviousSibling() {
55         SyntaxNode prev = findPrevious(this);
56         
57         // stop at start tag (it forms hiearchy)
58
if (prev instanceof StartTag) {
59             return null;
60         } else {
61             return prev;
62         }
63     }
64
65     /**
66      * Find previous SyntaxNode instance or <code>null</code>.
67      */

68     static SyntaxNode findPrevious(SyntaxElement el) {
69         SyntaxElement prev = el.getPrevious();
70         while ((prev instanceof SyntaxNode) == false) {
71             if (prev == null) return null;
72             prev = prev.getPrevious();
73         }
74         return (SyntaxNode) prev;
75     }
76
77     /**
78      * Find previous SyntaxNode instance or <code>null</code>.
79      */

80     SyntaxNode findPrevious() {
81         return findPrevious(this);
82     }
83     
84     /**
85      * Default implementation returning first next <code>SyntaxNode</code>
86      * or <code>null</code>. It is <code>EndTag</code> aware.
87      */

88     public Node getNextSibling() {
89         SyntaxNode next = findNext(this);
90         
91         // stop at end tag (it forms hiearchy)
92
if (next instanceof EndTag) {
93             return null;
94         } else {
95             return next;
96         }
97
98     }
99
100     
101     /**
102      * Find previous SyntaxNode instance or <code>null</code>.
103      */

104     static SyntaxNode findNext(SyntaxElement el) {
105         SyntaxElement next = el.getNext();
106         while ((next instanceof SyntaxNode) == false) {
107             if (next == null) return null;
108             next = next.getNext();
109         }
110         return (SyntaxNode) next;
111     }
112
113     /**
114      * Find previous SyntaxNode instance or <code>null</code>.
115      */

116     SyntaxNode findNext() {
117         return findNext(this);
118     }
119     
120     /**
121      * First previous start tag at higher level is my parent.
122      * Skip all end-tag start-tag pairs at the same level.
123      * @return SyntaxNode or <code>null</code>
124      */

125     public Node getParentNode() {
126         SyntaxNode prev = findPrevious();
127         
128         do {
129
130             while ( prev != null ) {
131                 if (prev instanceof StartTag) {
132                     return (Element) prev;
133                 } else if (prev instanceof EndTag) { // traverse end-start tag pairs
134
prev = ((EndTag)prev).getStartTag();
135                     if (prev == null) break;
136                     prev = prev.findPrevious();
137                 } else {
138                     prev = prev.findPrevious();
139                 }
140             }
141             
142             if (prev == null) break;
143             
144         } while ( (prev instanceof SyntaxNode) == false );
145
146         if (prev != null) {
147             return (Node) prev;
148         } else {
149             return getOwnerDocument(); //??? return a DocumentFragment with some kids? or null
150
}
151     }
152     
153     public org.w3c.dom.Document JavaDoc getOwnerDocument() {
154         return new DocumentImpl(this);
155     }
156
157     // default DOM Node implementation ~~~~~~~~~~~~~~~~~~~~~~~~``
158

159     public String JavaDoc getNodeName() {
160         return null;
161     }
162
163     /**
164      * @return false
165      */

166     public boolean isSupported(String JavaDoc str, String JavaDoc str1) {
167         throw new UOException();
168     }
169     
170     public void setPrefix(String JavaDoc str) throws org.w3c.dom.DOMException JavaDoc {
171         throw new ROException();
172     }
173     
174     public String JavaDoc getPrefix() {
175         throw new UOException();
176     }
177     
178     /**
179      * It is rather abstract to force all to reimplement.
180      */

181     public abstract short getNodeType();
182         
183     public org.w3c.dom.Node JavaDoc replaceChild(org.w3c.dom.Node JavaDoc node, org.w3c.dom.Node JavaDoc node1) throws org.w3c.dom.DOMException JavaDoc {
184         throw new ROException();
185     }
186     
187     public org.w3c.dom.Node JavaDoc cloneNode(boolean param) {
188         return (Node) this; //we are immutable, only problem with references may appear
189
}
190         
191     public org.w3c.dom.Node JavaDoc insertBefore(org.w3c.dom.Node JavaDoc node, org.w3c.dom.Node JavaDoc node1) throws org.w3c.dom.DOMException JavaDoc {
192         throw new ROException();
193     }
194     
195     public String JavaDoc getNamespaceURI() {
196         throw new UOException();
197     }
198     
199     public org.w3c.dom.NamedNodeMap JavaDoc getAttributes() {
200         return NamedNodeMapImpl.EMPTY;
201     }
202     
203     public org.w3c.dom.NodeList JavaDoc getChildNodes() {
204         return NodeListImpl.EMPTY;
205     }
206     
207     public String JavaDoc getNodeValue() throws org.w3c.dom.DOMException JavaDoc {
208         // attribute, text, pi data
209
return null;
210     }
211     
212     public org.w3c.dom.Node JavaDoc appendChild(org.w3c.dom.Node JavaDoc node) throws org.w3c.dom.DOMException JavaDoc {
213         throw new ROException();
214     }
215     
216     public String JavaDoc getLocalName() {
217         throw new UOException();
218     }
219         
220     public void setNodeValue(String JavaDoc str) throws org.w3c.dom.DOMException JavaDoc {
221         throw new ROException();
222     }
223     
224     public org.w3c.dom.Node JavaDoc getLastChild() {
225         // if broken null
226
return null;
227     }
228     
229     public boolean hasAttributes() {
230         throw new UOException();
231     }
232     
233     public void normalize() {
234         // ignore we are modmalized by default
235
}
236     
237     public org.w3c.dom.Node JavaDoc removeChild(org.w3c.dom.Node JavaDoc node) throws org.w3c.dom.DOMException JavaDoc {
238         throw new ROException();
239     }
240     
241     /**
242      * @return false
243      */

244     public boolean hasChildNodes() {
245         return false;
246     }
247     
248     /**
249      * @return null
250      */

251     public org.w3c.dom.Node JavaDoc getFirstChild() {
252         return null;
253     }
254     
255     //
256
// Implementation of DOM Level 3 methods
257
//
258

259     public short compareDocumentPosition (Node a) {
260         throw new UOException();
261     }
262     
263     public String JavaDoc getBaseURI() {
264         throw new UOException();
265     }
266     public Object JavaDoc getFeature(String JavaDoc a, String JavaDoc b) {
267         throw new UOException();
268     }
269     public String JavaDoc getTextContent () {
270         throw new UOException();
271     }
272     public Object JavaDoc getUserData(String JavaDoc a) {
273         throw new UOException();
274     }
275     public boolean isDefaultNamespace (String JavaDoc a) {
276         throw new UOException();
277     }
278     public boolean isEqualNode(Node a) {
279         throw new UOException();
280     }
281     public boolean isSameNode(Node a) {
282         throw new UOException();
283     }
284     public String JavaDoc lookupNamespaceURI(String JavaDoc a) {
285         throw new UOException();
286     }
287     public String JavaDoc lookupPrefix(String JavaDoc a) {
288         throw new UOException();
289     }
290     public void setTextContent(String JavaDoc a) {
291         throw new UOException();
292     }
293     public Object JavaDoc setUserData(String JavaDoc a, Object JavaDoc b, UserDataHandler c) {
294         throw new UOException();
295     }
296     // Implementation of DOM Level 3 methods for Text
297
public Text replaceWholeText (String JavaDoc a) {
298         throw new UOException ();
299     }
300     public String JavaDoc getWholeText() {
301         throw new UOException ();
302     }
303     public boolean isElementContentWhitespace() {
304         throw new UOException ();
305     }
306     // Implementation of DOM Level 3 methods for Element
307
public TypeInfo getSchemaTypeInfo() {
308         throw new UOException ();
309     }
310     public void setIdAttribute(String JavaDoc a, boolean b) {
311         throw new UOException ();
312     }
313     public void setIdAttributeNS(String JavaDoc a, String JavaDoc b, boolean c) {
314         throw new UOException ();
315     }
316     public void setIdAttributeNode(Attr a, boolean b) {
317         throw new UOException ();
318     }
319     
320 }
321
Popular Tags