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 import java.util.LinkedList;
24 import java.util.StringTokenizer;
25
26 /***
27 * This class is thread safe.
28 */
29 public class Executer {
30
31
32
33 private static Executer singleton = new Executer();
34
35 private Hashtable commandByAlias = new Hashtable();
36 private DefaultCompleter completer = new DefaultCompleter();
37
38
39
40 private Executer()
41 {
42 }
43
44
45
46 /***
47 * Returns the default completer.
48 */
49 public static Completer getCompleter()
50 {
51 return singleton.completer;
52 }
53
54 /***
55 * Adds command. Only the first command is added to the readline completer.
56 */
57 public static void add(Command command)
58 {
59 synchronized (singleton) {
60 singleton.commandByAlias.put(command.getCommand(), command);
61 singleton.completer.add(command.getCommand());
62
63 String[] aliases = command.getAliases();
64 if (aliases != null) {
65 for (int i = 0; i < aliases.length; i++) {
66 singleton.commandByAlias.put(aliases[i], command);
67 }
68 }
69 }
70 }
71
72 public static void add(Command[] commands)
73 {
74 for (int i = 0; i < commands.length; i++) {
75 add(commands[i]);
76 }
77 }
78
79 public static Command getCommand(String command)
80 {
81 synchronized (singleton) {
82 return (Command)singleton.commandByAlias.get(command);
83 }
84 }
85
86 public static Command[] getCommands()
87 {
88 synchronized (singleton) {
89 LinkedList list = new LinkedList();
90 String[] names = singleton.completer.getCompletions();
91 for (int i = 0; i < names.length; i++) {
92 list.add(getCommand(names[i]));
93 }
94 return (Command[])list.toArray(new Command[0]);
95 }
96 }
97
98 /***
99 * @return true, if the command existed and was executed
100 */
101 public static boolean parse(String args, Console console)
102 {
103 if (args == null) {
104 return false;
105 }
106 if (args.startsWith("/")) {
107 args = args.substring(1);
108 }
109
110 StringTokenizer t = new StringTokenizer(args);
111 if (!t.hasMoreTokens()) {
112 return false;
113 }
114
115 if (!console.isEchoing()) {
116 console.println("XNap> " + args);
117 }
118
119 Command cmd = console.getCommand(t.nextToken());
120 if (cmd != null) {
121 try {
122 cmd.execute(args, console);
123 }
124 catch (SyntaxException e) {
125 console.println("Usage: " + cmd.getCommand()
126 + " " + cmd.getParameter());
127 }
128 catch (ParameterException e) {
129 console.println(e.getMessage());
130 }
131
132 return true;
133 }
134
135 return false;
136 }
137
138 public static void remove(Command command)
139 {
140 synchronized (singleton) {
141 singleton.commandByAlias.remove(command.getCommand());
142 singleton.completer.remove(command.getCommand());
143
144 String[] aliases = command.getAliases();
145 if (aliases != null) {
146 for (int i = 0; i < aliases.length; i++) {
147 singleton.commandByAlias.remove(aliases[i]);
148 }
149 }
150 }
151 }
152
153 public static void remove(Command[] commands)
154 {
155 for (int i = 0; i < commands.length; i++) {
156 remove(commands[i]);
157 }
158 }
159
160 }