Package org.openntf.formula

Examples of org.openntf.formula.ValueHolder


  public ValueHolder evaluate(final FormulaContext ctx) throws FormulaReturnException {
    if (staticInc != null)
      return staticInc.evaluate(ctx);

    // TODO RPR: This is not yet implemented!
    ValueHolder vh = children[0].evaluate(ctx);
    String key = vh.getString(0);
    Node inc = (Node) ctx.getParser().getInclude(key);

    if (inc == null) {
      return ValueHolder.valueOf(new EvaluateException(codeLine, codeColumn, new IllegalArgumentException("'" + key
          + "' could not be included")));
View Full Code Here


    if (children.length == 1)
      return children[0].evaluate(ctx);

    // check for errors
    ValueHolder vh;
    ValueHolder[] res = new ValueHolder[children.length];
    int size = 0;
    for (int i = 0; i < children.length; i++) {
      vh = children[i].evaluate(ctx);
      if (vh != null && vh.dataType == DataType.ERROR)
        return vh;
      res[i] = vh;
      size = Math.max(size, vh.size);
    }

    int entry = 0;
    vh = res[0].newInstance(size);
    try {
      switch (res[0].dataType) {
      case DOUBLE:
      case INTEGER:
        for (int i = 0; i < size; i++) {
          double sum = 0;
          for (entry = 0; entry < res.length; entry++) {
            sum += res[entry].getDouble(i);
          }
          vh.add(sum);
        }
        break;

      case STRING:
        for (int i = 0; i < size; i++) {

          StringBuffer sb = new StringBuffer();
          for (entry = 0; entry < res.length; entry++) {
            sb.append(res[entry].getString(i));
          }
          vh.add(sb.toString());
        }
        break;
      default:
        throw new UnsupportedOperationException("Concat on " + res[0].dataType + " is not supported");
View Full Code Here

  @SuppressWarnings("deprecation")
  @Override
  public ValueHolder evaluate(final FormulaContext ctx) throws FormulaReturnException {
    try {
      ValueHolder list = children[0].evaluate(ctx);
      if (list.dataType == DataType.ERROR)
        return list;

      ValueHolder temp = children[1].evaluate(ctx);
      if (temp.dataType == DataType.ERROR)
        return temp;

      String varName = temp.getString(0).toLowerCase();
      ValueHolder[] tmpHolders = new ValueHolder[list.size];
      int valueSize = 0;
      int holders = 0;

      for (int i = 0; i < list.size; i++) {
        ValueHolder iter = list.newInstance(1);
        switch (list.dataType) {

        case INTEGER:
          iter.add(list.getInt(i));
          break;
        case DOUBLE:
          iter.add(list.getDouble(i));
          break;
        case BOOLEAN:
          iter.add(list.getBoolean(i));
          break;
        default:
          iter.add(list.getObject(i));
        }

        ValueHolder old = ctx.setVarLC(varName, iter);
        try {
          // Cumulate all return values
          ValueHolder vh = children[2].evaluate(ctx);
          if (vh != ValueHolder.valueNothing()) {
            valueSize += vh.size;
            tmpHolders[holders++] = vh;
            if (vh.dataType == DataType.ERROR)
              return vh;
          }
        } finally {
          ctx.setVarLC(varName, old);
        }
      }
      if (holders == 0)
        return null;

      ValueHolder vhRet = tmpHolders[0].newInstance(valueSize);
      for (int i = 0; i < holders; i++) {
        vhRet.addAll(tmpHolders[i]);
      }
      return vhRet;
    } catch (RuntimeException cause) {
      return ValueHolder.valueOf(new EvaluateException(codeLine, codeColumn, cause));
    }
View Full Code Here

   * AtDoWhile returns always TRUE, or an Error-ValueHolder, if an error occurs in the last parameter.
   */
  @Override
  public ValueHolder evaluate(final FormulaContext ctx) throws FormulaReturnException {

    ValueHolder ret = null;
    if (children != null) {
      do {
        for (int i = 0; i < children.length; ++i) {
          ret = children[i].evaluate(ctx);
        }
        if (ret == null)
          break; // should not happen

        if (ret.dataType == DataType.ERROR)
          return ret;

      } while (ret.isTrue(ctx));
    }
    return ValueHolder.valueOf(1); // returns always TRUE

  }
View Full Code Here

    try {
      boolean sortAscending = true;
      boolean sortCaseSensitive = true;
      boolean sortCustom = false;
      if (children.length >= 2) {
        ValueHolder options = children[1].evaluate(ctx);
        if (options.dataType == DataType.ERROR)
          return options;

        for (int i = 0; i < options.size; i++) {
          String opt = options.getString(i);
          if ("[ASCENDING]".equalsIgnoreCase(opt))
            sortAscending = true;
          else if ("[DESCENDING]".equalsIgnoreCase(opt))
            sortAscending = false;
          else if ("[CASESENSITIVE]".equalsIgnoreCase(opt))
            sortCaseSensitive = true;
          else if ("[CASEINSENSITIVE]".equalsIgnoreCase(opt))
            sortCaseSensitive = false;
          else if ("[CUSTOMSORT]".equalsIgnoreCase(opt))
            sortCustom = true;
          else if (!"[ACCENTSENSITIVE]".equalsIgnoreCase(opt) && !"[ACCENTINSENSITIVE]".equalsIgnoreCase(opt)
              && !"[PITCHSENSITIVE]".equalsIgnoreCase(opt) && !"[PITCHINSENSITIVE]".equalsIgnoreCase(opt))
            throw new IllegalArgumentException("Illegal Option: " + opt);
        }
      }
      Node customSort = null;
      if (sortCustom) {
        if (children.length < 3)
          throw new IllegalArgumentException("Third argument required since option [CUSTOMSORT] present");
        customSort = children[2];
      }
      ValueHolder toSort = children[0].evaluate(ctx);
      if (toSort.dataType == DataType.ERROR)
        return toSort;

      // we must duplicate our valueholder, since it can be Immutable
      ValueHolder ret = toSort.newInstance(toSort.size);
      ret.addAll(toSort);
      doSort(ctx, ret, sortAscending, sortCaseSensitive, customSort);
      return ret;
    } catch (RuntimeException ex) {
      return ValueHolder.valueOf(new EvaluateException(codeLine, codeColumn, ex));
    }
View Full Code Here

  private int doSortString(final FormulaContext ctx, final String s1, final String s2, final boolean sortCaseSensitive,
      final Node customSort) throws FormulaReturnException {
    if (customSort == null)
      return (sortCaseSensitive ? s1.compareTo(s2) : s1.compareToIgnoreCase(s2));

    ValueHolder oldA = ctx.setVarLC("$a", ValueHolder.valueOf(s1));
    ValueHolder oldB = ctx.setVarLC("$b", ValueHolder.valueOf(s2));
    try {
      ValueHolder vh = customSort.evaluate(ctx);
      return (vh.isTrue(ctx)) ? 1 : -1;
    } finally {
      ctx.setVarLC("$a", oldA);
      ctx.setVarLC("$b", oldB);
    }
View Full Code Here

  private int doSortNumber(final FormulaContext ctx, final double n1, final double n2, final Node customSort)
      throws FormulaReturnException {
    if (customSort == null)
      return Double.compare(n1, n2);

    ValueHolder oldA = ctx.setVarLC("$a", ValueHolder.valueOf(n1));
    ValueHolder oldB = ctx.setVarLC("$b", ValueHolder.valueOf(n2));
    try {
      ValueHolder vh = customSort.evaluate(ctx);
      return (vh.isTrue(ctx)) ? 1 : -1;
    } finally {
      ctx.setVarLC("$a", oldA);
      ctx.setVarLC("$b", oldB);
    }
  }
View Full Code Here

   */
  private int doSortDateTime(final FormulaContext ctx, final DateTime d1, final DateTime d2, final Node customSort)
      throws FormulaReturnException {
    if (customSort == null)
      return d1.compare(d1, d2);
    ValueHolder oldA = ctx.setVarLC("$a", ValueHolder.valueOf(d1));
    ValueHolder oldB = ctx.setVarLC("$b", ValueHolder.valueOf(d2));
    try {
      ValueHolder vh = customSort.evaluate(ctx);
      return (vh.isTrue(ctx)) ? 1 : -1;
    } finally {
      ctx.setVarLC("$a", oldA);
      ctx.setVarLC("$b", oldB);
    }
  }
View Full Code Here

  /**
   * Evaluates every entry and returns the last one
   */
  @Override
  public ValueHolder evaluate(final FormulaContext ctx) throws FormulaReturnException {
    ValueHolder vhEval = children[0].evaluate(ctx);
    if (vhEval.dataType == DataType.ERROR)
      return vhEval;

    ValueHolder ret[] = new ValueHolder[vhEval.size];
    int size = 0;
    try {
      for (int i = 0; i < vhEval.size; i++) {
        String toEval = vhEval.getString(i);

        Node n = (Node) ctx.getParser().parse(toEval, true);
        ValueHolder vh = ret[i] = n.evaluate(ctx);

        if (vh.dataType == DataType.ERROR)
          return ret[i];

        size += vh.size;
      }
      ValueHolder vh = ret[0].newInstance(size);
      for (int i = 0; i < vhEval.size; i++) {
        vh.addAll(ret[i]);
      }
      return vh;

    } catch (FormulaParseException e) {
      return ValueHolder.valueOf(new EvaluateException(codeLine, codeColumn, e));
View Full Code Here

  @Override
  public ValueHolder evaluate(final FormulaContext ctx) {
    if (dateValue != null)
      return ValueHolder.valueOf(dateValue);
    ValueHolder vh = ValueHolder.valueOf(image);
    vh.dataType = DataType.KEYWORD_STRING;
    return vh;
  }
View Full Code Here

TOP

Related Classes of org.openntf.formula.ValueHolder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.