2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
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.
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.
42
package org.netbeans.api.languages;
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;
53
* Represents one item in AST tree.
57
public class ASTItem {
59
private Language language;
61
private int length = -1;
62
private List<ASTItem> children;
65
@SuppressWarnings("unchecked")
70
List<? extends ASTItem> children
72
this.language = language;
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 ();
83
// throw new NullPointerException ();
84
// this.children.add (item);
88
this.children = (List<ASTItem>) children;
90
this.children = Collections.<ASTItem>emptyList ();
94
* Returns offset of this item.
96
* @return offset of this item
98
public int getOffset () {
102
public Language getLanguage () {
107
* Returns MIME type of this item.
109
* @return MIME type of this item
111
public String getMimeType () {
112
if (language == null) return null;
113
return language.getMimeType ();
117
* Returns list of all subitems (ASTItem).
119
* @return list of all subitems (ASTItem)
121
public List<ASTItem> getChildren () {
126
* Adds child to the end of list of children.
128
* @param item a child to be added
130
void addChildren (ASTItem item) {
131
if (children == Collections.<ASTItem>emptyList ())
132
children = new ArrayList<ASTItem> ();
134
if (childrenMap != null)
135
childrenMap.put (item.getOffset (), item);
139
* Adds child to the end of list of children.
141
* @param item a child to be added
143
void removeChildren (ASTItem item) {
144
if (children == Collections.<ASTItem>emptyList ())
146
children.remove (item);
147
if (childrenMap != null)
148
childrenMap.remove (item.getOffset ());
152
* Adds child to the end of list of children.
154
* @param item a child to be added
156
void setChildren (int index, ASTItem item) {
157
if (children == Collections.<ASTItem>emptyList ())
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);
168
* Locks children for any modifications.
170
public void lock () {
171
children = Collections.<ASTItem>unmodifiableList (children);
175
* Returns end offset of this item. Tt is the first offset that is not part
178
* @return end offset of this item
180
public int getEndOffset () {
181
return getOffset () + getLength ();
185
* Returns length of this item (end offset - start offset).
187
* @return length of this item (end offset - start offset)
189
public int getLength () {
191
List<ASTItem> l = getChildren ();
195
ASTItem last = l.get (l.size () - 1);
196
length = last.getEndOffset () - getOffset ();
203
* Returns path from this item to the item on given offset.
205
* @param offset offset
207
* @return path from this item to the item on given offset
209
public ASTPath findPath (int offset) {
210
return findPath (new ArrayList<ASTItem> (), offset);
213
ASTPath findPath (List<ASTItem> path, int offset) {
214
if (offset < getOffset ()) return ASTPath.create (path);
215
if (offset > getEndOffset ()) return ASTPath.create (path);
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
227
return item.findPath (path, offset);
229
return ASTPath.create (path);
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);
241
return ASTPath.create (path);
245
private TreeMap<Integer,ASTItem> childrenMap = null;
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);