KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > languages > ASTItem


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.api.languages;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.SortedMap JavaDoc;
27 import java.util.TreeMap JavaDoc;
28
29
30 /**
31  * Represents one item in AST tree.
32  *
33  * @author jan Jancura
34  */

35 public class ASTItem {
36    
37     private String JavaDoc mimeType;
38     private int offset;
39     private int length = -1;
40
41     private List JavaDoc<ASTItem> children;
42     private ASTPath path;
43
44     ASTItem (
45         String JavaDoc mimeType,
46         int offset,
47         int length,
48         List JavaDoc<? extends ASTItem> children
49     ) {
50         this.mimeType = mimeType;
51         this.offset = offset;
52         this.length = length;
53
54         // [PENDING]
55
// int lastOffset = offset;
56
this.children = new ArrayList JavaDoc<ASTItem> ();
57         if (children != null) {
58             Iterator JavaDoc<? extends ASTItem> it = children.iterator ();
59             while (it.hasNext ()) {
60                 ASTItem item = it.next ();
61                 if (item == null)
62                     throw new NullPointerException JavaDoc ();
63 // if (item.getOffset () != lastOffset)
64
// throw new IllegalArgumentException ();
65
this.children.add (item);
66 // lastOffset += item.getLength ();
67
}
68         }
69         this.children = Collections.unmodifiableList (this.children);
70     }
71
72     /**
73      * Returns offset of this item.
74      *
75      * @return offset of this item
76      */

77     public int getOffset () {
78         return offset;
79     }
80
81     /**
82      * Returns MIME type of this item.
83      *
84      * @return MIME type of this item
85      */

86     public String JavaDoc getMimeType () {
87         return mimeType;
88     }
89
90     /**
91      * Returns list of all subitems (ASTItem).
92      *
93      * @return list of all subitems (ASTItem)
94      */

95     public List JavaDoc<ASTItem> getChildren () {
96         return children;
97     }
98     
99     /**
100      * Returns end offset of this item. Tt is the first offset that is not part
101      * of this node.
102      *
103      * @return end offset of this item
104      */

105     public int getEndOffset () {
106         return getOffset () + getLength ();
107     }
108     
109     /**
110      * Returns length of this item (end offset - start offset).
111      *
112      * @return length of this item (end offset - start offset)
113      */

114     public int getLength () {
115         if (length < 0) {
116             List JavaDoc<ASTItem> l = getChildren ();
117             if (l.isEmpty ())
118                 length = 0;
119             else {
120                 ASTItem last = l.get (l.size () - 1);
121                 length = last.getEndOffset () - getOffset ();
122             }
123         }
124         return length;
125     }
126     
127     /**
128      * Returns path from this item to the item on given offset.
129      *
130      * @param offset offset
131      *
132      * @return path from this item to the item on given offset
133      */

134     public ASTPath findPath (int offset) {
135         return findPath (new ArrayList JavaDoc<ASTItem> (), offset);
136     }
137     
138     private ASTPath findPath (List JavaDoc<ASTItem> path, int offset) {
139         if (offset < getOffset ()) return ASTPath.create (path);
140         if (offset > getEndOffset ()) return ASTPath.create (path);
141         path.add (this);
142         if (getChildren ().isEmpty ())
143             return ASTPath.create (path);
144         if (getChildren ().size () > 10)
145             return findPath2 (path, offset);
146         Iterator JavaDoc<ASTItem> it = getChildren ().iterator ();
147         while (it.hasNext ()) {
148             ASTItem item = it.next ();
149             if (offset < item.getEndOffset () &&
150                 item.getOffset () <= offset
151             )
152                 return item.findPath (path, offset);
153         }
154         return ASTPath.create (path);
155     }
156
157     private ASTPath findPath2 (List JavaDoc<ASTItem> path, int offset) {
158         TreeMap JavaDoc<Integer JavaDoc,ASTItem> childrenMap = getChildrenMap ();
159         SortedMap JavaDoc<Integer JavaDoc,ASTItem> headMap = childrenMap.headMap (new Integer JavaDoc (offset + 1));
160         if (headMap.isEmpty ())
161             return ASTPath.create (path);
162         Integer JavaDoc key = headMap.lastKey ();
163         ASTItem item = childrenMap.get (key);
164         ASTPath path2 = item.findPath (path, offset);
165         if (path2 == null)
166             return ASTPath.create (path);
167         return path2;
168     }
169     
170     private TreeMap JavaDoc<Integer JavaDoc,ASTItem> childrenMap = null;
171     
172     private TreeMap JavaDoc<Integer JavaDoc,ASTItem> getChildrenMap () {
173         if (childrenMap == null) {
174             childrenMap = new TreeMap JavaDoc<Integer JavaDoc,ASTItem> ();
175             Iterator JavaDoc<ASTItem> it = getChildren ().iterator ();
176             while (it.hasNext ()) {
177                 ASTItem item = it.next ();
178                 childrenMap.put (new Integer JavaDoc (item.getOffset ()), item);
179             }
180         }
181         return childrenMap;
182     }
183 }
Popular Tags