From fb80e820097900c93d348a18a151a0c98cd14e53 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Tue, 19 Aug 2008 07:44:57 +0000 Subject: [PATCH] Fix for bug 45640 - avoid creating multiple GUTS records git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@686977 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hssf/model/Sheet.java | 57 +++++++------------ .../org/apache/poi/hssf/model/TestSheet.java | 31 +++++++++- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 710d57118..336b1dbd4 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -106,7 +106,7 @@ public final class Sheet implements Model { protected ArrayList records = null; int preoffset = 0; // offset of the sheet in a new file - protected int dimsloc = -1; // TODO - is it legal for dims record to be missing? + protected int dimsloc = -1; // TODO - remove dimsloc protected PrintGridlinesRecord printGridlines = null; protected GridsetRecord gridset = null; private GutsRecord _gutsRecord; @@ -125,7 +125,8 @@ public final class Sheet implements Model { private MergedCellsTable _mergedCellsTable; /** always present in this POI object, not always written to Excel file */ /*package*/ColumnInfoRecordsAggregate _columnInfos; - protected DimensionsRecord dims; + /** the DimensionsRecord is always present */ + private DimensionsRecord _dimensions; protected RowRecordsAggregate _rowsAggregate = null; private DataValidityTable _dataValidityTable= null; private ConditionalFormattingTable condFormatting; @@ -287,7 +288,7 @@ public final class Sheet implements Model { records.add(retval._columnInfos); } - retval.dims = ( DimensionsRecord ) rec; + retval._dimensions = ( DimensionsRecord ) rec; retval.dimsloc = records.size(); } else if (rec.getSid() == DefaultColWidthRecord.sid) @@ -333,7 +334,7 @@ public final class Sheet implements Model { records.add(rec); } - if (retval.dimsloc < 0) { + if (retval._dimensions == null) { throw new RuntimeException("DimensionsRecord was not found"); } retval.records = records; @@ -404,6 +405,8 @@ public final class Sheet implements Model { public static Sheet createSheet() { + // TODO - convert this method to a constructor + if (log.check( POILogger.DEBUG )) log.log(POILogger.DEBUG, "Sheet createsheet from scratch called"); Sheet retval = new Sheet(); @@ -423,7 +426,8 @@ public final class Sheet implements Model { records.add( retval.printGridlines ); retval.gridset = createGridset(); records.add( retval.gridset ); - records.add( retval.createGuts() ); + retval._gutsRecord = createGuts(); + records.add( retval._gutsRecord ); retval.defaultrowheight = createDefaultRowHeight(); records.add( retval.defaultrowheight ); records.add( retval.createWSBool() ); @@ -440,8 +444,8 @@ public final class Sheet implements Model { ColumnInfoRecordsAggregate columns = new ColumnInfoRecordsAggregate(); records.add( columns ); retval._columnInfos = columns; - retval.dims = createDimensions(); - records.add(retval.dims); + retval._dimensions = createDimensions(); + records.add(retval._dimensions); retval.dimsloc = records.size()-1; records.add(retval.windowTwo = retval.createWindowTwo()); retval.selection = createSelection(); @@ -460,7 +464,7 @@ public final class Sheet implements Model { if (_rowsAggregate == null) { _rowsAggregate = new RowRecordsAggregate(); - records.add(getDimsLoc() + 1, _rowsAggregate); + records.add(dimsloc + 1, _rowsAggregate); } } private MergedCellsTable getMergedRecords() { @@ -556,10 +560,10 @@ public final class Sheet implements Model { .append(lastrow).append("lastcol").append(lastcol) .toString()); } - dims.setFirstCol(firstcol); - dims.setFirstRow(firstrow); - dims.setLastCol(lastcol); - dims.setLastRow(lastrow); + _dimensions.setFirstCol(firstcol); + _dimensions.setFirstRow(firstrow); + _dimensions.setLastCol(lastcol); + _dimensions.setLastRow(lastrow); if (log.check( POILogger.DEBUG )) log.log(POILogger.DEBUG, "Sheet.setDimensions exiting"); } @@ -696,7 +700,7 @@ public final class Sheet implements Model { if(log.check(POILogger.DEBUG)) { log.log(POILogger.DEBUG, "add value record row" + row); } - DimensionsRecord d = ( DimensionsRecord ) records.get(getDimsLoc()); + DimensionsRecord d = _dimensions; if (col.getColumn() > d.getLastCol()) { @@ -720,8 +724,8 @@ public final class Sheet implements Model { */ public void removeValueRecord(int row, CellValueRecordInterface col) { - log.logFormatted(POILogger.DEBUG, "remove value record row,dimsloc %,%", - new int[]{row, dimsloc} ); + log.logFormatted(POILogger.DEBUG, "remove value record row %", + new int[]{row } ); _rowsAggregate.removeCell(col); } @@ -766,7 +770,7 @@ public final class Sheet implements Model { checkRows(); if (log.check( POILogger.DEBUG )) log.log(POILogger.DEBUG, "addRow "); - DimensionsRecord d = ( DimensionsRecord ) records.get(getDimsLoc()); + DimensionsRecord d = _dimensions; if (row.getRowNumber() >= d.getLastRow()) { @@ -1330,27 +1334,6 @@ public final class Sheet implements Model { } } - /** - * get the location of the DimensionsRecord (which is the last record before the value section) - * @return location in the array of records of the DimensionsRecord - */ - - public int getDimsLoc() - { - if (log.check( POILogger.DEBUG )) - log.log(POILogger.DEBUG, "getDimsLoc dimsloc= " + dimsloc); - return dimsloc; - } - - /** - * in the event the record is a dimensions record, resets both the loc index and dimsloc index - */ - public void checkDimsLoc(Record rec, int recloc) { - if (rec.getSid() == DimensionsRecord.sid) { - dimsloc = recloc; - } - } - /** * @return the serialized size of this sheet */ diff --git a/src/testcases/org/apache/poi/hssf/model/TestSheet.java b/src/testcases/org/apache/poi/hssf/model/TestSheet.java index 0b245c0a7..6c6dd9fb2 100644 --- a/src/testcases/org/apache/poi/hssf/model/TestSheet.java +++ b/src/testcases/org/apache/poi/hssf/model/TestSheet.java @@ -32,6 +32,7 @@ import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.ColumnInfoRecord; import org.apache.poi.hssf.record.DimensionsRecord; import org.apache.poi.hssf.record.EOFRecord; +import org.apache.poi.hssf.record.GutsRecord; import org.apache.poi.hssf.record.IndexRecord; import org.apache.poi.hssf.record.MergeCellsRecord; import org.apache.poi.hssf.record.Record; @@ -41,6 +42,8 @@ import org.apache.poi.hssf.record.UncalcedRecord; import org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate; import org.apache.poi.hssf.record.aggregates.PageSettingsBlock; import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddress; /** @@ -438,8 +441,8 @@ public final class TestSheet extends TestCase { if (false) { // make sure that RRA and VRA are in the right place // (Aug 2008) since the VRA is now part of the RRA, there is much less chance that - // they could get out of order. Still, one could write serialize the sheet here, - // and read back with EventRecordFactory to make sure... + // they could get out of order. Still, one could write serialize the sheet here, + // and read back with EventRecordFactory to make sure... } assertEquals(242, dbCellRecordPos); } @@ -475,5 +478,29 @@ public final class TestSheet extends TestCase { return _indexRecord; } } + + /** + * Checks for bug introduced around r682282-r683880 that caused a second GUTS records + * which in turn got the dimensions record out of alignment + */ + public void testGutsRecord_bug45640() { + + Sheet sheet = Sheet.createSheet(); + sheet.addRow(new RowRecord(0)); + sheet.addRow(new RowRecord(1)); + sheet.groupRowRange( 0, 1, true ); + sheet.toString(); + List recs = sheet.getRecords(); + int count=0; + for(int i=0; i< recs.size(); i++) { + if (recs.get(i) instanceof GutsRecord) { + count++; + } + } + if (count == 2) { + throw new AssertionFailedError("Identified bug 45640"); + } + assertEquals(1, count); + } }