You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2008/08/29 17:27:11 UTC
svn commit: r690299 [1/4] - in /poi/branches/ooxml: ./
src/java/org/apache/poi/hssf/dev/ src/java/org/apache/poi/hssf/model/
src/java/org/apache/poi/hssf/record/
src/java/org/apache/poi/hssf/record/aggregates/
src/java/org/apache/poi/hssf/record/formul...
Author: nick
Date: Fri Aug 29 08:27:07 2008
New Revision: 690299
URL: http://svn.apache.org/viewvc?rev=690299&view=rev
Log:
Merged revisions 638786-638802,638805-638811,638813-638814,638816-639230,639233-639241,639243-639253,639255-639486,639488-639601,639603-639835,639837-639917,639919-640056,640058-640710,640712-641156,641158-641184,641186-641795,641797-641798,641800-641933,641935-641963,641965-641966,641968-641995,641997-642230,642232-642562,642564-642565,642568-642570,642572-642573,642576-642736,642739-642877,642879,642881-642890,642892-642903,642905-642945,642947-643624,643626-643653,643655-643669,643671,643673-643830,643832-643833,643835-644342,644344-644472,644474-644508,644510-645347,645349-645351,645353-645559,645561-645565,645568-645951,645953-646193,646195-646311,646313-646404,646406-646665,646667-646853,646855-646869,646871-647151,647153-647185,647187-647277,647279-647566,647568-647573,647575,647578-647711,647714-647737,647739-647823,647825-648155,648157-648202,648204-648273,648275,648277-648302,648304-648333,648335-648588,648590-648622,648625-648673,648675-649141,649144,649146-649556,
649558-649795,649799,649801-649910,649912-649913,649915-650128,650131-650132,650134-650137,650140-650914,650916-651991,651993-652284,652286-652287,652289,652291,652293-652297,652299-652328,652330-652425,652427-652445,652447-652560,652562-652933,652935,652937-652993,652995-653116,653118-653124,653126-653483,653487-653519,653522-653550,653552-653607,653609-653667,653669-653674,653676-653814,653817-653830,653832-653891,653893-653944,653946-654055,654057-654355,654357-654365,654367-654648,654651-655215,655217-655277,655279-655281,655283-655911,655913-656212,656214,656216-656251,656253-656698,656700-656756,656758-656892,656894-657135,657137-657165,657168-657179,657181-657354,657356-657357,657359-657701,657703-657874,657876-658032,658034-658284,658286,658288-658301,658303-658307,658309-658321,658323-658335,658337-658348,658351,658353-658832,658834-658983,658985,658987-659066,659068-659402,659404-659428,659430-659451,659453-659454,659456-659461,659463-659477,659479-659524,659526-65
9571,659574,659576-660255,660257-660262,660264-660279,660281-660343,660345-660473,660475-660827,660829-660833,660835-660888,660890-663321,663323-663435,663437-663764,663766-663854,663856-664219,664221-664489,664494-664514,664516-668013,668015-668142,668144-668152,668154,668156-668256,668258,668260-669139,669141-669455,669457-669657,669659-669808,669810-670189,670191-671321,671323-672229,672231-672549,672551-672552,672554-672561,672563-672566,672568,672571-673049,673051-673852,673854-673862,673864-673986,673988-673996,673998-674347,674349-674890,674892-674910,674912-674936,674938-674952,674954-675078,675080-675085,675087-675217,675219-675660,675662-675670,675672-675716,675718-675726,675728-675733,675735-675775,675777-675782,675784,675786-675791,675794-675852,675854-676200,676202,676204,676206-676220,676222-676309,676311-676456,676458-676994,676996-677027,677030-677040,677042-677056,677058-677375,677377-677968,677970-677971,677973,677975-677994,677996-678286,678288-678538,6785
40-680393,680395-680469,680471-680529,680531-680852,680854-681529,681531-681571,681573-682224,682226,682228,682231-682281,682283-682335,682337-682507,682509,682512-682517,682519-682532,682534-682619,682622-682777,682779-682998,683000-683019,683021-683022,683024-683080,683082-683092,683094-683095,683097-683127,683129-683131,683133-683166,683168-683698,683700-683705,683707-683757,683759-683787,683789-683870,683872-683879,683881-683900,683902-684066,684068-684074,684076-684222,684224-684254,684257-684281,684283-684286,684288-684292,684294-684298,684300-684301,684303-684308,684310-684317,684320,684323-684335,684337-684348,684350-684354,684356-684361,684363-684369,684371-684453,684455-684883,684885-684937,684940-684958,684960-684970,684972-684985,684987-685053,685055-685063,685065-685259,685261-685262,685264-685266,685268-685282,685285-686035,686037-686045,686047-686052,686054-686206,686208-686215,686217-686277,686279-686289,686291-686620,686622-686623,686626-686627,686629-686639
,686641-686843,686845-686976,686978-687402,687404-687422,687424-687428,687430-687442,687444-688425,688427-688641,688643-688649,688651-688654,688656-688824,688826-688909,688911-689543,689545-689558,689560-689635,689637-689703,689705-689715,689717-689718,689720,689722-690280 via svnmerge from
https://svn.apache.org/repos/asf/poi/trunk
........
r689973 | josh | 2008-08-28 21:39:41 +0100 (Thu, 28 Aug 2008) | 1 line
Consolidated TableRecord inside FormulaRecordAggregate. Simplifications to FormulaRecord
........
r690091 | josh | 2008-08-29 04:25:23 +0100 (Fri, 29 Aug 2008) | 1 line
Changed FormulaRecord.getParsedExpression to return Ptg array
........
r690094 | josh | 2008-08-29 04:52:51 +0100 (Fri, 29 Aug 2008) | 1 line
Removing calls to AreaEval.getValues()
........
r690112 | josh | 2008-08-29 06:29:56 +0100 (Fri, 29 Aug 2008) | 1 line
Removing calls to AreaEval.getValues() from count and lookup functions
........
r690259 | yegor | 2008-08-29 14:58:56 +0100 (Fri, 29 Aug 2008) | 1 line
utility to dump POIFS into filesystem
........
r690262 | yegor | 2008-08-29 15:01:04 +0100 (Fri, 29 Aug 2008) | 1 line
initial support for embedded movies and controls.
........
Added:
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/CountUtils.java
- copied unchanged from r690262, poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CountUtils.java
poi/branches/ooxml/src/java/org/apache/poi/poifs/dev/POIFSDump.java
- copied unchanged from r690262, poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSDump.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/model/ActiveXShape.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/ActiveXShape.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/model/MovieShape.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/MovieShape.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfo.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfo.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfoAtom.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfoAtom.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/ExAviMovie.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/ExAviMovie.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/ExMCIMovie.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/ExMCIMovie.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/ExMediaAtom.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/ExMediaAtom.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/ExVideoContainer.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/ExVideoContainer.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/OEShapeAtom.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/OEShapeAtom.java
poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hslf/model/TestMovieShape.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestMovieShape.java
poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hslf/model/TestTable.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/model/TestTable.java
poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hslf/record/TestAnimationInfoAtom.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/record/TestAnimationInfoAtom.java
poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExControl.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExControl.java
poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExMediaAtom.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExMediaAtom.java
poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExOleObjAtom.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExOleObjAtom.java
poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExOleObjStg.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExOleObjStg.java
poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExVideoContainer.java
- copied unchanged from r690262, poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/record/TestExVideoContainer.java
Modified:
poi/branches/ooxml/ (props changed)
poi/branches/ooxml/src/java/org/apache/poi/hssf/dev/FormulaViewer.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/model/RecordStream.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/FormulaRecord.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/Ptg.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEval.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/BlankEval.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/And.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/BooleanFunction.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Count.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Counta.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Countif.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Hlookup.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Lookup.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Match.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Not.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Or.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Sumproduct.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Vlookup.java
poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
poi/branches/ooxml/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
poi/branches/ooxml/src/java/org/apache/poi/util/HexDump.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/blip/WMF.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/model/ShapeFactory.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/model/SimpleShape.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/model/Table.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/model/TextPainter.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/model/TextShape.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/ExControl.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/ExControlAtom.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/ExEmbed.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjAtom.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjStg.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/InteractiveInfoAtom.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/RecordTypes.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/record/TextRulerAtom.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java
poi/branches/ooxml/src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java
poi/branches/ooxml/src/scratchpad/testcases/org/apache/poi/hslf/record/TestTextRulerAtom.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/formula/functions/EvalFactory.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/formula/functions/TestCountFuncs.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/formula/functions/TestRowCol.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/record/formula/functions/TestSumproduct.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/FormulaExtractor.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/TestBug42464.java
poi/branches/ooxml/src/testcases/org/apache/poi/hssf/usermodel/TestFormulaEvaluatorBugs.java
poi/branches/ooxml/src/testcases/org/apache/poi/util/TestHexDump.java
Propchange: poi/branches/ooxml/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Fri Aug 29 08:27:07 2008
@@ -1 +1 @@
-/poi/trunk:1-638784,638786-639486,639488-639601,639603-640056,640058-642562,642564-642566,642568-642574,642576-642736,642739-650914,650916-689779
+/poi/trunk:1-638784,638786-639486,639488-639601,639603-640056,640058-642562,642564-642566,642568-642574,642576-642736,642739-650914,650916-690280
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/dev/FormulaViewer.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/dev/FormulaViewer.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/dev/FormulaViewer.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/dev/FormulaViewer.java Fri Aug 29 08:27:07 2008
@@ -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 @@
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 @@
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 @@
buf.append(sep);
buf.append(numArg);
System.out.println(buf.toString());
- } else {
- System.out.println("#NAME");
- }
}
/**
@@ -155,20 +149,18 @@
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;i<numptgs;i++) {
- token = (Ptg) tokens.get(i);
+ Ptg[] tokens = record.getParsedExpression();
+ for (int i = 0; i < tokens.length; i++) {
+ Ptg token = tokens[i];
buf.append( token.toFormulaString((HSSFWorkbook)null));
switch (token.getPtgClass()) {
case Ptg.CLASS_REF :
@@ -187,9 +179,9 @@
}
- private String composeFormula(FormulaRecord record)
+ private static String composeFormula(FormulaRecord record)
{
- return org.apache.poi.hssf.model.FormulaParser.toFormulaString((HSSFWorkbook)null,record.getParsedExpression());
+ return FormulaParser.toFormulaString((HSSFWorkbook)null, record.getParsedExpression());
}
/**
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/model/RecordStream.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/model/RecordStream.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/model/RecordStream.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/model/RecordStream.java Fri Aug 29 08:27:07 2008
@@ -30,19 +30,28 @@
private final List _list;
private int _nextIndex;
private int _countRead;
+ private final int _endIx;
- public RecordStream(List inputList, int startIndex) {
+ /**
+ * Creates a RecordStream bounded by startIndex and endIndex
+ */
+ public RecordStream(List inputList, int startIndex, int endIx) {
_list = inputList;
_nextIndex = startIndex;
+ _endIx = endIx;
_countRead = 0;
}
+ public RecordStream(List records, int startIx) {
+ this(records, startIx, records.size());
+ }
+
public boolean hasNext() {
- return _nextIndex < _list.size();
+ return _nextIndex < _endIx;
}
public Record getNext() {
- if(_nextIndex >= _list.size()) {
+ if(!hasNext()) {
throw new RuntimeException("Attempt to read past end of record stream");
}
_countRead ++;
@@ -53,14 +62,17 @@
* @return the {@link Class} of the next Record. <code>null</code> if this stream is exhausted.
*/
public Class peekNextClass() {
- if(_nextIndex >= _list.size()) {
+ if(!hasNext()) {
return null;
}
return _list.get(_nextIndex).getClass();
}
+ /**
+ * @return -1 if at end of records
+ */
public int peekNextSid() {
- if(_nextIndex >= _list.size()) {
+ if(!hasNext()) {
return -1;
}
return ((Record)_list.get(_nextIndex)).getSid();
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/FormulaRecord.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/FormulaRecord.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/FormulaRecord.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/FormulaRecord.java Fri Aug 29 08:27:07 2008
@@ -17,12 +17,13 @@
package org.apache.poi.hssf.record;
+import java.util.Arrays;
import java.util.List;
-import java.util.Stack;
import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory;
+import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
/**
@@ -33,69 +34,60 @@
* @version 2.0-pre
*/
public final class FormulaRecord extends Record implements CellValueRecordInterface {
-
+
public static final short sid = 0x0006; // docs say 406...because of a bug Microsoft support site article #Q184647)
+ private static int FIXED_SIZE = 22;
private static final BitField alwaysCalc = BitFieldFactory.getInstance(0x0001);
private static final BitField calcOnLoad = BitFieldFactory.getInstance(0x0002);
- private static final BitField sharedFormula = BitFieldFactory.getInstance(0x0008);
+ private static final BitField sharedFormula = BitFieldFactory.getInstance(0x0008);
- private int field_1_row;
+ private int field_1_row;
private short field_2_column;
private short field_3_xf;
private double field_4_value;
private short field_5_options;
private int field_6_zero;
- private short field_7_expression_len;
- private Stack field_8_parsed_expr;
-
+ private Ptg[] field_8_parsed_expr;
+
/**
* Since the NaN support seems sketchy (different constants) we'll store and spit it out directly
*/
- private byte[] value_data;
- private byte[] all_data; //if formula support is not enabled then
- //we'll just store/reserialize
+ private byte[] value_data;
/** Creates new FormulaRecord */
- public FormulaRecord()
- {
- field_8_parsed_expr = new Stack();
+ public FormulaRecord() {
+ field_8_parsed_expr = Ptg.EMPTY_PTG_ARRAY;
}
/**
* Constructs a Formula record and sets its fields appropriately.
- * Note - id must be 0x06 (NOT 0x406 see MSKB #Q184647 for an
+ * Note - id must be 0x06 (NOT 0x406 see MSKB #Q184647 for an
* "explanation of this bug in the documentation) or an exception
* will be throw upon validation
*
* @param in the RecordInputstream to read the record from
*/
- public FormulaRecord(RecordInputStream in)
- {
+ public FormulaRecord(RecordInputStream in) {
super(in);
}
- protected void fillFields(RecordInputStream in)
- {
- try {
+ protected void fillFields(RecordInputStream in) {
field_1_row = in.readUShort();
field_2_column = in.readShort();
field_3_xf = in.readShort();
field_4_value = in.readDouble();
field_5_options = in.readShort();
-
+
if (Double.isNaN(field_4_value)) {
value_data = in.getNANData();
}
-
+
field_6_zero = in.readInt();
- field_7_expression_len = in.readShort();
- field_8_parsed_expr = Ptg.createParsedExpressionTokens(field_7_expression_len, in);
- } catch (java.lang.UnsupportedOperationException uoe) {
- throw new RecordFormatException(uoe);
- }
+ int field_7_expression_len = in.readShort(); // this length does not include any extra array data
+ field_8_parsed_expr = Ptg.readTokens(field_7_expression_len, in);
if (in.remaining() == 10) {
// TODO - this seems to occur when IntersectionPtg is present
// 10 extra bytes are just 0x01 and 0x00
@@ -103,19 +95,15 @@
}
}
- //public void setRow(short row)
- public void setRow(int row)
- {
+ public void setRow(int row) {
field_1_row = row;
}
- public void setColumn(short column)
- {
+ public void setColumn(short column) {
field_2_column = column;
}
- public void setXFIndex(short xf)
- {
+ public void setXFIndex(short xf) {
field_3_xf = xf;
}
@@ -124,9 +112,7 @@
*
* @param value calculated value
*/
-
- public void setValue(double value)
- {
+ public void setValue(double value) {
field_4_value = value;
}
@@ -135,35 +121,19 @@
*
* @param options bitmask
*/
-
- public void setOptions(short options)
- {
+ public void setOptions(short options) {
field_5_options = options;
}
- /**
- * set the length (in number of tokens) of the expression
- * @param len length
- */
-
- public void setExpressionLength(short len)
- {
- field_7_expression_len = len;
- }
-
- //public short getRow()
- public int getRow()
- {
+ public int getRow() {
return field_1_row;
}
- public short getColumn()
- {
+ public short getColumn() {
return field_2_column;
}
- public short getXFIndex()
- {
+ public short getXFIndex() {
return field_3_xf;
}
@@ -172,8 +142,7 @@
*
* @return calculated value
*/
- public double getValue()
- {
+ public double getValue() {
return field_4_value;
}
@@ -182,108 +151,43 @@
*
* @return bitmask
*/
- public short getOptions()
- {
+ public short getOptions() {
return field_5_options;
- }
-
+ }
+
public boolean isSharedFormula() {
return sharedFormula.isSet(field_5_options);
}
public void setSharedFormula(boolean flag) {
- field_5_options =
- sharedFormula.setShortBoolean(field_5_options, flag);
+ field_5_options =
+ sharedFormula.setShortBoolean(field_5_options, flag);
}
-
+
public boolean isAlwaysCalc() {
- return alwaysCalc.isSet(field_5_options);
+ return alwaysCalc.isSet(field_5_options);
}
public void setAlwaysCalc(boolean flag) {
- field_5_options =
- alwaysCalc.setShortBoolean(field_5_options, flag);
+ field_5_options =
+ alwaysCalc.setShortBoolean(field_5_options, flag);
}
-
+
public boolean isCalcOnLoad() {
- return calcOnLoad.isSet(field_5_options);
+ return calcOnLoad.isSet(field_5_options);
}
public void setCalcOnLoad(boolean flag) {
- field_5_options =
- calcOnLoad.setShortBoolean(field_5_options, flag);
- }
-
- /**
- * get the length (in number of tokens) of the expression
- * @return expression length
- */
-
- public short getExpressionLength()
- {
- return field_7_expression_len;
- }
-
- /**
- * push a token onto the stack
- *
- * @param ptg the token
- */
-
- public void pushExpressionToken(Ptg ptg)
- {
- field_8_parsed_expr.push(ptg);
- }
-
- /**
- * pop a token off of the stack
- *
- * @return Ptg - the token
- */
-
- public Ptg popExpressionToken()
- {
- return ( Ptg ) field_8_parsed_expr.pop();
- }
-
- /**
- * peek at the token on the top of stack
- *
- * @return Ptg - the token
- */
-
- public Ptg peekExpressionToken()
- {
- return ( Ptg ) field_8_parsed_expr.peek();
+ field_5_options =
+ calcOnLoad.setShortBoolean(field_5_options, flag);
}
/**
- * get the size of the stack
- * @return size of the stack
+ * @return the formula tokens. never <code>null</code>
*/
-
- public int getNumberOfExpressionTokens()
- {
- if (this.field_8_parsed_expr == null) {
- return 0;
- } else {
- return field_8_parsed_expr.size();
- }
+ public Ptg[] getParsedExpression() {
+ return (Ptg[]) field_8_parsed_expr.clone();
}
- /**
- * get the stack as a list
- *
- * @return list of tokens (casts stack to a list and returns it!)
- * this method can return null is we are unable to create Ptgs from
- * existing excel file
- * callers should check for null!
- */
-
- public List getParsedExpression()
- {
- return field_8_parsed_expr;
- }
-
- public void setParsedExpression(Stack ptgs) {
- field_8_parsed_expr = ptgs;
+ public void setParsedExpression(Ptg[] ptgs) {
+ field_8_parsed_expr = ptgs;
}
/**
@@ -292,156 +196,86 @@
*
* @param id alleged id for this record
*/
-
- protected void validateSid(short id)
- {
- if (id != sid)
- {
+ protected void validateSid(short id) {
+ if (id != sid) {
throw new RecordFormatException("NOT A FORMULA RECORD");
}
}
- public short getSid()
- {
+ public short getSid() {
return sid;
}
- /**
- * called by the class that is responsible for writing this sucker.
- * Subclasses should implement this so that their data is passed back in a
- * byte array.
- *
- * @return byte array containing instance data
- */
+ private int getDataSize() {
+ return FIXED_SIZE + Ptg.getEncodedSize(field_8_parsed_expr);
+ }
+ public int serialize(int offset, byte [] data) {
- public int serialize(int offset, byte [] data)
- {
- if (this.field_8_parsed_expr != null) {
- int ptgSize = getTotalPtgSize();
+ int dataSize = getDataSize();
LittleEndian.putShort(data, 0 + offset, sid);
- LittleEndian.putShort(data, 2 + offset, ( short ) (22 + ptgSize));
- //LittleEndian.putShort(data, 4 + offset, getRow());
- LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
+ LittleEndian.putUShort(data, 2 + offset, dataSize);
+ LittleEndian.putUShort(data, 4 + offset, getRow());
LittleEndian.putShort(data, 6 + offset, getColumn());
LittleEndian.putShort(data, 8 + offset, getXFIndex());
-
+
//only reserialize if the value is still NaN and we have old nan data
- if (Double.isNaN(this.getValue()) && value_data != null) {
- System.arraycopy(value_data,0,data,10 + offset,value_data.length);
+ if (Double.isNaN(getValue()) && value_data != null) {
+ System.arraycopy(value_data,0,data,10 + offset,value_data.length);
} else {
- LittleEndian.putDouble(data, 10 + offset, field_4_value);
+ LittleEndian.putDouble(data, 10 + offset, field_4_value);
}
-
+
LittleEndian.putShort(data, 18 + offset, getOptions());
-
+
//when writing the chn field (offset 20), it's supposed to be 0 but ignored on read
//Microsoft Excel Developer's Kit Page 318
LittleEndian.putInt(data, 20 + offset, 0);
- LittleEndian.putShort(data, 24 + offset, getExpressionLength());
- Ptg.serializePtgStack(field_8_parsed_expr, data, 26+offset);
- } else {
- System.arraycopy(all_data,0,data,offset,all_data.length);
- }
- return getRecordSize();
+ int formulaTokensSize = Ptg.getEncodedSizeWithoutArrayData(field_8_parsed_expr);
+ LittleEndian.putUShort(data, 24 + offset, formulaTokensSize);
+ Ptg.serializePtgs(field_8_parsed_expr, data, 26+offset);
+ return 4 + dataSize;
}
-
-
-
-
- public int getRecordSize()
- {
- int retval =0;
-
- if (this.field_8_parsed_expr != null) {
- retval = getTotalPtgSize() + 26;
- } else {
- retval =all_data.length;
- }
- return retval;
- // return getTotalPtgSize() + 28;
- }
-
- private int getTotalPtgSize()
- {
- List list = getParsedExpression();
- int retval = 0;
-
- for (int k = 0; k < list.size(); k++)
- {
- Ptg ptg = ( Ptg ) list.get(k);
-
- retval += ptg.getSize();
- }
- return retval;
+ public int getRecordSize() {
+ return 4 + getDataSize();
}
- public boolean isInValueSection()
- {
+ public boolean isInValueSection() {
return true;
}
- public boolean isValue()
- {
+ public boolean isValue() {
return true;
}
-
- public String toString()
- {
- StringBuffer buffer = new StringBuffer();
- buffer.append("[FORMULA]\n");
- buffer.append(" .row = ")
- .append(Integer.toHexString(getRow())).append("\n");
- buffer.append(" .column = ")
- .append(Integer.toHexString(getColumn()))
- .append("\n");
- buffer.append(" .xf = ")
- .append(Integer.toHexString(getXFIndex())).append("\n");
- if (Double.isNaN(this.getValue()) && value_data != null)
- buffer.append(" .value (NaN) = ")
- .append(org.apache.poi.util.HexDump.dump(value_data,0,0))
- .append("\n");
- else
- buffer.append(" .value = ").append(getValue())
- .append("\n");
- buffer.append(" .options = ").append(getOptions())
- .append("\n");
- buffer.append(" .alwaysCalc = ").append(alwaysCalc.isSet(getOptions()))
- .append("\n");
- buffer.append(" .calcOnLoad = ").append(calcOnLoad.isSet(getOptions()))
- .append("\n");
- buffer.append(" .sharedFormula = ").append(sharedFormula.isSet(getOptions()))
- .append("\n");
- buffer.append(" .zero = ").append(field_6_zero)
- .append("\n");
- buffer.append(" .expressionlength= ").append(getExpressionLength())
- .append("\n");
-
- if (field_8_parsed_expr != null) {
- buffer.append(" .numptgsinarray = ").append(field_8_parsed_expr.size())
- .append("\n");
-
-
- for (int k = 0; k < field_8_parsed_expr.size(); k++ ) {
- buffer.append(" Ptg(")
- .append(k)
- .append(")=")
- .append(field_8_parsed_expr.get(k).toString())
- .append("\n")
- .append(((Ptg)field_8_parsed_expr.get(k)).toDebugString())
- .append("\n");
- }
- }else {
- buffer.append("Formula full data \n")
- .append(org.apache.poi.util.HexDump.dump(this.all_data,0,0));
- }
-
-
- buffer.append("[/FORMULA]\n");
- return buffer.toString();
+
+ public String toString() {
+
+ StringBuffer sb = new StringBuffer();
+ sb.append("[FORMULA]\n");
+ sb.append(" .row = ").append(HexDump.shortToHex(getRow())).append("\n");
+ sb.append(" .column = ").append(HexDump.shortToHex(getColumn())).append("\n");
+ sb.append(" .xf = ").append(HexDump.shortToHex(getXFIndex())).append("\n");
+ sb.append(" .value = ");
+ if (Double.isNaN(this.getValue()) && value_data != null) {
+ sb.append("(NaN)").append(HexDump.dump(value_data,0,0)).append("\n");
+ } else {
+ sb.append(getValue()).append("\n");
+ }
+ sb.append(" .options = ").append(HexDump.shortToHex(getOptions())).append("\n");
+ sb.append(" .alwaysCalc= ").append(alwaysCalc.isSet(getOptions())).append("\n");
+ sb.append(" .calcOnLoad= ").append(calcOnLoad.isSet(getOptions())).append("\n");
+ sb.append(" .shared = ").append(sharedFormula.isSet(getOptions())).append("\n");
+ sb.append(" .zero = ").append(HexDump.intToHex(field_6_zero)).append("\n");
+
+ for (int k = 0; k < field_8_parsed_expr.length; k++ ) {
+ sb.append(" Ptg[").append(k).append("]=");
+ sb.append(field_8_parsed_expr[k].toString()).append("\n");
+ }
+ sb.append("[/FORMULA]\n");
+ return sb.toString();
}
-
+
public Object clone() {
FormulaRecord rec = new FormulaRecord();
rec.field_1_row = field_1_row;
@@ -450,18 +284,14 @@
rec.field_4_value = field_4_value;
rec.field_5_options = field_5_options;
rec.field_6_zero = field_6_zero;
- rec.field_7_expression_len = field_7_expression_len;
- rec.field_8_parsed_expr = new Stack();
- int size = 0;
- if (field_8_parsed_expr != null)
- size = field_8_parsed_expr.size();
- for (int i=0; i< size; i++) {
- Ptg ptg = ((Ptg)field_8_parsed_expr.get(i)).copy();
- rec.field_8_parsed_expr.add(i, ptg);
+ int nTokens = field_8_parsed_expr.length;
+ Ptg[] ptgs = new Ptg[nTokens];
+ for (int i=0; i< nTokens; i++) {
+ ptgs[i] = field_8_parsed_expr[i].copy();
}
+ rec.field_8_parsed_expr = ptgs;
rec.value_data = value_data;
- rec.all_data = all_data;
return rec;
}
-
}
+
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java Fri Aug 29 08:27:07 2008
@@ -14,17 +14,22 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
+
package org.apache.poi.hssf.record;
+import java.util.List;
import java.util.Stack;
-import org.apache.poi.hssf.record.formula.*;
+import org.apache.poi.hssf.record.formula.AreaNPtg;
+import org.apache.poi.hssf.record.formula.AreaPtg;
+import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.hssf.record.formula.RefNPtg;
+import org.apache.poi.hssf.record.formula.RefPtg;
/**
* Title: SharedFormulaRecord
* Description: Primarily used as an excel optimization so that multiple similar formulas
- * are not written out too many times. We should recognize this record and
+ * are not written out too many times. We should recognize this record and
* serialize as is since this is used when reading templates.
* <p>
* Note: the documentation says that the SID is BC where biffviewer reports 4BC. The hex dump shows
@@ -33,15 +38,15 @@
* @author Danny Mui at apache dot org
*/
public final class SharedFormulaRecord extends Record {
- public final static short sid = 0x4BC;
-
+ public final static short sid = 0x04BC;
+
private int field_1_first_row;
private int field_2_last_row;
private short field_3_first_column;
private short field_4_last_column;
private int field_5_reserved;
private short field_6_expression_len;
- private Stack field_7_parsed_expr;
+ private Stack field_7_parsed_expr;
public SharedFormulaRecord()
{
@@ -55,15 +60,15 @@
{
super(in);
}
-
+
protected void validateSid(short id)
{
if (id != this.sid)
{
throw new RecordFormatException("Not a valid SharedFormula");
- }
- }
-
+ }
+ }
+
public int getFirstRow() {
return field_1_first_row;
}
@@ -139,7 +144,7 @@
.append(field_7_parsed_expr.get(k).toString())
.append("\n");
}
-
+
buffer.append("[/SHARED FORMULA RECORD]\n");
return buffer.toString();
}
@@ -163,7 +168,7 @@
private Stack getParsedExpressionTokens(RecordInputStream in)
{
Stack stack = new Stack();
-
+
while (in.remaining() != 0) {
Ptg ptg = Ptg.createPtg(in);
stack.push(ptg);
@@ -180,15 +185,15 @@
return ((getFirstRow() <= formulaRow) && (getLastRow() >= formulaRow) &&
(getFirstColumn() <= formulaColumn) && (getLastColumn() >= formulaColumn));
}
-
+
/**
- * Creates a non shared formula from the shared formula
+ * Creates a non shared formula from the shared formula
* counter part
*/
protected static Stack convertSharedFormulas(Stack ptgs, int formulaRow, int formulaColumn) {
if(false) {
/*
- * TODO - (May-2008) Stop converting relative ref Ptgs in shared formula records.
+ * TODO - (May-2008) Stop converting relative ref Ptgs in shared formula records.
* If/when POI writes out the workbook, this conversion makes an unnecessary diff in the BIFF records.
* Disabling this code breaks one existing junit.
* Some fix-up will be required to make Ptg.toFormulaString(HSSFWorkbook) work properly.
@@ -225,31 +230,30 @@
if (!ptg.isBaseToken()) {
ptg.setClass(originalOperandClass);
}
-
+
newPtgStack.add(ptg);
}
return newPtgStack;
}
- /**
- * Creates a non shared formula from the shared formula
+ /**
+ * Creates a non shared formula from the shared formula
* counter part
*/
public void convertSharedFormulaRecord(FormulaRecord formula) {
//Sanity checks
- final int formulaRow = formula.getRow();
- final int formulaColumn = formula.getColumn();
- if (isFormulaInShared(formula)) {
- formula.setExpressionLength(getExpressionLength());
-
- Stack newPtgStack =
- convertSharedFormulas(field_7_parsed_expr, formulaRow, formulaColumn);
- formula.setParsedExpression(newPtgStack);
+ if (!isFormulaInShared(formula)) {
+ throw new RuntimeException("Shared Formula Conversion: Coding Error");
+ }
+ final int formulaRow = formula.getRow();
+ final int formulaColumn = formula.getColumn();
+
+ List ptgList = convertSharedFormulas(field_7_parsed_expr, formulaRow, formulaColumn);
+ Ptg[] ptgs = new Ptg[ptgList.size()];
+ ptgList.toArray(ptgs);
+ formula.setParsedExpression(ptgs);
//Now its not shared!
formula.setSharedFormula(false);
- } else {
- throw new RuntimeException("Shared Formula Conversion: Coding Error");
- }
}
private static int fixupRelativeColumn(int currentcolumn, int column, boolean relative) {
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java Fri Aug 29 08:27:07 2008
@@ -17,9 +17,12 @@
package org.apache.poi.hssf.record.aggregates;
+import org.apache.poi.hssf.model.RecordStream;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.FormulaRecord;
+import org.apache.poi.hssf.record.SharedFormulaRecord;
import org.apache.poi.hssf.record.StringRecord;
+import org.apache.poi.hssf.record.TableRecord;
/**
* The formula record aggregate is used to join together the formula record and it's
@@ -29,61 +32,67 @@
*/
public final class FormulaRecordAggregate extends RecordAggregate implements CellValueRecordInterface {
- private FormulaRecord _formulaRecord;
+ private final FormulaRecord _formulaRecord;
+ /** caches the calculated result of the formula */
private StringRecord _stringRecord;
+ private TableRecord _tableRecord;
- public FormulaRecordAggregate( FormulaRecord formulaRecord, StringRecord stringRecord )
- {
+ public FormulaRecordAggregate(FormulaRecord formulaRecord) {
_formulaRecord = formulaRecord;
- _stringRecord = stringRecord;
+ _stringRecord = null;
}
-
- public void setStringRecord( StringRecord stringRecord ) {
- _stringRecord = stringRecord;
+ public FormulaRecordAggregate(FormulaRecord formulaRecord, RecordStream rs) {
+ _formulaRecord = formulaRecord;
+ Class nextClass = rs.peekNextClass();
+ if (nextClass == SharedFormulaRecord.class) {
+ // For (text) shared formulas, the SharedFormulaRecord comes before the StringRecord.
+ // In any case it is OK to skip SharedFormulaRecords because they were collected
+ // before constructing the ValueRecordsAggregate.
+ rs.getNext(); // skip the shared formula record
+ nextClass = rs.peekNextClass();
+ }
+ if (nextClass == StringRecord.class) {
+ _stringRecord = (StringRecord) rs.getNext();
+ } else if (nextClass == TableRecord.class) {
+ _tableRecord = (TableRecord) rs.getNext();
+ }
}
- public void setFormulaRecord( FormulaRecord formulaRecord )
- {
- _formulaRecord = formulaRecord;
+ public void setStringRecord(StringRecord stringRecord) {
+ _stringRecord = stringRecord;
+ _tableRecord = null; // probably can't have both present at the same time
+ // TODO - establish rules governing when each of these sub records may exist
}
- public FormulaRecord getFormulaRecord()
- {
+ public FormulaRecord getFormulaRecord() {
return _formulaRecord;
}
- public StringRecord getStringRecord()
- {
+ public StringRecord getStringRecord() {
return _stringRecord;
}
- public short getXFIndex()
- {
+ public short getXFIndex() {
return _formulaRecord.getXFIndex();
}
- public void setXFIndex(short xf)
- {
- _formulaRecord.setXFIndex( xf );
+ public void setXFIndex(short xf) {
+ _formulaRecord.setXFIndex(xf);
}
- public void setColumn(short col)
- {
- _formulaRecord.setColumn( col );
+ public void setColumn(short col) {
+ _formulaRecord.setColumn(col);
}
- public void setRow(int row)
- {
- _formulaRecord.setRow( row );
+ public void setRow(int row) {
+ _formulaRecord.setRow(row);
}
- public short getColumn()
- {
+ public short getColumn() {
return _formulaRecord.getColumn();
}
- public int getRow()
- {
+ public int getRow() {
return _formulaRecord.getRow();
}
@@ -94,8 +103,11 @@
public void visitContainedRecords(RecordVisitor rv) {
rv.visitRecord(_formulaRecord);
if (_stringRecord != null) {
- rv.visitRecord(_stringRecord);
+ rv.visitRecord(_stringRecord);
}
+ if (_tableRecord != null) {
+ rv.visitRecord(_tableRecord);
+ }
}
public String getStringValue() {
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java Fri Aug 29 08:27:07 2008
@@ -82,7 +82,7 @@
if (!rec.isValue()) {
throw new RuntimeException("Unexpected record type (" + rec.getClass().getName() + ")");
}
- i += _valuesAgg.construct(recs, i, endIx, sfh);
+ i += _valuesAgg.construct(recs, i, endIx, sfh)-1;
}
"".length();
}
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java Fri Aug 29 08:27:07 2008
@@ -21,6 +21,7 @@
import java.util.Iterator;
import java.util.List;
+import org.apache.poi.hssf.model.RecordStream;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.DBCellRecord;
import org.apache.poi.hssf.record.FormulaRecord;
@@ -111,12 +112,12 @@
public void removeAllCellsValuesForRow(int rowIndex) {
if (rowIndex >= records.length) {
- throw new IllegalArgumentException("Specified rowIndex " + rowIndex
+ throw new IllegalArgumentException("Specified rowIndex " + rowIndex
+ " is outside the allowable range (0.." +records.length + ")");
}
records[rowIndex] = null;
}
-
+
public int getPhysicalNumberOfCells()
{
@@ -142,62 +143,48 @@
}
/**
- * Processes a sequential group of cell value records. Stops at endIx or the first
+ * Processes a sequential group of cell value records. Stops at endIx or the first
* non-value record encountered.
* @param sfh used to resolve any shared formulas for the current sheet
* @return the number of records consumed
*/
public int construct(List records, int offset, int endIx, SharedFormulaHolder sfh) {
- int k = 0;
+ RecordStream rs = new RecordStream(records, offset, endIx);
- FormulaRecordAggregate lastFormulaAggregate = null;
-
// Now do the main processing sweep
- for (k = offset; k < endIx; k++) {
- Record rec = ( Record ) records.get(k);
-
- if (rec instanceof StringRecord) {
- if (lastFormulaAggregate == null) {
- throw new RuntimeException("StringRecord found without preceding FormulaRecord");
- }
- if (lastFormulaAggregate.getStringRecord() != null) {
- throw new RuntimeException("Multiple StringRecords found after FormulaRecord");
- }
- lastFormulaAggregate.setStringRecord((StringRecord)rec);
- lastFormulaAggregate = null;
- continue;
- }
-
- if (rec instanceof TableRecord) {
- // TODO - don't loose this record
- // DATATABLE probably belongs in formula record aggregate
- if (lastFormulaAggregate == null) {
- throw new RuntimeException("No preceding formula record found");
- }
- lastFormulaAggregate = null;
- continue;
+ while (rs.hasNext()) {
+ Class recClass = rs.peekNextClass();
+ if (recClass == StringRecord.class) {
+ throw new RuntimeException("Loose StringRecord found without preceding FormulaRecord");
}
-
- if (rec instanceof SharedFormulaRecord) {
- // Already handled, not to worry
- continue;
+
+ if (recClass == TableRecord.class) {
+ throw new RuntimeException("Loose TableRecord found without preceding FormulaRecord");
}
- if (rec instanceof UnknownRecord) {
+ if (recClass == UnknownRecord.class) {
break;
}
- if (rec instanceof RowRecord) {
- break;
+ if (recClass == RowRecord.class) {
+ break;
}
- if (rec instanceof DBCellRecord) {
+ if (recClass == DBCellRecord.class) {
// end of 'Row Block'. This record is ignored by POI
break;
}
- if (rec instanceof MergeCellsRecord) {
+
+ Record rec = rs.getNext();
+
+ if (recClass == SharedFormulaRecord.class) {
+ // Already handled, not to worry
+ continue;
+ }
+ if (recClass == MergeCellsRecord.class) {
// doesn't really belong here
// can safely be ignored, because it has been processed in a higher method
continue;
}
+
if (!rec.isValue()) {
throw new RuntimeException("bad record type");
}
@@ -206,14 +193,13 @@
if (formula.isSharedFormula()) {
sfh.convertSharedFormulaRecord(formula);
}
-
- lastFormulaAggregate = new FormulaRecordAggregate((FormulaRecord)rec, null);
- insertCell( lastFormulaAggregate );
+
+ insertCell(new FormulaRecordAggregate((FormulaRecord)rec, rs));
continue;
}
insertCell(( CellValueRecordInterface ) rec);
}
- return k - offset - 1;
+ return rs.getCountRead();
}
/** Tallies a count of the size of the cell records
@@ -235,7 +221,7 @@
/** Returns true if the row has cells attached to it */
public boolean rowHasCells(int row) {
- if (row > records.length-1) //previously this said row > records.length which means if
+ if (row > records.length-1) //previously this said row > records.length which means if
return false; // if records.length == 60 and I pass "60" here I get array out of bounds
CellValueRecordInterface[] rowCells=records[row]; //because a 60 length array has the last index = 59
if(rowCells==null) return false;
@@ -260,7 +246,7 @@
}
return pos - offset;
}
-
+
public int visitCellsForRow(int rowIndex, RecordVisitor rv) {
int result = 0;
CellValueRecordInterface[] cellRecs = records[rowIndex];
@@ -292,7 +278,7 @@
public CellValueRecordInterface[] getValueRecords() {
List temp = new ArrayList();
-
+
for (int i = 0; i < records.length; i++) {
CellValueRecordInterface[] rowCells = records[i];
if (rowCells == null) {
@@ -305,7 +291,7 @@
}
}
}
-
+
CellValueRecordInterface[] result = new CellValueRecordInterface[temp.size()];
temp.toArray(result);
return result;
@@ -314,7 +300,7 @@
{
return new MyIterator();
}
-
+
private final class MyIterator implements Iterator {
short nextColumn=-1;
int nextRow,lastRow;
@@ -325,7 +311,7 @@
this.lastRow=records.length-1;
findNext();
}
-
+
public MyIterator(int firstRow,int lastRow)
{
this.nextRow=firstRow;
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/ArrayPtg.java Fri Aug 29 08:27:07 2008
@@ -39,6 +39,11 @@
public static final byte sid = 0x20;
private static final int RESERVED_FIELD_LEN = 7;
+ /**
+ * The size of the plain tArray token written within the standard formula tokens
+ * (not including the data which comes after all formula tokens)
+ */
+ public static final int PLAIN_TOKEN_SIZE = 1+RESERVED_FIELD_LEN;
// TODO - fix up field visibility and subclasses
private byte[] field_1_reserved;
@@ -123,7 +128,7 @@
public int writeTokenValueBytes(byte[] data, int offset) {
LittleEndian.putByte(data, offset + 0, token_1_columns-1);
- LittleEndian.putShort(data, offset + 1, (short)(token_2_rows-1));
+ LittleEndian.putUShort(data, offset + 1, token_2_rows-1);
ConstantValueParser.encode(data, offset + 3, token_3_arrayValues);
return 3 + ConstantValueParser.getEncodedSize(token_3_arrayValues);
}
@@ -137,11 +142,11 @@
}
/** This size includes the size of the array Ptg plus the Array Ptg Token value size*/
- public int getSize()
- {
- int size = 1+7+1+2;
- size += ConstantValueParser.getEncodedSize(token_3_arrayValues);
- return size;
+ public int getSize() {
+ return PLAIN_TOKEN_SIZE
+ // data written after the all tokens:
+ + 1 + 2 // column, row
+ + ConstantValueParser.getEncodedSize(token_3_arrayValues);
}
public String toFormulaString(Workbook book)
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/Ptg.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/Ptg.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/Ptg.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/Ptg.java Fri Aug 29 08:27:07 2008
@@ -222,13 +222,6 @@
throw new RuntimeException("Unexpected base token id (" + id + ")");
}
/**
- *
- *
- */
- public static int getEncodedSize(Stack ptgs) {
- return getEncodedSize(toPtgArray(ptgs));
- }
- /**
* @return a distinct copy of this <tt>Ptg</tt> if the class is mutable, or the same instance
* if the class is immutable.
*/
@@ -265,6 +258,11 @@
}
return result;
}
+ /**
+ * This method will return the same result as {@link #getEncodedSizeWithoutArrayData(Ptg[])}
+ * if there are no array tokens present.
+ * @return the full size taken to encode the specified <tt>Ptg</tt>s
+ */
// TODO - several duplicates of this code should be refactored here
public static int getEncodedSize(Ptg[] ptgs) {
int result = 0;
@@ -274,6 +272,22 @@
return result;
}
/**
+ * Used to calculate value that should be encoded at the start of the encoded Ptg token array;
+ * @return the size of the encoded Ptg tokens not including any trailing array data.
+ */
+ public static int getEncodedSizeWithoutArrayData(Ptg[] ptgs) {
+ int result = 0;
+ for (int i = 0; i < ptgs.length; i++) {
+ Ptg ptg = ptgs[i];
+ if (ptg instanceof ArrayPtg) {
+ result += ArrayPtg.PLAIN_TOKEN_SIZE;
+ } else {
+ result += ptg.getSize();
+ }
+ }
+ return result;
+ }
+ /**
* Writes the ptgs to the data buffer, starting at the specified offset.
*
* <br/>
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEval.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEval.java Fri Aug 29 08:27:07 2008
@@ -1,28 +1,25 @@
-/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You under the Apache License, Version 2.0
-* (the "License"); you may not use this file except in compliance with
-* the License. You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-/*
- * Created on May 8, 2005
- *
- */
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
package org.apache.poi.hssf.record.formula.eval;
/**
* @author Amol S. Deshmukh < amolweb at ya hoo dot com >
- *
+ *
*/
public interface AreaEval extends ValueEval {
@@ -30,76 +27,81 @@
* returns the 0-based index of the first row in
* this area.
*/
- public int getFirstRow();
+ int getFirstRow();
/**
* returns the 0-based index of the last row in
* this area.
*/
- public int getLastRow();
+ int getLastRow();
/**
* returns the 0-based index of the first col in
* this area.
*/
- public int getFirstColumn();
+ int getFirstColumn();
/**
* returns the 0-based index of the last col in
* this area.
*/
- public int getLastColumn();
-
+ int getLastColumn();
+
/**
* returns true if the Area's start and end row indexes
* are same. This result of this method should agree
* with getFirstRow() == getLastRow().
*/
- public boolean isRow();
-
+ boolean isRow();
+
/**
* returns true if the Area's start and end col indexes
* are same. This result of this method should agree
* with getFirstColumn() == getLastColumn().
*/
- public boolean isColumn();
+ boolean isColumn();
/**
* The array of values in this area. Although the area
* maybe 1D (ie. isRow() or isColumn() returns true) or 2D
* the returned array is 1D.
*/
- public ValueEval[] getValues();
+ ValueEval[] getValues();
/**
- * returns the ValueEval from the values array at the specified
- * row and col index. The specified indexes should be absolute indexes
- * in the sheet and not relative indexes within the area. Also,
- * if contains(row, col) evaluates to true, a null value will
- * bre returned.
- * @param row
- * @param col
+ * @return the ValueEval from within this area at the specified row and col index. Never
+ * <code>null</code> (possibly {@link BlankEval}). The specified indexes should be absolute
+ * indexes in the sheet and not relative indexes within the area.
*/
- public ValueEval getValueAt(int row, int col);
-
+ ValueEval getValueAt(int row, int col);
+
/**
- * returns true if the cell at row and col specified
- * as absolute indexes in the sheet is contained in
+ * returns true if the cell at row and col specified
+ * as absolute indexes in the sheet is contained in
* this area.
* @param row
* @param col
*/
- public boolean contains(int row, int col);
-
+ boolean contains(int row, int col);
+
/**
* returns true if the specified col is in range
* @param col
*/
- public boolean containsColumn(short col);
-
+ boolean containsColumn(short col);
+
/**
* returns true if the specified row is in range
* @param row
*/
- public boolean containsRow(int row);
+ boolean containsRow(int row);
+
+ int getWidth();
+ int getHeight();
+ /**
+ * @return the ValueEval from within this area at the specified relativeRowIndex and
+ * relativeColumnIndex. Never <code>null</code> (possibly {@link BlankEval}). The
+ * specified indexes should relative to the top left corner of this area.
+ */
+ ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex);
}
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java Fri Aug 29 08:27:07 2008
@@ -1,19 +1,19 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
package org.apache.poi.hssf.record.formula.eval;
@@ -94,9 +94,7 @@
throw new IllegalArgumentException("Specified column index (" + col
+ ") is outside the allowed range (" + _firstColumn + ".." + col + ")");
}
-
- int index = rowOffsetIx * _nColumns + colOffsetIx;
- return _values[index];
+ return getRelativeValue(rowOffsetIx, colOffsetIx);
}
public final boolean contains(int row, int col) {
@@ -119,4 +117,20 @@
public final boolean isRow() {
return _firstRow == _lastRow;
}
+ public int getHeight() {
+ return _lastRow-_firstRow+1;
+ }
+
+ public ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex) {
+ int index = relativeRowIndex * _nColumns + relativeColumnIndex;
+ ValueEval result = _values[index];
+ if (result == null) {
+ return BlankEval.INSTANCE;
+ }
+ return result;
+ }
+
+ public int getWidth() {
+ return _lastColumn-_firstColumn+1;
+ }
}
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/BlankEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/BlankEval.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/BlankEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/BlankEval.java Fri Aug 29 08:27:07 2008
@@ -1,30 +1,27 @@
-/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You under the Apache License, Version 2.0
-* (the "License"); you may not use this file except in compliance with
-* the License. You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-/*
- * Created on May 9, 2005
- *
- */
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
package org.apache.poi.hssf.record.formula.eval;
/**
* @author Amol S. Deshmukh < amolweb at ya hoo dot com > This class is a
* marker class. It is a special value for empty cells.
*/
-public class BlankEval implements ValueEval {
+public final class BlankEval implements ValueEval {
public static BlankEval INSTANCE = new BlankEval();
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java Fri Aug 29 08:27:07 2008
@@ -27,7 +27,7 @@
private OperandResolver() {
// no instances of this class
}
-
+
/**
* Retrieves a single value from a variety of different argument types according to standard
* Excel rules. Does not perform any type conversion.
@@ -113,17 +113,17 @@
}
if (result instanceof ErrorEval) {
throw new EvaluationException((ErrorEval) result);
-
+
}
return result;
}
-
+
/**
* @return possibly <tt>ErrorEval</tt>, and <code>null</code>
*/
private static ValueEval chooseSingleElementFromAreaInternal(AreaEval ae,
int srcCellRow, short srcCellCol) throws EvaluationException {
-
+
if(false) {
// this is too simplistic
if(ae.containsRow(srcCellRow) && ae.containsColumn(srcCellCol)) {
@@ -131,25 +131,25 @@
}
/*
Circular references are not dealt with directly here, but it is worth noting some issues.
-
+
ANY one of the return statements in this method could return a cell that is identical
to the one immediately being evaluated. The evaluating cell is identified by srcCellRow,
srcCellRow AND sheet. The sheet is not available in any nearby calling method, so that's
one reason why circular references are not easy to detect here. (The sheet of the returned
cell can be obtained from ae if it is an Area3DEval.)
-
+
Another reason there's little value in attempting to detect circular references here is
that only direct circular references could be detected. If the cycle involved two or more
cells this method could not detect it.
-
+
Logic to detect evaluation cycles of all kinds has been coded in EvaluationCycleDetector
(and HSSFFormulaEvaluator).
*/
}
-
+
if (ae.isColumn()) {
if(ae.isRow()) {
- return ae.getValues()[0];
+ return ae.getRelativeValue(0, 0);
}
if(!ae.containsRow(srcCellRow)) {
throw EvaluationException.invalidValue();
@@ -199,20 +199,20 @@
*/
public static double coerceValueToDouble(ValueEval ev) throws EvaluationException {
- if (ev instanceof NumericValueEval) {
- // this also handles booleans
- return ((NumericValueEval)ev).getNumberValue();
- }
- if (ev instanceof StringEval) {
- Double dd = parseDouble(((StringEval) ev).getStringValue());
- if (dd == null) {
- throw EvaluationException.invalidValue();
- }
- return dd.doubleValue();
+ if (ev instanceof NumericValueEval) {
+ // this also handles booleans
+ return ((NumericValueEval)ev).getNumberValue();
+ }
+ if (ev instanceof StringEval) {
+ Double dd = parseDouble(((StringEval) ev).getStringValue());
+ if (dd == null) {
+ throw EvaluationException.invalidValue();
+ }
+ return dd.doubleValue();
}
throw new RuntimeException("Unexpected arg eval type (" + ev.getClass().getName() + ")");
}
-
+
/**
* Converts a string to a double using standard rules that Excel would use.<br/>
* Tolerates currency prefixes, commas, leading and trailing spaces.<p/>
@@ -245,7 +245,7 @@
return null;
}
// TODO - support notation like '1E3' (==1000)
-
+
double val;
try {
val = Double.parseDouble(text);
@@ -254,7 +254,7 @@
}
return new Double(isPositive ? +val : -val);
}
-
+
/**
* @param ve must be a <tt>NumberEval</tt>, <tt>StringEval</tt>, <tt>BoolEval</tt>, or <tt>BlankEval</tt>
* @return the converted string value. never <code>null</code>
@@ -274,4 +274,51 @@
}
throw new IllegalArgumentException("Unexpected eval class (" + ve.getClass().getName() + ")");
}
+
+ /**
+ * @return <code>null</code> to represent blank values
+ * @throws EvaluationException if ve is an ErrorEval, or if a string value cannot be converted
+ */
+ public static Boolean coerceValueToBoolean(ValueEval ve, boolean stringsAreBlanks) throws EvaluationException {
+
+ if (ve == null || ve instanceof BlankEval) {
+ // TODO - remove 've == null' condition once AreaEval is fixed
+ return null;
+ }
+ if (ve instanceof BoolEval) {
+ return Boolean.valueOf(((BoolEval) ve).getBooleanValue());
+ }
+
+ if (ve instanceof BlankEval) {
+ return null;
+ }
+
+ if (ve instanceof StringEval) {
+ if (stringsAreBlanks) {
+ return null;
+ }
+ String str = ((StringEval) ve).getStringValue();
+ if (str.equalsIgnoreCase("true")) {
+ return Boolean.TRUE;
+ }
+ if (str.equalsIgnoreCase("false")) {
+ return Boolean.FALSE;
+ }
+ // else - string cannot be converted to boolean
+ throw new EvaluationException(ErrorEval.VALUE_INVALID);
+ }
+
+ if (ve instanceof NumericValueEval) {
+ NumericValueEval ne = (NumericValueEval) ve;
+ double d = ne.getNumberValue();
+ if (Double.isNaN(d)) {
+ throw new EvaluationException(ErrorEval.VALUE_INVALID);
+ }
+ return Boolean.valueOf(d != 0);
+ }
+ if (ve instanceof ErrorEval) {
+ throw new EvaluationException((ErrorEval) ve);
+ }
+ throw new RuntimeException("Unexpected eval (" + ve.getClass().getName() + ")");
+ }
}
Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/And.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/And.java?rev=690299&r1=690298&r2=690299&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/And.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/And.java Fri Aug 29 08:27:07 2008
@@ -1,85 +1,32 @@
-/*
-* Licensed to the Apache Software Foundation (ASF) under one or more
-* contributor license agreements. See the NOTICE file distributed with
-* this work for additional information regarding copyright ownership.
-* The ASF licenses this file to You under the Apache License, Version 2.0
-* (the "License"); you may not use this file except in compliance with
-* the License. You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-/*
- * Created on May 9, 2005
- *
- */
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
package org.apache.poi.hssf.record.formula.functions;
-import org.apache.poi.hssf.record.formula.eval.AreaEval;
-import org.apache.poi.hssf.record.formula.eval.BoolEval;
-import org.apache.poi.hssf.record.formula.eval.ErrorEval;
-import org.apache.poi.hssf.record.formula.eval.Eval;
-import org.apache.poi.hssf.record.formula.eval.StringEval;
-import org.apache.poi.hssf.record.formula.eval.ValueEval;
-
-public class And extends BooleanFunction {
-
- public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
- ValueEval retval = null;
- boolean b = true;
- boolean atleastOneNonBlank = false;
-
- /*
- * Note: do not abort the loop if b is false, since we could be
- * dealing with errorevals later.
- */
- outer:
- for (int i=0, iSize=operands.length; i<iSize; i++) {
- if (operands[i] instanceof AreaEval) {
- AreaEval ae = (AreaEval) operands[i];
- ValueEval[] values = ae.getValues();
- for (int j=0, jSize=values.length; j<jSize; j++) {
- ValueEval tempVe = singleOperandEvaluate(values[j], srcRow, srcCol, true);
- if (tempVe instanceof BoolEval) {
- b = b && ((BoolEval) tempVe).getBooleanValue();
- atleastOneNonBlank = true;
- }
- else if (tempVe instanceof ErrorEval) {
- retval = tempVe;
- break outer;
- }
- }
- }
- else {
- ValueEval tempVe = singleOperandEvaluate(operands[i], srcRow, srcCol, false);
- if (tempVe instanceof BoolEval) {
- b = b && ((BoolEval) tempVe).getBooleanValue();
- atleastOneNonBlank = true;
- }
- else if (tempVe instanceof StringEval) {
- retval = ErrorEval.VALUE_INVALID;
- }
- else if (tempVe instanceof ErrorEval) {
- retval = tempVe;
- break outer;
- }
- }
- }
-
- if (!atleastOneNonBlank) {
- retval = ErrorEval.VALUE_INVALID;
- }
-
- if (retval == null) { // if no error
- retval = b ? BoolEval.TRUE : BoolEval.FALSE;
- }
-
- return retval;
- }
+/**
+ *
+ */
+public final class And extends BooleanFunction {
+
+ protected boolean getInitialResultValue() {
+ return true;
+ }
+ protected boolean partialEvaluate(boolean cumulativeResult, boolean currentValue) {
+ return cumulativeResult && currentValue;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org