Search in sources :

Example 1 with DrawingRecord

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

the class TestSheet method testCreateAggregate.

@Test
public void testCreateAggregate() {
    String msoDrawingRecord1 = "0F 00 02 F0 20 01 00 00 10 00 08 F0 08 00 00 00 \n" + "03 00 00 00 02 04 00 00 0F 00 03 F0 08 01 00 00 \n" + "0F 00 04 F0 28 00 00 00 01 00 09 F0 10 00 00 00 \n" + "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \n" + "02 00 0A F0 08 00 00 00 00 04 00 00 05 00 00 00 \n" + "0F 00 04 F0 64 00 00 00 42 01 0A F0 08 00 00 00 \n" + "01 04 00 00 00 0A 00 00 73 00 0B F0 2A 00 00 00 \n" + "BF 00 08 00 08 00 44 01 04 00 00 00 7F 01 00 00 \n" + "01 00 BF 01 00 00 11 00 C0 01 40 00 00 08 FF 01 \n" + "10 00 10 00 BF 03 00 00 08 00 00 00 10 F0 12 00 \n" + "00 00 00 00 01 00 54 00 05 00 45 00 01 00 88 03 \n" + "05 00 94 00 00 00 11 F0 00 00 00 00";
    String msoDrawingRecord2 = "0F 00 04 F0 64 00 00 00 42 01 0A F0 08 00 00 00 " + "02 04 00 00 80 0A 00 00 73 00 0B F0 2A 00 00 00 " + "BF 00 08 00 08 00 44 01 04 00 00 00 7F 01 00 00 " + "01 00 BF 01 00 00 11 00 C0 01 40 00 00 08 FF 01 " + "10 00 10 00 BF 03 00 00 08 00 00 00 10 F0 12 00 " + "00 00 00 00 01 00 8D 03 05 00 E4 00 03 00 4D 03 " + "0B 00 0C 00 00 00 11 F0 00 00 00 00";
    DrawingRecord d1 = new DrawingRecord();
    d1.setData(HexRead.readFromString(msoDrawingRecord1));
    ObjRecord r1 = new ObjRecord();
    DrawingRecord d2 = new DrawingRecord();
    d2.setData(HexRead.readFromString(msoDrawingRecord2));
    TextObjectRecord r2 = new TextObjectRecord();
    r2.setStr(new HSSFRichTextString("Aggregated"));
    NoteRecord n2 = new NoteRecord();
    List<Record> recordStream = new ArrayList<Record>();
    recordStream.add(InternalSheet.createBOF());
    recordStream.add(d1);
    recordStream.add(r1);
    recordStream.add(createWindow2Record());
    recordStream.add(EOFRecord.instance);
    confirmAggregatedRecords(recordStream);
    recordStream = new ArrayList<Record>();
    recordStream.add(InternalSheet.createBOF());
    recordStream.add(d1);
    recordStream.add(r1);
    recordStream.add(d2);
    recordStream.add(r2);
    recordStream.add(createWindow2Record());
    recordStream.add(EOFRecord.instance);
    confirmAggregatedRecords(recordStream);
    recordStream = new ArrayList<Record>();
    recordStream.add(InternalSheet.createBOF());
    recordStream.add(d1);
    recordStream.add(r1);
    recordStream.add(d2);
    recordStream.add(r2);
    recordStream.add(n2);
    recordStream.add(createWindow2Record());
    recordStream.add(EOFRecord.instance);
    confirmAggregatedRecords(recordStream);
}
Also used : TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) NoteRecord(org.apache.poi.hssf.record.NoteRecord) ArrayList(java.util.ArrayList) ObjRecord(org.apache.poi.hssf.record.ObjRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) GutsRecord(org.apache.poi.hssf.record.GutsRecord) MergeCellsRecord(org.apache.poi.hssf.record.MergeCellsRecord) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) Record(org.apache.poi.hssf.record.Record) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) ColumnInfoRecord(org.apache.poi.hssf.record.ColumnInfoRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) IndexRecord(org.apache.poi.hssf.record.IndexRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) UncalcedRecord(org.apache.poi.hssf.record.UncalcedRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) Test(org.junit.Test)

Example 2 with DrawingRecord

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

the class TestDrawingAggregate method testSerializeDrawingBigger8k.

/**
     * Try to check file with such record sequence
     * ...
     * DrawingRecord
     * ContinueRecord
     * ObjRecord | TextObjRecord
     * ...
     */
@Test
public void testSerializeDrawingBigger8k() throws IOException {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("DrawingContinue.xls");
    InternalWorkbook iworkbook = HSSFTestHelper.getWorkbookForTest(wb);
    HSSFSheet sh = wb.getSheetAt(0);
    InternalSheet isheet = HSSFTestHelper.getSheetForTest(sh);
    List<RecordBase> records = isheet.getRecords();
    // the sheet's drawing is not aggregated
    assertEquals("wrong size of sheet records stream", 32, 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, 26);
    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(26) is expected to be Window2", records.get(26) 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", 26, 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 3 with DrawingRecord

use of org.apache.poi.hssf.record.DrawingRecord 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 4 with DrawingRecord

use of org.apache.poi.hssf.record.DrawingRecord 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 5 with DrawingRecord

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

the class InternalSheet method cloneSheet.

/**
     * 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
     * can be added to a sheet. The <b>Record</b> object does not implement cloneable.
     * When adding a new record, implement a public clone method if and only if the record
     * belongs to a sheet.
     * 
     * @return the cloned sheet
     */
public InternalSheet cloneSheet() {
    List<Record> clonedRecords = new ArrayList<Record>(_records.size());
    for (int i = 0; i < _records.size(); i++) {
        RecordBase rb = _records.get(i);
        if (rb instanceof RecordAggregate) {
            ((RecordAggregate) rb).visitContainedRecords(new RecordCloner(clonedRecords));
            continue;
        }
        if (rb instanceof EscherAggregate) {
            /**
                 * this record will be removed after reading actual data from EscherAggregate
                 */
            rb = new DrawingRecord();
        }
        try {
            Record rec = (Record) ((Record) rb).clone();
            clonedRecords.add(rec);
        } catch (CloneNotSupportedException e) {
            throw new RecordFormatException(e);
        }
    }
    return createSheet(new RecordStream(clonedRecords, 0));
}
Also used : EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) RecordFormatException(org.apache.poi.hssf.record.RecordFormatException) RecordBase(org.apache.poi.hssf.record.RecordBase) ArrayList(java.util.ArrayList) GutsRecord(org.apache.poi.hssf.record.GutsRecord) MergeCellsRecord(org.apache.poi.hssf.record.MergeCellsRecord) DefaultColWidthRecord(org.apache.poi.hssf.record.DefaultColWidthRecord) DefaultRowHeightRecord(org.apache.poi.hssf.record.DefaultRowHeightRecord) GridsetRecord(org.apache.poi.hssf.record.GridsetRecord) SCLRecord(org.apache.poi.hssf.record.SCLRecord) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) CFHeaderRecord(org.apache.poi.hssf.record.CFHeaderRecord) Record(org.apache.poi.hssf.record.Record) EOFRecord(org.apache.poi.hssf.record.EOFRecord) PrintHeadersRecord(org.apache.poi.hssf.record.PrintHeadersRecord) RefModeRecord(org.apache.poi.hssf.record.RefModeRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) CalcCountRecord(org.apache.poi.hssf.record.CalcCountRecord) ColumnInfoRecord(org.apache.poi.hssf.record.ColumnInfoRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) PrintGridlinesRecord(org.apache.poi.hssf.record.PrintGridlinesRecord) IterationRecord(org.apache.poi.hssf.record.IterationRecord) FeatHdrRecord(org.apache.poi.hssf.record.FeatHdrRecord) CalcModeRecord(org.apache.poi.hssf.record.CalcModeRecord) SelectionRecord(org.apache.poi.hssf.record.SelectionRecord) IndexRecord(org.apache.poi.hssf.record.IndexRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) DVALRecord(org.apache.poi.hssf.record.DVALRecord) DeltaRecord(org.apache.poi.hssf.record.DeltaRecord) PaneRecord(org.apache.poi.hssf.record.PaneRecord) FeatRecord(org.apache.poi.hssf.record.FeatRecord) WSBoolRecord(org.apache.poi.hssf.record.WSBoolRecord) SaveRecalcRecord(org.apache.poi.hssf.record.SaveRecalcRecord) CFHeader12Record(org.apache.poi.hssf.record.CFHeader12Record) UncalcedRecord(org.apache.poi.hssf.record.UncalcedRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) CustomViewSettingsRecordAggregate(org.apache.poi.hssf.record.aggregates.CustomViewSettingsRecordAggregate) RecordAggregate(org.apache.poi.hssf.record.aggregates.RecordAggregate) ChartSubstreamRecordAggregate(org.apache.poi.hssf.record.aggregates.ChartSubstreamRecordAggregate) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord)

Aggregations

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