KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29
30 /**
31  * Represents one AST node.
32  *
33  * @author Jan Jancura
34  */

35 public final class ASTNode extends ASTItem {
36    
37     /**
38      * Creates new ASTNode.
39      *
40      * @param mimeType MIME type
41      * @param nt right side of grammar rule
42      * @param rule rule id
43      * @param children list of tokens (ASTToken) and subnodes (ASTNode)
44      * @param offset start offset of this AST node
45      *
46      * @return returns new instance of AST node
47      */

48     public static ASTNode create (
49         String JavaDoc mimeType,
50         String JavaDoc nt,
51         List JavaDoc<ASTItem> children,
52         int offset
53     ) {
54         return new ASTNode (mimeType, nt, offset, children);
55     }
56     
57     /**
58      * Creates new ASTNode.
59      *
60      * @param mimeType MIME type
61      * @param nt right side of grammar rule
62      * @param rule rule id
63      * @param offset start offset of this AST node
64      *
65      * @return returns new instance of AST node
66      */

67     public static ASTNode create (
68         String JavaDoc mimeType,
69         String JavaDoc nt,
70         int offset
71     ) {
72         return new ASTNode (mimeType, nt, offset, Collections.<ASTItem>emptyList ());
73     }
74
75     
76     private String JavaDoc nt;
77
78     private ASTNode (
79         String JavaDoc mimeType,
80         String JavaDoc nt,
81         int offset,
82         List JavaDoc<ASTItem> children
83     ) {
84         super (mimeType, offset, -1, children);
85         this.nt = nt;
86     }
87
88     /**
89      * Returns the name of non terminal.
90      *
91      * @return name of non terminal
92      */

93     public String JavaDoc getNT () {
94         return nt;
95     }
96
97     /**
98      * Returns id of rule that has created this node.
99      *
100      * @return id of rule that has created this node
101      */

102 // public int getRule () {
103
// return rule;
104
// }
105

106     /**
107      * Finds path to the first token defined by type and identifier or null.
108      *
109      * @param type a type of token or null
110      * @param identifier a value of token or null
111      *
112      * @return path to the first token defined by type and identifier or null
113      */

114     public ASTPath findToken (String JavaDoc type, String JavaDoc identifier) {
115         List JavaDoc<ASTItem> path = new ArrayList JavaDoc<ASTItem> ();
116         findToken (type, identifier, path);
117         if (path.isEmpty ()) return null;
118         return ASTPath.create (path);
119     }
120     
121     private boolean findToken (String JavaDoc type, String JavaDoc identifier, List JavaDoc<ASTItem> path) {
122         path.add (this);
123         Iterator JavaDoc it = getChildren ().iterator ();
124         while (it.hasNext ()) {
125             Object JavaDoc e = it.next ();
126             if (e instanceof ASTToken) {
127                 ASTToken t = (ASTToken) e;
128                 if (type != null && !type.equals (t.getType ())) continue;
129                 if (identifier != null && !identifier.equals (t.getIdentifier ())) continue;
130                 return true;
131             } else
132                 if (((ASTNode) e).findToken (type, identifier, path))
133                     return true;
134         }
135         path.remove (path.size () - 1);
136         return false;
137     }
138     
139     /**
140      * Returns top-most subnode of this node on given offset with given
141      * non terminal name.
142      *
143      * @param nt name of non terminal
144      * @param offset offset of node
145      *
146      * @return MIME top-most subnode of this node on given offset
147      * with given non terminal name
148      */

149     public ASTNode findNode (String JavaDoc nt, int offset) {
150         if (nt.equals (getNT ())) return this;
151         Iterator JavaDoc it = getChildren ().iterator ();
152         while (it.hasNext ()) {
153             Object JavaDoc e = (Object JavaDoc) it.next ();
154             if (e instanceof ASTNode) {
155                 ASTNode node = (ASTNode) e;
156                 if (node.getOffset () <= offset &&
157                     offset < node.getEndOffset ()
158                 )
159                     return node.findNode (nt, offset);
160             }
161         }
162         return null;
163     }
164
165     /**
166      * Returns identifier of some subtoken with given type.
167      *
168      * @param type type of subtoken to be returned
169      *
170      * @return identifier of some subtoken with given type
171      */

172     public String JavaDoc getTokenTypeIdentifier (String JavaDoc type) {
173         ASTToken token = getTokenType (type);
174         if (token == null) return null;
175         return token.getIdentifier ();
176     }
177     
178     /**
179      * Returns some subtoken with given type.
180      *
181      * @param type type of subtoken to be returned
182      *
183      * @return some subtoken with given type
184      */

185     public ASTToken getTokenType (String JavaDoc type) {
186         ASTNode node = this;
187         int i = type.lastIndexOf ('.');
188         if (i >= 0)
189             node = getNode (type.substring (0, i));
190         if (node == null) return null;
191         Object JavaDoc o = node.getChild ("token-type-" + type.substring (i + 1));
192         if (o == null) return null;
193         if (!(o instanceof ASTToken)) return null;
194         return (ASTToken) o;
195     }
196     
197     /**
198      * Returns child node of this node with given path ("foo.goo.boo").
199      *
200      * @param path "foo.goo.boo" like path to some subnode
201      *
202      * @return child node of this node with given path
203      */

204     public ASTNode getNode (String JavaDoc path) {
205         ASTNode node = this;
206         int s = 0, e = path.indexOf ('.');
207         while (e >= 0) {
208             node = (ASTNode) node.getChild ("node-" + path.substring (s, e));
209             if (node == null) return null;
210             s = e + 1;
211             e = path.indexOf ('.', s);
212         }
213         return (ASTNode) node.getChild ("node-" + path.substring (s));
214     }
215     
216     private Map JavaDoc<String JavaDoc,ASTItem> nameToChild = null;
217     
218     private Object JavaDoc getChild (String JavaDoc name) {
219         if (nameToChild == null) {
220             nameToChild = new HashMap JavaDoc<String JavaDoc,ASTItem> ();
221             Iterator JavaDoc<ASTItem> it = getChildren ().iterator ();
222             while (it.hasNext ()) {
223                 ASTItem item = it.next ();
224                 if (item instanceof ASTToken) {
225                     ASTToken t = (ASTToken) item;
226                     nameToChild.put ("token-type-" + t.getType (), t);
227                 } else {
228                     nameToChild.put (
229                         "node-" + ((ASTNode) item).getNT (),
230                         item
231                     );
232                 }
233             }
234         }
235         return nameToChild.get (name);
236     }
237     
238     /**
239      * Returns text representation of this node.
240      *
241      * @return text representation of this node
242      */

243     public String JavaDoc print () {
244         return print ("");
245     }
246     
247     private String JavaDoc print (String JavaDoc indent) {
248         StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
249         sb.append (indent).append ("ASTNode ").append (getNT ()).append (' ').
250             append (getOffset ()).append ('-').append (getEndOffset ());
251         indent = " " + indent;
252         Iterator JavaDoc it = getChildren ().iterator ();
253         while (it.hasNext ()) {
254             Object JavaDoc elem = it.next ();
255             if (elem instanceof ASTNode) {
256                 sb.append ('\n').append (((ASTNode) elem).print (indent));
257             } else
258                 sb.append ('\n').append (indent).append (elem);
259         }
260         return sb.toString ();
261     }
262     
263     /**
264      * Returns text content of this node.
265      *
266      * @return text content of this node
267      */

268     public String JavaDoc getAsText () {
269         StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
270         Iterator JavaDoc it = getChildren ().iterator ();
271         while (it.hasNext ()) {
272             Object JavaDoc elem = it.next ();
273             if (elem instanceof ASTNode)
274                 sb.append (((ASTNode) elem).getAsText ());
275             else
276                 sb.append (((ASTToken) elem).getIdentifier ());
277         }
278         return sb.toString ();
279     }
280     
281     /**
282      * Returns string representation of this object.
283      *
284      * @return string representation of this object
285      */

286     public String JavaDoc toString () {
287         StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
288         sb.append ("ASTNode ").append (getNT ()).append (' ').
289             append (getOffset ()).append ('-').append (getEndOffset ());
290         Iterator JavaDoc it = getChildren ().iterator ();
291         while (it.hasNext ()) {
292             Object JavaDoc elem = it.next ();
293             if (elem instanceof ASTNode)
294                 sb.append ("\n ").append (((ASTNode) elem).getNT () + "...");
295             else
296                 sb.append ("\n ").append (elem);
297         }
298         return sb.toString ();
299     }
300 }
301
Popular Tags