/
CommandArguments.java
242 lines (209 loc) · 7.9 KB
/
CommandArguments.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (c) 2006-2015 DMDirc Developers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.dmdirc.commandparser;
import com.dmdirc.Precondition;
import com.dmdirc.interfaces.CommandController;
import java.util.Arrays;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.google.common.base.Preconditions.checkPositionIndex;
/**
* Represents a command and its arguments. In this class, input is split into 'words' which are
* separated by any number of whitespace characters; 'arguments' are the same but exclude the first
* word, which will normally be the command name.
*
* @since 0.6.3m1
*/
public class CommandArguments {
/** The raw line that was input. */
private final String line;
/** The line split into whitespace-delimited words. */
private String[] words;
/** Command controller to consult for command chars, etc. */
private final CommandController controller;
/**
* Creates a new command arguments parser for the specified line.
*
* @param controller The command controller to consult for information about command characters,
* etc.
* @param line The line to be parsed
*
* @since 0.6.7
*/
public CommandArguments(final CommandController controller, final String line) {
this.controller = controller;
this.line = line;
}
/**
* Creates a new command arguments parser for the specified words.
*
* @param controller The command controller to consult for information about command characters,
* etc.
* @param words The words which form the line ot be parsed
*
* @since 0.6.7
*/
public CommandArguments(final CommandController controller, final Collection<String> words) {
this.controller = controller;
this.words = words.toArray(new String[words.size()]);
final StringBuilder builder = new StringBuilder();
for (String word : words) {
if (builder.length() > 0) {
builder.append(' ');
}
builder.append(word);
}
this.line = builder.toString();
}
/**
* Retrieves the raw line that was input, including any command character(s) and names.
*
* @return The raw line entered
*/
public String getLine() {
return line;
}
/**
* Retrieves the raw line that was input, including the command name but stripped of any command
* characters.
*
* @return The raw line entered, without command chars
*/
public String getStrippedLine() {
final int offset = isCommand() ? isSilent() ? 2 : 1 : 0;
return line.substring(offset);
}
/**
* Retrieves the input split into distinct, whitespace-separated words. The first item in the
* array will be the command name complete with any command characters.
*
* @return An array of 'words' that make up the input
*/
public String[] getWords() {
parse();
return words;
}
/**
* Retrieves the arguments to the command split into distinct, whitespace-separated words.
*
* @return An array of 'words' that make up the command's arguments
*/
public String[] getArguments() {
parse();
return Arrays.copyOfRange(words, Math.min(1, words.length), words.length);
}
/**
* Retrieves all the arguments to the command (i.e., not including the command name) with their
* original whitespace separation preserved.
*
* @return A String representation of the command arguments
*/
public String getArgumentsAsString() {
return getArgumentsAsString(0);
}
/**
* Retrieves arguments to the command (i.e., not including the command name) starting with the
* specified argument, with their original whitespace separation preserved.
*
* @param start The index of the first argument to include
*
* @return A String representation of the command arguments
*/
public String getArgumentsAsString(final int start) {
parse();
return getArgumentsAsString(start, Math.max(start, words.length - 2));
}
/**
* Retrieves arguments to the command (i.e., not including the command name) starting with the
* specified argument, with their original whitespace separation preserved.
*
* @param start The index of the first argument to include
* @param end The index of the last argument to include
*
* @return A String representation of the command arguments
*/
public String getArgumentsAsString(final int start, final int end) {
return getWordsAsString(start + 1, end + 1);
}
/**
* Retrieves the specified words with their original whitespace separation preserved.
*
* @param start The index of the first word to include (starting at 0)
*
* @return A String representation of the requested words
*/
public String getWordsAsString(final int start) {
parse();
return getWordsAsString(start, words.length - 1);
}
/**
* Retrieves the specified words with their original whitespace separation preserved.
*
* @param start The index of the first word to include (starting at 0)
* @param end The index of the last word to include
*
* @return A String representation of the requested words
*/
@Precondition("Start index is less than or equal to end index")
public String getWordsAsString(final int start, final int end) {
checkPositionIndex(start, end);
final Pattern pattern = Pattern.compile("(\\S+\\s+){" + start + '}'
+ "((\\S+\\s+){" + Math.max(0, end - start) + "}\\S+(\\s+$)?).*?");
final Matcher matcher = pattern.matcher(line);
return matcher.matches() ? matcher.group(2) : "";
}
/**
* Parses the input into a set of words, if it has not been done before.
*/
protected synchronized void parse() {
if (words == null) {
words = line.split("\\s+");
}
}
/**
* Determines if the input was a command or not.
*
* @return True if the input was a command, false otherwise
*/
public boolean isCommand() {
return !line.isEmpty() && line.charAt(0) == controller.getCommandChar();
}
/**
* Determines if the input was a silenced command or not.
*
* @return True if the input was a silenced command, false otherwise
*/
public boolean isSilent() {
return isCommand() && line.length() >= 2
&& line.charAt(1) == controller.getSilenceChar();
}
/**
* Retrieves the name of the command that was used.
*
* @return The command name used
*/
public String getCommandName() {
final int offset = isCommand() ? isSilent() ? 2 : 1 : 0;
return getWords()[0].substring(offset);
}
}