Search in sources :

Example 11 with RecordBase

use of org.apache.poi.hssf.record.RecordBase in project poi by apache.

the class TestDrawingAggregate method testSerializeDrawingWithComments.

@Test
public void testSerializeDrawingWithComments() throws IOException {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("DrawingAndComments.xls");
    HSSFSheet sh = wb.getSheetAt(0);
    InternalWorkbook iworkbook = HSSFTestHelper.getWorkbookForTest(wb);
    InternalSheet isheet = HSSFTestHelper.getSheetForTest(sh);
    List<RecordBase> records = isheet.getRecords();
    // the sheet's drawing is not aggregated
    assertEquals("wrong size of sheet records stream", 46, records.size());
    // the last record before the drawing block
    assertTrue("records.get(18) is expected to be RowRecordsAggregate but was " + records.get(18).getClass().getSimpleName(), records.get(18) instanceof RowRecordsAggregate);
    // records to be aggregated
    List<RecordBase> dgRecords = records.subList(19, 39);
    for (RecordBase rb : dgRecords) {
        Record r = (Record) rb;
        short sid = r.getSid();
        // we expect that drawing block consists of either
        // DrawingRecord or ContinueRecord or ObjRecord or TextObjectRecord
        assertTrue(sid == DrawingRecord.sid || sid == ContinueRecord.sid || sid == ObjRecord.sid || sid == NoteRecord.sid || sid == TextObjectRecord.sid);
    }
    // collect drawing records into a byte buffer.
    byte[] dgBytes = toByteArray(dgRecords);
    // the first record after the drawing block
    assertTrue("records.get(39) is expected to be Window2", records.get(39) instanceof WindowTwoRecord);
    // aggregate drawing records.
    // The subrange [19, 38] is expected to be replaced with a EscherAggregate object
    DrawingManager2 drawingManager = iworkbook.findDrawingGroup();
    int loc = isheet.aggregateDrawingRecords(drawingManager, false);
    EscherAggregate agg = (EscherAggregate) records.get(loc);
    assertEquals("wrong size of the aggregated sheet records stream", 27, records.size());
    assertTrue("records.get(18) is expected to be RowRecordsAggregate but was " + records.get(18).getClass().getSimpleName(), records.get(18) instanceof RowRecordsAggregate);
    assertTrue("records.get(19) is expected to be EscherAggregate but was " + records.get(19).getClass().getSimpleName(), records.get(19) instanceof EscherAggregate);
    assertTrue("records.get(20) is expected to be Window2 but was " + records.get(20).getClass().getSimpleName(), records.get(20) instanceof WindowTwoRecord);
    byte[] dgBytesAfterSave = agg.serialize();
    assertEquals("different size of drawing data before and after save", dgBytes.length, dgBytesAfterSave.length);
    assertTrue("drawing data before and after save is different", Arrays.equals(dgBytes, dgBytesAfterSave));
    wb.close();
}
Also used : WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) RowRecordsAggregate(org.apache.poi.hssf.record.aggregates.RowRecordsAggregate) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) RecordBase(org.apache.poi.hssf.record.RecordBase) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) EscherRecord(org.apache.poi.ddf.EscherRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) Record(org.apache.poi.hssf.record.Record) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) Test(org.junit.Test)

Example 12 with RecordBase

use of org.apache.poi.hssf.record.RecordBase in project poi by apache.

the class TestDrawingAggregate method testFileWithPictures.

@Test
public void testFileWithPictures() throws IOException {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ContinueRecordProblem.xls");
    HSSFSheet sh = wb.getSheetAt(0);
    InternalWorkbook iworkbook = HSSFTestHelper.getWorkbookForTest(wb);
    InternalSheet isheet = HSSFTestHelper.getSheetForTest(sh);
    List<RecordBase> records = isheet.getRecords();
    // the sheet's drawing is not aggregated
    assertEquals("wrong size of sheet records stream", 315, records.size());
    // the last record before the drawing block
    assertTrue("records.get(21) is expected to be RowRecordsAggregate but was " + records.get(21).getClass().getSimpleName(), records.get(21) instanceof RowRecordsAggregate);
    // records to be aggregated
    List<RecordBase> dgRecords = records.subList(22, 300);
    for (RecordBase rb : dgRecords) {
        Record r = (Record) rb;
        short sid = r.getSid();
        // we expect that drawing block consists of either
        // DrawingRecord or ContinueRecord or ObjRecord or TextObjectRecord
        assertTrue(sid == DrawingRecord.sid || sid == ContinueRecord.sid || sid == ObjRecord.sid || sid == TextObjectRecord.sid);
    }
    // collect drawing records into a byte buffer.
    byte[] dgBytes = toByteArray(dgRecords);
    // the first record after the drawing block
    assertTrue("records.get(300) is expected to be Window2", records.get(300) instanceof WindowTwoRecord);
    // aggregate drawing records.
    // The subrange [19, 299] is expected to be replaced with a EscherAggregate object
    DrawingManager2 drawingManager = iworkbook.findDrawingGroup();
    int loc = isheet.aggregateDrawingRecords(drawingManager, false);
    EscherAggregate agg = (EscherAggregate) records.get(loc);
    assertEquals("wrong size of the aggregated sheet records stream", 38, records.size());
    assertTrue("records.get(21) is expected to be RowRecordsAggregate but was " + records.get(21).getClass().getSimpleName(), records.get(21) instanceof RowRecordsAggregate);
    assertTrue("records.get(22) is expected to be EscherAggregate but was " + records.get(22).getClass().getSimpleName(), records.get(22) instanceof EscherAggregate);
    assertTrue("records.get(23) is expected to be Window2 but was " + records.get(23).getClass().getSimpleName(), records.get(23) instanceof WindowTwoRecord);
    byte[] dgBytesAfterSave = agg.serialize();
    assertEquals("different size of drawing data before and after save", dgBytes.length, dgBytesAfterSave.length);
    assertTrue("drawing data before and after save is different", Arrays.equals(dgBytes, dgBytesAfterSave));
    wb.close();
}
Also used : WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) RowRecordsAggregate(org.apache.poi.hssf.record.aggregates.RowRecordsAggregate) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) RecordBase(org.apache.poi.hssf.record.RecordBase) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) EscherRecord(org.apache.poi.ddf.EscherRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) Record(org.apache.poi.hssf.record.Record) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) Test(org.junit.Test)

Example 13 with RecordBase

use of org.apache.poi.hssf.record.RecordBase in project poi by apache.

the class InternalSheet method getNoteRecords.

/**
     * Get the {@link NoteRecord}s (related to cell comments) for this sheet
     * @return never <code>null</code>, typically empty array
     */
public NoteRecord[] getNoteRecords() {
    List<NoteRecord> temp = new ArrayList<NoteRecord>();
    for (int i = _records.size() - 1; i >= 0; i--) {
        RecordBase rec = _records.get(i);
        if (rec instanceof NoteRecord) {
            temp.add((NoteRecord) rec);
        }
    }
    if (temp.size() < 1) {
        return NoteRecord.EMPTY_ARRAY;
    }
    NoteRecord[] result = new NoteRecord[temp.size()];
    temp.toArray(result);
    return result;
}
Also used : RecordBase(org.apache.poi.hssf.record.RecordBase) NoteRecord(org.apache.poi.hssf.record.NoteRecord) ArrayList(java.util.ArrayList)

Example 14 with RecordBase

use of org.apache.poi.hssf.record.RecordBase in project poi by apache.

the class InternalSheet method getSizeOfInitialSheetRecords.

/**
     * 'initial sheet records' are between INDEX and the 'Row Blocks'
     * @param bofRecordIndex index of record after which INDEX record is to be placed
     * @return count of bytes from end of INDEX record to first ROW record.
     */
private int getSizeOfInitialSheetRecords(int bofRecordIndex) {
    int result = 0;
    // start just after BOF record (INDEX is not present in this list)
    for (int j = bofRecordIndex + 1; j < _records.size(); j++) {
        RecordBase tmpRec = _records.get(j);
        if (tmpRec instanceof RowRecordsAggregate) {
            break;
        }
        result += tmpRec.getRecordSize();
    }
    if (_isUncalced) {
        result += UncalcedRecord.getStaticRecordSize();
    }
    return result;
}
Also used : RecordBase(org.apache.poi.hssf.record.RecordBase) RowRecordsAggregate(org.apache.poi.hssf.record.aggregates.RowRecordsAggregate)

Example 15 with RecordBase

use of org.apache.poi.hssf.record.RecordBase in project poi by apache.

the class InternalSheet method visitContainedRecords.

public void visitContainedRecords(RecordVisitor rv, int offset) {
    PositionTrackingVisitor ptv = new PositionTrackingVisitor(rv, offset);
    boolean haveSerializedIndex = false;
    for (int k = 0; k < _records.size(); k++) {
        RecordBase record = _records.get(k);
        if (record instanceof RecordAggregate) {
            RecordAggregate agg = (RecordAggregate) record;
            agg.visitContainedRecords(ptv);
        } else {
            ptv.visitRecord((Record) record);
        }
        // If the BOF record was just serialized then add the IndexRecord
        if (record instanceof BOFRecord) {
            if (!haveSerializedIndex) {
                haveSerializedIndex = true;
                //  and one shouldn't go in after that!
                if (_isUncalced) {
                    ptv.visitRecord(new UncalcedRecord());
                }
                //remove this guard. So be safe it is left here.
                if (_rowsAggregate != null) {
                    // find forward distance to first RowRecord
                    int initRecsSize = getSizeOfInitialSheetRecords(k);
                    int currentPos = ptv.getPosition();
                    ptv.visitRecord(_rowsAggregate.createIndexRecord(currentPos, initRecsSize));
                }
            }
        }
    }
}
Also used : PositionTrackingVisitor(org.apache.poi.hssf.record.aggregates.RecordAggregate.PositionTrackingVisitor) UncalcedRecord(org.apache.poi.hssf.record.UncalcedRecord) RecordBase(org.apache.poi.hssf.record.RecordBase) CustomViewSettingsRecordAggregate(org.apache.poi.hssf.record.aggregates.CustomViewSettingsRecordAggregate) RecordAggregate(org.apache.poi.hssf.record.aggregates.RecordAggregate) ChartSubstreamRecordAggregate(org.apache.poi.hssf.record.aggregates.ChartSubstreamRecordAggregate) BOFRecord(org.apache.poi.hssf.record.BOFRecord)

Aggregations

RecordBase (org.apache.poi.hssf.record.RecordBase)29 Test (org.junit.Test)14 EOFRecord (org.apache.poi.hssf.record.EOFRecord)12 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)12 Record (org.apache.poi.hssf.record.Record)12 DrawingRecord (org.apache.poi.hssf.record.DrawingRecord)9 NoteRecord (org.apache.poi.hssf.record.NoteRecord)9 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)9 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)9 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)8 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)8 ObjRecord (org.apache.poi.hssf.record.ObjRecord)8 TextObjectRecord (org.apache.poi.hssf.record.TextObjectRecord)8 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)8 EscherRecord (org.apache.poi.ddf.EscherRecord)7 ContinueRecord (org.apache.poi.hssf.record.ContinueRecord)7 BOFRecord (org.apache.poi.hssf.record.BOFRecord)6 ArrayList (java.util.ArrayList)5 RowRecordsAggregate (org.apache.poi.hssf.record.aggregates.RowRecordsAggregate)5 DimensionsRecord (org.apache.poi.hssf.record.DimensionsRecord)4