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
This commit is contained in:
Josh Micich 2008-08-19 07:44:57 +00:00
parent 86513f0dbe
commit fb80e82009
2 changed files with 49 additions and 39 deletions

View File

@ -106,7 +106,7 @@ public final class Sheet implements Model {
protected ArrayList records = null; protected ArrayList records = null;
int preoffset = 0; // offset of the sheet in a new file 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 PrintGridlinesRecord printGridlines = null;
protected GridsetRecord gridset = null; protected GridsetRecord gridset = null;
private GutsRecord _gutsRecord; private GutsRecord _gutsRecord;
@ -125,7 +125,8 @@ public final class Sheet implements Model {
private MergedCellsTable _mergedCellsTable; private MergedCellsTable _mergedCellsTable;
/** always present in this POI object, not always written to Excel file */ /** always present in this POI object, not always written to Excel file */
/*package*/ColumnInfoRecordsAggregate _columnInfos; /*package*/ColumnInfoRecordsAggregate _columnInfos;
protected DimensionsRecord dims; /** the DimensionsRecord is always present */
private DimensionsRecord _dimensions;
protected RowRecordsAggregate _rowsAggregate = null; protected RowRecordsAggregate _rowsAggregate = null;
private DataValidityTable _dataValidityTable= null; private DataValidityTable _dataValidityTable= null;
private ConditionalFormattingTable condFormatting; private ConditionalFormattingTable condFormatting;
@ -287,7 +288,7 @@ public final class Sheet implements Model {
records.add(retval._columnInfos); records.add(retval._columnInfos);
} }
retval.dims = ( DimensionsRecord ) rec; retval._dimensions = ( DimensionsRecord ) rec;
retval.dimsloc = records.size(); retval.dimsloc = records.size();
} }
else if (rec.getSid() == DefaultColWidthRecord.sid) else if (rec.getSid() == DefaultColWidthRecord.sid)
@ -333,7 +334,7 @@ public final class Sheet implements Model {
records.add(rec); records.add(rec);
} }
if (retval.dimsloc < 0) { if (retval._dimensions == null) {
throw new RuntimeException("DimensionsRecord was not found"); throw new RuntimeException("DimensionsRecord was not found");
} }
retval.records = records; retval.records = records;
@ -404,6 +405,8 @@ public final class Sheet implements Model {
public static Sheet createSheet() public static Sheet createSheet()
{ {
// TODO - convert this method to a constructor
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG ))
log.log(POILogger.DEBUG, "Sheet createsheet from scratch called"); log.log(POILogger.DEBUG, "Sheet createsheet from scratch called");
Sheet retval = new Sheet(); Sheet retval = new Sheet();
@ -423,7 +426,8 @@ public final class Sheet implements Model {
records.add( retval.printGridlines ); records.add( retval.printGridlines );
retval.gridset = createGridset(); retval.gridset = createGridset();
records.add( retval.gridset ); records.add( retval.gridset );
records.add( retval.createGuts() ); retval._gutsRecord = createGuts();
records.add( retval._gutsRecord );
retval.defaultrowheight = createDefaultRowHeight(); retval.defaultrowheight = createDefaultRowHeight();
records.add( retval.defaultrowheight ); records.add( retval.defaultrowheight );
records.add( retval.createWSBool() ); records.add( retval.createWSBool() );
@ -440,8 +444,8 @@ public final class Sheet implements Model {
ColumnInfoRecordsAggregate columns = new ColumnInfoRecordsAggregate(); ColumnInfoRecordsAggregate columns = new ColumnInfoRecordsAggregate();
records.add( columns ); records.add( columns );
retval._columnInfos = columns; retval._columnInfos = columns;
retval.dims = createDimensions(); retval._dimensions = createDimensions();
records.add(retval.dims); records.add(retval._dimensions);
retval.dimsloc = records.size()-1; retval.dimsloc = records.size()-1;
records.add(retval.windowTwo = retval.createWindowTwo()); records.add(retval.windowTwo = retval.createWindowTwo());
retval.selection = createSelection(); retval.selection = createSelection();
@ -460,7 +464,7 @@ public final class Sheet implements Model {
if (_rowsAggregate == null) if (_rowsAggregate == null)
{ {
_rowsAggregate = new RowRecordsAggregate(); _rowsAggregate = new RowRecordsAggregate();
records.add(getDimsLoc() + 1, _rowsAggregate); records.add(dimsloc + 1, _rowsAggregate);
} }
} }
private MergedCellsTable getMergedRecords() { private MergedCellsTable getMergedRecords() {
@ -556,10 +560,10 @@ public final class Sheet implements Model {
.append(lastrow).append("lastcol").append(lastcol) .append(lastrow).append("lastcol").append(lastcol)
.toString()); .toString());
} }
dims.setFirstCol(firstcol); _dimensions.setFirstCol(firstcol);
dims.setFirstRow(firstrow); _dimensions.setFirstRow(firstrow);
dims.setLastCol(lastcol); _dimensions.setLastCol(lastcol);
dims.setLastRow(lastrow); _dimensions.setLastRow(lastrow);
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG ))
log.log(POILogger.DEBUG, "Sheet.setDimensions exiting"); log.log(POILogger.DEBUG, "Sheet.setDimensions exiting");
} }
@ -696,7 +700,7 @@ public final class Sheet implements Model {
if(log.check(POILogger.DEBUG)) { if(log.check(POILogger.DEBUG)) {
log.log(POILogger.DEBUG, "add value record row" + row); log.log(POILogger.DEBUG, "add value record row" + row);
} }
DimensionsRecord d = ( DimensionsRecord ) records.get(getDimsLoc()); DimensionsRecord d = _dimensions;
if (col.getColumn() > d.getLastCol()) if (col.getColumn() > d.getLastCol())
{ {
@ -720,8 +724,8 @@ public final class Sheet implements Model {
*/ */
public void removeValueRecord(int row, CellValueRecordInterface col) { public void removeValueRecord(int row, CellValueRecordInterface col) {
log.logFormatted(POILogger.DEBUG, "remove value record row,dimsloc %,%", log.logFormatted(POILogger.DEBUG, "remove value record row %",
new int[]{row, dimsloc} ); new int[]{row } );
_rowsAggregate.removeCell(col); _rowsAggregate.removeCell(col);
} }
@ -766,7 +770,7 @@ public final class Sheet implements Model {
checkRows(); checkRows();
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG ))
log.log(POILogger.DEBUG, "addRow "); log.log(POILogger.DEBUG, "addRow ");
DimensionsRecord d = ( DimensionsRecord ) records.get(getDimsLoc()); DimensionsRecord d = _dimensions;
if (row.getRowNumber() >= d.getLastRow()) 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 * @return the serialized size of this sheet
*/ */

View File

@ -32,6 +32,7 @@ import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.ColumnInfoRecord; import org.apache.poi.hssf.record.ColumnInfoRecord;
import org.apache.poi.hssf.record.DimensionsRecord; import org.apache.poi.hssf.record.DimensionsRecord;
import org.apache.poi.hssf.record.EOFRecord; 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.IndexRecord;
import org.apache.poi.hssf.record.MergeCellsRecord; import org.apache.poi.hssf.record.MergeCellsRecord;
import org.apache.poi.hssf.record.Record; 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.ColumnInfoRecordsAggregate;
import org.apache.poi.hssf.record.aggregates.PageSettingsBlock; import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate; 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; import org.apache.poi.hssf.util.CellRangeAddress;
/** /**
@ -438,8 +441,8 @@ public final class TestSheet extends TestCase {
if (false) { if (false) {
// make sure that RRA and VRA are in the right place // 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 // (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, // they could get out of order. Still, one could write serialize the sheet here,
// and read back with EventRecordFactory to make sure... // and read back with EventRecordFactory to make sure...
} }
assertEquals(242, dbCellRecordPos); assertEquals(242, dbCellRecordPos);
} }
@ -475,5 +478,29 @@ public final class TestSheet extends TestCase {
return _indexRecord; 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);
}
} }