Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CommandArguments.java 8.3KB

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