File Source: AliasCommand.java

         /* 
    P/P   *  Method: com.dmdirc.commandparser.commands.global.AliasCommand__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 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  
    23  package com.dmdirc.commandparser.commands.global;
    24  
    25  import com.dmdirc.actions.Action;
    26  import com.dmdirc.actions.ActionManager;
    27  import com.dmdirc.actions.wrappers.Alias;
    28  import com.dmdirc.actions.wrappers.AliasWrapper;
    29  import com.dmdirc.commandparser.CommandArguments;
    30  import com.dmdirc.commandparser.CommandManager;
    31  import com.dmdirc.commandparser.commands.GlobalCommand;
    32  import com.dmdirc.commandparser.commands.IntelligentCommand;
    33  import com.dmdirc.ui.input.AdditionalTabTargets;
    34  import com.dmdirc.ui.input.TabCompleter;
    35  import com.dmdirc.ui.interfaces.InputWindow;
    36  
    37  import java.util.List;
    38  
    39  /**
    40   * The alias command allows users to create aliases on-the-fly.
    41   * 
    42   * @author chris
    43   */
    44  public final class AliasCommand extends GlobalCommand implements
    45          IntelligentCommand {
    46  
    47      /**
    48       * Creates a new instance of Active.
    49       */
    50      public AliasCommand() {
                 /* 
    P/P           *  Method: void com.dmdirc.commandparser.commands.global.AliasCommand()
                  * 
                  *  Preconditions:
                  *    init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
                  */
    51          super();
    52  
    53          CommandManager.registerCommand(this);
    54      }
    55  
    56      /** {@inheritDoc} */
    57      @Override
    58      public void execute(final InputWindow origin, final boolean isSilent,
    59                          final CommandArguments args) {
                 /* 
    P/P           *  Method: void execute(InputWindow, bool, CommandArguments)
                  * 
                  *  Preconditions:
                  *    args != null
                  *    init'ed(args.words)
                  *    (soft) args.line != null
                  *    (soft) init'ed(com/dmdirc/commandparser/CommandManager.commandChar)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.actions.wrappers.Alias:createAction(...)@94 != null
                  *    com.dmdirc.actions.wrappers.AliasWrapper:getAliasWrapper(...)@83 != null
                  *    com.dmdirc.actions.wrappers.AliasWrapper:getCommandName(...)@84 != null
                  *    com.dmdirc.actions.wrappers.AliasWrapper:iterator(...)@83 != null
                  *    getArguments(...).length@65 >= 1
                  *    ...
                  * 
                  *  Postconditions:
                  *    args.words != null
                  *    init'ed(java.lang.String:split(...)._tainted)
                  *    java.lang.String:split(...)._tainted == 0
                  *    init'ed(java.lang.String:split(...).length)
                  * 
                  *  Test Vectors:
                  *    getArguments(...).length@60: {2..+Inf}, {0,1}
                  *    java.lang.String:equalsIgnoreCase(...)@65: {0}, {1}
                  *    java.lang.String:equalsIgnoreCase(...)@84: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@83: {0}, {1}
                  */
    60          if (args.getArguments().length < 2) {
    61              showUsage(origin, isSilent, "alias", "[--remove] <name> [command]");
    62              return;
    63          }
    64  
    65          if (args.getArguments()[0].equalsIgnoreCase("--remove")) {
    66              final String name = args.getArguments()[1].charAt(0) == CommandManager.getCommandChar()
    67                  ? args.getArguments()[1].substring(1) : args.getArguments()[1];
    68  
    69              if (doRemove(name)) {
    70                  sendLine(origin, isSilent, FORMAT_OUTPUT, "Alias '" + name +
    71                           "' removed.");
    72              } else {
    73                  sendLine(origin, isSilent, FORMAT_ERROR, "Alias '" + name +
    74                           "' not found.");
    75              }
    76  
    77              return;
    78          }
    79  
    80          final String name = args.getArguments()[0].charAt(0) == CommandManager.getCommandChar()
    81                  ? args.getArguments()[0].substring(0) : args.getArguments()[0];
    82  
    83          for (Action alias : AliasWrapper.getAliasWrapper()) {
    84              if (AliasWrapper.getCommandName(alias).substring(1).equalsIgnoreCase(
    85                      name)) {
    86                  sendLine(origin, isSilent, FORMAT_ERROR, "Alias '" + name +
    87                           "' already exists.");
    88                  return;
    89              }
    90          }
    91  
    92          final Alias myAlias = new Alias(name);
    93          myAlias.setResponse(new String[]{args.getArgumentsAsString(1)});
    94          myAlias.createAction().save();
    95  
    96          sendLine(origin, isSilent, FORMAT_OUTPUT, "Alias '" + name +
    97                   "' created.");
    98      }
    99  
   100      /**
   101       * Removes the alias with the specified name.
   102       * 
   103       * @param name The name of the alias to remove
   104       * @return True if the alias was deleted, false otherwise
   105       */
   106      private boolean doRemove(final String name) {
                 /* 
    P/P           *  Method: bool doRemove(String)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.actions.wrappers.AliasWrapper:getAliasWrapper(...)@107 != null
                  *    com.dmdirc.actions.wrappers.AliasWrapper:getCommandName(...)@108 != null
                  *    com.dmdirc.actions.wrappers.AliasWrapper:iterator(...)@107 != null
                  *    java.util.Iterator:next(...)@107 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equalsIgnoreCase(...)@108: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@107: {0}, {1}
                  */
   107          for (Action alias : AliasWrapper.getAliasWrapper()) {
   108              if (AliasWrapper.getCommandName(alias).substring(1).equalsIgnoreCase(
   109                      name)) {
   110                  alias.delete();
   111                  ActionManager.unregisterAction(alias);
   112  
   113                  return true;
   114              }
   115          }
   116  
   117          return false;
   118      }
   119  
   120      /** {@inheritDoc} */
   121      @Override
   122      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;"alias"
                  */
   123          return "alias";
   124      }
   125  
   126      /** {@inheritDoc} */
   127      @Override
   128      public boolean showInHelp() {
                 /* 
    P/P           *  Method: bool showInHelp()
                  * 
                  *  Postconditions:
                  *    return_value == 1
                  */
   129          return true;
   130      }
   131  
   132      /** {@inheritDoc} */
   133      @Override
   134      public String getHelp() {
                 /* 
    P/P           *  Method: String getHelp()
                  * 
                  *  Postconditions:
                  *    return_value == &amp;"alias [--remove] <name> [command] - creates or removes the specified alias"
                  */
   135          return "alias [--remove] <name> [command] - creates or removes the specified alias";
   136      }
   137  
   138      /** {@inheritDoc} */
   139      @Override
   140      public AdditionalTabTargets getSuggestions(final int arg,
   141                                                 final List<String> previousArgs) {
                 /* 
    P/P           *  Method: AdditionalTabTargets getSuggestions(int, List)
                  * 
                  *  Preconditions:
                  *    (soft) previousArgs != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.actions.wrappers.AliasWrapper:getAliasWrapper(...)@147 != null
                  *    com.dmdirc.actions.wrappers.AliasWrapper:iterator(...)@147 != null
                  *    com.dmdirc.ui.input.AdditionalTabTargets:excludeAll(...)@142 != null
                  *    java.util.List:get(...)@146 != null
                  *    java.util.List:get(...)@150 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    arg: {-231..-1}, {0}, {1}
                  *    java.lang.String:equals(...)@146: {0}, {1}
                  *    java.lang.String:equals(...)@150: {1}, {0}
                  */
   142          final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
   143  
   144          if (arg == 0) {
   145              res.add("--remove");
   146          } else if (arg == 1 && previousArgs.get(0).equals("--remove")) {
   147              for (Action alias : AliasWrapper.getAliasWrapper()) {
   148                  res.add(AliasWrapper.getCommandName(alias));
   149              }
   150          } else if (arg >= 1 && !previousArgs.get(0).equals("--remove")) {
   151              return TabCompleter.getIntelligentResults(arg, previousArgs, 1);
   152          }
   153  
   154          return res;
   155      }
   156  
   157  }








SofCheck Inspector Build Version : 2.17854
AliasCommand.java 2009-Jun-25 01:54:24
AliasCommand.class 2009-Sep-02 17:04:16