123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- /*
- * Copyright (c) 2006-2011 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.logger.Logger;
-
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * 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;
-
- /**
- * Creates a new command arguments parser for the specified line.
- *
- * @param line The line to be parsed
- */
- public CommandArguments(final String line) {
- this.line = line;
- }
-
- /**
- * Creates a new command arguments parser for the specified words.
- *
- * @param words The words which form the line ot be parsed
- * @since 0.6.3
- */
- public CommandArguments(final Collection<String> words) {
- 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 disticnt,
- * 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, 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) {
- Logger.assertTrue(start <= end + 1);
-
- 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) == CommandManager.getCommandManager().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) == CommandManager.getCommandManager().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);
- }
-
- }
|