Przeglądaj źródła

Extract interface for CommandParser

pull/729/head
Chris Smith 7 lat temu
rodzic
commit
c539721d58

+ 333
- 0
src/main/java/com/dmdirc/commandparser/parsers/BaseCommandParser.java Wyświetl plik

@@ -0,0 +1,333 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.commandparser.parsers;
24
+
25
+import com.dmdirc.commandparser.CommandArguments;
26
+import com.dmdirc.commandparser.CommandInfo;
27
+import com.dmdirc.commandparser.CommandInfoPair;
28
+import com.dmdirc.commandparser.CommandType;
29
+import com.dmdirc.commandparser.commands.Command;
30
+import com.dmdirc.commandparser.commands.CommandOptions;
31
+import com.dmdirc.commandparser.commands.ExternalCommand;
32
+import com.dmdirc.commandparser.commands.PreviousCommand;
33
+import com.dmdirc.commandparser.commands.context.CommandContext;
34
+import com.dmdirc.events.CommandErrorEvent;
35
+import com.dmdirc.events.UnknownCommandEvent;
36
+import com.dmdirc.interfaces.CommandController;
37
+import com.dmdirc.interfaces.Connection;
38
+import com.dmdirc.interfaces.EventBus;
39
+import com.dmdirc.interfaces.GroupChat;
40
+import com.dmdirc.interfaces.InputModel;
41
+import com.dmdirc.interfaces.WindowModel;
42
+import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
43
+import com.dmdirc.util.collections.RollingList;
44
+
45
+import java.io.Serializable;
46
+import java.util.HashMap;
47
+import java.util.Map;
48
+import java.util.Optional;
49
+
50
+import javax.annotation.Nonnull;
51
+import javax.annotation.Nullable;
52
+
53
+import static com.google.common.base.Preconditions.checkNotNull;
54
+
55
+/**
56
+ * Represents a generic command parser. A command parser takes a line of input from the user,
57
+ * determines if it is an attempt at executing a command (based on the character at the start of the
58
+ * string), and handles it appropriately.
59
+ */
60
+public abstract class BaseCommandParser implements Serializable, CommandParser {
61
+
62
+    /** A version number for this class. */
63
+    private static final long serialVersionUID = 1;
64
+    /** Commands that are associated with this parser. */
65
+    private final Map<String, CommandInfoPair> commands;
66
+    /** A history of commands that have been entered into this parser. */
67
+    private final RollingList<PreviousCommand> history;
68
+    /** Command manager to use. */
69
+    protected final CommandController commandManager;
70
+    /** Event bus to post events to. */
71
+    private final EventBus eventBus;
72
+
73
+    /**
74
+     * Creates a new instance of CommandParser.
75
+     *
76
+     * @param configManager  Config manager to read settings
77
+     * @param commandManager Command manager to load plugins from
78
+     * @param eventBus       The event bus to post events to.
79
+     */
80
+    protected BaseCommandParser(
81
+            final ReadOnlyConfigProvider configManager,
82
+            final CommandController commandManager,
83
+            final EventBus eventBus) {
84
+        this.eventBus = eventBus;
85
+        commands = new HashMap<>();
86
+        history = new RollingList<>(configManager.getOptionInt("general", "commandhistory"));
87
+        this.commandManager = commandManager;
88
+        loadCommands();
89
+    }
90
+
91
+    /** Loads the relevant commands into the parser. */
92
+    protected abstract void loadCommands();
93
+
94
+    @Override
95
+    public final void registerCommand(final Command command, final CommandInfo info) {
96
+        commands.put(info.getName().toLowerCase(), new CommandInfoPair(info, command));
97
+    }
98
+
99
+    @Override
100
+    public final void unregisterCommand(final CommandInfo info) {
101
+        commands.remove(info.getName().toLowerCase());
102
+    }
103
+
104
+    @Override
105
+    public Map<String, CommandInfoPair> getCommands() {
106
+        return new HashMap<>(commands);
107
+    }
108
+
109
+    @Override
110
+    public final void parseCommand(@Nonnull final WindowModel origin, final String line,
111
+                                   final boolean parseChannel) {
112
+        checkNotNull(origin);
113
+
114
+        final CommandArguments args = new CommandArguments(commandManager, line);
115
+        if (args.isCommand()) {
116
+            if (handleChannelCommand(origin, args, parseChannel)) {
117
+                return;
118
+            }
119
+
120
+            if (commands.containsKey(args.getCommandName().toLowerCase())) {
121
+                final CommandInfoPair pair = commands.get(args.getCommandName().toLowerCase());
122
+                addHistory(args.getStrippedLine());
123
+                executeCommand(origin, pair.getCommandInfo(), pair.getCommand(), args,
124
+                        getCommandContext(origin, pair.getCommandInfo(), pair.getCommand(), args));
125
+            } else {
126
+                handleInvalidCommand(origin, args);
127
+            }
128
+        } else {
129
+            handleNonCommand(origin, line);
130
+        }
131
+    }
132
+
133
+    /**
134
+     * Checks to see whether the inputted command is a channel or external command, and if it is
135
+     * whether one or more channels have been specified for its execution. If it is a channel or
136
+     * external command, and channels are specified, this method invoke the appropriate command
137
+     * parser methods to handle the command, and will return true. If the command is not handled,
138
+     * the method returns false.
139
+     *
140
+     * @param origin       The container which received the command
141
+     * @param args         The command and its arguments
142
+     * @param parseChannel Whether or not to try parsing channel names
143
+     *
144
+     * @return True iff the command was handled, false otherwise
145
+     */
146
+    protected boolean handleChannelCommand(@Nonnull final WindowModel origin,
147
+            final CommandArguments args, final boolean parseChannel) {
148
+        final boolean silent = args.isSilent();
149
+        final String command = args.getCommandName();
150
+        final String[] cargs = args.getArguments();
151
+        final Optional<Connection> connection = origin.getConnection();
152
+
153
+        if (cargs.length == 0
154
+                || !parseChannel
155
+                || !connection.isPresent()
156
+                || !commandManager.isChannelCommand(command)) {
157
+            return false;
158
+        }
159
+
160
+        final Connection server = connection.get();
161
+        final String[] parts = cargs[0].split(",");
162
+        boolean someValid = false;
163
+        for (String part : parts) {
164
+            someValid |= server.getGroupChatManager().isValidChannelName(part);
165
+        }
166
+
167
+        if (someValid) {
168
+            for (String channelName : parts) {
169
+                if (!server.getGroupChatManager().isValidChannelName(channelName)) {
170
+                    origin.getEventBus().publishAsync(new CommandErrorEvent(origin,
171
+                            "Invalid channel name: " + channelName));
172
+                    continue;
173
+                }
174
+
175
+                final String newCommandString = commandManager.getCommandChar()
176
+                        + (silent ? String.valueOf(commandManager.getSilenceChar()) : "")
177
+                        + args.getCommandName()
178
+                        + (cargs.length > 1 ? ' ' + args.getArgumentsAsString(1) : "");
179
+
180
+                final Optional<GroupChat> channel = server.getGroupChatManager()
181
+                        .getChannel(channelName);
182
+                if (channel.isPresent()) {
183
+                    channel.get().getWindowModel().getInputModel().map(InputModel::getCommandParser)
184
+                            .ifPresent(cp -> cp.parseCommand(origin, newCommandString, false));
185
+                } else {
186
+                    final Map.Entry<CommandInfo, Command> actCommand = commandManager.getCommand(
187
+                            CommandType.TYPE_CHANNEL, command);
188
+
189
+                    if (actCommand != null && actCommand.getValue() instanceof ExternalCommand) {
190
+                        ((ExternalCommand) actCommand.getValue()).execute(
191
+                                origin, server, channelName, silent,
192
+                                new CommandArguments(commandManager, newCommandString));
193
+                    }
194
+                }
195
+            }
196
+
197
+            return true;
198
+        }
199
+
200
+        return false;
201
+    }
202
+
203
+    /**
204
+     * Adds a command to this parser's history.
205
+     *
206
+     * @param command The command name and arguments that were used
207
+     */
208
+    private void addHistory(final String command) {
209
+        synchronized (history) {
210
+            final PreviousCommand pc = new PreviousCommand(command);
211
+            history.remove(pc);
212
+            history.add(pc);
213
+        }
214
+    }
215
+
216
+    /**
217
+     * Retrieves the most recent time that the specified command was used. Commands should not
218
+     * include command or silence chars.
219
+     *
220
+     * @param command The command to search for
221
+     *
222
+     * @return The timestamp that the command was used, or 0 if it wasn't
223
+     */
224
+    public long getCommandTime(final String command) {
225
+        long res = 0;
226
+
227
+        synchronized (history) {
228
+            for (PreviousCommand pc : history.getList()) {
229
+                if (pc.getLine().matches("(?i)" + command)) {
230
+                    res = Math.max(res, pc.getTime());
231
+                }
232
+            }
233
+        }
234
+
235
+        return res;
236
+    }
237
+
238
+    @Override
239
+    public void parseCommand(@Nonnull final WindowModel origin, final String line) {
240
+        parseCommand(origin, line, true);
241
+    }
242
+
243
+    @Override
244
+    public void parseCommandCtrl(final WindowModel origin, final String line) {
245
+        handleNonCommand(origin, line);
246
+    }
247
+
248
+    @Override
249
+    @Nullable
250
+    public CommandInfoPair getCommand(final String commandName) {
251
+        return commands.get(commandName);
252
+    }
253
+
254
+    /**
255
+     * Gets the context that the command will execute with.
256
+     *
257
+     * @param origin      The container which received the command
258
+     * @param commandInfo The command information object matched by the command
259
+     * @param command     The command to be executed
260
+     * @param args        The arguments to the command
261
+     *
262
+     * @return The context for the command.
263
+     */
264
+    protected abstract CommandContext getCommandContext(
265
+            final WindowModel origin,
266
+            final CommandInfo commandInfo,
267
+            final Command command,
268
+            final CommandArguments args);
269
+
270
+    /**
271
+     * Executes the specified command with the given arguments.
272
+     *
273
+     * @param origin      The container which received the command
274
+     * @param commandInfo The command information object matched by the command
275
+     * @param command     The command to be executed
276
+     * @param args        The arguments to the command
277
+     * @param context     The context to use when executing the command
278
+     */
279
+    protected abstract void executeCommand(
280
+            @Nonnull final WindowModel origin,
281
+            final CommandInfo commandInfo, final Command command,
282
+            final CommandArguments args, final CommandContext context);
283
+
284
+    /**
285
+     * Called when the user attempted to issue a command (i.e., used the command character) that
286
+     * wasn't found. It could be that the command has a different arity, or that it plain doesn't
287
+     * exist.
288
+     *
289
+     * @param origin The window in which the command was typed
290
+     * @param args   The arguments passed to the command
291
+     *
292
+     * @since 0.6.3m1
293
+     */
294
+    protected void handleInvalidCommand(final WindowModel origin,
295
+            final CommandArguments args) {
296
+        eventBus.publishAsync(new UnknownCommandEvent(origin, args.getCommandName(),
297
+                args.getArguments()));
298
+    }
299
+
300
+    /**
301
+     * Called when the input was a line of text that was not a command. This normally means it is
302
+     * sent to the server/channel/user as-is, with no further processing.
303
+     *
304
+     * @param origin The window in which the command was typed
305
+     * @param line   The line input by the user
306
+     */
307
+    protected abstract void handleNonCommand(final WindowModel origin,
308
+            final String line);
309
+
310
+    /**
311
+     * Determines if the specified command has defined any command options.
312
+     *
313
+     * @param command The command to investigate
314
+     *
315
+     * @return True if the command defines options, false otherwise
316
+     */
317
+    protected boolean hasCommandOptions(final Command command) {
318
+        return command.getClass().isAnnotationPresent(CommandOptions.class);
319
+    }
320
+
321
+    /**
322
+     * Retrieves the command options for the specified command. If the command does not define
323
+     * options, this method will return null.
324
+     *
325
+     * @param command The command whose options should be retrieved
326
+     *
327
+     * @return The command's options, or null if not available
328
+     */
329
+    protected CommandOptions getCommandOptions(final Command command) {
330
+        return command.getClass().getAnnotation(CommandOptions.class);
331
+    }
332
+
333
+}

+ 12
- 291
src/main/java/com/dmdirc/commandparser/parsers/CommandParser.java Wyświetl plik

@@ -1,5 +1,5 @@
1 1
 /*
2
- * Copyright (c) 2006-2015 DMDirc Developers
2
+ * Copyright (c) 2006-2016 DMDirc Developers
3 3
  *
4 4
  * Permission is hereby granted, free of charge, to any person obtaining a copy
5 5
  * of this software and associated documentation files (the "Software"), to deal
@@ -22,106 +22,42 @@
22 22
 
23 23
 package com.dmdirc.commandparser.parsers;
24 24
 
25
-import com.dmdirc.commandparser.CommandArguments;
26 25
 import com.dmdirc.commandparser.CommandInfo;
27 26
 import com.dmdirc.commandparser.CommandInfoPair;
28
-import com.dmdirc.commandparser.CommandType;
29 27
 import com.dmdirc.commandparser.commands.Command;
30
-import com.dmdirc.commandparser.commands.CommandOptions;
31
-import com.dmdirc.commandparser.commands.ExternalCommand;
32
-import com.dmdirc.commandparser.commands.PreviousCommand;
33
-import com.dmdirc.commandparser.commands.context.CommandContext;
34
-import com.dmdirc.events.CommandErrorEvent;
35
-import com.dmdirc.events.UnknownCommandEvent;
36
-import com.dmdirc.interfaces.CommandController;
37
-import com.dmdirc.interfaces.Connection;
38
-import com.dmdirc.interfaces.EventBus;
39
-import com.dmdirc.interfaces.GroupChat;
40
-import com.dmdirc.interfaces.InputModel;
41 28
 import com.dmdirc.interfaces.WindowModel;
42
-import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
43
-import com.dmdirc.util.collections.RollingList;
44
-
45
-import java.io.Serializable;
46
-import java.util.HashMap;
47
-import java.util.Map;
48
-import java.util.Optional;
49 29
 
50 30
 import javax.annotation.Nonnull;
51 31
 import javax.annotation.Nullable;
52
-
53
-import static com.google.common.base.Preconditions.checkNotNull;
32
+import java.util.Map;
54 33
 
55 34
 /**
56
- * Represents a generic command parser. A command parser takes a line of input from the user,
57
- * determines if it is an attempt at executing a command (based on the character at the start of the
58
- * string), and handles it appropriately.
35
+ * A command parser takes a line of input from the user, determines if it is an attempt at executing a command (based
36
+ * on the character at the start of the string), and handles it appropriately.
59 37
  */
60
-public abstract class CommandParser implements Serializable {
61
-
62
-    /** A version number for this class. */
63
-    private static final long serialVersionUID = 1;
64
-    /** Commands that are associated with this parser. */
65
-    private final Map<String, CommandInfoPair> commands;
66
-    /** A history of commands that have been entered into this parser. */
67
-    private final RollingList<PreviousCommand> history;
68
-    /** Command manager to use. */
69
-    protected final CommandController commandManager;
70
-    /** Event bus to post events to. */
71
-    private final EventBus eventBus;
72
-
73
-    /**
74
-     * Creates a new instance of CommandParser.
75
-     *
76
-     * @param configManager  Config manager to read settings
77
-     * @param commandManager Command manager to load plugins from
78
-     * @param eventBus       The event bus to post events to.
79
-     */
80
-    protected CommandParser(
81
-            final ReadOnlyConfigProvider configManager,
82
-            final CommandController commandManager,
83
-            final EventBus eventBus) {
84
-        this.eventBus = eventBus;
85
-        commands = new HashMap<>();
86
-        history = new RollingList<>(configManager.getOptionInt("general", "commandhistory"));
87
-        this.commandManager = commandManager;
88
-        loadCommands();
89
-    }
90
-
91
-    /** Loads the relevant commands into the parser. */
92
-    protected abstract void loadCommands();
38
+public interface CommandParser {
93 39
 
94 40
     /**
95 41
      * Registers the specified command with this parser.
96 42
      *
97
-     * @since 0.6.3m1
98 43
      * @param command Command to be registered
99 44
      * @param info    The information the command should be registered with
100 45
      */
101
-    public final void registerCommand(final Command command, final CommandInfo info) {
102
-        commands.put(info.getName().toLowerCase(), new CommandInfoPair(info, command));
103
-    }
46
+    void registerCommand(Command command, CommandInfo info);
104 47
 
105 48
     /**
106 49
      * Unregisters the specified command with this parser.
107 50
      *
108 51
      * @param info Command information to be unregistered
109
-     *
110
-     * @since 0.6.3m1
111 52
      */
112
-    public final void unregisterCommand(final CommandInfo info) {
113
-        commands.remove(info.getName().toLowerCase());
114
-    }
53
+    void unregisterCommand(CommandInfo info);
115 54
 
116 55
     /**
117 56
      * Retrieves a map of commands known by this command parser.
118 57
      *
119
-     * @since 0.6.3m1
120 58
      * @return A map of commands known to this parser
121 59
      */
122
-    public Map<String, CommandInfoPair> getCommands() {
123
-        return new HashMap<>(commands);
124
-    }
60
+    Map<String, CommandInfoPair> getCommands();
125 61
 
126 62
     /**
127 63
      * Parses the specified string as a command.
@@ -129,148 +65,16 @@ public abstract class CommandParser implements Serializable {
129 65
      * @param origin       The container which received the command
130 66
      * @param line         The line to be parsed
131 67
      * @param parseChannel Whether or not to try and parse the first argument as a channel name
132
-     *
133
-     * @since 0.6.4
134
-     */
135
-    public final void parseCommand(@Nonnull final WindowModel origin, final String line,
136
-            final boolean parseChannel) {
137
-        checkNotNull(origin);
138
-
139
-        final CommandArguments args = new CommandArguments(commandManager, line);
140
-        if (args.isCommand()) {
141
-            if (handleChannelCommand(origin, args, parseChannel)) {
142
-                return;
143
-            }
144
-
145
-            if (commands.containsKey(args.getCommandName().toLowerCase())) {
146
-                final CommandInfoPair pair = commands.get(args.getCommandName().toLowerCase());
147
-                addHistory(args.getStrippedLine());
148
-                executeCommand(origin, pair.getCommandInfo(), pair.getCommand(), args,
149
-                        getCommandContext(origin, pair.getCommandInfo(), pair.getCommand(), args));
150
-            } else {
151
-                handleInvalidCommand(origin, args);
152
-            }
153
-        } else {
154
-            handleNonCommand(origin, line);
155
-        }
156
-    }
157
-
158
-    /**
159
-     * Checks to see whether the inputted command is a channel or external command, and if it is
160
-     * whether one or more channels have been specified for its execution. If it is a channel or
161
-     * external command, and channels are specified, this method invoke the appropriate command
162
-     * parser methods to handle the command, and will return true. If the command is not handled,
163
-     * the method returns false.
164
-     *
165
-     * @param origin       The container which received the command
166
-     * @param args         The command and its arguments
167
-     * @param parseChannel Whether or not to try parsing channel names
168
-     *
169
-     * @return True iff the command was handled, false otherwise
170
-     */
171
-    protected boolean handleChannelCommand(@Nonnull final WindowModel origin,
172
-            final CommandArguments args, final boolean parseChannel) {
173
-        final boolean silent = args.isSilent();
174
-        final String command = args.getCommandName();
175
-        final String[] cargs = args.getArguments();
176
-        final Optional<Connection> connection = origin.getConnection();
177
-
178
-        if (cargs.length == 0
179
-                || !parseChannel
180
-                || !connection.isPresent()
181
-                || !commandManager.isChannelCommand(command)) {
182
-            return false;
183
-        }
184
-
185
-        final Connection server = connection.get();
186
-        final String[] parts = cargs[0].split(",");
187
-        boolean someValid = false;
188
-        for (String part : parts) {
189
-            someValid |= server.getGroupChatManager().isValidChannelName(part);
190
-        }
191
-
192
-        if (someValid) {
193
-            for (String channelName : parts) {
194
-                if (!server.getGroupChatManager().isValidChannelName(channelName)) {
195
-                    origin.getEventBus().publishAsync(new CommandErrorEvent(origin,
196
-                            "Invalid channel name: " + channelName));
197
-                    continue;
198
-                }
199
-
200
-                final String newCommandString = commandManager.getCommandChar()
201
-                        + (silent ? String.valueOf(commandManager.getSilenceChar()) : "")
202
-                        + args.getCommandName()
203
-                        + (cargs.length > 1 ? ' ' + args.getArgumentsAsString(1) : "");
204
-
205
-                final Optional<GroupChat> channel = server.getGroupChatManager()
206
-                        .getChannel(channelName);
207
-                if (channel.isPresent()) {
208
-                    channel.get().getWindowModel().getInputModel().map(InputModel::getCommandParser)
209
-                            .ifPresent(cp -> cp.parseCommand(origin, newCommandString, false));
210
-                } else {
211
-                    final Map.Entry<CommandInfo, Command> actCommand = commandManager.getCommand(
212
-                            CommandType.TYPE_CHANNEL, command);
213
-
214
-                    if (actCommand != null && actCommand.getValue() instanceof ExternalCommand) {
215
-                        ((ExternalCommand) actCommand.getValue()).execute(
216
-                                origin, server, channelName, silent,
217
-                                new CommandArguments(commandManager, newCommandString));
218
-                    }
219
-                }
220
-            }
221
-
222
-            return true;
223
-        }
224
-
225
-        return false;
226
-    }
227
-
228
-    /**
229
-     * Adds a command to this parser's history.
230
-     *
231
-     * @param command The command name and arguments that were used
232
-     */
233
-    private void addHistory(final String command) {
234
-        synchronized (history) {
235
-            final PreviousCommand pc = new PreviousCommand(command);
236
-            history.remove(pc);
237
-            history.add(pc);
238
-        }
239
-    }
240
-
241
-    /**
242
-     * Retrieves the most recent time that the specified command was used. Commands should not
243
-     * include command or silence chars.
244
-     *
245
-     * @param command The command to search for
246
-     *
247
-     * @return The timestamp that the command was used, or 0 if it wasn't
248 68
      */
249
-    public long getCommandTime(final String command) {
250
-        long res = 0;
251
-
252
-        synchronized (history) {
253
-            for (PreviousCommand pc : history.getList()) {
254
-                if (pc.getLine().matches("(?i)" + command)) {
255
-                    res = Math.max(res, pc.getTime());
256
-                }
257
-            }
258
-        }
259
-
260
-        return res;
261
-    }
69
+    void parseCommand(@Nonnull WindowModel origin, String line, boolean parseChannel);
262 70
 
263 71
     /**
264 72
      * Parses the specified string as a command.
265 73
      *
266 74
      * @param origin The container which received the command
267 75
      * @param line   The line to be parsed
268
-     *
269
-     * @since 0.6.4
270 76
      */
271
-    public void parseCommand(@Nonnull final WindowModel origin, final String line) {
272
-        parseCommand(origin, line, true);
273
-    }
77
+    void parseCommand(@Nonnull WindowModel origin, String line);
274 78
 
275 79
     /**
276 80
      * Handles the specified string as a non-command.
@@ -278,9 +82,7 @@ public abstract class CommandParser implements Serializable {
278 82
      * @param origin The window in which the command was typed
279 83
      * @param line   The line to be parsed
280 84
      */
281
-    public void parseCommandCtrl(final WindowModel origin, final String line) {
282
-        handleNonCommand(origin, line);
283
-    }
85
+    void parseCommandCtrl(WindowModel origin, String line);
284 86
 
285 87
     /**
286 88
      * Gets the command with the given name that was previously registered with this parser.
@@ -289,87 +91,6 @@ public abstract class CommandParser implements Serializable {
289 91
      * @return The command info pair, or {@code null} if the command does not exist.
290 92
      */
291 93
     @Nullable
292
-    public CommandInfoPair getCommand(final String commandName) {
293
-        return commands.get(commandName);
294
-    }
295
-
296
-    /**
297
-     * Gets the context that the command will execute with.
298
-     *
299
-     * @param origin      The container which received the command
300
-     * @param commandInfo The command information object matched by the command
301
-     * @param command     The command to be executed
302
-     * @param args        The arguments to the command
303
-     *
304
-     * @return The context for the command.
305
-     */
306
-    protected abstract CommandContext getCommandContext(
307
-            final WindowModel origin,
308
-            final CommandInfo commandInfo,
309
-            final Command command,
310
-            final CommandArguments args);
311
-
312
-    /**
313
-     * Executes the specified command with the given arguments.
314
-     *
315
-     * @param origin      The container which received the command
316
-     * @param commandInfo The command information object matched by the command
317
-     * @param command     The command to be executed
318
-     * @param args        The arguments to the command
319
-     * @param context     The context to use when executing the command
320
-     */
321
-    protected abstract void executeCommand(
322
-            @Nonnull final WindowModel origin,
323
-            final CommandInfo commandInfo, final Command command,
324
-            final CommandArguments args, final CommandContext context);
325
-
326
-    /**
327
-     * Called when the user attempted to issue a command (i.e., used the command character) that
328
-     * wasn't found. It could be that the command has a different arity, or that it plain doesn't
329
-     * exist.
330
-     *
331
-     * @param origin The window in which the command was typed
332
-     * @param args   The arguments passed to the command
333
-     *
334
-     * @since 0.6.3m1
335
-     */
336
-    protected void handleInvalidCommand(final WindowModel origin,
337
-            final CommandArguments args) {
338
-        eventBus.publishAsync(new UnknownCommandEvent(origin, args.getCommandName(),
339
-                args.getArguments()));
340
-    }
341
-
342
-    /**
343
-     * Called when the input was a line of text that was not a command. This normally means it is
344
-     * sent to the server/channel/user as-is, with no further processing.
345
-     *
346
-     * @param origin The window in which the command was typed
347
-     * @param line   The line input by the user
348
-     */
349
-    protected abstract void handleNonCommand(final WindowModel origin,
350
-            final String line);
351
-
352
-    /**
353
-     * Determines if the specified command has defined any command options.
354
-     *
355
-     * @param command The command to investigate
356
-     *
357
-     * @return True if the command defines options, false otherwise
358
-     */
359
-    protected boolean hasCommandOptions(final Command command) {
360
-        return command.getClass().isAnnotationPresent(CommandOptions.class);
361
-    }
362
-
363
-    /**
364
-     * Retrieves the command options for the specified command. If the command does not define
365
-     * options, this method will return null.
366
-     *
367
-     * @param command The command whose options should be retrieved
368
-     *
369
-     * @return The command's options, or null if not available
370
-     */
371
-    protected CommandOptions getCommandOptions(final Command command) {
372
-        return command.getClass().getAnnotation(CommandOptions.class);
373
-    }
94
+    CommandInfoPair getCommand(String commandName);
374 95
 
375 96
 }

+ 1
- 1
src/main/java/com/dmdirc/commandparser/parsers/GlobalCommandParser.java Wyświetl plik

@@ -45,7 +45,7 @@ import static com.dmdirc.util.LogUtils.USER_ERROR;
45 45
  * The command parser used for global commands.
46 46
  */
47 47
 @Singleton
48
-public class GlobalCommandParser extends CommandParser {
48
+public class GlobalCommandParser extends BaseCommandParser {
49 49
 
50 50
     private static final Logger LOG = LoggerFactory.getLogger(GlobalCommandParser.class);
51 51
     /** A version number for this class. */

+ 2
- 2
src/test/java/com/dmdirc/harness/TestCommandParser.java Wyświetl plik

@@ -26,7 +26,7 @@ import com.dmdirc.commandparser.CommandArguments;
26 26
 import com.dmdirc.commandparser.CommandInfo;
27 27
 import com.dmdirc.commandparser.commands.Command;
28 28
 import com.dmdirc.commandparser.commands.context.CommandContext;
29
-import com.dmdirc.commandparser.parsers.CommandParser;
29
+import com.dmdirc.commandparser.parsers.BaseCommandParser;
30 30
 import com.dmdirc.interfaces.CommandController;
31 31
 import com.dmdirc.interfaces.EventBus;
32 32
 import com.dmdirc.interfaces.WindowModel;
@@ -34,7 +34,7 @@ import com.dmdirc.interfaces.config.AggregateConfigProvider;
34 34
 
35 35
 import javax.annotation.Nonnull;
36 36
 
37
-public class TestCommandParser extends CommandParser {
37
+public class TestCommandParser extends BaseCommandParser {
38 38
 
39 39
     private static final long serialVersionUID = 7073002401375438532L;
40 40
 

Ładowanie…
Anuluj
Zapisz