From d2fccf5b0c56be0c26f70f6c7c4a3d6b7523ddfd Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Mon, 26 May 2008 18:25:02 +0000 Subject: [PATCH] Small fix for FormulaParser. Need case-insentive match for IF function name git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@660263 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/model/FormulaParser.java | 31 ++++++++++--------- .../poi/hssf/model/TestFormulaParser.java | 14 +++++++-- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/FormulaParser.java b/src/java/org/apache/poi/hssf/model/FormulaParser.java index b6c7117290..94cf9978a6 100644 --- a/src/java/org/apache/poi/hssf/model/FormulaParser.java +++ b/src/java/org/apache/poi/hssf/model/FormulaParser.java @@ -382,7 +382,7 @@ public final class FormulaParser { } else { retval = new FuncPtg(funcIx); } - if (!name.equals(AbstractFunctionPtg.FUNCTION_NAME_IF)) { + if (!name.equalsIgnoreCase(AbstractFunctionPtg.FUNCTION_NAME_IF)) { // early return for everything else besides IF() return retval; } @@ -1014,19 +1014,8 @@ end; } } - final OperationPtg o = (OperationPtg) ptg; - int nOperands = o.getNumberOfOperands(); - final 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 suppled to operation token (" - + o.getClass().getName() + "). Expected (" + nOperands - + ") operands but got (" + (nOperands - j - 1) + ")"; - throw new IllegalStateException(msg); - } - operands[j] = (String) stack.pop(); - } + OperationPtg o = (OperationPtg) ptg; + String[] operands = getOperands(stack, o.getNumberOfOperands()); stack.push(o.toFormulaString(operands)); } if(stack.isEmpty()) { @@ -1042,6 +1031,20 @@ end; } 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 diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java index f2821140f3..29619501e0 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java +++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java @@ -644,8 +644,16 @@ public final class TestFormulaParser extends TestCase { Class[] expClss; - expClss = new Class[] { ReferencePtg.class, MissingArgPtg.class, ReferencePtg.class, - FuncVarPtg.class, }; + expClss = new Class[] { + ReferencePtg.class, + AttrPtg.class, // tAttrIf + MissingArgPtg.class, + AttrPtg.class, // tAttrSkip + ReferencePtg.class, + AttrPtg.class, // tAttrSkip + FuncVarPtg.class, + }; + confirmTokenClasses("if(A1, ,C1)", expClss); expClss = new Class[] { MissingArgPtg.class, AreaPtg.class, MissingArgPtg.class, @@ -814,7 +822,7 @@ public final class TestFormulaParser extends TestCase { fail("Expected exception was not thrown"); } catch (IllegalStateException e) { // expected during successful test - assertTrue(e.getMessage().startsWith("Too few arguments suppled to operation token")); + assertTrue(e.getMessage().startsWith("Too few arguments supplied to operation")); } } /** -- 2.39.5