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

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


    Stack<ValueEval> stack = new Stack<ValueEval>();
    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);
            }
View Full Code Here


  }

  private static void confirmOperandClasses(Ptg[] originalPtgs, Ptg[] convertedPtgs) {
    assertEquals(originalPtgs.length, convertedPtgs.length);
    for (int i = 0; i < convertedPtgs.length; i++) {
      Ptg originalPtg = originalPtgs[i];
      Ptg convertedPtg = convertedPtgs[i];
      if (originalPtg.getPtgClass() != convertedPtg.getPtgClass()) {
        throw new ComparisonFailure("Different operand class for token[" + i + "]",
            String.valueOf(originalPtg.getPtgClass()), String.valueOf(convertedPtg.getPtgClass()));
      }
    }
  }
View Full Code Here

    public void testLinkFormula() {
        RecordInputStream is = new RecordInputStream(new ByteArrayInputStream(linkData));
        is.nextRecord();
        TextObjectRecord rec = new TextObjectRecord(is);

        Ptg ptg = rec.getLinkRefPtg();
        assertNotNull(ptg);
        assertEquals(RefPtg.class, ptg.getClass());
        RefPtg rptg = (RefPtg) ptg;
        assertEquals("T2", rptg.toFormulaString());

        byte [] data2 = rec.serialize();
        assertEquals(linkData.length, data2.length);
View Full Code Here

        assertEquals( 0, record.getIndexNumberFmtRecord());

        Area3DPtg ptgExpected = new Area3DPtg(0, 7936, 0, 0,
                false, false, false, false, 0);
       
        Ptg ptgActual = record.getFormulaOfLink()[0];
        assertEquals(ptgExpected.toString(),  ptgActual.toString());

        assertEquals( data.length + 4, record.getRecordSize() );
    }
View Full Code Here

  }

  private static void confirmUnary(String formulaText, double val, Class<?>...expectedTokenTypes) {
    Ptg[] ptgs = parseFormula(formulaText);
    confirmTokenClasses(ptgs, expectedTokenTypes);
    Ptg ptg0 = ptgs[0];
    if (ptg0 instanceof IntPtg) {
      IntPtg intPtg = (IntPtg) ptg0;
      assertEquals((int)val, intPtg.getValue());
    } else if (ptg0 instanceof NumberPtg) {
      NumberPtg numberPtg = (NumberPtg) ptg0;
View Full Code Here

  }

  private static Ptg parseSingleToken(String formula, Class<? extends Ptg> ptgClass) {
    Ptg[] ptgs = parseFormula(formula);
    assertEquals(1, ptgs.length);
    Ptg result = ptgs[0];
    assertEquals(ptgClass, result.getClass());
    return result;
  }
View Full Code Here

    cell.setCellFormula("[multibookFormulaB.xls]AnotherSheet!B1");
    assertEquals("[multibookFormulaB.xls]AnotherSheet!B1", cell.getCellFormula());
  }
  private static void confirmSingle3DRef(Ptg[] ptgs, int expectedExternSheetIndex) {
    assertEquals(1, ptgs.length);
    Ptg ptg0 = ptgs[0];
    assertTrue(ptg0 instanceof Ref3DPtg);
    assertEquals(expectedExternSheetIndex, ((Ref3DPtg)ptg0).getExternSheetIndex());
  }
View Full Code Here

    AbstractFunctionPtg afp = (AbstractFunctionPtg) ptgs[i];
    assertEquals(expectedFunctionName, afp.getName());
  }

  private void confirmTokenClass(Ptg[] ptgs, int i, byte operandClass) {
    Ptg ptg = ptgs[i];
    if (ptg.isBaseToken()) {
      throw new AssertionFailedError("ptg[" + i + "] is a base token");
    }
    if (operandClass != ptg.getPtgClass()) {
      throw new AssertionFailedError("Wrong operand class for ptg ("
          + ptg.toString() + "). Expected " + getOperandClassName(operandClass)
          + " but got " + getOperandClassName(ptg.getPtgClass()));
    }
  }
View Full Code Here

  private static Ptg[] confirmTokenClasses(String formula, Class<?>[] expectedClasses) {
    return TestFormulaParser.confirmTokenClasses(formula, expectedClasses);
  }

  private static void confirmAttrData(Ptg[] ptgs, int i, int expectedData) {
    Ptg ptg = ptgs[i];
    if (!(ptg instanceof AttrPtg)) {
      throw new AssertionFailedError("Token[" + i + "] was not AttrPtg as expected");
    }
    AttrPtg attrPtg = (AttrPtg) ptg;
    assertEquals(expectedData, attrPtg.getData());
View Full Code Here

      }
    }
    boolean hasMismatch = false;
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < nExcelTokens; i++) {
      Ptg poiPtg = poiPtgs[i];
      Ptg excelPtg = excelPtgs[i];
      if (excelPtg.getClass() != poiPtg.getClass()) {
        hasMismatch = true;
        sb.append("  mismatch token type[" + i + "] " + getShortClassName(excelPtg) + " "
            + excelPtg.getRVAType() + " - " + getShortClassName(poiPtg) + " "
            + poiPtg.getRVAType());
        sb.append(NEW_LINE);
        continue;
      }
      if (poiPtg.isBaseToken()) {
        continue;
      }
      sb.append("  token[" + i + "] " + excelPtg.toString() + " "
          + excelPtg.getRVAType());

      if (excelPtg.getPtgClass() != poiPtg.getPtgClass()) {
        hasMismatch = true;
        sb.append(" - was " + poiPtg.getRVAType());
      }
      sb.append(NEW_LINE);
    }
View Full Code Here

TOP

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

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.