From 2dd98d377e128fffcd25157c714d324a1118cc00 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Sat, 23 Aug 2008 22:47:51 +0000 Subject: [PATCH] Fix for bug 45672 - prevent MissingRecordAwareHSSFListener generating multiple LastCellOfRowDummyRecords when shared formulas are present git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@688426 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../MissingRecordAwareHSSFListener.java | 196 +++++++----------- .../org/apache/poi/hssf/data/ex45672.xls | Bin 0 -> 16384 bytes .../TestMissingRecordAwareHSSFListener.java | 86 ++++---- 5 files changed, 120 insertions(+), 164 deletions(-) create mode 100644 src/testcases/org/apache/poi/hssf/data/ex45672.xls diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index e23cb3169f..26a9c72109 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 45672 - Fix for MissingRecordAwareHSSFListener to prevent multiple LastCellOfRowDummyRecords when shared formulas are present 45645 - Fix for HSSFSheet.autoSizeColumn() for widths exceeding Short.MAX_VALUE 45623 - Support for additional HSSF header and footer fields, including bold and full file path 45623 - Support stripping HSSF header and footer fields (eg page number) out of header and footer text if required diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 33c86d5896..1b086fddc7 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 45672 - Fix for MissingRecordAwareHSSFListener to prevent multiple LastCellOfRowDummyRecords when shared formulas are present 45645 - Fix for HSSFSheet.autoSizeColumn() for widths exceeding Short.MAX_VALUE 45623 - Support for additional HSSF header and footer fields, including bold and full file path 45623 - Support stripping HSSF header and footer fields (eg page number) out of header and footer text if required diff --git a/src/java/org/apache/poi/hssf/eventusermodel/MissingRecordAwareHSSFListener.java b/src/java/org/apache/poi/hssf/eventusermodel/MissingRecordAwareHSSFListener.java index 0bdcb1d3d9..e41b0b92ae 100644 --- a/src/java/org/apache/poi/hssf/eventusermodel/MissingRecordAwareHSSFListener.java +++ b/src/java/org/apache/poi/hssf/eventusermodel/MissingRecordAwareHSSFListener.java @@ -17,22 +17,15 @@ package org.apache.poi.hssf.eventusermodel; -import org.apache.poi.hssf.eventusermodel.HSSFListener; import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord; import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord; import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord; import org.apache.poi.hssf.record.BOFRecord; -import org.apache.poi.hssf.record.BlankRecord; -import org.apache.poi.hssf.record.BoolErrRecord; -import org.apache.poi.hssf.record.BoundSheetRecord; -import org.apache.poi.hssf.record.FormulaRecord; -import org.apache.poi.hssf.record.LabelRecord; -import org.apache.poi.hssf.record.LabelSSTRecord; +import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.NoteRecord; -import org.apache.poi.hssf.record.NumberRecord; -import org.apache.poi.hssf.record.RKRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.RowRecord; +import org.apache.poi.hssf.record.SharedFormulaRecord; /** *

A HSSFListener which tracks rows and columns, and will @@ -44,16 +37,16 @@ import org.apache.poi.hssf.record.RowRecord; * file, or was skipped from being written as it was * blank. */ -public class MissingRecordAwareHSSFListener implements HSSFListener { +public final class MissingRecordAwareHSSFListener implements HSSFListener { private HSSFListener childListener; // Need to have different counters for cell rows and // row rows, as you sometimes get a RowRecord in the // middle of some cells, and that'd break everything - private int lastRowRow = -1; + private int lastRowRow; - private int lastCellRow = -1; - private int lastCellColumn = -1; + private int lastCellRow; + private int lastCellColumn; /** * Constructs a new MissingRecordAwareHSSFListener, which @@ -62,128 +55,80 @@ public class MissingRecordAwareHSSFListener implements HSSFListener { * @param listener The HSSFListener to pass records on to */ public MissingRecordAwareHSSFListener(HSSFListener listener) { + resetCounts(); childListener = listener; } public void processRecord(Record record) { - int thisRow = -1; - int thisColumn = -1; - - switch (record.getSid()) - { - // the BOFRecord can represent either the beginning of a sheet or the workbook - case BOFRecord.sid: - BOFRecord bof = (BOFRecord) record; - if (bof.getType() == bof.TYPE_WORKBOOK) - { - // Reset the row and column counts - new workbook - lastRowRow = -1; - lastCellRow = -1; - lastCellColumn = -1; - //System.out.println("Encountered workbook"); - } else if (bof.getType() == bof.TYPE_WORKSHEET) - { - // Reset the row and column counts - new sheet - lastRowRow = -1; - lastCellRow = -1; - lastCellColumn = -1; - //System.out.println("Encountered sheet reference"); - } - break; - case BoundSheetRecord.sid: - BoundSheetRecord bsr = (BoundSheetRecord) record; - //System.out.println("New sheet named: " + bsr.getSheetname()); - break; - case RowRecord.sid: - RowRecord rowrec = (RowRecord) record; - //System.out.println("Row " + rowrec.getRowNumber() + " found, first column at " - // + rowrec.getFirstCol() + " last column at " + rowrec.getLastCol()); - - // If there's a jump in rows, fire off missing row records - if(lastRowRow+1 < rowrec.getRowNumber()) { - for(int i=(lastRowRow+1); i -1) { for(int i=lastCellRow; im)paa2|r7Kru3oy_>i(cIw!W zNJKF$s8yjTu}jhhQ$$0``xV}8D4{=~NTx+C6hWa12q-O83n^6rk!-(n@7~#&yEBuC z+9H9N)t#L=_ulWEd(OG%KKAZQzb!s;`@_@TBJDbh!gM@cK>0ShfoD$QDk7SW2Yx@E zPNyZ22#;T1Um^`0gRC=Qyf9J(DG!Nl9z`;d3XsMi6(WsAIt6JQ(s(4e02L!mK$?hD zf;0(fGSU>JsYs_HO+#WI;S{qye~Hxel@;|u>cyv@;&cx6;Jc5u!$QHY$?zDZoDizT zO}-Ny7I!+Azpg7ew0mRT3&w|wKDd*=XMi>+i_v;;@1xDMLCCRL);fv%1PU4Kw@QW} zRhe~ptt^>G7tsJ+iBG#|`}xp9O31{Ne(J(=j}K@i_;U>8{=+i(6m?|1oO#Qmv%#T5 z$dD8kI5~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Ep4`L}cLNdA15RQayS9<;q3C9i{ zg=8|E4u{53y&W78R0l710hdwK13aC$>+qk@5C(6sgCmma;1gWHWi<5wmlKST0AFMW zM?}@aN_e6RI7d}!_o!Vh?3Im!X0GptOabC<#9 z1?+CwF3x)&%Fw()@?Qp?BAmBCL~QBm*_`amM4k&YVWuJ?wq#sXx^6c1gspyHML`w! zcta!~${vg392mP-CkOib+7Z~aA;Fk|Az?Kt2n_K`ztDT z^4>@`qRTfGJLQ{-o$_rsREVQG*q~lB&@T*pwZ?Q3px1~d=bG3)ws0tRn3xG!N#vW7 zM7}9WpynDi`OW`1DM=b)=A4I7!%VNOO;H2Fsl zejB1a;Iqe;v;19yZUOBU8;yC~2-=M{8rSAC7xEZ-k>eP8VW@<&#l>}R#eVwJHT)M8 zKDa#=_f8x@;OAo8`R^zueSY^xzgx9oZK6%k=L`A*asRfU4BFaM0k6iXNN>r5Vi#C0*GKtYUSe6bGrFfTZS_yhqzn@q&xcz8pppz(DM&I6T*|7aoNLa97iknjA1BL8pe z>+7qjf#Vpu^2ERbL)$L)izCah#LjQ;i7!vK4_w*X!*f(YXUur!naix| z>gem;+MDW+pR=ta*)8}EzWv$FQyZT%nvp+v>|yc0GWOwHxxBn$1?i1YpwMZOGFG1q-K3kA5560?FwrvpK-0troO-9;<#QOLV66@z)BwZit zFE~4XQE&GE?<;IYzHHN2fs0qq?^uzPiTkab7SLFb7@k$lVy*A(+q{)$3|j>%w}9)& zh4mK9Bsb7E1?^;R{4Xqf&j?bNeWfo*!GL8>mVZFlz{S4I{0&6+Fh)6UgZ|&mInY41 z@2yFtx;m1w3&!heYU<;bLx~Y%c~>g67?V@pA3-mz7G7Arm_A-|@LPdZ`E|iBSOfn7 D*Z@mO literal 0 HcmV?d00001 diff --git a/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java b/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java index 37e594940c..aa4bbcc6f0 100644 --- a/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java +++ b/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java @@ -21,6 +21,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.poi.hssf.HSSFTestDataSamples; @@ -31,6 +32,7 @@ import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.RowRecord; +import org.apache.poi.hssf.record.SharedFormulaRecord; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * Tests for MissingRecordAwareHSSFListener @@ -39,25 +41,7 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { private Record[] r; - public void openNormal() { - HSSFRequest req = new HSSFRequest(); - MockHSSFListener mockListen = new MockHSSFListener(); - MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(mockListen); - req.addListenerForAllRecords(listener); - - HSSFEventFactory factory = new HSSFEventFactory(); - try { - InputStream is = HSSFTestDataSamples.openSampleFileStream("MissingBits.xls"); - POIFSFileSystem fs = new POIFSFileSystem(is); - factory.processWorkbookEvents(req, fs); - } catch (IOException e) { - throw new RuntimeException(e); - } - - r = mockListen.getRecords(); - assertTrue(r.length > 100); - } - public void openAlt() { + private void readRecords(String sampleFileName) { HSSFRequest req = new HSSFRequest(); MockHSSFListener mockListen = new MockHSSFListener(); MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(mockListen); @@ -65,7 +49,7 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { HSSFEventFactory factory = new HSSFEventFactory(); try { - InputStream is = HSSFTestDataSamples.openSampleFileStream("MRExtraLines.xls"); + InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleFileName); POIFSFileSystem fs = new POIFSFileSystem(is); factory.processWorkbookEvents(req, fs); } catch (IOException e) { @@ -75,8 +59,11 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { r = mockListen.getRecords(); assertTrue(r.length > 100); } + public void openNormal() { + readRecords("MissingBits.xls"); + } - public void testMissingRowRecords() throws Exception { + public void testMissingRowRecords() { openNormal(); // We have rows 0, 1, 2, 20 and 21 @@ -126,7 +113,7 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { assertEquals(19, mr.getRowNumber()); } - public void testEndOfRowRecords() throws Exception { + public void testEndOfRowRecords() { openNormal(); // Find the cell at 0,0 @@ -248,7 +235,7 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { } - public void testMissingCellRecords() throws Exception { + public void testMissingCellRecords() { openNormal(); // Find the cell at 0,0 @@ -350,29 +337,21 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { // Make sure we don't put in any extra new lines // that aren't already there - public void testNoExtraNewLines() throws Exception { + public void testNoExtraNewLines() { // Load a different file - openAlt(); - - // This file has has something in lines 1-33 - List lcor = new ArrayList(); + readRecords("MRExtraLines.xls"); + + int rowCount=0; for(int i=0; i