From 1d568d4da328327821e73ea16273b23971a47da1 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Thu, 18 Sep 2008 21:22:23 +0000 Subject: [PATCH] Partitioning common formula logic. Introduced FormulaRenderingWorkbook interface to make merge with ooxml branch easier git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@696813 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/dev/FormulaViewer.java | 4 +- .../apache/poi/hssf/model/FormulaParser.java | 128 +++++------------- .../record/formula/AbstractFunctionPtg.java | 3 +- .../hssf/record/formula/Area2DPtgBase.java | 3 +- .../poi/hssf/record/formula/Area3DPtg.java | 9 +- .../poi/hssf/record/formula/AreaErrPtg.java | 3 +- .../poi/hssf/record/formula/AreaPtgBase.java | 12 +- .../poi/hssf/record/formula/ArrayPtg.java | 4 +- .../poi/hssf/record/formula/AttrPtg.java | 12 +- .../poi/hssf/record/formula/BoolPtg.java | 2 +- .../hssf/record/formula/DeletedArea3DPtg.java | 10 +- .../hssf/record/formula/DeletedRef3DPtg.java | 10 +- .../poi/hssf/record/formula/ErrPtg.java | 2 +- .../poi/hssf/record/formula/ExpPtg.java | 4 +- .../formula/ExternSheetNameResolver.java | 7 +- .../poi/hssf/record/formula/IntPtg.java | 2 +- .../hssf/record/formula/IntersectionPtg.java | 3 +- .../poi/hssf/record/formula/MemAreaPtg.java | 5 +- .../poi/hssf/record/formula/MemErrPtg.java | 2 +- .../poi/hssf/record/formula/MemFuncPtg.java | 5 +- .../hssf/record/formula/MissingArgPtg.java | 2 +- .../poi/hssf/record/formula/NamePtg.java | 13 +- .../poi/hssf/record/formula/NameXPtg.java | 14 +- .../poi/hssf/record/formula/NumberPtg.java | 2 +- .../poi/hssf/record/formula/OperandPtg.java | 1 + .../poi/hssf/record/formula/OperationPtg.java | 7 + .../hssf/record/formula/ParenthesisPtg.java | 6 +- .../apache/poi/hssf/record/formula/Ptg.java | 2 +- .../poi/hssf/record/formula/RangePtg.java | 3 +- .../poi/hssf/record/formula/Ref2DPtgBase.java | 3 +- .../poi/hssf/record/formula/Ref3DPtg.java | 9 +- .../poi/hssf/record/formula/RefErrorPtg.java | 2 +- .../record/formula/ScalarConstantPtg.java | 7 - .../poi/hssf/record/formula/StringPtg.java | 2 +- .../poi/hssf/record/formula/TblPtg.java | 4 +- .../poi/hssf/record/formula/UnionPtg.java | 3 +- .../poi/hssf/record/formula/UnknownPtg.java | 3 +- .../hssf/record/formula/ValueOperatorPtg.java | 2 +- .../usermodel/HSSFEvaluationWorkbook.java | 38 ++++++ .../poi/ss/formula/FormulaRenderer.java | 124 +++++++++++++++++ .../ss/formula/FormulaRenderingWorkbook.java | 11 ++ .../ss/formula/WorkbookDependentFormula.java | 5 + .../org/apache/poi/ss/formula/package.html | 29 ++++ .../TestEventWorkbookBuilder.java | 4 +- .../poi/hssf/model/TestFormulaParser.java | 6 +- .../poi/hssf/model/TestFormulaParserIf.java | 3 +- .../hssf/record/TestSharedFormulaRecord.java | 2 +- .../hssf/record/formula/TestArea3DPtg.java | 27 ++-- .../poi/hssf/record/formula/TestAreaPtg.java | 16 +-- .../poi/hssf/record/formula/TestArrayPtg.java | 2 +- .../poi/hssf/record/formula/TestRef3DPtg.java | 20 +-- .../usermodel/TestFormulaEvaluatorBugs.java | 3 +- .../poi/hssf/util/TestAreaReference.java | 10 +- 53 files changed, 385 insertions(+), 230 deletions(-) create mode 100644 src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java create mode 100644 src/java/org/apache/poi/ss/formula/FormulaRenderer.java create mode 100644 src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java create mode 100644 src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java create mode 100644 src/java/org/apache/poi/ss/formula/package.html diff --git a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java index 36e527ae5b..167eaba532 100644 --- a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java +++ b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java @@ -97,7 +97,7 @@ public class FormulaViewer StringBuffer buf = new StringBuffer(); if (token instanceof ExpPtg) return; - buf.append(((OperationPtg) token).toFormulaString((HSSFWorkbook)null)); + buf.append(((OperationPtg) token).toFormulaString()); buf.append(sep); switch (token.getPtgClass()) { case Ptg.CLASS_REF : @@ -161,7 +161,7 @@ public class FormulaViewer Ptg[] tokens = record.getParsedExpression(); for (int i = 0; i < tokens.length; i++) { Ptg token = tokens[i]; - buf.append( token.toFormulaString((HSSFWorkbook)null)); + buf.append( token.toFormulaString()); switch (token.getPtgClass()) { case Ptg.CLASS_REF : buf.append("(R)"); diff --git a/src/java/org/apache/poi/hssf/model/FormulaParser.java b/src/java/org/apache/poi/hssf/model/FormulaParser.java index 6d0c23374f..a24c7be607 100644 --- a/src/java/org/apache/poi/hssf/model/FormulaParser.java +++ b/src/java/org/apache/poi/hssf/model/FormulaParser.java @@ -19,21 +19,53 @@ package org.apache.poi.hssf.model; import java.util.ArrayList; import java.util.List; -import java.util.Stack; -//import PTGs .. since we need everything, import * import org.apache.poi.hssf.record.UnicodeString; import org.apache.poi.hssf.record.constant.ErrorConstant; -import org.apache.poi.hssf.record.formula.*; +import org.apache.poi.hssf.record.formula.AbstractFunctionPtg; +import org.apache.poi.hssf.record.formula.AddPtg; +import org.apache.poi.hssf.record.formula.Area3DPtg; +import org.apache.poi.hssf.record.formula.AreaPtg; +import org.apache.poi.hssf.record.formula.ArrayPtg; +import org.apache.poi.hssf.record.formula.BoolPtg; +import org.apache.poi.hssf.record.formula.ConcatPtg; +import org.apache.poi.hssf.record.formula.DividePtg; +import org.apache.poi.hssf.record.formula.EqualPtg; +import org.apache.poi.hssf.record.formula.ErrPtg; +import org.apache.poi.hssf.record.formula.FuncPtg; +import org.apache.poi.hssf.record.formula.FuncVarPtg; +import org.apache.poi.hssf.record.formula.GreaterEqualPtg; +import org.apache.poi.hssf.record.formula.GreaterThanPtg; +import org.apache.poi.hssf.record.formula.IntPtg; +import org.apache.poi.hssf.record.formula.LessEqualPtg; +import org.apache.poi.hssf.record.formula.LessThanPtg; +import org.apache.poi.hssf.record.formula.MissingArgPtg; +import org.apache.poi.hssf.record.formula.MultiplyPtg; +import org.apache.poi.hssf.record.formula.NamePtg; +import org.apache.poi.hssf.record.formula.NameXPtg; +import org.apache.poi.hssf.record.formula.NotEqualPtg; +import org.apache.poi.hssf.record.formula.NumberPtg; +import org.apache.poi.hssf.record.formula.ParenthesisPtg; +import org.apache.poi.hssf.record.formula.PercentPtg; +import org.apache.poi.hssf.record.formula.PowerPtg; +import org.apache.poi.hssf.record.formula.Ptg; +import org.apache.poi.hssf.record.formula.Ref3DPtg; +import org.apache.poi.hssf.record.formula.RefPtg; +import org.apache.poi.hssf.record.formula.StringPtg; +import org.apache.poi.hssf.record.formula.SubtractPtg; +import org.apache.poi.hssf.record.formula.UnaryMinusPtg; +import org.apache.poi.hssf.record.formula.UnaryPlusPtg; import org.apache.poi.hssf.record.formula.function.FunctionMetadata; import org.apache.poi.hssf.record.formula.function.FunctionMetadataRegistry; import org.apache.poi.hssf.usermodel.HSSFErrorConstants; +import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook; import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; import org.apache.poi.hssf.usermodel.HSSFName; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.AreaReference; import org.apache.poi.hssf.util.CellReference; import org.apache.poi.hssf.util.CellReference.NameType; +import org.apache.poi.ss.formula.FormulaRenderer; /** * This class parses a formula string into a List of tokens in RPN order. @@ -1001,94 +1033,6 @@ end; * @return a human readable String */ public static String toFormulaString(HSSFWorkbook book, Ptg[] ptgs) { - if (ptgs == null || ptgs.length == 0) { - // TODO - what is the justification for returning "#NAME" (which is not "#NAME?", btw) - return "#NAME"; - } - Stack stack = new Stack(); - - for (int i=0 ; i < ptgs.length; i++) { - Ptg ptg = ptgs[i]; - // TODO - what about MemNoMemPtg? - if(ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) { - // marks the start of a list of area expressions which will be naturally combined - // by their trailing operators (e.g. UnionPtg) - // TODO - put comment and throw exception in toFormulaString() of these classes - continue; - } - if (ptg instanceof ParenthesisPtg) { - String contents = (String)stack.pop(); - stack.push ("(" + contents + ")"); - continue; - } - if (ptg instanceof AttrPtg) { - AttrPtg attrPtg = ((AttrPtg) ptg); - if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isGoto()) { - 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 OperationPtg)) { - stack.push(ptg.toFormulaString(book)); - continue; - } - - OperationPtg o = (OperationPtg) ptg; - String[] operands = getOperands(stack, o.getNumberOfOperands()); - stack.push(o.toFormulaString(operands)); - } - if(stack.isEmpty()) { - // inspection of the code above reveals that every stack.pop() is followed by a - // stack.push(). So this is either an internal error or impossible. - throw new IllegalStateException("Stack underflow"); - } - String result = (String) stack.pop(); - if(!stack.isEmpty()) { - // Might be caused by some tokens like AttrPtg and Mem*Ptg, which really shouldn't - // put anything on the stack - throw new IllegalStateException("too much stuff left on the stack"); - } - return result; - } - - private static String[] getOperands(Stack stack, int nOperands) { - String[] operands = new String[nOperands]; - - for (int j = nOperands-1; j >= 0; j--) { // reverse iteration because args were pushed in-order - if(stack.isEmpty()) { - String msg = "Too few arguments supplied to operation. Expected (" + nOperands - + ") operands but got (" + (nOperands - j - 1) + ")"; - throw new IllegalStateException(msg); - } - operands[j] = (String) stack.pop(); - } - return operands; - } - /** - * Static method to convert an array of Ptgs in RPN order - * to a human readable string format in infix mode. Works - * on the current workbook for named and 3D references. - * @param ptgs array of Ptg, can be null or empty - * @return a human readable String - */ - public String toFormulaString(Ptg[] ptgs) { - return toFormulaString(book, ptgs); + return FormulaRenderer.toFormulaString(HSSFEvaluationWorkbook.create(book), ptgs); } } diff --git a/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java b/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java index 6b071c216d..037ebb83ae 100644 --- a/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java @@ -17,7 +17,6 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.record.formula.function.FunctionMetadata; import org.apache.poi.hssf.record.formula.function.FunctionMetadataRegistry; @@ -71,7 +70,7 @@ public abstract class AbstractFunctionPtg extends OperationPtg { return field_2_fnc_index == FUNCTION_INDEX_EXTERNAL; } - public String toFormulaString(HSSFWorkbook book) { + public String toFormulaString() { return getName(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/Area2DPtgBase.java b/src/java/org/apache/poi/hssf/record/formula/Area2DPtgBase.java index 2193854f47..f16a200779 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Area2DPtgBase.java +++ b/src/java/org/apache/poi/hssf/record/formula/Area2DPtgBase.java @@ -18,7 +18,6 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.hssf.record.RecordInputStream; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.util.LittleEndian; /** @@ -45,7 +44,7 @@ public abstract class Area2DPtgBase extends AreaPtgBase { public final int getSize() { return SIZE; } - public final String toFormulaString(HSSFWorkbook book) { + public final String toFormulaString() { return formatReferenceAsString(); } public final String toString() { diff --git a/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java b/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java index 047b18f5ba..46ff3382fe 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/Area3DPtg.java @@ -19,6 +19,8 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.formula.WorkbookDependentFormula; +import org.apache.poi.ss.formula.FormulaRenderingWorkbook; import org.apache.poi.util.LittleEndian; /** @@ -30,7 +32,7 @@ import org.apache.poi.util.LittleEndian; * @author Jason Height (jheight at chariot dot net dot au) * @version 1.0-pre */ -public final class Area3DPtg extends AreaPtgBase { +public final class Area3DPtg extends AreaPtgBase implements WorkbookDependentFormula { public final static byte sid = 0x3b; private final static int SIZE = 11; // 10 + 1 for Ptg @@ -87,7 +89,10 @@ public final class Area3DPtg extends AreaPtgBase { * @return text representation of this area reference that can be used in text * formulas. The sheet name will get properly delimited if required. */ - public String toFormulaString(HSSFWorkbook book) { + public String toFormulaString(FormulaRenderingWorkbook book) { return ExternSheetNameResolver.prependSheetName(book, field_1_index_extern_sheet, formatReferenceAsString()); } + public String toFormulaString() { + throw new RuntimeException("3D references need a workbook to determine formula text"); + } } diff --git a/src/java/org/apache/poi/hssf/record/formula/AreaErrPtg.java b/src/java/org/apache/poi/hssf/record/formula/AreaErrPtg.java index 743c4bfe67..bfe247897d 100644 --- a/src/java/org/apache/poi/hssf/record/formula/AreaErrPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/AreaErrPtg.java @@ -19,7 +19,6 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.usermodel.HSSFErrorConstants; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.util.LittleEndian; /** @@ -49,7 +48,7 @@ public final class AreaErrPtg extends OperandPtg { LittleEndian.putInt(array, offset + 5, unused2); } - public String toFormulaString(HSSFWorkbook book) { + public String toFormulaString() { return HSSFErrorConstants.getText(HSSFErrorConstants.ERROR_REF); } diff --git a/src/java/org/apache/poi/hssf/record/formula/AreaPtgBase.java b/src/java/org/apache/poi/hssf/record/formula/AreaPtgBase.java index f35f7f67fc..74c509b425 100644 --- a/src/java/org/apache/poi/hssf/record/formula/AreaPtgBase.java +++ b/src/java/org/apache/poi/hssf/record/formula/AreaPtgBase.java @@ -17,14 +17,12 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.util.LittleEndian; -import org.apache.poi.util.BitField; -import org.apache.poi.util.BitFieldFactory; - +import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.util.AreaReference; import org.apache.poi.hssf.util.CellReference; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.hssf.record.RecordInputStream; +import org.apache.poi.util.BitField; +import org.apache.poi.util.BitFieldFactory; +import org.apache.poi.util.LittleEndian; /** * Specifies a rectangular area of cells A1:A4 for instance. @@ -269,7 +267,7 @@ public abstract class AreaPtgBase extends OperandPtg implements AreaI { return topLeft.formatAsString() + ":" + botRight.formatAsString(); } - public String toFormulaString(HSSFWorkbook book) { + public String toFormulaString() { return formatReferenceAsString(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java b/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java index c8d10b15fe..3ee23b63f9 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java @@ -184,7 +184,7 @@ public final class ArrayPtg extends Ptg { + ConstantValueParser.getEncodedSize(token_3_arrayValues); } - public String formatAsString() { + public String formatAsString() { // TODO - fold into toFormulaString StringBuffer b = new StringBuffer(); b.append("{"); for (int y=0;y= 0; j--) { // reverse iteration because args were pushed in-order + if(stack.isEmpty()) { + String msg = "Too few arguments supplied to operation. Expected (" + nOperands + + ") operands but got (" + (nOperands - j - 1) + ")"; + throw new IllegalStateException(msg); + } + operands[j] = (String) stack.pop(); + } + return operands; + } +} diff --git a/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java b/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java new file mode 100644 index 0000000000..3a92aa8cbc --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java @@ -0,0 +1,11 @@ +package org.apache.poi.ss.formula; + +import org.apache.poi.hssf.record.formula.NamePtg; +import org.apache.poi.hssf.record.formula.NameXPtg; + +public interface FormulaRenderingWorkbook { + + String getSheetNameByExternSheet(int externSheetIndex); + String resolveNameXText(NameXPtg nameXPtg); + String getNameText(NamePtg namePtg); +} diff --git a/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java b/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java new file mode 100644 index 0000000000..420527dd81 --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java @@ -0,0 +1,5 @@ +package org.apache.poi.ss.formula; + +public interface WorkbookDependentFormula { + String toFormulaString(FormulaRenderingWorkbook book); +} diff --git a/src/java/org/apache/poi/ss/formula/package.html b/src/java/org/apache/poi/ss/formula/package.html new file mode 100644 index 0000000000..1af2f84ac7 --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/package.html @@ -0,0 +1,29 @@ + + + + + + + +This package contains common internal POI code for manipulating formulas. +Client applications should not refer to these classes directly. + + + diff --git a/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java b/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java index e88d274eac..a17414af74 100644 --- a/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java +++ b/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java @@ -32,6 +32,7 @@ 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.HSSFEvaluationWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** @@ -110,7 +111,8 @@ public final class TestEventWorkbookBuilder extends TestCase { assertTrue(ptgs[0] instanceof Ref3DPtg); Ref3DPtg ptg = (Ref3DPtg)ptgs[0]; - assertEquals("Sheet1!A1", ptg.toFormulaString(stubHSSF)); + HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(stubHSSF); + assertEquals("Sheet1!A1", ptg.toFormulaString(book)); // Now check we get the right formula back for diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java index e1ffc2538d..bb9f3de605 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java +++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java @@ -51,6 +51,7 @@ import org.apache.poi.hssf.record.formula.UnaryMinusPtg; import org.apache.poi.hssf.record.formula.UnaryPlusPtg; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFErrorConstants; +import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook; import org.apache.poi.hssf.usermodel.HSSFName; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; @@ -130,13 +131,14 @@ public final class TestFormulaParser extends TestCase { public void testMacroFunction() { // testNames.xls contains a VB function called 'myFunc' HSSFWorkbook w = HSSFTestDataSamples.openSampleWorkbook("testNames.xls"); + HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(w); Ptg[] ptg = FormulaParser.parse("myFunc()", w); // myFunc() actually takes 1 parameter. Don't know if POI will ever be able to detect this problem // the name gets encoded as the first arg NamePtg tname = (NamePtg) ptg[0]; - assertEquals("myFunc", tname.toFormulaString(w)); + assertEquals("myFunc", tname.toFormulaString(book)); AbstractFunctionPtg tfunc = (AbstractFunctionPtg) ptg[1]; assertTrue(tfunc.isExternalFunction()); @@ -871,7 +873,7 @@ public final class TestFormulaParser extends TestCase { assertEquals(2, ptgs.length); Ptg ptg0 = ptgs[0]; assertEquals(ArrayPtg.class, ptg0.getClass()); - assertEquals("{1.0,2.0,2.0,#REF!;FALSE,3.0,3.0,2.0}", ptg0.toFormulaString(null)); + assertEquals("{1.0,2.0,2.0,#REF!;FALSE,3.0,3.0,2.0}", ptg0.toFormulaString()); ArrayPtg aptg = (ArrayPtg) ptg0; Object[][] values = aptg.getTokenArrayValues(); diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java index 6f7c4747cf..415cca33f1 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java +++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParserIf.java @@ -33,7 +33,6 @@ import org.apache.poi.hssf.record.formula.NotEqualPtg; import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.RefPtg; import org.apache.poi.hssf.record.formula.StringPtg; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * Tests FormulaParser specifically with respect to IF() functions @@ -202,7 +201,7 @@ public final class TestFormulaParserIf extends TestCase { assertEquals(true, flag.getValue()); assertEquals("Y", y.getValue()); assertEquals("N", n.getValue()); - assertEquals("IF", funif.toFormulaString((HSSFWorkbook) null)); + assertEquals("IF", funif.toFormulaString()); assertTrue("Goto ptg exists", goto1.isGoto()); } /** diff --git a/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java b/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java index 3742342360..7a13cfe5fd 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java @@ -66,7 +66,7 @@ public final class TestSharedFormulaRecord extends TestCase { Ptg[] convertedFormula = SharedFormulaRecord.convertSharedFormulas(sharedFormula, 100, 200); RefPtg refPtg = (RefPtg) convertedFormula[1]; - assertEquals("$C101", refPtg.toFormulaString(null)); + assertEquals("$C101", refPtg.toFormulaString()); if (refPtg.getPtgClass() == Ptg.CLASS_REF) { throw new AssertionFailedError("Identified bug 45123"); } diff --git a/src/testcases/org/apache/poi/hssf/record/formula/TestArea3DPtg.java b/src/testcases/org/apache/poi/hssf/record/formula/TestArea3DPtg.java index e83a59b42d..b4aa683833 100644 --- a/src/testcases/org/apache/poi/hssf/record/formula/TestArea3DPtg.java +++ b/src/testcases/org/apache/poi/hssf/record/formula/TestArea3DPtg.java @@ -14,10 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - package org.apache.poi.hssf.record.formula; +import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** @@ -27,21 +27,22 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; */ public final class TestArea3DPtg extends AbstractPtgTestCase { - /** - * confirms that sheet names get properly escaped - */ + /** + * confirms that sheet names get properly escaped + */ public void testToFormulaString() { - + Area3DPtg target = new Area3DPtg("A1:B1", (short)0); - + String sheetName = "my sheet"; - HSSFWorkbook book = createWorkbookWithSheet(sheetName); + HSSFWorkbook wb = createWorkbookWithSheet(sheetName); + HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(wb); assertEquals("'my sheet'!A1:B1", target.toFormulaString(book)); - - book.setSheetName(0, "Sheet1"); - assertEquals("Sheet1!A1:B1", target.toFormulaString(book)); - - book.setSheetName(0, "C64"); - assertEquals("'C64'!A1:B1", target.toFormulaString(book)); + + wb.setSheetName(0, "Sheet1"); + assertEquals("Sheet1!A1:B1", target.toFormulaString(book)); + + wb.setSheetName(0, "C64"); + assertEquals("'C64'!A1:B1", target.toFormulaString(book)); } } diff --git a/src/testcases/org/apache/poi/hssf/record/formula/TestAreaPtg.java b/src/testcases/org/apache/poi/hssf/record/formula/TestAreaPtg.java index 3a7f2f29a4..f5b80f6dac 100644 --- a/src/testcases/org/apache/poi/hssf/record/formula/TestAreaPtg.java +++ b/src/testcases/org/apache/poi/hssf/record/formula/TestAreaPtg.java @@ -21,6 +21,7 @@ package org.apache.poi.hssf.record.formula; import junit.framework.TestCase; import org.apache.poi.hssf.model.FormulaParser; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * Tests for {@link AreaPtg}. @@ -83,14 +84,10 @@ public final class TestAreaPtg extends TestCase { assertEquals("Relative references changed", expectedFormula2, newFormula2); } - private String shiftAllColumnsBy1(String formula) - { + private static String shiftAllColumnsBy1(String formula) { int letUsShiftColumn1By1Column=1; - - FormulaParser parser = new FormulaParser(formula,null); - parser.parse(); - - final Ptg[] ptgs = parser.getRPNPtg(); + HSSFWorkbook wb = null; + Ptg[] ptgs = FormulaParser.parse(formula, wb); for(int i=0; i 0); // make sure the counter is actually working if (evalCount > 10) { // Without caching, evaluating cell 'A9' takes 21845 evaluations which consumes // much time (~3 sec on Core 2 Duo 2.2GHz) diff --git a/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java b/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java index 0b6d73462b..5a796aa626 100644 --- a/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java +++ b/src/testcases/org/apache/poi/hssf/util/TestAreaReference.java @@ -30,6 +30,7 @@ import org.apache.poi.hssf.record.formula.MemFuncPtg; import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.UnionPtg; import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook; import org.apache.poi.hssf.usermodel.HSSFName; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; @@ -190,6 +191,7 @@ public final class TestAreaReference extends TestCase { InputStream is = HSSFTestDataSamples.openSampleFileStream("44167.xls"); HSSFWB wb = new HSSFWB(is); Workbook workbook = wb.getWorkbook(); + HSSFEvaluationWorkbook eb = HSSFEvaluationWorkbook.create(wb); assertEquals(1, wb.getNumberOfNames()); String sheetName = "Tabelle1"; @@ -211,10 +213,10 @@ public final class TestAreaReference extends TestCase { Area3DPtg ptgB = (Area3DPtg)def[1]; Area3DPtg ptgC = (Area3DPtg)def[2]; UnionPtg ptgD = (UnionPtg)def[3]; - assertEquals("", ptgA.toFormulaString(wb)); - assertEquals(refA, ptgB.toFormulaString(wb)); - assertEquals(refB, ptgC.toFormulaString(wb)); - assertEquals(",", ptgD.toFormulaString(wb)); + assertEquals("", ptgA.toFormulaString()); + assertEquals(refA, ptgB.toFormulaString(eb)); + assertEquals(refB, ptgC.toFormulaString(eb)); + assertEquals(",", ptgD.toFormulaString()); assertEquals(ref, nr.getAreaReference(wb));