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.

CommandUtils.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. /**
  21. * Utilities methods associated with commands.
  22. */
  23. public final class CommandUtils {
  24. /** Private contructor to stop instantiation of class. */
  25. private CommandUtils() {
  26. //Shouldn't be used.
  27. }
  28. /**
  29. * Parses the specified command into an array of arguments. Arguments are
  30. * separated by spaces. Multi-word arguments may be specified by starting
  31. * the argument with a quote (") and finishing it with a quote (").
  32. *
  33. * @param command The command to parse
  34. * @return An array of arguments corresponding to the command
  35. */
  36. public static String[] parseArguments(final String command) {
  37. final List<String> args = new ArrayList<>();
  38. final StringBuilder builder = new StringBuilder();
  39. boolean inquote = false;
  40. for (String word : command.split(" ")) {
  41. if (word.endsWith("\"") && inquote) {
  42. args.add(builder.toString() + ' ' + word.substring(0, word.length() - 1));
  43. builder.delete(0, builder.length());
  44. inquote = false;
  45. } else if (inquote) {
  46. builder.append(' ');
  47. builder.append(word);
  48. } else if (word.startsWith("\"") && !word.endsWith("\"")) {
  49. inquote = true;
  50. builder.append(word.substring(1));
  51. } else if (word.startsWith("\"") && word.endsWith("\"")) {
  52. if (word.length() == 1) {
  53. inquote = true;
  54. } else {
  55. args.add(word.substring(1, word.length() - 1));
  56. }
  57. } else {
  58. args.add(word);
  59. }
  60. }
  61. return args.toArray(new String[args.size()]);
  62. }
  63. }