From: Josh Micich Date: Sat, 23 Aug 2008 22:47:51 +0000 (+0000) Subject: Fix for bug 45672 - prevent MissingRecordAwareHSSFListener generating multiple LastCe... X-Git-Tag: REL_3_2_FINAL~140 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2dd98d377e128fffcd25157c714d324a1118cc00;p=poi.git 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 --- 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; i 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