1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29
30
31
32 private Hashtable table = new Hashtable();
33
34
35
36 public AbstractCommand()
37 {
38 }
39
40
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 }