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.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.Iterator;
30 import java.util.LinkedList;
31
32 import org.xnap.XNap;
33 import org.xnap.util.FileHelper;
34
35 /***
36 * Provides a set of common commands and static helper methods.
37 */
38 public class CommandHelper {
39
40
41
42
43
44
45
46 /***
47 * Adds some default commands to the {@link Executer}.
48 */
49 public static void init()
50 {
51 Executer.add(new HelpCmd());
52 Executer.add(new LicenseCmd());
53 Executer.add(new ReadmeCmd());
54 Executer.add(new StartGUICmd());
55 Executer.add(new StopGUICmd());
56 }
57
58 /***
59 * Prints <code>filename</code> to <code>console</code>.
60 */
61 public static void printFile(String filename, Console console)
62 {
63 InputStream inStream = FileHelper.getResourceAsStream(filename);
64 if (inStream == null) {
65 console.println("File " + filename + " not found");
66 return;
67 }
68
69 BufferedReader in = null;
70 try {
71 in = new BufferedReader(new InputStreamReader(inStream));
72
73 String s;
74 while ((s = in.readLine()) != null) {
75 console.println(s);
76 }
77 }
78 catch(IOException e) {
79 console.println("error: " + e.getMessage());
80 }
81 finally {
82 if (in != null) {
83 try {
84 in.close();
85 }
86 catch (IOException e) {
87 }
88 }
89 }
90 }
91
92
93
94 private static class HelpCmd extends AbstractCommand
95 {
96 public HelpCmd()
97 {
98 putValue(CMD, "help");
99 putValue(ALIASES, new String[] { "h", "?" });
100 putValue(PARAMETER, "[command]");
101 putValue(SHORT_HELP, "Shows help about command.");
102 }
103
104 public void execute(String command, Console console)
105 throws SyntaxException
106 {
107 String[] argv = tokenize(command);
108
109 if (argv.length == 1) {
110 LinkedList list
111 = new LinkedList(Arrays.asList(console.getCommands()));
112 Collections.sort(list, new CommandComparator());
113 for (Iterator i = list.iterator(); i.hasNext();) {
114 Command cmd = (Command)i.next();
115 console.println(cmd.getCommand() + " - "
116 + cmd.getShortHelp());
117 }
118 }
119 else if (argv.length == 2) {
120 Command cmd = console.getCommand(argv[1]);
121 if (cmd != null) {
122 console.println("Command : " + cmd.getCommand() + " "
123 + cmd.getParameter());
124
125 String[] aliases = cmd.getAliases();
126 if (aliases != null) {
127 StringBuffer sb = new StringBuffer();
128 for (int i = 0; i < aliases.length; i++) {
129 sb.append(" ");
130 sb.append(aliases[i]);
131 }
132 console.println("Aliases :" + sb.toString());
133 }
134 console.println("Description: " + cmd.getLongHelp());
135 }
136 else {
137 console.println("Command not found.");
138 }
139 }
140 else {
141 throw new SyntaxException();
142 }
143 }
144 }
145
146 private static class LicenseCmd extends AbstractCommand
147 {
148 public LicenseCmd()
149 {
150 putValue(CMD, "license");
151 putValue(SHORT_HELP, "Print license.");
152 }
153
154 public void execute(String command, Console console)
155 {
156 printFile("COPYING", console);
157 }
158 }
159
160 private static class ReadmeCmd extends AbstractCommand
161 {
162 public ReadmeCmd()
163 {
164 putValue(CMD, "readme");
165 putValue(SHORT_HELP, "Print readme.");
166 }
167
168 public void execute(String command, Console console)
169 {
170 printFile("README", console);
171 }
172 }
173
174 private static class StartGUICmd extends AbstractCommand
175 {
176 public StartGUICmd()
177 {
178 putValue(CMD, "startgui");
179 putValue(SHORT_HELP, "Starts the GUI.");
180 }
181
182 public void execute(String command, Console console)
183 {
184 XNap.startGUI();
185 }
186 }
187
188 private static class StopGUICmd extends AbstractCommand
189 {
190 public StopGUICmd()
191 {
192 putValue(CMD, "stopgui");
193 putValue(SHORT_HELP, "Stops the GUI.");
194 }
195
196 public void execute(String command, Console console)
197 {
198 XNap.stopGUI();
199 }
200 }
201
202 /***
203 * Compares {@link Command} objects by name.
204 */
205 private static class CommandComparator implements Comparator
206 {
207
208 public int compare(Object o1, Object o2)
209 {
210 return ((Command)o1).getCommand().compareToIgnoreCase
211 (((Command)o2).getCommand());
212 }
213
214 }
215
216 }