KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parser > TokenIterator


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  * TokenIterator.java
21  *
22  * Created on December 4, 2003, 11:59 AM
23  */

24
25 package org.netbeans.modules.javacore.parser;
26
27 import org.netbeans.lib.java.parser.Token;
28 import org.netbeans.lib.java.parser.Factory;
29 import org.netbeans.lib.java.parser.JScanner;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.io.Reader JavaDoc;
34
35 /**
36  *
37  * @author Tomas Hurka
38  */

39 public class TokenIterator {
40     JScanner scanner;
41     ASTProvider provider;
42     int tokenIndex;
43     Token currentToken;
44
45     public TokenIterator(InputStream JavaDoc stream,String JavaDoc sourceLevel) {
46     this(new InputStreamReader JavaDoc(stream),sourceLevel);
47     }
48     
49     public TokenIterator(Reader JavaDoc reader,String JavaDoc sourceLevel) {
50     this(reader, sourceLevel, false);
51     }
52
53     public TokenIterator(InputStream JavaDoc stream,String JavaDoc sourceLevel, boolean liteScan) {
54     this(new InputStreamReader JavaDoc(stream),sourceLevel,liteScan);
55     }
56     
57     public TokenIterator(Reader JavaDoc reader,String JavaDoc sourceLevel, boolean liteScan) {
58         scanner=Factory.getDefault().getScanner(reader,sourceLevel,liteScan);
59     }
60     
61     public TokenIterator(ASTProvider p) {
62         provider=p;
63     }
64     
65     public int getNextTokenType() throws IOException JavaDoc {
66         if (scanner!=null)
67             return scanner.yylex();
68         try {
69             currentToken=provider.getToken(tokenIndex++);
70             return currentToken != null ? currentToken.getType() : 0;
71         } catch (IndexOutOfBoundsException JavaDoc ex) {
72             // end of file
73
return 0;
74         }
75     }
76     
77     public String JavaDoc getIdentifierText() {
78         if (scanner!=null)
79             return scanner.yytext();
80         return (String JavaDoc)currentToken.getValue();
81     }
82
83     public int getIdentifierHash() {
84         if (scanner!=null)
85             return scanner.yyhash();
86         return currentToken.getValue().hashCode();
87     }
88
89     /**
90      * Returns true if a doc-comment deprecated tag was found.
91      */

92     public boolean isDeprecated() {
93         if (scanner != null) {
94         boolean b = scanner.isDeprecated();
95         scanner.resetDeprecatedFlag();
96         return b;
97     }
98     return currentToken.getDeprecatedFlag();
99     }
100 }
101
Popular Tags