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.cmdl;
21  
22  import java.util.Hashtable;
23  
24  import org.xnap.util.QuotedStringTokenizer;
25  
26  public abstract class AbstractCommand implements Command {
27  
28      //--- Constant(s) ---
29  
30      //--- Data Field(s) ---
31  
32      private Hashtable table = new Hashtable();
33      
34      //--- Constructor(s) ---
35      
36      public AbstractCommand() 
37      {
38      }
39      
40      //--- Method(s) ---
41  
42  	public String[] getAliases()
43  	{
44  		return (String[])table.get(ALIASES);
45  	}
46  
47      public String getCommand()
48      {
49  		return (String)table.get(CMD);
50      }
51  
52      public String getLongHelp()
53      {
54  		Object s = table.get(LONG_HELP);
55  		return (s != null) ? s.toString() : getShortHelp();
56      }
57  
58      public String getParameter()
59      {
60  		Object s = table.get(PARAMETER);
61  		return (s != null) ? s.toString() : "";
62      }
63  
64      public String getShortHelp()
65      {
66  		Object s = table.get(SHORT_HELP);
67  		return (s != null) ? s.toString() : "Sorry, no help available.";
68      }
69  
70  	protected String[] tokenize(String command)
71  	{
72  		QuotedStringTokenizer t = new QuotedStringTokenizer(command);
73  		return t.getTokens();
74  	}
75  
76      protected void putValue(String key, Object value)
77      {
78  		table.put(key, value);
79      }
80  
81      protected int getInt(String[] argv, int min, int max) 
82  		throws SyntaxException, ParameterException
83      {
84  	    if (argv.length != 2) {
85  			throw(new SyntaxException());
86  	    }
87  
88  	    int i;
89  	    try {
90  			i = Integer.parseInt(argv[1]);
91  	    }
92  	    catch (NumberFormatException e) {
93  			throw(new SyntaxException());
94  	    }
95  
96  	    if (i < min || i > max) {
97  			throw new ParameterException("Invalid id.");
98  	    }
99  
100 	    return i;
101     }
102 
103 }