View Javadoc

1   package org.lsst.ccs.command;
2   
3   import java.util.List;
4   import org.lsst.ccs.command.StringTokenizer.Token;
5   
6   /**
7    * A command line that has been split into tokens. This could in future be
8    * expanded to also support command options (beginning with - or --)
9    *
10   * @author tonyj
11   */
12  public class TokenizedCommand implements BasicCommand {
13  
14      private final List<Token> tokens;
15  
16      /**
17       * Builds a Tokenized command from an unparsed string.
18       *
19       * @param command The command string to be tokenized.
20       */
21      public TokenizedCommand(String command) {
22          tokens = StringTokenizer.tokenize(command);
23      }
24  
25      /**
26       * Get the root command name (the zeroth token)
27       *
28       * @return The command name
29       */
30      @Override
31      public String getCommand() {
32          return tokens.get(0).getString();
33      }
34  
35      /**
36       * Get the start position on the line of the command name.
37       *
38       * @return The position of the command
39       */
40      int getCommandLocation() {
41          return tokens.get(0).getLocation();
42      }
43  
44      /**
45       * Get a specific command argument
46       *
47       * @param index The index of the argument
48       * @return The command argument at the given index
49       */
50      public String getArgument(int index) {
51          return tokens.get(index + 1).getString();
52      }
53  
54      /**
55       * The start point in the original command line of a specific argument.
56       *
57       * @param index The index of the argument
58       * @return The position of the argument
59       */
60      int getArgumentLocation(int index) {
61          return tokens.get(index + 1).getLocation();
62      }
63  
64      /**
65       * Get the number of arguments associated with this command
66       *
67       * @return The argument count
68       */
69      @Override
70      public int getArgumentCount() {
71          return Math.max(tokens.size() - 1, 0);
72      }
73  
74      /**
75       * Test if the command is completely empty (no tokens)
76       *
77       * @return <code>true</code> only if the command is empty.
78       */
79      public boolean isEmpty() {
80          return tokens.isEmpty();
81      }
82      
83  }