KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > Parser


1 package example;
2
3 import com.caucho.util.L10N;
4 import java.util.logging.Logger JavaDoc;
5 import java.util.logging.Level JavaDoc;
6
7 import com.caucho.vfs.ReadStream;
8 import java.io.IOException JavaDoc;
9 import java.util.HashMap JavaDoc;
10
11 /**
12  * Parse a request made using the magic8ball protocol, returning
13  * appropriate objects that are instances of AbstractCommand.
14  *
15  * This class is both a parser and a command factory.
16  */

17 public class Parser {
18   static protected final Logger JavaDoc log =
19     Logger.getLogger(Parser.class.getName());
20   static final L10N L = new L10N(Parser.class);
21
22   // set in init() for each new parse
23
private ReadStream _readStream;
24   String JavaDoc _error;
25
26   // parsing buffer
27
private StringBuffer JavaDoc _buffer = new StringBuffer JavaDoc();
28
29   // commands
30
private HashMap JavaDoc _commands = new HashMap JavaDoc();
31
32   public Parser()
33   {
34     _commands.put("set-prophet",new SetProphetCommand());
35     _commands.put("ask",new AskCommand());
36   }
37
38   public void init(ReadStream readStream)
39   {
40     _readStream = readStream;
41     _error = null;
42   }
43
44
45   /**
46    * Parse one command out of the ReadStream.
47    * Once the last command is encountered, null is returned.
48    *
49    * @return null if there were no more commands to parse, or there is a parse
50    * error.
51    */

52   public AbstractCommand nextCommand()
53     throws IOException JavaDoc
54   {
55     String JavaDoc cmd = parseToken();
56     if (cmd == null) {
57       return null;
58     }
59     AbstractCommand command = (AbstractCommand) _commands.get(cmd.toLowerCase());
60
61     if (command == null) {
62       _error = "Unknown command `" + cmd + "'";
63     } else {
64       command.parse(this);
65       if (command.isError()) {
66         _error = command.getError();
67         command = null;
68       }
69     }
70
71     return command;
72   }
73
74   public boolean isError()
75   {
76     return _error != null;
77   }
78
79   public String JavaDoc getError()
80   {
81     return _error;
82   }
83
84
85   /**
86    * @return true if ch is an indication that the end-of-stream was reached
87    */

88   boolean isEOS(int ch)
89   {
90     return ch < 0;
91   }
92
93   /**
94    * @return true if ch is a whitespace character or end-of-stream indicator
95    */

96   boolean isWhitespace(int ch)
97   {
98     return isEOS(ch) || Character.isWhitespace((char) ch);
99   }
100
101   /**
102    * Eat whitespace characters out of readStream
103    *
104    * @return the first non-whitespace character (which
105    * is still in the _readStream), or -1 if end of stream encountered.
106    */

107   int eatWhitespace()
108     throws IOException JavaDoc
109   {
110     int ch;
111     while (!isEOS(ch = _readStream.read())) {
112       if (!isWhitespace(ch)) {
113         _readStream.unread();
114         break;
115       }
116     }
117     return ch;
118   }
119
120   /**
121    * Parse optional whitespace then a token containing no whitespace.
122    *
123    * @return the token, or null if there are no tokens left on the stream
124    */

125
126   String JavaDoc parseToken()
127     throws IOException JavaDoc
128   {
129     String JavaDoc token = null;
130
131     int ch = eatWhitespace();
132
133     if (ch >= 0) {
134       _buffer.setLength(0);
135
136       while (!isEOS(ch = _readStream.read())) {
137         if (isWhitespace(ch)) {
138           _readStream.unread();
139           break;
140         }
141         _buffer.append((char)ch);
142       }
143       token = _buffer.toString();
144     }
145
146     return token;
147   }
148 }
149
150
Popular Tags