View Javadoc

1   package org.lsst.ccs.command;
2   
3   import java.lang.reflect.Array;
4   import java.lang.reflect.Method;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   /**
9    * A command with pre-parsed arguments (Objects).
10   *
11   * @author tonyj
12   */
13  public class RawCommand implements BasicCommand {
14  
15      private final String commandName;
16      private final List<Object> arguments;
17  
18      /**
19       * Builds a Raw Command.
20       *
21       * @param commandName The command name.
22       * @param arguments The arguments for the command
23       */
24      public RawCommand(String commandName, List<Object> arguments) {
25          this.commandName = commandName;
26          this.arguments = arguments;
27      }
28  
29      /**
30       * Get the root command name (the zeroth token)
31       *
32       * @return The command name
33       */
34      @Override
35      public String getCommand() {
36          return commandName;
37      }
38  
39      /**
40       * Get a specific command argument
41       *
42       * @param index The index of the argument
43       * @return The command argument at the given index
44       */
45      public Object getArgument(int index) {
46          return arguments.get(index);
47      }
48      
49      public Object[] getArguments() {
50          return arguments.toArray();
51      }
52  
53      /**
54       * Get the number of arguments associated with this command
55       *
56       * @return The argument count
57       */
58      @Override
59      public int getArgumentCount() {
60          return arguments.size();
61      }
62  
63      /**
64       * Convert a BasicCommand to a RawCommand. If the BasicCommand is already a
65       * raw command it is returned unchanged. If it is a tokenized command it
66       * will be converted to a RawCommand.
67       *
68       * @param command The input command
69       * @param method The method thar will be invoked, used to find the type of the command arguments
70       * @param engine The input conversion engine that will be used to convert strings to objects
71       * @return The converted command
72       * @throws org.lsst.ccs.command.CommandInvocationException If the command cannot be successfully converted
73       */
74      public static RawCommand toRawCommand(BasicCommand command, Method method, InputConversionEngine engine) throws CommandInvocationException {
75          if (command instanceof RawCommand) {
76              return (RawCommand) command;
77          } else if (command instanceof TokenizedCommand) {
78              return convertToRaw((TokenizedCommand) command, method, engine);
79          } else {
80              throw new CommandInvocationException("Error: Unknown type of command " + command.getClass().getName());
81          }
82      }
83  
84      private static RawCommand convertToRaw(TokenizedCommand tokenizedCommand, Method method, InputConversionEngine engine) throws CommandInvocationException {
85          Class<?>[] parameterTypes = method.getParameterTypes();
86          List<Object> args = new ArrayList(parameterTypes.length);
87          boolean varArgs = method.isVarArgs();
88          for (int i = 0; i < parameterTypes.length; i++) {
89              if (varArgs && i == parameterTypes.length - 1) {
90                  Class varClass = parameterTypes[i];
91                  Class elemClass = varClass.getComponentType();
92                  Object theArray = Array.newInstance(elemClass, tokenizedCommand.getArgumentCount() - parameterTypes.length + 1);
93                  for (int j = 0; j < Array.getLength(theArray); j++) {
94                      Array.set(theArray, j, engine.convertArgToType(tokenizedCommand.getArgument(i + j), elemClass));
95                  }
96                  args.add(theArray);
97              } else {
98                  args.add(engine.convertArgToType(tokenizedCommand.getArgument(i), parameterTypes[i]));
99              }
100         }
101         return new RawCommand(tokenizedCommand.getCommand(), args);
102     }
103 }