diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index aad645049d..11ee42b6a9 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -77,7 +77,6 @@ import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate; import org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate; import org.apache.poi.hssf.record.aggregates.ConditionalFormattingTable; import org.apache.poi.hssf.record.aggregates.DataValidityTable; -import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate; import org.apache.poi.hssf.record.aggregates.MergedCellsTable; import org.apache.poi.hssf.record.aggregates.RecordAggregate; import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate; @@ -389,6 +388,7 @@ public final class Sheet implements Model { _destList.add(r.clone()); } } + /** * Clones the low level records of this sheet and returns the new sheet instance. * This method is implemented by adding methods for deep cloning to all records that @@ -396,53 +396,18 @@ public final class Sheet implements Model { * When adding a new record, implement a public clone method if and only if the record * belongs to a sheet. */ - public Sheet cloneSheet() - { - ArrayList clonedRecords = new ArrayList(this.records.size()); - for (int i=0; inull + */ void visitRecord(Record r); } diff --git a/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java index 91028e3600..c9a302b614 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java @@ -27,8 +27,6 @@ import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.DBCellRecord; import org.apache.poi.hssf.record.IndexRecord; import org.apache.poi.hssf.record.Record; -import org.apache.poi.hssf.record.RecordBase; -import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.record.RowRecord; /** @@ -36,7 +34,7 @@ import org.apache.poi.hssf.record.RowRecord; * @author andy * @author Jason Height (jheight at chariot dot net dot au) */ -public final class RowRecordsAggregate extends Record { +public final class RowRecordsAggregate extends RecordAggregate { private int _firstrow = -1; private int _lastrow = -1; private final Map _rowRecords; @@ -162,128 +160,64 @@ public final class RowRecordsAggregate extends Record { } return row.getRowNumber(); } - - - /** Serializes a block of the rows */ - private int serializeRowBlock(final int block, final int offset, byte[] data) { - final int startIndex = block*DBCellRecord.BLOCK_SIZE; - final int endIndex = startIndex + DBCellRecord.BLOCK_SIZE; - - Iterator rowIterator = _rowRecords.values().iterator(); - int pos = offset; - - //Given that we basically iterate through the rows in order, - //For a performance improvement, it would be better to return an instance of - //an iterator and use that instance throughout, rather than recreating one and - //having to move it to the right position. - int i=0; - for (;i= records.length) { - throw new RuntimeException("cell row is out of range"); + throw new RuntimeException("cell row is out of range"); } CellValueRecordInterface[] rowCells = records[row]; if (rowCells == null) { - throw new RuntimeException("cell row is already empty"); + throw new RuntimeException("cell row is already empty"); } short column = cell.getColumn(); if (column >= rowCells.length) { - throw new RuntimeException("cell column is out of range"); + throw new RuntimeException("cell column is out of range"); } rowCells[column] = null; } @@ -266,6 +267,35 @@ public final class ValueRecordsAggregate { } return pos - offset; } + + public int visitCellsForRow(int rowIndex, RecordVisitor rv) { + int result = 0; + CellValueRecordInterface[] cellRecs = records[rowIndex]; + if (cellRecs != null) { + for (int i = 0; i < cellRecs.length; i++) { + CellValueRecordInterface cvr = cellRecs[i]; + if (cvr == null) { + continue; + } + if (cvr instanceof FormulaRecordAggregate) { + FormulaRecordAggregate fmAgg = (FormulaRecordAggregate) cvr; + Record fmAggRec = fmAgg.getFormulaRecord(); + rv.visitRecord(fmAggRec); + result += fmAggRec.getRecordSize(); + fmAggRec = fmAgg.getStringRecord(); + if (fmAggRec != null) { + rv.visitRecord(fmAggRec); + result += fmAggRec.getRecordSize(); + } + } else { + Record rec = (Record) cvr; + rv.visitRecord(rec); + result += rec.getRecordSize(); + } + } + } + return result; + } public CellValueRecordInterface[] getValueRecords() { List temp = new ArrayList(); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 13f46255c6..5966c0d80f 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -256,21 +256,11 @@ public class HSSFWorkbook extends POIDocument // convert all LabelRecord records to LabelSSTRecord convertLabelRecords(records, recOffset); - while (recOffset < records.size()) - { + while (recOffset < records.size()) { Sheet sheet = Sheet.createSheet(records, sheetNum++, recOffset ); - recOffset = sheet.getEofLoc()+1; - if (recOffset == 1) - { - break; - } - - HSSFSheet hsheet = new HSSFSheet(this, sheet); - - _sheets.add(hsheet); - - // workbook.setSheetName(sheets.size() -1, "Sheet"+sheets.size()); + recOffset = sheet.getEofLoc()+1; // TODO - use better technique to keep track of the used records + _sheets.add(new HSSFSheet(this, sheet)); } for (int i = 0 ; i < workbook.getNumNames() ; ++i){ diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java index 09b93cf870..ebbe6cbc00 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java @@ -17,6 +17,7 @@ package org.apache.poi.hssf.usermodel; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -26,6 +27,8 @@ import java.util.Iterator; import junit.framework.TestCase; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.hssf.eventmodel.ERFListener; +import org.apache.poi.hssf.eventmodel.EventRecordFactory; import org.apache.poi.hssf.model.Workbook; import org.apache.poi.hssf.record.BackupRecord; import org.apache.poi.hssf.record.LabelSSTRecord; @@ -453,13 +456,13 @@ public final class TestWorkbook extends TestCase { } private static void confirmRegion(CellRangeAddress ra, CellRangeAddress rb) { - assertEquals(ra.getFirstRow(), rb.getFirstRow()); - assertEquals(ra.getLastRow(), rb.getLastRow()); - assertEquals(ra.getFirstColumn(), rb.getFirstColumn()); - assertEquals(ra.getLastColumn(), rb.getLastColumn()); - } + assertEquals(ra.getFirstRow(), rb.getFirstRow()); + assertEquals(ra.getLastRow(), rb.getLastRow()); + assertEquals(ra.getFirstColumn(), rb.getFirstColumn()); + assertEquals(ra.getLastColumn(), rb.getLastColumn()); + } - /** + /** * Test the backup field gets set as expected. */ @@ -476,38 +479,44 @@ public final class TestWorkbook extends TestCase { assertEquals(1, record.getBackup()); } + private static final class RecordCounter implements ERFListener { + private int _count; + + public RecordCounter() { + _count=0; + } + public int getCount() { + return _count; + } + public boolean processRecord(Record rec) { + _count++; + return true; + } + } + /** * This tests is for bug [ #506658 ] Repeating output. * * We need to make sure only one LabelSSTRecord is produced. */ - public void testRepeatingBug() throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Design Variants"); - HSSFRow row = sheet.createRow(( short ) 2); - HSSFCell cell = row.createCell(( short ) 1); + HSSFRow row = sheet.createRow(2); + HSSFCell cell = row.createCell(1); cell.setCellValue(new HSSFRichTextString("Class")); - cell = row.createCell(( short ) 2); + cell = row.createCell(2); - // workbook.write(new FileOutputStream("/a2.xls")); - RowRecordsAggregate rra = (RowRecordsAggregate) - sheet.getSheet().findFirstRecordBySid((short)-1000); + byte[] data = new byte[sheet.getSheet().getSize()]; + sheet.getSheet().serialize(0, data); + RecordCounter rc = new RecordCounter(); + EventRecordFactory erf = new EventRecordFactory(rc, new short[] { LabelSSTRecord.sid, }); + erf.processRecords(new ByteArrayInputStream(data)); - int sstRecords = 0; - Iterator iterator = rra.getAllRecordsIterator(); - - while (iterator.hasNext()) - { - if ((( Record ) iterator.next()).getSid() == LabelSSTRecord.sid) - { - sstRecords++; - } - } - assertEquals(1, sstRecords); + assertEquals(1, rc.getCount()); }