KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > decl > parser > ParserReader


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 package org.netbeans.tax.decl.parser;
20
21 import java.io.*;
22
23 import org.netbeans.tax.*;
24
25 /**
26  * A simple kind of reader with some enhanced methods
27  * suitable for parsing purposes.
28  */

29 public class ParserReader extends PushbackReader {
30
31     /** */
32     // private TreeElementDecl elementDecl;
33

34
35     //
36
// init
37
//
38

39     public ParserReader (String JavaDoc source) {
40         super (new StringReader (source), 20);
41
42         // this.elementDecl = elementDecl;
43
}
44
45
46     //
47
// itself
48
//
49

50     // /**
51
// */
52
// public final TreeElementDecl getRoot () {
53
// return elementDecl;
54
// }
55

56     /** Trim out starting whitespaces. */
57     public ParserReader trim () {
58         int ch;
59         while ( true ) {
60             // read until non WS or EOF
61
try {
62                 ch = read ();
63                 if (ch == -1 || ! Character.isWhitespace ((char)ch))
64                     break;
65             } catch (IOException ex) {
66                 ex.printStackTrace ();
67             }
68         }
69         
70         try {
71             if (ch != -1) unread (ch);
72         } catch (IOException ex) {
73             ex.printStackTrace ();
74         }
75         
76         return this;
77     }
78     
79     /** SE: Move on if true with otherwise push back.
80      * @return true if prefix is at the beginig of the stream
81      */

82     public boolean startsWith (String JavaDoc prefix) {
83         char buf[] = new char[prefix.length ()];
84         try {
85             read (buf);
86             // if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("startsWith(" + prefix + " got " + new String(buf)); // NOI18N
87
boolean ret = new String JavaDoc (buf).equals (prefix);
88             if (ret) return true;
89         } catch (IOException ex) {
90             ex.printStackTrace ();
91             return false;
92         }
93         
94         try {
95             unread (buf);
96         } catch (IOException ex) {
97             ex.printStackTrace ();
98         }
99         return false;
100     }
101     
102     /** @return next character or -1 */
103     public int peek () {
104         try {
105             int ch = read ();
106             unread (ch);
107             return ch;
108         } catch (IOException ex) {
109             return -1;
110         }
111     }
112     
113     /** @return whitespace or "()?+*" separated token or "". */ // NOI18N
114
public String JavaDoc getToken () {
115         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
116         
117         int ch = -1;
118         
119         trim ();
120         
121         boolean reading = true; //is a char in reading buffer
122
int len = 0;
123         
124         try {
125             readChars:
126                 while (reading) { //read until token recognized
127
ch = read ();
128                     if ( ch == -1 || Character.isWhitespace ((char)ch) )
129                         break;
130                     switch (ch) {
131                         //do not eat interesting chars
132
case ')': case '(': case '?': case '+': case '*':
133                             break readChars;
134                             
135                             //these are tokens alone
136
case ',': case '|':
137                             if (len == 0) {
138                                 reading = false; // finnish, no unread
139
} else {
140                                 break readChars; // finnish current token
141
}
142                     }
143                     
144                     sb.append ((char)ch);
145                     len++;
146                 }
147                 if (ch != -1 && reading) unread (ch);
148         } catch (IOException ex) {
149             // return most of recognized
150
}
151         
152         String JavaDoc toret = sb.toString ();
153         // if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Token: " + toret); // NOI18N
154
return toret;
155     }
156 }
157
Popular Tags