Package org.apache.poi.ss.formula.ptg

Examples of org.apache.poi.ss.formula.ptg.AttrPtg


    for (int i = 0, iSize = ptgs.length; i < iSize; i++) {

      // since we don't know how to handle these yet :(
      Ptg ptg = ptgs[i];
      if (ptg instanceof AttrPtg) {
        AttrPtg attrPtg = (AttrPtg) ptg;
        if (attrPtg.isSum()) {
          // Excel prefers to encode 'SUM()' as a tAttr token, but this evaluator
          // expects the equivalent function token
          ptg = FuncVarPtg.SUM;
        }
        if (attrPtg.isOptimizedChoose()) {
          ValueEval arg0 = stack.pop();
          int[] jumpTable = attrPtg.getJumpTable();
          int dist;
          int nChoices = jumpTable.length;
          try {
            int switchIndex = Choose.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
            if (switchIndex<1 || switchIndex > nChoices) {
              stack.push(ErrorEval.VALUE_INVALID);
              dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
            } else {
              dist = jumpTable[switchIndex-1];
            }
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
          }
          // Encoded dist for tAttrChoose includes size of jump table, but
          // countTokensToBeSkipped() does not (it counts whole tokens).
          dist -= nChoices*2+2; // subtract jump table size
          i+= countTokensToBeSkipped(ptgs, i, dist);
          continue;
        }
        if (attrPtg.isOptimizedIf()) {
          ValueEval arg0 = stack.pop();
          boolean evaluatedPredicate;
          try {
            evaluatedPredicate = IfFunc.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            attrPtg = (AttrPtg) ptgs[i];
            dist = attrPtg.getData()+1;
            i+= countTokensToBeSkipped(ptgs, i, dist);
            continue;
          }
          if (evaluatedPredicate) {
            // nothing to skip - true param folows
          } else {
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            Ptg nextPtg = ptgs[i+1];
            if (ptgs[i] instanceof AttrPtg && nextPtg instanceof FuncVarPtg) {
              // this is an if statement without a false param (as opposed to MissingArgPtg as the false param)
              i++;
              stack.push(BoolEval.FALSE);
            }
          }
          continue;
        }
        if (attrPtg.isSkip()) {
          int dist = attrPtg.getData()+1;
          i+= countTokensToBeSkipped(ptgs, i, dist);
          if (stack.peek() == MissingArgEval.instance) {
            stack.pop();
            stack.push(BlankEval.instance);
          }
View Full Code Here


    for (int i = 0, iSize = ptgs.length; i < iSize; i++) {

      // since we don't know how to handle these yet :(
      Ptg ptg = ptgs[i];
      if (ptg instanceof AttrPtg) {
        AttrPtg attrPtg = (AttrPtg) ptg;
        if (attrPtg.isSum()) {
          // Excel prefers to encode 'SUM()' as a tAttr token, but this evaluator
          // expects the equivalent function token
          ptg = FuncVarPtg.SUM;
        }
        if (attrPtg.isOptimizedChoose()) {
          ValueEval arg0 = stack.pop();
          int[] jumpTable = attrPtg.getJumpTable();
          int dist;
          int nChoices = jumpTable.length;
          try {
            int switchIndex = Choose.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
            if (switchIndex<1 || switchIndex > nChoices) {
              stack.push(ErrorEval.VALUE_INVALID);
              dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
            } else {
              dist = jumpTable[switchIndex-1];
            }
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
          }
          // Encoded dist for tAttrChoose includes size of jump table, but
          // countTokensToBeSkipped() does not (it counts whole tokens).
          dist -= nChoices*2+2; // subtract jump table size
          i+= countTokensToBeSkipped(ptgs, i, dist);
          continue;
        }
        if (attrPtg.isOptimizedIf()) {
          ValueEval arg0 = stack.pop();
          boolean evaluatedPredicate;
          try {
            evaluatedPredicate = IfFunc.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            attrPtg = (AttrPtg) ptgs[i];
            dist = attrPtg.getData()+1;
            i+= countTokensToBeSkipped(ptgs, i, dist);
            continue;
          }
          if (evaluatedPredicate) {
            // nothing to skip - true param folows
          } else {
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            Ptg nextPtg = ptgs[i+1];
            if (ptgs[i] instanceof AttrPtg && nextPtg instanceof FuncVarPtg) {
              // this is an if statement without a false param (as opposed to MissingArgPtg as the false param)
              i++;
              stack.push(BoolEval.FALSE);
            }
          }
          continue;
        }
        if (attrPtg.isSkip()) {
          int dist = attrPtg.getData()+1;
          i+= countTokensToBeSkipped(ptgs, i, dist);
          if (stack.peek() == MissingArgEval.instance) {
            stack.pop();
            stack.push(BlankEval.instance);
          }
View Full Code Here

                String contents = stack.pop();
                stack.push ("(" + contents + ")");
                continue;
            }
            if (ptg instanceof AttrPtg) {
                AttrPtg attrPtg = ((AttrPtg) ptg);
                if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isSkip()) {
                    continue;
                }
                if (attrPtg.isSpace()) {
                    // POI currently doesn't render spaces in formulas
                    continue;
                    // but if it ever did, care must be taken:
                    // tAttrSpace comes *before* the operand it applies to, which may be consistent
                    // with how the formula text appears but is against the RPN ordering assumed here
                }
                if (attrPtg.isSemiVolatile()) {
                    // similar to tAttrSpace - RPN is violated
                    continue;
                }
                if (attrPtg.isSum()) {
                    String[] operands = getOperands(stack, attrPtg.getNumberOfOperands());
                    stack.push(attrPtg.toFormulaString(operands));
                    continue;
                }
                throw new RuntimeException("Unexpected tAttr: " + attrPtg.toString());
            }

            if (ptg instanceof WorkbookDependentFormula) {
                WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg;
                stack.push(optg.toFormulaString(book));
View Full Code Here

  public void testSpaceAtStartOfFormula() {
    // Simulating cell formula of "= 4" (note space)
    // The same Ptg array can be observed if an excel file is saved with that exact formula

    AttrPtg spacePtg = AttrPtg.createSpace(AttrPtg.SpaceType.SPACE_BEFORE, 1);
    Ptg[] ptgs = { spacePtg, new IntPtg(4), };
    String formulaString;
    try {
      formulaString = toFormulaString(ptgs);
    } catch (IllegalStateException e) {
View Full Code Here

      Ptg ptg = ptgs[i];
      if (dbgEvaluationOutputIndent > 0) {
        EVAL_LOG.log(POILogger.INFO, dbgIndentStr + "  * ptg " + i + ": " + ptg);
      }
      if (ptg instanceof AttrPtg) {
        AttrPtg attrPtg = (AttrPtg) ptg;
        if (attrPtg.isSum()) {
          // Excel prefers to encode 'SUM()' as a tAttr token, but this evaluator
          // expects the equivalent function token
          ptg = FuncVarPtg.SUM;
        }
        if (attrPtg.isOptimizedChoose()) {
          ValueEval arg0 = stack.pop();
          int[] jumpTable = attrPtg.getJumpTable();
          int dist;
          int nChoices = jumpTable.length;
          try {
            int switchIndex = Choose.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
            if (switchIndex<1 || switchIndex > nChoices) {
              stack.push(ErrorEval.VALUE_INVALID);
              dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
            } else {
              dist = jumpTable[switchIndex-1];
            }
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
          }
          // Encoded dist for tAttrChoose includes size of jump table, but
          // countTokensToBeSkipped() does not (it counts whole tokens).
          dist -= nChoices*2+2; // subtract jump table size
          i+= countTokensToBeSkipped(ptgs, i, dist);
          continue;
        }
        if (attrPtg.isOptimizedIf()) {
          ValueEval arg0 = stack.pop();
          boolean evaluatedPredicate;
          try {
            evaluatedPredicate = IfFunc.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            attrPtg = (AttrPtg) ptgs[i];
            dist = attrPtg.getData()+1;
            i+= countTokensToBeSkipped(ptgs, i, dist);
            continue;
          }
          if (evaluatedPredicate) {
            // nothing to skip - true param folows
          } else {
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            Ptg nextPtg = ptgs[i+1];
            if (ptgs[i] instanceof AttrPtg && nextPtg instanceof FuncVarPtg) {
              // this is an if statement without a false param (as opposed to MissingArgPtg as the false param)
              i++;
              stack.push(BoolEval.FALSE);
            }
          }
          continue;
        }
        if (attrPtg.isSkip()) {
          int dist = attrPtg.getData()+1;
          i+= countTokensToBeSkipped(ptgs, i, dist);
          if (stack.peek() == MissingArgEval.instance) {
            stack.pop();
            stack.push(BlankEval.instance);
          }
View Full Code Here

    for (int i = 0, iSize = ptgs.length; i < iSize; i++) {

      // since we don't know how to handle these yet :(
      Ptg ptg = ptgs[i];
      if (ptg instanceof AttrPtg) {
        AttrPtg attrPtg = (AttrPtg) ptg;
        if (attrPtg.isSum()) {
          // Excel prefers to encode 'SUM()' as a tAttr token, but this evaluator
          // expects the equivalent function token
          ptg = FuncVarPtg.SUM;
        }
        if (attrPtg.isOptimizedChoose()) {
          ValueEval arg0 = stack.pop();
          int[] jumpTable = attrPtg.getJumpTable();
          int dist;
          int nChoices = jumpTable.length;
          try {
            int switchIndex = Choose.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
            if (switchIndex<1 || switchIndex > nChoices) {
              stack.push(ErrorEval.VALUE_INVALID);
              dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
            } else {
              dist = jumpTable[switchIndex-1];
            }
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
          }
          // Encoded dist for tAttrChoose includes size of jump table, but
          // countTokensToBeSkipped() does not (it counts whole tokens).
          dist -= nChoices*2+2; // subtract jump table size
          i+= countTokensToBeSkipped(ptgs, i, dist);
          continue;
        }
        if (attrPtg.isOptimizedIf()) {
          ValueEval arg0 = stack.pop();
          boolean evaluatedPredicate;
          try {
            evaluatedPredicate = IfFunc.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            attrPtg = (AttrPtg) ptgs[i];
            dist = attrPtg.getData()+1;
            i+= countTokensToBeSkipped(ptgs, i, dist);
            continue;
          }
          if (evaluatedPredicate) {
            // nothing to skip - true param folows
          } else {
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            Ptg nextPtg = ptgs[i+1];
            if (ptgs[i] instanceof AttrPtg && nextPtg instanceof FuncVarPtg) {
              // this is an if statement without a false param (as opposed to MissingArgPtg as the false param)
              i++;
              stack.push(BoolEval.FALSE);
            }
          }
          continue;
        }
        if (attrPtg.isSkip()) {
          int dist = attrPtg.getData()+1;
          i+= countTokensToBeSkipped(ptgs, i, dist);
          if (stack.peek() == MissingArgEval.instance) {
            stack.pop();
            stack.push(BlankEval.instance);
          }
View Full Code Here

                String contents = stack.pop();
                stack.push ("(" + contents + ")");
                continue;
            }
            if (ptg instanceof AttrPtg) {
                AttrPtg attrPtg = ((AttrPtg) ptg);
                if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isSkip()) {
                    continue;
                }
                if (attrPtg.isSpace()) {
                    // POI currently doesn't render spaces in formulas
                    continue;
                    // but if it ever did, care must be taken:
                    // tAttrSpace comes *before* the operand it applies to, which may be consistent
                    // with how the formula text appears but is against the RPN ordering assumed here
                }
                if (attrPtg.isSemiVolatile()) {
                    // similar to tAttrSpace - RPN is violated
                    continue;
                }
                if (attrPtg.isSum()) {
                    String[] operands = getOperands(stack, attrPtg.getNumberOfOperands());
                    stack.push(attrPtg.toFormulaString(operands));
                    continue;
                }
                throw new RuntimeException("Unexpected tAttr: " + attrPtg.toString());
            }

            if (ptg instanceof WorkbookDependentFormula) {
                WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg;
                stack.push(optg.toFormulaString(book));
View Full Code Here

    token.setClass(transformClass(token.getPtgClass(), desiredOperandClass, callerForceArrayFlag));
  }

  private static boolean isSingleArgSum(Ptg token) {
    if (token instanceof AttrPtg) {
      AttrPtg attrPtg = (AttrPtg) token;
      return attrPtg.isSum();
    }
    return false;
  }
View Full Code Here

    // placeholder for first skip attr
    int skipAfterTrueParamIndex = temp.createPlaceholder();
    int trueParamSize = temp.sumTokenSizes(ifAttrIndex+1, skipAfterTrueParamIndex);

    AttrPtg attrIf = AttrPtg.createIf(trueParamSize + 4); // distance to start of false parameter/tFuncVar. +4 for tAttrSkip after true

    if (getChildren().length > 2) {
      // false param present

      // false parameter
      getChildren()[2].collectPtgs(temp);

      int skipAfterFalseParamIndex = temp.createPlaceholder();

      int falseParamSize =  temp.sumTokenSizes(skipAfterTrueParamIndex+1, skipAfterFalseParamIndex);

      AttrPtg attrSkipAfterTrue = AttrPtg.createSkip(falseParamSize + 4 + 4 - 1); // 1 less than distance to end of if FuncVar(size=4). +4 for attr skip before
      AttrPtg attrSkipAfterFalse = AttrPtg.createSkip(4 - 1); // 1 less than distance to end of if FuncVar(size=4).

      temp.setPlaceholder(ifAttrIndex, attrIf);
      temp.setPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
      temp.setPlaceholder(skipAfterFalseParamIndex, attrSkipAfterFalse);
    } else {
      // false parameter not present
      AttrPtg attrSkipAfterTrue = AttrPtg.createSkip(4 - 1); // 1 less than distance to end of if FuncVar(size=4).

      temp.setPlaceholder(ifAttrIndex, attrIf);
      temp.setPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
    }
    temp.add(_token);
View Full Code Here

      Ptg ptg = ptgs[i];
      if (dbgEvaluationOutputIndent > 0) {
        EVAL_LOG.log(POILogger.INFO, dbgIndentStr + "  * ptg " + i + ": " + ptg);
      }
      if (ptg instanceof AttrPtg) {
        AttrPtg attrPtg = (AttrPtg) ptg;
        if (attrPtg.isSum()) {
          // Excel prefers to encode 'SUM()' as a tAttr token, but this evaluator
          // expects the equivalent function token
          ptg = FuncVarPtg.SUM;
        }
        if (attrPtg.isOptimizedChoose()) {
          ValueEval arg0 = stack.pop();
          int[] jumpTable = attrPtg.getJumpTable();
          int dist;
          int nChoices = jumpTable.length;
          try {
            int switchIndex = Choose.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
            if (switchIndex<1 || switchIndex > nChoices) {
              stack.push(ErrorEval.VALUE_INVALID);
              dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
            } else {
              dist = jumpTable[switchIndex-1];
            }
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            dist = attrPtg.getChooseFuncOffset() + 4; // +4 for tFuncFar(CHOOSE)
          }
          // Encoded dist for tAttrChoose includes size of jump table, but
          // countTokensToBeSkipped() does not (it counts whole tokens).
          dist -= nChoices*2+2; // subtract jump table size
          i+= countTokensToBeSkipped(ptgs, i, dist);
          continue;
        }
        if (attrPtg.isOptimizedIf()) {
          ValueEval arg0 = stack.pop();
          boolean evaluatedPredicate;
          try {
            evaluatedPredicate = IfFunc.evaluateFirstArg(arg0, ec.getRowIndex(), ec.getColumnIndex());
          } catch (EvaluationException e) {
            stack.push(e.getErrorEval());
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            attrPtg = (AttrPtg) ptgs[i];
            dist = attrPtg.getData()+1;
            i+= countTokensToBeSkipped(ptgs, i, dist);
            continue;
          }
          if (evaluatedPredicate) {
            // nothing to skip - true param folows
          } else {
            int dist = attrPtg.getData();
            i+= countTokensToBeSkipped(ptgs, i, dist);
            Ptg nextPtg = ptgs[i+1];
            if (ptgs[i] instanceof AttrPtg && nextPtg instanceof FuncVarPtg) {
              // this is an if statement without a false param (as opposed to MissingArgPtg as the false param)
              i++;
              stack.push(BoolEval.FALSE);
            }
          }
          continue;
        }
        if (attrPtg.isSkip()) {
          int dist = attrPtg.getData()+1;
          i+= countTokensToBeSkipped(ptgs, i, dist);
          if (stack.peek() == MissingArgEval.instance) {
            stack.pop();
            stack.push(BlankEval.instance);
          }
View Full Code Here

TOP

Related Classes of org.apache.poi.ss.formula.ptg.AttrPtg

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.