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

« back to all changes in this revision

Viewing changes to languages/engine/src/org/netbeans/api/languages/ASTItem.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.api.languages;
 
43
 
 
44
import java.util.ArrayList;
 
45
import java.util.Collections;
 
46
import java.util.Iterator;
 
47
import java.util.List;
 
48
import java.util.SortedMap;
 
49
import java.util.TreeMap;
 
50
 
 
51
 
 
52
/**
 
53
 * Represents one item in AST tree.
 
54
 * 
 
55
 * @author jan Jancura
 
56
 */
 
57
public class ASTItem {
 
58
   
 
59
    private Language        language;
 
60
    private int             offset;
 
61
    private int             length = -1;
 
62
    private List<ASTItem>   children;
 
63
 
 
64
    
 
65
    @SuppressWarnings("unchecked")
 
66
    ASTItem (
 
67
        Language            language,
 
68
        int                 offset,
 
69
        int                 length,
 
70
        List<? extends ASTItem> children
 
71
    ) {
 
72
        this.language =     language;
 
73
        this.offset =       offset;
 
74
        this.length =       length;
 
75
 
 
76
        // [PENDING]
 
77
//        this.children = new ArrayList<ASTItem> ();
 
78
//        if (children != null) {
 
79
//            Iterator<ASTItem> it = children.iterator ();
 
80
//            while (it.hasNext ()) {
 
81
//                ASTItem item = it.next ();
 
82
//                if (item == null)
 
83
//                    throw new NullPointerException ();
 
84
//                this.children.add (item);
 
85
//            }
 
86
//        }
 
87
        if (children != null) 
 
88
            this.children = (List<ASTItem>) children;
 
89
        else
 
90
            this.children = Collections.<ASTItem>emptyList ();
 
91
    }
 
92
 
 
93
    /**
 
94
     * Returns offset of this item.
 
95
     * 
 
96
     * @return offset of this item
 
97
     */
 
98
    public int getOffset () {
 
99
        return offset;
 
100
    }
 
101
    
 
102
    public Language getLanguage () {
 
103
        return language;
 
104
    }
 
105
 
 
106
    /**
 
107
     * Returns MIME type of this item.
 
108
     * 
 
109
     * @return MIME type of this item
 
110
     */
 
111
    public String getMimeType () {
 
112
        if (language == null) return null;
 
113
        return language.getMimeType ();
 
114
    }
 
115
 
 
116
    /**
 
117
     * Returns list of all subitems (ASTItem).
 
118
     * 
 
119
     * @return list of all subitems (ASTItem)
 
120
     */
 
121
    public List<ASTItem> getChildren () {
 
122
        return children;
 
123
    }
 
124
    
 
125
    /**
 
126
     * Adds child to the end of list of children.
 
127
     * 
 
128
     * @param item a child to be added
 
129
     */
 
130
    void addChildren (ASTItem item) {
 
131
        if (children == Collections.<ASTItem>emptyList ())
 
132
            children = new ArrayList<ASTItem> ();
 
133
        children.add (item);
 
134
        if (childrenMap != null)
 
135
            childrenMap.put (item.getOffset (), item);
 
136
    }
 
137
    
 
138
    /**
 
139
     * Adds child to the end of list of children.
 
140
     * 
 
141
     * @param item a child to be added
 
142
     */
 
143
    void removeChildren (ASTItem item) {
 
144
        if (children == Collections.<ASTItem>emptyList ())
 
145
            return;
 
146
        children.remove (item);
 
147
        if (childrenMap != null)
 
148
            childrenMap.remove (item.getOffset ());
 
149
    }
 
150
    
 
151
    /**
 
152
     * Adds child to the end of list of children.
 
153
     * 
 
154
     * @param item a child to be added
 
155
     */
 
156
    void setChildren (int index, ASTItem item) {
 
157
        if (children == Collections.<ASTItem>emptyList ())
 
158
            return;
 
159
        int oldOffset = children.get (index).getOffset ();
 
160
        children.set (index, item);
 
161
        if (childrenMap != null) {
 
162
            childrenMap.remove (oldOffset);
 
163
            childrenMap.put (item.getOffset (), item);
 
164
        }
 
165
    }
 
166
    
 
167
    /**
 
168
     * Locks children for any modifications.
 
169
     */
 
170
    public void lock () {
 
171
        children = Collections.<ASTItem>unmodifiableList (children);
 
172
    }
 
173
    
 
174
    /**
 
175
     * Returns end offset of this item. Tt is the first offset that is not part 
 
176
     * of this node.
 
177
     * 
 
178
     * @return end offset of this item
 
179
     */
 
180
    public int getEndOffset () {
 
181
        return getOffset () + getLength ();
 
182
    }
 
183
    
 
184
    /**
 
185
     * Returns length of this item (end offset - start offset).
 
186
     * 
 
187
     * @return length of this item (end offset - start offset)
 
188
     */
 
189
    public int getLength () {
 
190
        if (length < 0) {
 
191
            List<ASTItem> l = getChildren ();
 
192
            if (l.isEmpty ())
 
193
                length = 0;
 
194
            else {
 
195
                ASTItem last = l.get (l.size () - 1);
 
196
                length = last.getEndOffset () - getOffset ();
 
197
            }
 
198
        }
 
199
        return length;
 
200
    }
 
201
    
 
202
    /**
 
203
     * Returns path from this item to the item on given offset.
 
204
     * 
 
205
     * @param offset offset
 
206
     * 
 
207
     * @return path from this item to the item on given offset
 
208
     */
 
209
    public ASTPath findPath (int offset) {
 
210
        return findPath (new ArrayList<ASTItem> (), offset);
 
211
    }
 
212
    
 
213
    ASTPath findPath (List<ASTItem> path, int offset) {
 
214
        if (offset < getOffset ()) return ASTPath.create (path);
 
215
        if (offset > getEndOffset ()) return ASTPath.create (path);
 
216
        path.add (this);
 
217
        if (getChildren ().isEmpty ())
 
218
            return ASTPath.create (path);
 
219
        if (getChildren ().size () > 10)
 
220
            return findPath2 (path, offset);
 
221
        Iterator<ASTItem> it = getChildren ().iterator ();
 
222
        while (it.hasNext ()) {
 
223
            ASTItem item = it.next ();
 
224
            if (offset < item.getEndOffset () &&
 
225
                item.getOffset () <= offset
 
226
            )
 
227
                return item.findPath (path, offset);
 
228
        }
 
229
        return ASTPath.create (path);
 
230
    }
 
231
 
 
232
    private ASTPath findPath2 (List<ASTItem> path, int offset) {
 
233
        TreeMap<Integer,ASTItem> childrenMap = getChildrenMap ();
 
234
        SortedMap<Integer,ASTItem> headMap = childrenMap.headMap (new Integer (offset + 1));
 
235
        if (headMap.isEmpty ())
 
236
            return ASTPath.create (path);
 
237
        Integer key = headMap.lastKey ();
 
238
        ASTItem item = childrenMap.get (key);
 
239
        ASTPath path2 =  item.findPath (path, offset);
 
240
        if (path2 == null)
 
241
            return ASTPath.create (path);
 
242
        return path2;
 
243
    }
 
244
    
 
245
    private TreeMap<Integer,ASTItem> childrenMap = null;
 
246
    
 
247
    private TreeMap<Integer,ASTItem> getChildrenMap () {
 
248
        if (childrenMap == null) {
 
249
            childrenMap = new TreeMap<Integer,ASTItem> ();
 
250
            Iterator<ASTItem> it = getChildren ().iterator ();
 
251
            while (it.hasNext ()) {
 
252
                ASTItem item = it.next ();
 
253
                childrenMap.put (new Integer (item.getOffset ()), item);
 
254
            }
 
255
        }
 
256
        return childrenMap;
 
257
    }
 
258
}
 
259
 
 
260
 
 
261
 
 
262
 
 
263