コード例 #1
0
ファイル: StringParser.java プロジェクト: phspaelti/basex
  /**
   * Returns the found command or throws an exception.
   *
   * @param cmp possible completions
   * @param par parent command
   * @param <E> token type
   * @return index
   * @throws QueryException query exception
   */
  private <E extends Enum<E>> E consume(final Class<E> cmp, final Cmd par) throws QueryException {

    final String token = command(null);
    if (!suggest || token == null || !token.isEmpty()) {
      try {
        // return command reference; allow empty strings as input ("NULL")
        return Enum.valueOf(cmp, token == null ? "NULL" : token.toUpperCase(Locale.ENGLISH));
      } catch (final IllegalArgumentException ignore) {
      }
    }

    final Enum<?>[] alt = startWith(cmp, token);
    // handle empty input
    if (token == null) {
      if (par != null) throw help(alt, par);
      if (suggest) throw error(alt, EXPECTING_CMD);
      return null;
    }

    // output error for similar commands
    final byte[] name = uc(token(token));
    final Levenshtein ls = new Levenshtein();
    for (final Enum<?> s : startWith(cmp, null)) {
      final byte[] sm = uc(token(s.name()));
      if (ls.similar(name, sm) && Cmd.class.isInstance(s)) {
        throw error(alt, UNKNOWN_SIMILAR_X, name, sm);
      }
    }

    // show unknown command error or available command extensions
    throw par == null ? error(alt, UNKNOWN_TRY_X, token) : help(alt, par);
  }
コード例 #2
0
ファイル: Command.java プロジェクト: plutext/basex
 /**
  * Returns the specified command option.
  *
  * @param s string to be found
  * @param typ options enumeration
  * @param <E> token type
  * @return option
  */
 protected static <E extends Enum<E>> E getOption(final String s, final Class<E> typ) {
   try {
     return Enum.valueOf(typ, s.toUpperCase(Locale.ENGLISH));
   } catch (final Exception ex) {
     return null;
   }
 }