avik 2005/04/21 06:01:29
Modified: src/java/org/apache/poi/hssf/model FormulaParser.java
src/testcases/org/apache/poi/hssf/model
TestFormulaParser.java
Log:
bug 33160, original patch and testcase supplied by Amol Deshmukh.. thanks.
This version slightly more generic
Revision Changes Path
1.18 +18 -2
jakarta-poi/src/java/org/apache/poi/hssf/model/FormulaParser.java
Index: FormulaParser.java
===================================================================
RCS file:
/home/cvs/jakarta-poi/src/java/org/apache/poi/hssf/model/FormulaParser.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- FormulaParser.java 29 Apr 2004 07:16:36 -0000 1.17
+++ FormulaParser.java 21 Apr 2005 13:01:28 -0000 1.18
@@ -484,12 +484,28 @@
if (IsDigit(look)) number = number +"."+ GetNum(); //this
also takes care of someone entering "1234."
tokens.add(new NumberPtg(number));
} else {
- tokens.add(new IntPtg(number)); //TODO:what if the number
is too big to be a short? ..add factory to return Int or Number!
+ tokens.add(getNumberPtgFromString(number)); //TODO:what if
the number is too big to be a short? ..add factory to return Int or Number!
}
}
}
- private void StringLiteral()
+ /** Get a PTG for an integer from its string representation.
+ * return Int or Number Ptg based on size of input
+ * @param number
+ * @return
+ */
+ private Ptg getNumberPtgFromString(String number) {
+ try {
+ return new IntPtg(number);
+ } catch (NumberFormatException e) {
+ System.out.println("Caught NFE, returning Number Ptg");
+ return new NumberPtg(number);
+ }
+
+ }
+
+
+ private void StringLiteral()
{
// Can't use match here 'cuz it consumes whitespace
// which we need to preserve inside the string.
1.15 +25 -0
jakarta-poi/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
Index: TestFormulaParser.java
===================================================================
RCS file:
/home/cvs/jakarta-poi/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- TestFormulaParser.java 1 Jan 2005 07:06:34 -0000 1.14
+++ TestFormulaParser.java 21 Apr 2005 13:01:29 -0000 1.15
@@ -23,6 +23,7 @@
import org.apache.poi.hssf.record.formula.AddPtg;
import org.apache.poi.hssf.record.formula.AttrPtg;
import org.apache.poi.hssf.record.formula.BoolPtg;
+import org.apache.poi.hssf.record.formula.DividePtg;
import org.apache.poi.hssf.record.formula.EqualPtg;
import org.apache.poi.hssf.record.formula.FuncVarPtg;
import org.apache.poi.hssf.record.formula.IntPtg;
@@ -30,6 +31,7 @@
import org.apache.poi.hssf.record.formula.LessThanPtg;
import org.apache.poi.hssf.record.formula.NamePtg;
import org.apache.poi.hssf.record.formula.NotEqualPtg;
+import org.apache.poi.hssf.record.formula.NumberPtg;
import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.hssf.record.formula.ReferencePtg;
import org.apache.poi.hssf.record.formula.StringPtg;
@@ -352,6 +354,29 @@
assertTrue("got 3 ptg", ptg.length == 3);
assertTrue("ptg0 has Value class", ptg[0].getPtgClass() ==
Ptg.CLASS_VALUE);
}
+
+ /** bug 33160*/
+ public void testLargeInt() {
+ FormulaParser fp = new FormulaParser("40", null);
+ fp.parse();
+ Ptg[] ptg=fp.getRPNPtg();
+ assertTrue("ptg is Int, is "+ptg[0].getClass(),ptg[0]
instanceof IntPtg);
+
+ fp = new FormulaParser("40000", null);
+ fp.parse();
+ ptg=fp.getRPNPtg();
+ assertTrue("ptg should be Number, is "+ptg[0].getClass(),
ptg[0] instanceof NumberPtg);
+ }
+ /** bug 33160, testcase by Amol Deshmukh*/
+ public void testSimpleLongFormula() {
+ FormulaParser fp = new FormulaParser("40000/2", null);
+ fp.parse();
+ Ptg[] ptgs = fp.getRPNPtg();
+ assertTrue("three tokens expected, got
"+ptgs.length,ptgs.length == 3);
+ assertTrue("NumberPtg",(ptgs[0] instanceof NumberPtg));
+ assertTrue("IntPtg",(ptgs[1] instanceof IntPtg));
+ assertTrue("DividePtg",(ptgs[2] instanceof DividePtg));
+ }
public static void main(String [] args) {
System.out.println("Testing
org.apache.poi.hssf.record.formula.FormulaParser");
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List: http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/