From: Nick Burch Date: Sat, 28 Jun 2008 18:21:21 +0000 (+0000) Subject: Avoid spurious missing lines with the MissingRecordAware event code, and odd files... X-Git-Tag: REL_3_2_FINAL~274 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9147f07e308e6aabec08548336e9a02598bc5cf9;p=poi.git Avoid spurious missing lines with the MissingRecordAware event code, and odd files that contain RowRecords in the middle of the cell Records. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@672562 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 3789216d2d..d8e6c421d7 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + Avoid spurious missing lines with the MissingRecordAware event code, and odd files that contain RowRecords in the middle of the cell Records. Support for parsing formulas during EventUserModel processing, via the new EventWorkbookBuilder diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index a226686c0d..93a290c0b0 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Avoid spurious missing lines with the MissingRecordAware event code, and odd files that contain RowRecords in the middle of the cell Records. Support for parsing formulas during EventUserModel processing, via the new EventWorkbookBuilder diff --git a/src/java/org/apache/poi/hssf/eventusermodel/MissingRecordAwareHSSFListener.java b/src/java/org/apache/poi/hssf/eventusermodel/MissingRecordAwareHSSFListener.java index a727d064b9..0bdcb1d3d9 100644 --- a/src/java/org/apache/poi/hssf/eventusermodel/MissingRecordAwareHSSFListener.java +++ b/src/java/org/apache/poi/hssf/eventusermodel/MissingRecordAwareHSSFListener.java @@ -46,8 +46,14 @@ import org.apache.poi.hssf.record.RowRecord; */ public class MissingRecordAwareHSSFListener implements HSSFListener { private HSSFListener childListener; - private int lastSeenRow = -1; - private int lastSeenColumn = -1; + + // 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 lastCellRow = -1; + private int lastCellColumn = -1; /** * Constructs a new MissingRecordAwareHSSFListener, which @@ -71,14 +77,16 @@ public class MissingRecordAwareHSSFListener implements HSSFListener { if (bof.getType() == bof.TYPE_WORKBOOK) { // Reset the row and column counts - new workbook - lastSeenRow = -1; - lastSeenColumn = -1; + 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 - lastSeenRow = -1; - lastSeenColumn = -1; + lastRowRow = -1; + lastCellRow = -1; + lastCellColumn = -1; //System.out.println("Encountered sheet reference"); } break; @@ -92,15 +100,15 @@ public class MissingRecordAwareHSSFListener implements HSSFListener { // + rowrec.getFirstCol() + " last column at " + rowrec.getLastCol()); // If there's a jump in rows, fire off missing row records - if(lastSeenRow+1 < rowrec.getRowNumber()) { - for(int i=(lastSeenRow+1); i -1) { + for(int i=lastCellRow; i 100); + } + public void openAlt() { + 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("MRExtraLines.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 testMissingRowRecords() throws Exception { + openNormal(); // We have rows 0, 1, 2, 20 and 21 int row0 = -1; @@ -108,6 +127,7 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { } public void testEndOfRowRecords() throws Exception { + openNormal(); // Find the cell at 0,0 int cell00 = -1; @@ -194,7 +214,7 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { assertTrue(r[cell00+57] instanceof LastCellOfRowDummyRecord); // Check the numbers of the last seen columns - LastCellOfRowDummyRecord[] lrs = new LastCellOfRowDummyRecord[23]; + LastCellOfRowDummyRecord[] lrs = new LastCellOfRowDummyRecord[24]; int lrscount = 0; for(int i=0; i