1 /*
2  *  CVS: $Id: CommandParser.java,v 1.3 2004/07/16 16:22:33 marcus Exp $
3  * 
4  *  This file is part of JZuul.
5  *
6  *  JZuul 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; either version 2 of the License, or
9  *  (at your option) any later version.
10 *
11 *  JZuul is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with Zuul; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 * 
20 *  Copyrigth 2004 by marcus, leh
21 *  Initially based on an example by Michael Kolling and David J. Barnes
22 * 
23 */
24 
25package org.jzuul.engine;
26
27/**
28 * Dieser Parser liest Benutzereingaben und wandelt sie in
29 * Befehle für das Adventure-Game um. Bei jedem Aufruf
30 * liest er eine Zeile von der Konsole und versucht, diese als
31 * einen Befehl aus bis zu zwei Wörtern zu interpretieren. Er
32 * liefert den Befehl als ein Objekt der Klasse Befehl zurück.
33 * 
34 * Der Parser verfügt über einen Satz an bekannten Befehlen. Er
35 * vergleicht die Eingabe mit diesen Befehlen. Wenn die Eingabe
36 * keinen bekannten Befehl enthält, dann liefert der Parser ein als 
37 * unbekannter Befehl gekennzeichnetes Objekt zurück.
38 * 
39 * 
40 * @version $Revision: 1.3 $
41 */
42
43public class CommandParser {
44
45    /**
46     * Erstellt aus dem gegebenen String ein CommandContainer Objekt
47     * 
48     * @param eingabezeile  ein einzeiliger String mit einer Eingabe
49     * @return  ein CommandContainer Objekt oder null bei einem Fehler
50     */
51    public CommandContainer liefereBefehl(String eingabezeile) {
52        
53        CommandContainer retval;
54            if (eingabezeile == null) {
55                System.exit(0);
56            }
57
58            String[] commandline = eingabezeile.split(" ");
59
60            if ((commandline != null) && (commandline.length > 0)) {
61                retval = new CommandContainer(commandline[0]);
62                String[] arguments = new String[commandline.length - 1];
63                for (int i = 0; i < commandline.length - 1; i++) {
64                    arguments[i] = (commandline[i + 1]).toLowerCase();
65                }
66                retval.setArgs(arguments);
67                return retval;
68            }
69        return null;
70    }
71}
72