~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/text-edit/src/org/netbeans/modules/xml/text/syntax/dom/SyntaxNode.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.xml.text.syntax.dom;
 
43
 
 
44
import org.w3c.dom.*;
 
45
import org.netbeans.modules.xml.text.syntax.*;
 
46
import org.netbeans.modules.xml.spi.dom.*;
 
47
import org.netbeans.editor.*;
 
48
 
 
49
/**
 
50
 * It represents higher level abstraction elements implementing
 
51
 * DOM Node interface.
 
52
 * <p>
 
53
 * Known differences to DOM specs:
 
54
 * <li> <code>getParentNode()</code> may return <code>null</code>
 
55
 * <li> NOT_SUPPORTED_ERR is thrown from <code>get_XXX_By_YYY()</code>
 
56
 * <li> implements <code>equals</code>ity at DOM Node level
 
57
 * <p>
 
58
 * Instances are produced by {@link XMLSyntaxSupport}.
 
59
 *
 
60
 * @author  Petr Kuzel
 
61
 *
 
62
 * @version 1.0
 
63
 */
 
64
 
 
65
public abstract class SyntaxNode extends SyntaxElement implements Node {
 
66
 
 
67
    /** Creates new SyntaxNode */
 
68
    public SyntaxNode(XMLSyntaxSupport support, TokenItem first, int to)  {
 
69
        super( support, first, to);
 
70
    }
 
71
 
 
72
    /**
 
73
     * Default implementation returning first previous <code>SyntaxNode</code>
 
74
     * or <code>null</code>. It is <code>StartTag</code> aware.
 
75
     */
 
76
    public Node getPreviousSibling() {
 
77
        SyntaxNode prev = findPrevious(this);
 
78
        
 
79
        // stop at start tag (it forms hiearchy)
 
80
        if (prev instanceof StartTag) {
 
81
            return null;
 
82
        } else {
 
83
            return prev;
 
84
        }
 
85
    }
 
86
 
 
87
    /**
 
88
     * Find previous SyntaxNode instance or <code>null</code>.
 
89
     */
 
90
    static SyntaxNode findPrevious(SyntaxElement el) {
 
91
        SyntaxElement prev = el.getPrevious();
 
92
        while ((prev instanceof SyntaxNode) == false) {
 
93
            if (prev == null) return null;            
 
94
            prev = prev.getPrevious();
 
95
        }
 
96
        return (SyntaxNode) prev;
 
97
    }
 
98
 
 
99
    /**
 
100
     * Find previous SyntaxNode instance or <code>null</code>.
 
101
     */    
 
102
    SyntaxNode findPrevious() {
 
103
        return findPrevious(this);
 
104
    }
 
105
    
 
106
    /**
 
107
     * Default implementation returning first next <code>SyntaxNode</code>
 
108
     * or <code>null</code>. It is <code>EndTag</code> aware.
 
109
     */    
 
110
    public Node getNextSibling() {        
 
111
        SyntaxNode next = findNext(this);
 
112
        
 
113
        // stop at end tag (it forms hiearchy)
 
114
        if (next instanceof EndTag) {
 
115
            return null;
 
116
        } else {
 
117
            return next;
 
118
        }
 
119
 
 
120
    }
 
121
 
 
122
    
 
123
    /**
 
124
     * Find previous SyntaxNode instance or <code>null</code>.
 
125
     */
 
126
    static SyntaxNode findNext(SyntaxElement el) {
 
127
        SyntaxElement next = el.getNext();
 
128
        while ((next instanceof SyntaxNode) == false) {
 
129
            if (next == null) return null;            
 
130
            next = next.getNext();
 
131
        }
 
132
        return (SyntaxNode) next;
 
133
    }
 
134
 
 
135
    /**
 
136
     * Find previous SyntaxNode instance or <code>null</code>.
 
137
     */
 
138
    SyntaxNode findNext() {
 
139
        return findNext(this);
 
140
    }
 
141
    
 
142
    /**
 
143
     * First previous start tag at higher level is my parent.
 
144
     * Skip all end-tag start-tag pairs at the same level.
 
145
     * @return SyntaxNode or <code>null</code>
 
146
     */
 
147
    public Node getParentNode() {
 
148
        SyntaxNode prev = findPrevious();
 
149
        
 
150
        do {
 
151
 
 
152
            while ( prev != null )  {
 
153
                if (prev instanceof StartTag) {
 
154
                    return (Element) prev;
 
155
                } else if (prev instanceof EndTag) {       // traverse end-start tag pairs
 
156
                    prev = ((EndTag)prev).getStartTag(); 
 
157
                    if (prev == null) break;                
 
158
                    prev = prev.findPrevious();
 
159
                } else {
 
160
                    prev = prev.findPrevious();
 
161
                }
 
162
            }
 
163
            
 
164
            if (prev == null) break;
 
165
            
 
166
        } while ( (prev instanceof SyntaxNode) == false );
 
167
 
 
168
        if (prev != null) {
 
169
            return (Node) prev;
 
170
        } else {
 
171
            return getOwnerDocument(); //??? return a DocumentFragment with some kids? or null
 
172
        }
 
173
    }
 
174
    
 
175
    public org.w3c.dom.Document getOwnerDocument() {
 
176
        return new DocumentImpl(this);
 
177
    }
 
178
 
 
179
    // default DOM Node implementation ~~~~~~~~~~~~~~~~~~~~~~~~``
 
180
    
 
181
    public String getNodeName() {
 
182
        return null;
 
183
    }
 
184
 
 
185
    /**
 
186
     * @return false
 
187
     */
 
188
    public boolean isSupported(String str, String str1) {
 
189
        throw new UOException();
 
190
    }
 
191
    
 
192
    public void setPrefix(String str) throws org.w3c.dom.DOMException {
 
193
        throw new ROException();
 
194
    }
 
195
    
 
196
    public String getPrefix() {
 
197
        throw new UOException();
 
198
    }
 
199
    
 
200
    /**
 
201
     * It is rather abstract to force all to reimplement.
 
202
     */
 
203
    public abstract short getNodeType();
 
204
        
 
205
    public org.w3c.dom.Node replaceChild(org.w3c.dom.Node node, org.w3c.dom.Node node1) throws org.w3c.dom.DOMException {
 
206
        throw new ROException();
 
207
    }
 
208
    
 
209
    public org.w3c.dom.Node cloneNode(boolean param) {
 
210
        return (Node) this;  //we are immutable, only problem with references may appear
 
211
    }
 
212
        
 
213
    public org.w3c.dom.Node insertBefore(org.w3c.dom.Node node, org.w3c.dom.Node node1) throws org.w3c.dom.DOMException {
 
214
        throw new ROException();
 
215
    }
 
216
    
 
217
    public String getNamespaceURI() {
 
218
        throw new UOException();
 
219
    }
 
220
    
 
221
    public org.w3c.dom.NamedNodeMap getAttributes() {
 
222
        return NamedNodeMapImpl.EMPTY;
 
223
    }
 
224
    
 
225
    public org.w3c.dom.NodeList getChildNodes() {       
 
226
        return NodeListImpl.EMPTY;
 
227
    }
 
228
    
 
229
    public String getNodeValue() throws org.w3c.dom.DOMException {
 
230
        // attribute, text, pi data
 
231
        return null;
 
232
    }
 
233
    
 
234
    public org.w3c.dom.Node appendChild(org.w3c.dom.Node node) throws org.w3c.dom.DOMException {
 
235
        throw new ROException();
 
236
    }
 
237
    
 
238
    public String getLocalName() {
 
239
        throw new UOException();
 
240
    }
 
241
        
 
242
    public void setNodeValue(String str) throws org.w3c.dom.DOMException {
 
243
        throw new ROException();
 
244
    }
 
245
    
 
246
    public org.w3c.dom.Node getLastChild() {
 
247
        // if broken null
 
248
        return null;
 
249
    }
 
250
    
 
251
    public boolean hasAttributes() {
 
252
        throw new UOException();
 
253
    }
 
254
    
 
255
    public void normalize() {
 
256
        // ignore we are modmalized by default
 
257
    }
 
258
    
 
259
    public org.w3c.dom.Node removeChild(org.w3c.dom.Node node) throws org.w3c.dom.DOMException {
 
260
        throw new ROException();
 
261
    }
 
262
    
 
263
    /**
 
264
     * @return false
 
265
     */
 
266
    public boolean hasChildNodes() {
 
267
        return false;
 
268
    }
 
269
    
 
270
    /**
 
271
     * @return null
 
272
     */
 
273
    public org.w3c.dom.Node getFirstChild() {
 
274
        return null;
 
275
    }
 
276
    
 
277
    //
 
278
    // Implementation of DOM Level 3 methods
 
279
    //
 
280
    
 
281
    public short compareDocumentPosition (Node a) {
 
282
        throw new UOException();
 
283
    }
 
284
    
 
285
    public String getBaseURI() {
 
286
        throw new UOException();
 
287
    }
 
288
    public Object getFeature(String a, String b) {
 
289
        throw new UOException();
 
290
    }
 
291
    public String getTextContent () {
 
292
        throw new UOException();
 
293
    }
 
294
    public Object getUserData(String a) {
 
295
        throw new UOException();
 
296
    }
 
297
    public boolean isDefaultNamespace (String a)  {
 
298
        throw new UOException();
 
299
    }
 
300
    public boolean isEqualNode(Node a) {
 
301
        throw new UOException();
 
302
    }
 
303
    public boolean isSameNode(Node a) {
 
304
        throw new UOException();
 
305
    }
 
306
    public String lookupNamespaceURI(String a) {
 
307
        throw new UOException();
 
308
    }
 
309
    public String lookupPrefix(String a) {
 
310
        throw new UOException();
 
311
    }
 
312
    public void setTextContent(String a) {
 
313
        throw new UOException();
 
314
    }
 
315
    public Object setUserData(String a, Object b, UserDataHandler c) {
 
316
        throw new UOException();
 
317
    }
 
318
    // Implementation of DOM Level 3 methods for Text
 
319
    public Text replaceWholeText (String a) {
 
320
        throw new UOException ();
 
321
    }
 
322
    public String getWholeText() {
 
323
        throw new UOException ();
 
324
    }
 
325
    public boolean isElementContentWhitespace() {
 
326
        throw new UOException ();
 
327
    }
 
328
    // Implementation of DOM Level 3 methods for Element
 
329
    public TypeInfo getSchemaTypeInfo() {
 
330
        throw new UOException ();
 
331
    }
 
332
    public void setIdAttribute(String a, boolean b) {
 
333
        throw new UOException ();
 
334
    }
 
335
    public void setIdAttributeNS(String a, String b, boolean c) {
 
336
        throw new UOException ();
 
337
    }
 
338
    public void setIdAttributeNode(Attr a, boolean b) {
 
339
        throw new UOException ();
 
340
    }
 
341
    
 
342
}