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 7.8KB

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