KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > java > SimpleNodeUtil


1 package org.incava.java;
2
3 import java.util.*;
4 import net.sourceforge.pmd.ast.JavaParserConstants;
5 import net.sourceforge.pmd.ast.SimpleNode;
6 import net.sourceforge.pmd.ast.Token;
7
8
9 /**
10  * Miscellaneous routines for the SimpleNode.
11  */

12 public class SimpleNodeUtil
13 {
14     /**
15      * Returns the token images for the node.
16      */

17     public static String JavaDoc toString(SimpleNode node)
18     {
19         Token tk = node.getFirstToken();
20         Token last = node.getLastToken();
21         String JavaDoc str = tk.image;
22         while (tk != last) {
23             tk = tk.next;
24             str += tk.image;
25         }
26         return str;
27     }
28
29     /**
30      * Returns whether the node has any children.
31      */

32     public static boolean hasChildren(SimpleNode node)
33     {
34         return node.jjtGetNumChildren() > 0;
35     }
36
37     /**
38      * Returns the parent node.
39      */

40     public static SimpleNode getParent(SimpleNode node)
41     {
42         return (SimpleNode)node.jjtGetParent();
43     }
44
45     /**
46      * Returns a list of children, both nodes and tokens.
47      */

48     public static List getChildren(SimpleNode node)
49     {
50         List list = new ArrayList();
51         
52         Token t = new Token();
53         t.next = node.getFirstToken();
54         
55         int nChildren = node.jjtGetNumChildren();
56         for (int ord = 0; ord < nChildren; ++ord) {
57             SimpleNode n = (SimpleNode)node.jjtGetChild(ord);
58             while (true) {
59                 t = t.next;
60                 if (t == n.getFirstToken()) {
61                     break;
62                 }
63                 list.add(t);
64             }
65             list.add(n);
66             t = n.getLastToken();
67         }
68
69         while (t != node.getLastToken()) {
70             t = t.next;
71             list.add(t);
72         }
73
74         return list;
75     }
76
77     /**
78      * Returns a list of children, optionally nodes and tokens.
79      */

80     public static List getChildren(SimpleNode node, boolean getNodes, boolean getTokens)
81     {
82         List list = new ArrayList();
83         
84         Token t = new Token();
85         t.next = node.getFirstToken();
86         
87         int nChildren = node.jjtGetNumChildren();
88         for (int ord = 0; ord < nChildren; ++ord) {
89             SimpleNode n = (SimpleNode)node.jjtGetChild(ord);
90             while (true) {
91                 t = t.next;
92                 if (t == n.getFirstToken()) {
93                     break;
94                 }
95                 if (getTokens) {
96                     list.add(t);
97                 }
98             }
99             if (getNodes) {
100                 list.add(n);
101             }
102             t = n.getLastToken();
103         }
104
105         while (t != node.getLastToken()) {
106             t = t.next;
107             if (getTokens) {
108                 list.add(t);
109             }
110         }
111
112         return list;
113     }
114
115     public static SimpleNode findChild(SimpleNode parent, Class JavaDoc childType)
116     {
117         return findChild(parent, childType, 0);
118     }
119
120     public static SimpleNode findChild(SimpleNode parent, Class JavaDoc childType, int index)
121     {
122         if (parent != null) {
123             int nChildren = parent.jjtGetNumChildren();
124             if (index >= 0 && index < nChildren) {
125                 int nFound = 0;
126                 for (int i = 0; i < nChildren; ++i) {
127                     SimpleNode child = (SimpleNode)parent.jjtGetChild(i);
128                     // tr.Ace.log("considering " + child.getClass() + " as match against " + childType);
129
if (childType == null || child.getClass().equals(childType)) {
130                         if (nFound == index) {
131                             // tr.Ace.log("got match");
132
return child;
133                         }
134                         else {
135                             ++nFound;
136                         }
137                     }
138                 }
139             }
140             else {
141                 tr.Ace.stack("WARNING: index " + index + " out of bounds (" + 0 + ", " + nChildren + ")");
142             }
143             // tr.Ace.log("no match for " + childType);
144
}
145         return null;
146     }
147
148     /**
149      * Returns a list of child tokens, non-hierarchically.
150      */

151     public static List getChildrenSerially(SimpleNode node)
152     {
153         return getChildrenSerially(node, null);
154     }
155
156     /**
157      * Returns a list of child tokens, non-hierarchically.
158      */

159     public static List getChildrenSerially(SimpleNode node, List list)
160     {
161         List children = list;
162         if (children == null) {
163             children = new ArrayList();
164         }
165         
166         Token t = new Token();
167         t.next = node.getFirstToken();
168         
169         int nChildren = node.jjtGetNumChildren();
170         for (int ord = 0; ord < nChildren; ++ord) {
171             SimpleNode n = (SimpleNode)node.jjtGetChild(ord);
172             while (true) {
173                 t = t.next;
174                 if (t == n.getFirstToken()) {
175                     break;
176                 }
177                 children.add(t);
178             }
179             getChildrenSerially(n, children);
180
181             t = n.getLastToken();
182         }
183
184         while (t != node.getLastToken()) {
185             t = t.next;
186             children.add(t);
187         }
188
189         return children;
190     }
191
192     public static SimpleNode[] findChildren(SimpleNode parent, Class JavaDoc childType)
193     {
194         List kids = new ArrayList();
195         int nChildren = parent == null ? 0 : parent.jjtGetNumChildren();
196         for (int i = 0; i < nChildren; ++i) {
197             SimpleNode child = (SimpleNode)parent.jjtGetChild(i);
198             if (childType == null || child.getClass().equals(childType)) {
199                 kids.add(child);
200             }
201         }
202
203         if (childType == null) {
204             return (SimpleNode[])kids.toArray(new SimpleNode[0]);
205         }
206         else {
207             int size = kids.size();
208             SimpleNode[] ary = (SimpleNode[])java.lang.reflect.Array.newInstance(childType, size);
209             System.arraycopy(kids.toArray(), 0, ary, 0, size);
210             return ary;
211         }
212     }
213
214     /**
215      * Returns all children of the node.
216      */

217     public static SimpleNode[] findChildren(SimpleNode parent)
218     {
219         return findChildren(parent, null);
220     }
221
222     /**
223      * Returns the tokens for a node.
224      */

225     public static List getTokens(SimpleNode node)
226     {
227         // tr.Ace.log("node: " + node);
228

229         List tokens = new ArrayList();
230         Token tk = new Token();
231         tk.next = node.getFirstToken();
232
233         if (tk != null) {
234             tokens.add(tk);
235             // tr.Ace.log("node.getLastToken(): " + node.getLastToken());
236
do {
237                 tk = tk.next;
238                 // tr.Ace.log("token: " + tk);
239
tokens.add(tk);
240             } while (tk != node.getLastToken());
241         }
242         return tokens;
243     }
244
245     public static Token findToken(SimpleNode node, int tokenType)
246     {
247         List childTokens = getChildren(node, false, true);
248         Iterator it = childTokens.iterator();
249         while (it.hasNext()) {
250             Token tk = (Token)it.next();
251             if (tk.kind == tokenType) {
252                 return tk;
253             }
254         }
255         return null;
256     }
257
258     /**
259      * Returns whether the node has a matching token, occurring prior to any
260      * non-tokens (i.e., before any child nodes).
261      */

262     public static boolean hasLeadingToken(SimpleNode node, int tokenType)
263     {
264         return getLeadingToken(node, tokenType) != null;
265     }
266
267     /**
268      * Returns whether the node has a matching token, occurring prior to any
269      * non-tokens (i.e., before any child nodes).
270      */

271     public static Token getLeadingToken(SimpleNode node, int tokenType)
272     {
273         if (node.jjtGetNumChildren() > 0) {
274             SimpleNode n = (SimpleNode)node.jjtGetChild(0);
275
276             Token t = new Token();
277             t.next = node.getFirstToken();
278             
279             while (true) {
280                 t = t.next;
281                 if (t == n.getFirstToken()) {
282                     break;
283                 }
284                 else if (t.kind == tokenType) {
285                     return t;
286                 }
287             }
288         }
289
290         return null;
291     }
292
293     /**
294      * Returns the tokens preceding the first child of the node.
295      */

296     public static List getLeadingTokens(SimpleNode node)
297     {
298         List list = new ArrayList();
299         
300         if (node.jjtGetNumChildren() > 0) {
301             SimpleNode n = (SimpleNode)node.jjtGetChild(0);
302
303             Token t = new Token();
304             t.next = node.getFirstToken();
305             
306             while (true) {
307                 t = t.next;
308                 if (t == n.getFirstToken()) {
309                     break;
310                 }
311                 else {
312                     list.add(t);
313                 }
314             }
315         }
316
317         return list;
318     }
319
320     public static void print(SimpleNode node)
321     {
322         print(node, "");
323     }
324
325     public static void print(SimpleNode node, String JavaDoc prefix)
326     {
327         Token first = node.getFirstToken();
328         Token last = node.getLastToken();
329
330         tr.Ace.log(prefix + "<" + node.toString() + ">" + getLocation(first, last));
331     }
332
333     public static void dump(SimpleNode node, String JavaDoc prefix)
334     {
335         dump(node, prefix, false);
336     }
337
338     public static void dump(SimpleNode node, String JavaDoc prefix, boolean showWhitespace)
339     {
340         print(node, prefix);
341
342         List children = getChildren(node);
343         Iterator it = children.iterator();
344         while (it.hasNext()) {
345             Object JavaDoc obj = it.next();
346             if (obj instanceof Token) {
347                 Token tk = (Token)obj;
348                 
349                 if (showWhitespace) {
350                     Token st = tk.specialToken;
351                     if (st != null) {
352                         while (st.specialToken != null) {
353                             st = st.specialToken;
354                         }
355                         
356                         while (st != null) {
357                             String JavaDoc s = st.toString().replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
358                             tr.Ace.log(prefix + " s[" + getLocation(st, st) + "] \"" + s + "\"");
359                             st = st.next;
360                         }
361                     }
362                 }
363                 
364                 tr.Ace.log(prefix + " t" + getLocation(tk, tk) + " \"" + tk + "\" (" + tk.kind + ")");
365             }
366             else {
367                 SimpleNode sn = (SimpleNode)obj;
368                 dump(sn, prefix + " ", showWhitespace);
369             }
370         }
371     }
372
373     protected static String JavaDoc getLocation(Token t1, Token t2)
374     {
375         return "[" + t1.beginLine + ":" + t1.beginColumn + ":" + t2.endLine + ":" + t2.endColumn + "]";
376     }
377
378     /**
379      * Returns a numeric "level" for the node. Zero is public or abstract, one
380      * is protected, two is package, and three is private.
381      */

382     public static int getLevel(SimpleNode node)
383     {
384         List tokens = getLeadingTokens(node);
385         Iterator tit = tokens.iterator();
386         while (tit.hasNext()) {
387             Token t = (Token)tit.next();
388             switch (t.kind) {
389                 case JavaParserConstants.PUBLIC:
390                     // fallthrough
391
case JavaParserConstants.ABSTRACT:
392                     return 0;
393                 case JavaParserConstants.PROTECTED:
394                     return 1;
395                 case JavaParserConstants.PRIVATE:
396                     return 3;
397             }
398         }
399
400         // AKA "package"
401
return 2;
402     }
403
404 }
405
Popular Tags