コード例 #1
0
  /** @see org.overlord.sramp.common.shell.ShellCommandReader#read() */
  @Override
  public final ShellCommand read() throws Exception {
    String line = readLine();
    if (line == null) {
      return null;
    }

    Arguments arguments = new Arguments(line);
    if (arguments.isEmpty()) {
      return new NoOpCommand();
    }

    // The first argument is the qualified command name.
    QName commandName = arguments.removeCommandName();

    // Create the command.
    ShellCommand command = factory.createCommand(commandName);
    command.setContext(this.context);
    command.setArguments(arguments);
    command.setOutput(getCommandOutput());
    return command;
  }
コード例 #2
0
ファイル: TabCompleter.java プロジェクト: rcernich/s-ramp
  /** @see jline.console.completer.Completer#complete(java.lang.String, int, java.util.List) */
  @Override
  public int complete(String buffer, int cursor, List<CharSequence> candidates) {
    // Case 1 - nothing has been typed yet - show all command namespaces
    if (buffer.trim().length() == 0) {
      for (String ns : factory.getNamespaces()) {
        candidates.add(ns + ":"); // $NON-NLS-1$
      }
      return cursor;
    }

    // Case 2 - a partial namespace has been typed - show all namespaces that match
    if (!buffer.contains(":") && !buffer.contains(" ")) { // $NON-NLS-1$ //$NON-NLS-2$
      for (String ns : factory.getNamespaces()) {
        if (ns.startsWith(buffer)) {
          candidates.add(ns + ":"); // $NON-NLS-1$
        }
      }
      // If no namespaces matched, then try to match the default commands
      if (candidates.isEmpty()) {
        for (QName cmdName : factory.getCommandNames("s-ramp")) { // $NON-NLS-1$
          if (cmdName.getLocalPart().startsWith(buffer)) {
            candidates.add("s-ramp:" + cmdName.getLocalPart() + " "); // $NON-NLS-1$ //$NON-NLS-2$
          }
        }
      }
      return 0;
    }

    // Case 3 - a namespace has been typed and we're waiting at the colon
    if (buffer.endsWith(":") && !buffer.contains(" ")) { // $NON-NLS-1$ //$NON-NLS-2$
      String ns = buffer.substring(0, buffer.length() - 1);
      for (QName cmdName : factory.getCommandNames(ns)) {
        candidates.add(cmdName.getLocalPart() + " "); // $NON-NLS-1$
      }
      return cursor;
    }

    // Case 4 - a partial command has been typed - show all command names that match
    if (buffer.contains(":")
        && !buffer.endsWith(":")
        && !buffer.contains(" ")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      int colonIdx = buffer.indexOf(':');
      String ns = buffer.substring(0, colonIdx);
      String name = buffer.substring(colonIdx + 1);
      for (QName cmdName : factory.getCommandNames(ns)) {
        if (cmdName.getLocalPart().startsWith(name)) {
          candidates.add(cmdName.getLocalPart() + " "); // $NON-NLS-1$
        }
      }
      return colonIdx + 1;
    }

    // Case 5 - a full command name has been entered, delegate further procesing
    // to the specific command
    String args = buffer.substring(0, cursor);
    Arguments arguments = null;
    try {
      arguments = new Arguments(args, true);
    } catch (InvalidCommandArgumentException e1) {
      // should never happen...but if it does, just bail
      return cursor;
    }
    QName commandName = arguments.removeCommandName();
    String lastArgument = null;
    if (arguments.size() > 0 && !args.endsWith(" ")) { // $NON-NLS-1$
      lastArgument = arguments.remove(arguments.size() - 1);
    }
    try {
      ShellCommand command = factory.createCommand(commandName);
      if (command != null) {
        command.setContext(this.context);
        command.setArguments(arguments);
        int rval = command.tabCompletion(lastArgument, candidates);
        if (!candidates.isEmpty()) {
          int curs = args.length() - (lastArgument == null ? 0 : lastArgument.length());
          return curs + rval;
        }
      }
    } catch (Exception e) {
    }

    return cursor;
  }