Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CommandArguments.java 7.3KB

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