From 70f9a0a528835661e49d758f1833e5a8f9fa48ed Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sat, 28 Jun 2008 18:21:21 +0000 Subject: [PATCH] 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 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../MissingRecordAwareHSSFListener.java | 68 +++++++++------- .../org/apache/poi/hssf/data/MRExtraLines.xls | Bin 0 -> 25088 bytes .../TestMissingRecordAwareHSSFListener.java | 77 ++++++++++++++++-- 5 files changed, 112 insertions(+), 35 deletions(-) create mode 100644 src/testcases/org/apache/poi/hssf/data/MRExtraLines.xls diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 3789216d2..d8e6c421d 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 a226686c0..93a290c0b 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 a727d064b..0bdcb1d3d 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;EE6+> z%hH5SC(sPo1zK>MA(Rej0wF;2n34qYN0RA?w9|$#1n7_sP?C1iK+|cH32cAAbG~o) ze)oH5*9k39KFvM4-}%lx=iGD8J@+a7{ol2|^1zoC{>V7bW>aV0DK(o$3!g%sNuCyC z)*!?8cS@y_B8@@z!{>cufw!USd1$;kq1cETprM79lN0$|5a6YC~Fzv@k4*iZRP{=85I+-Wi;K(w{2n(q!MfEy^WZUl#nLF2%@7*%v_Nw) ze|_)KJv;NyeK(c+@#-yG@VlKzIvk#BAbs5IGrL8PeTu9B&Nk^3{npo^nxfRO`AVg1 zFjtu)WAP-usbxMoPos!~$DJeEjL6I_jjy+sWQ>J$c3ImGd z)=yzv3|ifl!cjAsM;c>f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#-DnaEmi7~=O6q0-)75Hhe#LTL{T3-L^5eT*LkMjO+Tfu7953~P&EWlvOSj)i7|EdZBGFfNB5_CRdL-`H z9!26F<0p~0r*bC}CB2G5oGY?y7Rk+X0Nyq_Jbv)-!I6pVMMsAVVo9@Ddf$x1oezVY$j zJ$3y)=p|EFv+yro4&jpbZu7C4tIPkuY)8au81Qs#yc)tPX=;^UCEKdW-p}&?2ly?x A^8f$< 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 16a406cba..bbad7b7f4 100644 --- a/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java +++ b/src/testcases/org/apache/poi/hssf/eventusermodel/TestMissingRecordAwareHSSFListener.java @@ -16,8 +16,6 @@ ==================================================================== */ package org.apache.poi.hssf.eventusermodel; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -29,6 +27,7 @@ import org.apache.poi.hssf.HSSFTestDataSamples; 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.LabelSSTRecord; import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.RowRecord; @@ -40,8 +39,7 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { private Record[] r; - public void setUp() { - + public void openNormal() { HSSFRequest req = new HSSFRequest(); MockHSSFListener mockListen = new MockHSSFListener(); MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(mockListen); @@ -55,10 +53,31 @@ public final class TestMissingRecordAwareHSSFListener extends TestCase { } catch (IOException e) { throw new RuntimeException(e); } + r = mockListen.getRecords(); + assertTrue(r.length > 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