From 357f3eaf7d92a09650d36f54dd49d1c771cbbc4c Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Fri, 29 Aug 2008 03:25:23 +0000 Subject: [PATCH] Changed FormulaRecord.getParsedExpression to return Ptg array git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@690091 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/dev/FormulaViewer.java | 60 ++++++++----------- .../apache/poi/hssf/record/FormulaRecord.java | 14 +---- .../TestEventWorkbookBuilder.java | 43 +++++++------ .../poi/hssf/record/TestFormulaRecord.java | 41 ++++++------- .../poi/hssf/usermodel/FormulaExtractor.java | 8 +-- .../poi/hssf/usermodel/TestBug42464.java | 11 ++-- .../usermodel/TestFormulaEvaluatorBugs.java | 12 ++-- 7 files changed, 82 insertions(+), 107 deletions(-) diff --git a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java index 91d342099b..36e527ae5b 100644 --- a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java +++ b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java @@ -15,25 +15,21 @@ limitations under the License. ==================================================================== */ - -/* - * FormulaViewer.java - finds formulas in a BIFF8 file and attempts to parse them and - * display info about them. - * - * Created on November 18, 2001, 7:58 AM - */ package org.apache.poi.hssf.dev; import java.io.FileInputStream; - -//import java.io.*; import java.util.List; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.apache.poi.hssf.record.*; -import org.apache.poi.hssf.record.formula.*; +import org.apache.poi.hssf.model.FormulaParser; +import org.apache.poi.hssf.record.FormulaRecord; +import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.RecordFactory; +import org.apache.poi.hssf.record.formula.ExpPtg; +import org.apache.poi.hssf.record.formula.FuncPtg; +import org.apache.poi.hssf.record.formula.OperationPtg; +import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.hssf.model.*; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * FormulaViewer - finds formulas in a BIFF8 file and attempts to read them/display @@ -87,20 +83,21 @@ public class FormulaViewer private void listFormula(FormulaRecord record) { String sep="~"; - List tokens= record.getParsedExpression(); - int numptgs = record.getNumberOfExpressionTokens(); - Ptg token = null; - String name,numArg; - if (tokens != null) { - token = (Ptg) tokens.get(numptgs-1); + Ptg[] tokens= record.getParsedExpression(); + Ptg token; + int numptgs = tokens.length; + String numArg; + token = tokens[numptgs-1]; if (token instanceof FuncPtg) { numArg = String.valueOf(numptgs-1); - } else { numArg = String.valueOf(-1);} + } else { + numArg = String.valueOf(-1); + } StringBuffer buf = new StringBuffer(); if (token instanceof ExpPtg) return; - buf.append(name=((OperationPtg) token).toFormulaString((HSSFWorkbook)null)); + buf.append(((OperationPtg) token).toFormulaString((HSSFWorkbook)null)); buf.append(sep); switch (token.getPtgClass()) { case Ptg.CLASS_REF : @@ -116,7 +113,7 @@ public class FormulaViewer buf.append(sep); if (numptgs>1) { - token = (Ptg) tokens.get(numptgs-2); + token = tokens[numptgs-2]; switch (token.getPtgClass()) { case Ptg.CLASS_REF : buf.append("REF"); @@ -134,9 +131,6 @@ public class FormulaViewer buf.append(sep); buf.append(numArg); System.out.println(buf.toString()); - } else { - System.out.println("#NAME"); - } } /** @@ -155,20 +149,18 @@ public class FormulaViewer System.out.println("value = " + record.getValue()); System.out.print("xf = " + record.getXFIndex()); System.out.print(", number of ptgs = " - + record.getNumberOfExpressionTokens()); + + record.getParsedExpression().length); System.out.println(", options = " + record.getOptions()); System.out.println("RPN List = "+formulaString(record)); System.out.println("Formula text = "+ composeFormula(record)); } private String formulaString(FormulaRecord record) { - StringBuffer formula = new StringBuffer("="); - int numptgs = record.getNumberOfExpressionTokens(); - List tokens = record.getParsedExpression(); - Ptg token; + StringBuffer buf = new StringBuffer(); - for (int i=0;inull */ - public int getNumberOfExpressionTokens() { - return field_8_parsed_expr.length; - } - - /** - * @return list of formula tokens. never null - */ - public List getParsedExpression() { - return Arrays.asList(field_8_parsed_expr); // TODO - return array + public Ptg[] getParsedExpression() { + return (Ptg[]) field_8_parsed_expr.clone(); } public void setParsedExpression(Ptg[] ptgs) { diff --git a/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java b/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java index 215732deb5..c8beb7b78b 100644 --- a/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java +++ b/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java @@ -29,6 +29,7 @@ import org.apache.poi.hssf.model.FormulaParser; import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.Ref3DPtg; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.SheetReferences; @@ -56,13 +57,13 @@ public final class TestEventWorkbookBuilder extends TestCase { } } - public void testBasics() throws Exception { + public void testBasics() { assertNotNull(listener.getSSTRecord()); assertNotNull(listener.getBoundSheetRecords()); assertNotNull(listener.getExternSheetRecords()); } - public void testGetStubWorkbooks() throws Exception { + public void testGetStubWorkbooks() { assertNotNull(listener.getStubWorkbook()); assertNotNull(listener.getStubHSSFWorkbook()); @@ -70,7 +71,7 @@ public final class TestEventWorkbookBuilder extends TestCase { assertNotNull(listener.getStubHSSFWorkbook().getSheetReferences()); } - public void testContents() throws Exception { + public void testContents() { assertEquals(2, listener.getSSTRecord().getNumStrings()); assertEquals(3, listener.getBoundSheetRecords().length); assertEquals(1, listener.getExternSheetRecords().length); @@ -83,11 +84,12 @@ public final class TestEventWorkbookBuilder extends TestCase { assertEquals("S2", ref.getSheetName(2)); } - public void testFormulas() throws Exception { - FormulaRecord fr; + public void testFormulas() { + + FormulaRecord[] fRecs = mockListen.getFormulaRecords(); // Check our formula records - assertEquals(6, mockListen._frecs.size()); + assertEquals(6, fRecs.length); Workbook stubWB = listener.getStubWorkbook(); assertNotNull(stubWB); @@ -100,47 +102,45 @@ public final class TestEventWorkbookBuilder extends TestCase { assertEquals("Sh3", stubWB.getSheetName(2)); // Check we can get the formula without breaking - for(int i=0; i " + cell.getCellFormula()); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java index 7dbb9c3e16..6ebcf96bb6 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java @@ -20,7 +20,6 @@ package org.apache.poi.hssf.usermodel; import java.io.File; import java.io.FileOutputStream; import java.util.Iterator; -import java.util.List; import junit.framework.AssertionFailedError; import junit.framework.TestCase; @@ -29,6 +28,7 @@ import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate; import org.apache.poi.hssf.record.formula.AreaPtg; import org.apache.poi.hssf.record.formula.FuncVarPtg; +import org.apache.poi.hssf.record.formula.Ptg; /** * @@ -183,14 +183,14 @@ public final class TestFormulaEvaluatorBugs extends TestCase { HSSFCell cellSUM = rowSUM.getCell(0); FormulaRecordAggregate frec = (FormulaRecordAggregate) cellSUM.getCellValueRecord(); - List ops = frec.getFormulaRecord().getParsedExpression(); - assertEquals(2, ops.size()); - assertEquals(AreaPtg.class, ops.get(0).getClass()); - assertEquals(FuncVarPtg.class, ops.get(1).getClass()); + Ptg[] ops = frec.getFormulaRecord().getParsedExpression(); + assertEquals(2, ops.length); + assertEquals(AreaPtg.class, ops[0].getClass()); + assertEquals(FuncVarPtg.class, ops[1].getClass()); // Actually stored as C1 to C65536 // (last row is -1 === 65535) - AreaPtg ptg = (AreaPtg) ops.get(0); + AreaPtg ptg = (AreaPtg) ops[0]; assertEquals(2, ptg.getFirstColumn()); assertEquals(2, ptg.getLastColumn()); assertEquals(0, ptg.getFirstRow()); -- 2.39.5