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.

AliasCommandHandler.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2006-2015 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.aliases;
  23. import com.dmdirc.commandparser.CommandArguments;
  24. import com.dmdirc.commandparser.commands.BaseCommand;
  25. import com.dmdirc.commandparser.commands.Command;
  26. import com.dmdirc.commandparser.commands.context.CommandContext;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.InputModel;
  29. import com.dmdirc.interfaces.WindowModel;
  30. import javax.annotation.Nonnull;
  31. /**
  32. * {@link Command} implementation that handles an alias.
  33. */
  34. public class AliasCommandHandler extends BaseCommand {
  35. private final Alias alias;
  36. public AliasCommandHandler(final CommandController controller, final Alias alias) {
  37. super(controller);
  38. this.alias = alias;
  39. }
  40. @Override
  41. public void execute(@Nonnull final WindowModel origin, final CommandArguments args,
  42. final CommandContext context) {
  43. if (args.getArguments().length >= alias.getMinArguments()) {
  44. for (String line : alias.getSubstitution().split("\n")) {
  45. origin.getInputModel().map(InputModel::getCommandParser)
  46. .ifPresent(cp -> cp.parseCommand(origin,
  47. getSubstituteCommand(line, args)));
  48. }
  49. } else {
  50. showError(origin, args.isSilent(), alias.getName() + " requires at least "
  51. + alias.getMinArguments() + " argument"
  52. + (alias.getMinArguments() == 1 ? "" : "s") + '.');
  53. }
  54. }
  55. /**
  56. * Gets the command that should be executed, with the appropriate substitutions made.
  57. * <p>
  58. * The returned command will have arguments substituted (replacing "$1-", "$2", etc), and will
  59. * be silenced if the given args are silent.
  60. *
  61. * @param line The line to substitute.
  62. * @param args The arguments entered by the user.
  63. *
  64. * @return The substituted command to execute.
  65. */
  66. private String getSubstituteCommand(final String line, final CommandArguments args) {
  67. final StringBuilder builder = new StringBuilder(line.trim());
  68. final String[] arguments = args.getArguments();
  69. for (int i = 0; i < arguments.length; i++) {
  70. replaceAll(builder, "$" + (i + 1) + '-', args.getArgumentsAsString(i));
  71. replaceAll(builder, "$" + (i + 1), arguments[i]);
  72. }
  73. if (args.isSilent()) {
  74. builder.insert(0, getController().getSilenceChar());
  75. }
  76. builder.insert(0, getController().getCommandChar());
  77. return builder.toString();
  78. }
  79. /**
  80. * Replaces all instances of the specified substring in the builder.
  81. *
  82. * @param builder The builder to modify.
  83. * @param substr The substring to replace.
  84. * @param replacement The string to use as a replacement.
  85. */
  86. private static void replaceAll(final StringBuilder builder,
  87. final String substr, final String replacement) {
  88. int index = builder.indexOf(substr);
  89. while (index > -1) {
  90. builder.replace(index, index + substr.length(), replacement);
  91. index = builder.indexOf(substr);
  92. }
  93. }
  94. }