File Source: CommandArguments.java
/*
P/P * Method: com.dmdirc.commandparser.CommandArguments__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
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;
24
25 import java.util.Arrays;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 /**
30 * Represents a command and its arguments. In this class, input is split into
31 * 'words' which are separated by any number of whitespace characters;
32 * 'arguments' are the same but exclude the first word, which will normally be
33 * the command name.
34 *
35 * @since 0.6.3m1
36 * @author chris
37 */
38 public class CommandArguments {
39
40 /** The raw line that was input. */
41 private final String line;
42
43 /** The line split into whitespace-delimited words. */
44 private String[] words;
45
46 /**
47 * Creates a new command arguments parser for the specified line.
48 *
49 * @param line The line to be parsed
50 */
/*
P/P * Method: void com.dmdirc.commandparser.CommandArguments(String)
*
* Postconditions:
* this.line == line
* init'ed(this.line)
*/
51 public CommandArguments(final String line) {
52 this.line = line;
53 }
54
55 /**
56 * Retrieves the raw line that was input, including any command character(s)
57 * and names.
58 *
59 * @return The raw line entered
60 */
61 public String getLine() {
/*
P/P * Method: String getLine()
*
* Postconditions:
* return_value == this.line
* init'ed(return_value)
*/
62 return line;
63 }
64
65 /**
66 * Retrieves the raw line that was input, including the command name but
67 * stripped of any command characters.
68 *
69 * @return The raw line entered, without command chars
70 */
71 public String getStrippedLine() {
/*
P/P * Method: String getStrippedLine()
*
* Preconditions:
* this.line != null
* (soft) init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
* (soft) init'ed(com/dmdirc/commandparser/CommandManager.silenceChar)
*
* Postconditions:
* java.lang.String:substring(...)._tainted == this.line._tainted
* init'ed(java.lang.String:substring(...)._tainted)
* return_value == &java.lang.String:substring(...)
*/
72 final int offset = isCommand() ? isSilent() ? 2 : 1 : 0;
73
74 return line.substring(offset);
75 }
76
77 /**
78 * Retrieves the input split into distinct, whitespace-separated words. The
79 * first item in the array will be the command name complete with any
80 * command characters.
81 *
82 * @return An array of 'words' that make up the input
83 */
84 public String[] getWords() {
/*
P/P * Method: String[] getWords()
*
* Preconditions:
* init'ed(this.words)
* (soft) this.line != null
*
* Postconditions:
* init'ed(java.lang.String:split(...)._tainted)
* return_value == One-of{old this.words, &java.lang.String:split(...)}
* return_value != null
* this.words == return_value
*/
85 parse();
86
87 return words;
88 }
89
90 /**
91 * Retrieves the arguments to the command split into disticnt,
92 * whitespace-separated words.
93 *
94 * @return An array of 'words' that make up the command's arguments
95 */
96 public String[] getArguments() {
/*
P/P * Method: String[] getArguments()
*
* Preconditions:
* init'ed(this.words)
* (soft) this.line != null
*
* Presumptions:
* this.words.length@97 <= 232-1
*
* Postconditions:
* init'ed(java.lang.String:split(...)._tainted)
* init'ed(java.lang.String:split(...).length)
* init'ed(return_value)
* this.words == One-of{old this.words, &java.lang.String:split(...)}
* this.words != null
*/
97 parse();
98
99 return Arrays.copyOfRange(words, 1, words.length);
100 }
101
102 /**
103 * Retrieves all the arguments to the command (i.e., not including the
104 * command name) with their original whitespace separation preserved.
105 *
106 * @return A String representation of the command arguments
107 */
108 public String getArgumentsAsString() {
/*
P/P * Method: String getArgumentsAsString()
*
* Preconditions:
* init'ed(this.words)
* (soft) this.line != null
*
* Postconditions:
* init'ed(java.lang.String:split(...)._tainted)
* java.lang.String:split(...)._tainted == 0
* init'ed(java.lang.String:split(...).length)
* init'ed(return_value)
* this.words != null
*/
109 parse();
110
111 return getArgumentsAsString(0);
112 }
113
114 /**
115 * Retrieves arguments to the command (i.e., not including the
116 * command name) starting with the specified argument, with their original
117 * whitespace separation preserved.
118 *
119 * @param start The index of the first argument to include
120 * @return A String representation of the command arguments
121 */
122 public String getArgumentsAsString(final int start) {
/*
P/P * Method: String getArgumentsAsString(int)
*
* Preconditions:
* init'ed(this.words)
* start <= 232-2
* (soft) this.line != null
*
* Presumptions:
* this.words.length@123 <= 232-1
* this.words.length@123 - start in {-231+1..232}
*
* Postconditions:
* init'ed(java.lang.String:split(...)._tainted)
* init'ed(java.lang.String:split(...).length)
* init'ed(return_value)
* this.words == One-of{old this.words, &java.lang.String:split(...)}
* this.words != null
*/
123 parse();
124
125 return getWordsAsString(start + 1);
126 }
127
128 /**
129 * Retrieves the specified words with their original whitespace separation
130 * preserved.
131 *
132 * @param start The index of the first word to include (starting at 0)
133 * @return A String representation of the requested words
134 */
135 public String getWordsAsString(final int start) {
/*
P/P * Method: String getWordsAsString(int)
*
* Preconditions:
* this.words != null
* this.words.length <= 232-1
* this.words.length - start in {-231..232-1}
*
* Postconditions:
* init'ed(return_value)
*/
136 return getWordsAsString(start, words.length);
137 }
138
139 /**
140 * Retrieves the specified words with their original whitespace separation
141 * preserved.
142 *
143 * @param start The index of the first word to include (starting at 0)
144 * @param end The index of the last word to include
145 * @return A String representation of the requested words
146 */
147 public String getWordsAsString(final int start, final int end) {
/*
P/P * Method: String getWordsAsString(int, int)
*
* Preconditions:
* end - start in {-231..232-1}
*
* Presumptions:
* java.util.regex.Pattern:compile(...)@148 != null
* java.util.regex.Pattern:matcher(...)@150 != null
*
* Postconditions:
* init'ed(return_value)
*/
148 final Pattern pattern = Pattern.compile("(\\S+\\s*){" + (start) + "}"
149 + "((\\S+\\s*){" + (end - start) + "}).*?");
150 final Matcher matcher = pattern.matcher(line);
151
152 return matcher.matches() ? matcher.group(2) : "";
153 }
154
155 /**
156 * Parses the input into a set of words, if it has not been done before.
157 */
158 protected synchronized void parse() {
/*
P/P * Method: void parse()
*
* Preconditions:
* init'ed(this.words)
* (soft) this.line != null
*
* Postconditions:
* init'ed(java.lang.String:split(...)._tainted)
* this.words == One-of{old this.words, &java.lang.String:split(...)}
* this.words != null
*
* Test Vectors:
* this.words: Inverse{null}, Addr_Set{null}
*/
159 if (words == null) {
160 words = line.split("\\s+");
161 }
162 }
163
164 /**
165 * Determines if the input was a command or not.
166 *
167 * @return True if the input was a command, false otherwise
168 */
169 public boolean isCommand() {
/*
P/P * Method: bool isCommand()
*
* Preconditions:
* this.line != null
* (soft) init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
*
* Postconditions:
* init'ed(return_value)
*/
170 return !line.isEmpty() && line.charAt(0) == CommandManager.getCommandChar();
171 }
172
173 /**
174 * Determines if the input was a silenced command or not.
175 *
176 * @return True if the input was a silenced command, false otherwise
177 */
178 public boolean isSilent() {
/*
P/P * Method: bool isSilent()
*
* Preconditions:
* this.line != null
* (soft) init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
* (soft) init'ed(com/dmdirc/commandparser/CommandManager.silenceChar)
*
* Postconditions:
* init'ed(return_value)
*/
179 return isCommand() && line.length() >= 2 &&
180 line.charAt(1) == CommandManager.getSilenceChar();
181 }
182
183 /**
184 * Retrieves the name of the command that was used.
185 *
186 * @return The command name used
187 */
188 public String getCommandName() {
/*
P/P * Method: String getCommandName()
*
* Preconditions:
* init'ed(this.words)
* this.line != null
* (soft) init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
* (soft) init'ed(com/dmdirc/commandparser/CommandManager.silenceChar)
*
* Presumptions:
* getWords(...).length@190 >= 1
* getWords(...)[0]@190 != null
*
* Postconditions:
* init'ed(java.lang.String:split(...)._tainted)
* init'ed(java.lang.String:split(...).length)
* init'ed(java.lang.String:split(...)[0])
* init'ed(java.lang.String:substring(...)._tainted)
* return_value == &java.lang.String:substring(...)
* this.words == One-of{old this.words, &java.lang.String:split(...)}
* this.words != null
*/
189 final int offset = isCommand() ? isSilent() ? 2 : 1 : 0;
190 return getWords()[0].substring(offset);
191 }
192
193 }
SofCheck Inspector Build Version : 2.17854
| CommandArguments.java |
2009-Jun-25 01:54:24 |
| CommandArguments.class |
2009-Sep-02 17:04:12 |