You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2008/11/15 18:25:43 UTC

svn commit: r717883 - in /poi/trunk/src: documentation/content/xdocs/ java/org/apache/poi/hssf/record/aggregates/ testcases/org/apache/poi/hssf/record/aggregates/ testcases/org/apache/poi/hssf/usermodel/

Author: josh
Date: Sat Nov 15 09:25:32 2008
New Revision: 717883

URL: http://svn.apache.org/viewvc?rev=717883&view=rev
Log:
Fix for bug 46213 - FormulaRecordAggregate should gracefully ignore extra StringRecords

Modified:
    poi/trunk/src/documentation/content/xdocs/changes.xml
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/RecordInspector.java

Modified: poi/trunk/src/documentation/content/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/changes.xml?rev=717883&r1=717882&r2=717883&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/changes.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/changes.xml Sat Nov 15 09:25:32 2008
@@ -37,6 +37,7 @@
 
 		<!-- Don't forget to update status.xml too! -->
         <release version="3.5-beta4" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">46213 - Fixed FormulaRecordAggregate to gracefully ignore extra StringRecords</action>
            <action dev="POI-DEVELOPERS" type="fix">46174 - Fixed HSSFName to handle general formulas (not just area references)</action>
            <action dev="POI-DEVELOPERS" type="add">46189 - added chart records: CHARTFRTINFO, STARTBLOCK, ENDBLOCK, STARTOBJECT, ENDOBJECT, and CATLAB</action>
            <action dev="POI-DEVELOPERS" type="fix">46199 - More tweaks to EmbeddedObjectRefSubRecord</action>

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=717883&r1=717882&r2=717883&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Sat Nov 15 09:25:32 2008
@@ -34,6 +34,7 @@
 	<!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.5-beta4" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">46213 - Fixed FormulaRecordAggregate to gracefully ignore extra StringRecords</action>
            <action dev="POI-DEVELOPERS" type="fix">46174 - Fixed HSSFName to handle general formulas (not just area references)</action>
            <action dev="POI-DEVELOPERS" type="add">46189 - added chart records: CHARTFRTINFO, STARTBLOCK, ENDBLOCK, STARTOBJECT, ENDOBJECT, and CATLAB</action>
            <action dev="POI-DEVELOPERS" type="fix">46199 - More tweaks to EmbeddedObjectRefSubRecord</action>

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java?rev=717883&r1=717882&r2=717883&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java Sat Nov 15 09:25:32 2008
@@ -26,7 +26,6 @@
 import org.apache.poi.hssf.record.formula.ExpPtg;
 import org.apache.poi.hssf.record.formula.Ptg;
 import org.apache.poi.hssf.util.CellReference;
-import org.apache.poi.ss.formula.Formula;
 
 /**
  * The formula record aggregate is used to join together the formula record and it's
@@ -51,17 +50,19 @@
 		if (svm == null) {
 			throw new IllegalArgumentException("sfm must not be null");
 		}
-		boolean hasStringRec = stringRec != null;
-		boolean hasCachedStringFlag = formulaRec.hasCachedResultString();
-		if (hasStringRec != hasCachedStringFlag) {
-			throw new RecordFormatException("String record was "
-					+ (hasStringRec ? "": "not ") + " supplied but formula record flag is "
-					+ (hasCachedStringFlag ? "" : "not ") + " set");
+		if (formulaRec.hasCachedResultString()) {
+			if (stringRec == null) {
+				throw new RecordFormatException("Formula record flag is set but String record was not found");
+			}
+			_stringRecord = stringRec;
+		} else {
+			// Usually stringRec is null here (in agreement with what the formula rec says).
+			// In the case where an extra StringRecord is erroneously present, Excel (2007)
+			// ignores it (see bug 46213). 
+			_stringRecord = null;
 		}
 
 		if (formulaRec.isSharedFormula()) {
-			CellReference cr = new CellReference(formulaRec.getRow(), formulaRec.getColumn());
-			
 			CellReference firstCell = formulaRec.getFormula().getExpReference();
 			if (firstCell == null) {
 				handleMissingSharedFormulaRecord(formulaRec);
@@ -71,7 +72,6 @@
 		}
 		_formulaRecord = formulaRec;
 		_sharedValueManager = svm;
-		_stringRecord = stringRec;
 	}
 	/**
 	 * Sometimes the shared formula flag "seems" to be erroneously set (because the corresponding

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java?rev=717883&r1=717882&r2=717883&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java Sat Nov 15 09:25:32 2008
@@ -17,23 +17,58 @@
 
 package org.apache.poi.hssf.record.aggregates;
 
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
 import org.apache.poi.hssf.record.FormulaRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.RecordFormatException;
 import org.apache.poi.hssf.record.StringRecord;
+import org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector;
 
 /**
- *
- * @author  avik
+ * 
+ * @author avik
  */
 public final class TestFormulaRecordAggregate extends TestCase {
-    
-    public void testBasic() throws Exception {
-        FormulaRecord f = new FormulaRecord();
-        f.setCachedResultTypeString();
-        StringRecord s = new StringRecord();
-        s.setString("abc");
-        FormulaRecordAggregate fagg = new FormulaRecordAggregate(f, s, SharedValueManager.EMPTY);
-        assertEquals("abc", fagg.getStringValue());
-    }
+
+	public void testBasic() throws Exception {
+		FormulaRecord f = new FormulaRecord();
+		f.setCachedResultTypeString();
+		StringRecord s = new StringRecord();
+		s.setString("abc");
+		FormulaRecordAggregate fagg = new FormulaRecordAggregate(f, s, SharedValueManager.EMPTY);
+		assertEquals("abc", fagg.getStringValue());
+	}
+
+	/**
+	 * Sometimes a {@link StringRecord} appears after a {@link FormulaRecord} even though the
+	 * formula has evaluated to a text value.  This might be more likely to occur when the formula
+	 * <i>can</i> evaluate to a text value.<br/>
+	 * Bug 46213 attachment 22874 has such an extra {@link StringRecord} at stream offset 0x5765.
+	 * This file seems to open in Excel (2007) with no trouble.  When it is re-saved, Excel omits
+	 * the extra record.  POI should do the same.
+	 */
+	public void testExtraStringRecord_bug46213() {
+		FormulaRecord fr = new FormulaRecord();
+		fr.setValue(2.0);
+		StringRecord sr = new StringRecord();
+		sr.setString("NA");
+		SharedValueManager svm = SharedValueManager.EMPTY;
+		FormulaRecordAggregate fra;
+		
+		try {
+			fra = new FormulaRecordAggregate(fr, sr, svm);
+		} catch (RecordFormatException e) {
+			if ("String record was  supplied but formula record flag is not  set".equals(e.getMessage())) {
+				throw new AssertionFailedError("Identified bug 46213");
+			}
+			throw e;
+		}
+		RecordCollector rc = new RecordCollector();
+		fra.visitContainedRecords(rc);
+		Record[] vraRecs = rc.getRecords();
+		assertEquals(1, vraRecs.length);
+		assertEquals(fr, vraRecs[0]);
+	}
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/RecordInspector.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/RecordInspector.java?rev=717883&r1=717882&r2=717883&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/RecordInspector.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/RecordInspector.java Sat Nov 15 09:25:32 2008
@@ -24,7 +24,7 @@
 import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
 
 /**
- * Test utility class to get {@link Record}s out HSSF objects
+ * Test utility class to get {@link Record}s out of HSSF objects
  * 
  * @author Josh Micich
  */
@@ -34,12 +34,12 @@
 		// no instances of this class
 	}
 
-	private static final class RecordCollector implements RecordVisitor {
+	public static final class RecordCollector implements RecordVisitor {
 
-		private List _list;
+		private List<Record> _list;
 
 		public RecordCollector() {
-			_list = new ArrayList(128);
+			_list = new ArrayList<Record>(128);
 		}
 
 		public void visitRecord(Record r) {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org