You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandArguments.java 6.2KB

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