Exemplo n.º 1
0
 private static boolean getBoolean(ServletConfig cfg, String param) throws ServletException {
   String n = cfg.getInitParameter(param);
   if (n == null) return false;
   try {
     return StringUtils.toBoolean(n);
   } catch (IllegalArgumentException err) {
     throw new ServletException(
         MessageFormat.format(HttpServerText.get().invalidBoolean, param, n));
   }
 }
Exemplo n.º 2
0
 /**
  * Get a boolean value from the git config
  *
  * @param section section the key is grouped within.
  * @param subsection subsection name, such a remote or branch name.
  * @param name name of the key to get.
  * @param defaultValue default value to return if no value was present.
  * @return true if any value or defaultValue is true, false for missing or explicit false
  */
 public boolean getBoolean(
     final String section, String subsection, final String name, final boolean defaultValue) {
   String n = getRawString(section, subsection, name);
   if (n == null) return defaultValue;
   if (MAGIC_EMPTY_VALUE == n) return true;
   try {
     return StringUtils.toBoolean(n);
   } catch (IllegalArgumentException err) {
     throw new IllegalArgumentException(
         MessageFormat.format(JGitText.get().invalidBooleanValue, section, name, n));
   }
 }
Exemplo n.º 3
0
  /**
   * Parse an enumeration from the configuration.
   *
   * @param <T> type of the enumeration object.
   * @param all all possible values in the enumeration which should be recognized. Typically {@code
   *     EnumType.values()}.
   * @param section section the key is grouped within.
   * @param subsection subsection name, such a remote or branch name.
   * @param name name of the key to get.
   * @param defaultValue default value to return if no value was present.
   * @return the selected enumeration value, or {@code defaultValue}.
   */
  public <T extends Enum<?>> T getEnum(
      final T[] all,
      final String section,
      final String subsection,
      final String name,
      final T defaultValue) {
    String value = getString(section, subsection, name);
    if (value == null) return defaultValue;

    String n = value.replace(' ', '_');
    T trueState = null;
    T falseState = null;
    for (T e : all) {
      if (StringUtils.equalsIgnoreCase(e.name(), n)) return e;
      else if (StringUtils.equalsIgnoreCase(e.name(), "TRUE")) trueState = e;
      else if (StringUtils.equalsIgnoreCase(e.name(), "FALSE")) falseState = e;
    }

    // This is an odd little fallback. C Git sometimes allows boolean
    // values in a tri-state with other things. If we have both a true
    // and a false value in our enumeration, assume its one of those.
    //
    if (trueState != null && falseState != null) {
      try {
        return StringUtils.toBoolean(n) ? trueState : falseState;
      } catch (IllegalArgumentException err) {
        // Fall through and use our custom error below.
      }
    }

    if (subsection != null)
      throw new IllegalArgumentException(
          MessageFormat.format(JGitText.get().enumValueNotSupported3, section, name, value));
    else
      throw new IllegalArgumentException(
          MessageFormat.format(JGitText.get().enumValueNotSupported2, section, name, value));
  }