View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      //~ Instance fields --------------------------------------------------------
38  
39      private BufferedReader reader;
40  
41      //~ Constructors -----------------------------------------------------------
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      //~ Methods ----------------------------------------------------------------
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  		// read until command is complete!
89          while ((cint = reader.read()) != ';') {
90              char ch = (char) cint;
91              cmd.append(ch);
92          }
93          return cmd.toString().trim() + ";";
94      }
95  }