1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.plugin.gift.net.lexer;
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26
27
28 /***
29 * StreamLexer
30 *
31 * Parses GiFT-Commands
32 *
33 * @author <a href="mailto:tvanlessen@taval.de">Tammo van Lessen</a>
34 * @version CVS $Id: StreamLexer.java,v 1.1 2003/10/05 00:20:32 squig Exp $
35 */
36 public class StreamLexer {
37
38
39 private BufferedReader reader;
40
41
42
43 /***
44 * Constructor for StreamLexer.
45 *
46 * @param in the input stream
47 */
48 public StreamLexer(InputStream in) {
49 reader = new BufferedReader(new InputStreamReader(in));
50 }
51
52
53
54 /***
55 * Main method, only for testing purposes.
56 *
57 * @param args commandline args
58 *
59 * @throws IOException
60 */
61 public static void main(String[] args) throws IOException {
62 Command command;
63 StreamLexer lexer = new StreamLexer(System.in);
64 while (!(command = lexer.parse()).getCommand().equals("QUIT")) {
65 System.out.println(command.print());
66 }
67 }
68
69 /***
70 * Parses the Inputstream and returns the corresponding command
71 * object.
72 *
73 * Blocks until a whole command is read from the stream.
74 *
75 * @return the parsed command object
76 *
77 * @throws IOException
78 */
79 public Command parse() throws IOException {
80 Command cmd = new Command();
81 cmd.parse(nextCommand());
82 return cmd;
83 }
84
85 private String nextCommand() throws IOException {
86 StringBuffer cmd = new StringBuffer();
87 int cint;
88
89 while ((cint = reader.read()) != ';') {
90 char ch = (char) cint;
91 cmd.append(ch);
92 }
93 return cmd.toString().trim() + ";";
94 }
95 }