Beispiel #1
0
  /**
   * 取得property的值,不计算表达式。
   *
   * @param prop 属性
   * @param defaultValue 是否使用默认值
   */
  private String getPropertyValue(ConfigProperty prop, boolean defaultValue) {
    Object value = getValues().get(prop.getName());

    if (defaultValue && value == null) {
      value = prop.getDefaultValue();
    }

    if (value instanceof Expression) {
      value = ((Expression) value).getExpressionText();
    }

    if (value instanceof String) {
      String stringValue = (String) value;

      if (stringValue != null) {
        stringValue = stringValue.trim();
      }

      if (stringValue == null || stringValue.length() == 0) {
        stringValue = null;
      }

      return stringValue;
    }

    return value == null ? null : value.toString();
  }
Beispiel #2
0
  /**
   * 计算property的值。
   *
   * @param prop 属性
   * @param defaultValue 是否使用默认值
   */
  private String evaluatePropertyValue(ConfigProperty prop, boolean defaultValue) {
    final String ref = prop.getName();
    Object value = getValues().get(ref);

    if (defaultValue && value == null) {
      value = prop.getDefaultValue();

      if (value instanceof String) {
        Expression expr = CompositeExpression.parse((String) value);

        if (expr != null) {
          value = expr;
        }
      }
    }

    if (value instanceof Expression) {
      value =
          ((Expression) value)
              .evaluate(
                  new ExpressionContext() {
                    public Object get(String key) {
                      // 避免无限递归
                      if (ref.equals(key)
                          || StringUtil.getValidIdentifier(ref)
                              .equals(StringUtil.getValidIdentifier(key))) {
                        return null;
                      } else {
                        return getValues().get(key);
                      }
                    }

                    public void put(String key, Object value) {
                      getValues().put(key, value);
                    }
                  });
    }

    if (value instanceof String) {
      String stringValue = (String) value;

      if (stringValue != null) {
        stringValue = stringValue.trim();
      }

      if (stringValue == null || stringValue.length() == 0) {
        stringValue = null;
      }

      return stringValue;
    }

    return value == null ? null : value.toString();
  }
Beispiel #3
0
  private boolean validateSave() {
    if (!validate()) {
      println();
      println(" 字段" + validatorProperty.getName() + "不合法: " + validatorMessage);
      println();
      print(" 您仍然要保存吗? [Yes=强制保存/No=继续编辑] ");

      String input = null;

      try {
        input = in.readLine();
      } catch (IOException e) {
        throw new ConfigWizardException(e);
      }

      input = input == null ? "" : input.trim().toLowerCase();

      if (input.equals("y") || input.equals("yes")) {
        return true;
      }

      printTitle();
      printGroup();
      processInput(validatorIndex);

      return false;
    }

    return true;
  }
Beispiel #4
0
  /** 填充默认值。 */
  private void fillDefaultValues() {
    int savedStep = step;

    for (int i = 0; i < groups.length; i++) {
      setStep(i);

      for (ConfigProperty prop2 : props) {
        ConfigProperty prop = prop2;

        // 除非key存在且value不为空,否则设置默认值
        if (getValues().get(prop.getName()) == null || !getKeys().contains(prop.getName())) {
          String value = getPropertyValue(prop, true);

          setProperty(prop.getName(), value == null ? "" : value);
        }
      }
    }

    setStep(savedStep);
  }
Beispiel #5
0
  private void processInput(int index) {
    ConfigProperty prop = props[index];
    StringBuffer buffer = new StringBuffer(" 请输入");

    // 显示property描述
    if (prop.getDescription() != null) {
      buffer.append(prop.getDescription()).append(" ");
    }

    // 显示property名称
    buffer.append(prop.getName()).append(" = ");

    // 显示property值
    String value = getPropertyValue(prop, true);

    if (value != null) {
      buffer.append("[").append(value).append("] ");
    }

    print(buffer);

    // 等待输入
    String input = null;

    try {
      input = in.readLine();
    } catch (IOException e) {
      throw new ConfigWizardException(e);
    }

    input = input == null ? "" : input.trim();

    if (input == null || input.length() == 0) {
      input = value;
    }

    setProperty(prop.getName(), input);
  }
Beispiel #6
0
  /**
   * 验证属性文件是否满足所有descriptor的需要。
   *
   * @return 如果满足要求,则返回true
   */
  public boolean validate() {
    for (int i = 0; i < groups.length; i++) {
      setStep(i);

      for (int j = 0; j < props.length; j++) {
        ConfigProperty prop = props[j];

        String value = evaluatePropertyValue(prop, false);

        for (Object element : prop.getValidators()) {
          ConfigValidator validator = (ConfigValidator) element;

          if (!validator.validate(value)) {
            validatorIndex = j;
            validatorProperty = prop;
            validatorMessage = validator.getMessage();
            return false;
          }
        }
      }
    }

    return true;
  }
Beispiel #7
0
  private void printGroup() {
    if (group.getDescription() != null) {
      println(" " + group.getDescription() + " (? - 该值在用户配置文件中不存在,* - 必填项,S - 覆盖共享默认值,s - 共享值)");
    } else {
      println(" (? - 该值在用户配置文件中不存在,* - 必填项,S - 覆盖共享默认值,s - 共享值)");
    }

    println();

    // 找出最长的名称和值
    int maxLength = -1;
    int maxLengthValue = -1;

    for (ConfigProperty prop : props) {
      int length = prop.getName().length();

      if (length > maxLength && length < MAX_ALIGN) {
        maxLength = length;
      }
    }

    for (ConfigProperty prop : props) {
      String value = getPropertyValue(prop, true);
      int length =
          Math.max(prop.getName().length(), maxLength)
              + (value == null ? 0 : "  = ".length() + value.length());

      if (length > maxLengthValue && length < MAX_ALIGN * 2) {
        maxLengthValue = length;
      }
    }

    for (int i = 0; i < props.length; i++) {
      ConfigProperty prop = props[i];

      StringBuffer buffer = new StringBuffer();

      // 如果项目在配置文件中不存在,则显示?
      boolean absent = !getKeys().contains(prop.getName());

      // 如果是必填项, 则显示*号
      boolean required = prop.isRequired();

      // 如果项目定义在shared properties中,则显示S
      boolean shared = propSet.isShared(prop.getName());

      if (shared) {
        if (absent) {
          buffer.append("s");
        } else {
          buffer.append("S");
        }
      } else {
        if (absent) {
          buffer.append("?");
        } else {
          buffer.append(" ");
        }
      }

      if (required) {
        buffer.append("* ");
      } else {
        buffer.append("  ");
      }

      // 显示property序号
      buffer.append(i + 1).append(" - ");

      // 显示property名称
      buffer.append(prop.getName());

      // 显示property值
      String value = getPropertyValue(prop, true);

      if (value != null) {
        for (int j = 0; j < maxLength - prop.getName().length(); j++) {
          buffer.append(' ');
        }

        buffer.append("  = ").append(value);
      }

      // 显示property描述
      if (prop.getDescription() != null) {
        int length =
            value == null
                ? prop.getName().length()
                : Math.max(prop.getName().length(), maxLength) + "  = ".length() + value.length();

        for (int j = 0; j < maxLengthValue - length; j++) {
          buffer.append(' ');
        }

        buffer.append("   # ").append(prop.getDescription());
      }

      // 如果值是表达式,则同时显示表达式的计算值
      String evaluatedValue = evaluatePropertyValue(prop, true);

      if (evaluatedValue != null && !ObjectUtil.equals(value, evaluatedValue)) {
        buffer.append("\n");

        for (int j = 0; j < maxLength; j++) {
          buffer.append(' ');
        }

        buffer.append("          (").append(evaluatedValue).append(")");

        if (i < props.length - 1) {
          buffer.append("\n");
        }
      }

      println(buffer);
    }

    println();
  }