Search in sources :

Example 21 with EscherAggregate

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

the class TestDrawingAggregate method testFileWithCharts.

@Test
public void testFileWithCharts() throws IOException {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("49581.xls");
    HSSFSheet sh = wb.getSheetAt(0);
    InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
    List<RecordBase> records = ish.getRecords();
    // records to be aggregated
    List<RecordBase> dgRecords = records.subList(19, 21);
    byte[] dgBytes = toByteArray(dgRecords);
    sh.getDrawingPatriarch();
    // collect drawing records into a byte buffer.
    EscherAggregate agg = (EscherAggregate) ish.findFirstRecordBySid(EscherAggregate.sid);
    byte[] dgBytesAfterSave = agg.serialize();
    assertEquals("different size of drawing data before and after save", dgBytes.length, dgBytesAfterSave.length);
    for (int i = 0; i < dgBytes.length; i++) {
        if (dgBytes[i] != dgBytesAfterSave[i]) {
            System.out.println("pos = " + i);
        }
    }
    assertTrue("drawing data before and after save is different", Arrays.equals(dgBytes, dgBytesAfterSave));
    wb.close();
}
Also used : EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) RecordBase(org.apache.poi.hssf.record.RecordBase) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 22 with EscherAggregate

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

the class TestDrawingAggregate method assertWriteAndReadBack.

/**
     * iterate over all sheets, aggregate drawing records (if there are any)
     * and remember information about the aggregated data.
     * Then serialize the workbook, read back and assert that the aggregated data is preserved.
     *
     * The assertion is strict meaning that the drawing data before and after save must be equal.
     */
private static void assertWriteAndReadBack(HSSFWorkbook wb) throws IOException {
    // map aggregate info by sheet index
    Map<Integer, DrawingAggregateInfo> aggs = new HashMap<Integer, DrawingAggregateInfo>();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        HSSFSheet sheet = wb.getSheetAt(i);
        DrawingAggregateInfo info = DrawingAggregateInfo.get(sheet);
        if (info != null) {
            aggs.put(i, info);
            HSSFPatriarch p = sheet.getDrawingPatriarch();
            // compare aggregate.serialize() with raw bytes from the record stream
            EscherAggregate agg = HSSFTestHelper.getEscherAggregate(p);
            byte[] dgBytes1 = info.getRawBytes();
            byte[] dgBytes2 = agg.serialize();
            assertEquals("different size of raw data ande aggregate.serialize()", dgBytes1.length, dgBytes2.length);
            assertTrue("raw drawing data (" + dgBytes1.length + " bytes) and aggregate.serialize() are different.", Arrays.equals(dgBytes1, dgBytes2));
        }
    }
    if (aggs.size() != 0) {
        HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb);
        for (int i = 0; i < wb2.getNumberOfSheets(); i++) {
            DrawingAggregateInfo info1 = aggs.get(i);
            if (info1 != null) {
                HSSFSheet sheet2 = wb2.getSheetAt(i);
                DrawingAggregateInfo info2 = DrawingAggregateInfo.get(sheet2);
                byte[] dgBytes1 = info1.getRawBytes();
                byte[] dgBytes2 = info2.getRawBytes();
                assertEquals("different size of drawing data before and after save", dgBytes1.length, dgBytes2.length);
                assertTrue("drawing data (" + dgBytes1.length + " bytes) before and after save is different.", Arrays.equals(dgBytes1, dgBytes2));
            }
        }
        wb2.close();
    }
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) HashMap(java.util.HashMap) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 23 with EscherAggregate

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

the class HSSFSimpleShape method afterInsert.

@Override
void afterInsert(HSSFPatriarch patriarch) {
    EscherAggregate agg = patriarch.getBoundAggregate();
    agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
    if (null != getTextObjectRecord()) {
        agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherTextboxRecord.RECORD_ID), getTextObjectRecord());
    }
}
Also used : EscherAggregate(org.apache.poi.hssf.record.EscherAggregate)

Example 24 with EscherAggregate

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

the class TestEscherRecordFactory method testDgContainerMustBeRootOfHSSFSheetEscherRecords.

public void testDgContainerMustBeRootOfHSSFSheetEscherRecords() throws IOException {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("47251.xls");
    HSSFSheet sh = wb.getSheetAt(0);
    InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
    List<RecordBase> records = ish.getRecords();
    // records to be aggregated
    List<RecordBase> dgRecords = records.subList(19, 23);
    byte[] dgBytes = toByteArray(dgRecords);
    sh.getDrawingPatriarch();
    EscherAggregate agg = (EscherAggregate) ish.findFirstRecordBySid(EscherAggregate.sid);
    assertEquals(true, agg.getEscherRecords().get(0) instanceof EscherContainerRecord);
    assertEquals(EscherContainerRecord.DG_CONTAINER, agg.getEscherRecords().get(0).getRecordId());
    assertEquals((short) 0x0, agg.getEscherRecords().get(0).getOptions());
    agg = (EscherAggregate) ish.findFirstRecordBySid(EscherAggregate.sid);
    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));
}
Also used : EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) RecordBase(org.apache.poi.hssf.record.RecordBase) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 25 with EscherAggregate

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

the class HSSFPatriarch method createPatriarch.

/**
     * used to clone patriarch
     *
     * create patriarch from existing one
     * @param patriarch - copy all the shapes from this patriarch to new one
     * @param sheet where must be located new patriarch
     * @return new patriarch with copies of all shapes from the existing patriarch
     */
static HSSFPatriarch createPatriarch(HSSFPatriarch patriarch, HSSFSheet sheet) {
    HSSFPatriarch newPatriarch = new HSSFPatriarch(sheet, new EscherAggregate(true));
    newPatriarch.afterCreate();
    for (HSSFShape shape : patriarch.getChildren()) {
        HSSFShape newShape;
        if (shape instanceof HSSFShapeGroup) {
            newShape = ((HSSFShapeGroup) shape).cloneShape(newPatriarch);
        } else {
            newShape = shape.cloneShape();
        }
        newPatriarch.onCreate(newShape);
        newPatriarch.addShape(newShape);
    }
    return newPatriarch;
}
Also used : EscherAggregate(org.apache.poi.hssf.record.EscherAggregate)

Aggregations

EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)27 Test (org.junit.Test)16 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)13 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 RecordBase (org.apache.poi.hssf.record.RecordBase)12 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)8 EscherRecord (org.apache.poi.ddf.EscherRecord)8 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)7 DrawingRecord (org.apache.poi.hssf.record.DrawingRecord)7 EOFRecord (org.apache.poi.hssf.record.EOFRecord)7 NoteRecord (org.apache.poi.hssf.record.NoteRecord)7 Record (org.apache.poi.hssf.record.Record)7 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)7 ContinueRecord (org.apache.poi.hssf.record.ContinueRecord)6 ObjRecord (org.apache.poi.hssf.record.ObjRecord)6 TextObjectRecord (org.apache.poi.hssf.record.TextObjectRecord)6 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)5 EscherDgRecord (org.apache.poi.ddf.EscherDgRecord)3 RowRecordsAggregate (org.apache.poi.hssf.record.aggregates.RowRecordsAggregate)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2