Search in sources :

Example 11 with EscherAggregate

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

the class TestShapeGroup method testClearShapes.

@Test
public void testClearShapes() throws IOException {
    HSSFWorkbook wb1 = new HSSFWorkbook();
    HSSFSheet sheet = wb1.createSheet();
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
    group.createShape(new HSSFChildAnchor());
    group.createShape(new HSSFChildAnchor());
    EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch);
    assertEquals(agg.getShapeToObjMapping().size(), 5);
    assertEquals(agg.getTailRecords().size(), 0);
    assertEquals(group.getChildren().size(), 2);
    group.clear();
    assertEquals(agg.getShapeToObjMapping().size(), 1);
    assertEquals(agg.getTailRecords().size(), 0);
    assertEquals(group.getChildren().size(), 0);
    HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    group = (HSSFShapeGroup) patriarch.getChildren().get(0);
    assertEquals(agg.getShapeToObjMapping().size(), 1);
    assertEquals(agg.getTailRecords().size(), 0);
    assertEquals(group.getChildren().size(), 0);
    wb2.close();
}
Also used : EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) Test(org.junit.Test)

Example 12 with EscherAggregate

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

the class InternalWorkbook method cloneDrawings.

/**
     * Check if the cloned sheet has drawings. If yes, then allocate a new drawing group ID and
     * re-generate shape IDs
     *
     * @param sheet the cloned sheet
     */
public void cloneDrawings(InternalSheet sheet) {
    findDrawingGroup();
    if (drawingManager == null) {
        //this workbook does not have drawings
        return;
    }
    //check if the cloned sheet has drawings
    int aggLoc = sheet.aggregateDrawingRecords(drawingManager, false);
    if (aggLoc == -1) {
        return;
    }
    EscherAggregate agg = (EscherAggregate) sheet.findFirstRecordBySid(EscherAggregate.sid);
    EscherContainerRecord escherContainer = agg.getEscherContainer();
    if (escherContainer == null) {
        return;
    }
    EscherDggRecord dgg = drawingManager.getDgg();
    //register a new drawing group for the cloned sheet
    int dgId = drawingManager.findNewDrawingGroupId();
    dgg.addCluster(dgId, 0);
    dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
    EscherDgRecord dg = null;
    for (EscherRecord er : escherContainer) {
        if (er instanceof EscherDgRecord) {
            dg = (EscherDgRecord) er;
            //update id of the drawing in the cloned sheet
            dg.setOptions((short) (dgId << 4));
        } else if (er instanceof EscherContainerRecord) {
            // iterate over shapes and re-generate shapeId
            for (EscherRecord er2 : (EscherContainerRecord) er) {
                for (EscherRecord shapeChildRecord : (EscherContainerRecord) er2) {
                    int recordId = shapeChildRecord.getRecordId();
                    if (recordId == EscherSpRecord.RECORD_ID) {
                        if (dg == null) {
                            throw new RecordFormatException("EscherDgRecord wasn't set/processed before.");
                        }
                        EscherSpRecord sp = (EscherSpRecord) shapeChildRecord;
                        int shapeId = drawingManager.allocateShapeId((short) dgId, dg);
                        //allocateShapeId increments the number of shapes. roll back to the previous value
                        dg.setNumShapes(dg.getNumShapes() - 1);
                        sp.setShapeId(shapeId);
                    } else if (recordId == EscherOptRecord.RECORD_ID) {
                        EscherOptRecord opt = (EscherOptRecord) shapeChildRecord;
                        EscherSimpleProperty prop = (EscherSimpleProperty) opt.lookup(EscherProperties.BLIP__BLIPTODISPLAY);
                        if (prop != null) {
                            int pictureIndex = prop.getPropertyValue();
                            // increment reference count for pictures
                            EscherBSERecord bse = getBSERecord(pictureIndex);
                            bse.setRef(bse.getRef() + 1);
                        }
                    }
                }
            }
        }
    }
}
Also used : EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) RecordFormatException(org.apache.poi.util.RecordFormatException) EscherSimpleProperty(org.apache.poi.ddf.EscherSimpleProperty) EscherSpRecord(org.apache.poi.ddf.EscherSpRecord) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) EscherDgRecord(org.apache.poi.ddf.EscherDgRecord) EscherRecord(org.apache.poi.ddf.EscherRecord) EscherOptRecord(org.apache.poi.ddf.EscherOptRecord) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord)

Example 13 with EscherAggregate

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

the class TestCloneSheet method testCloneSheetWithEmptyDrawingAggregate.

@Test
public void testCloneSheetWithEmptyDrawingAggregate() {
    HSSFWorkbook b = new HSSFWorkbook();
    HSSFSheet s = b.createSheet("Test");
    HSSFPatriarch patriarch = s.createDrawingPatriarch();
    EscherAggregate agg1 = patriarch.getBoundAggregate();
    HSSFSheet s2 = s.cloneSheet(b);
    patriarch = s2.getDrawingPatriarch();
    EscherAggregate agg2 = patriarch.getBoundAggregate();
    EscherSpRecord sp1 = (EscherSpRecord) agg1.getEscherContainer().getChild(1).getChild(0).getChild(1);
    EscherSpRecord sp2 = (EscherSpRecord) agg2.getEscherContainer().getChild(1).getChild(0).getChild(1);
    assertEquals(sp1.getShapeId(), 1024);
    assertEquals(sp2.getShapeId(), 2048);
    EscherDgRecord dg = (EscherDgRecord) agg2.getEscherContainer().getChild(0);
    assertEquals(dg.getLastMSOSPID(), 2048);
    assertEquals(dg.getInstance(), 0x2);
    //everything except id and DgRecord.lastMSOSPID and DgRecord.Instance must be the same
    sp2.setShapeId(1024);
    dg.setLastMSOSPID(1024);
    dg.setInstance((short) 0x1);
    assertEquals(agg1.serialize().length, agg2.serialize().length);
    assertEquals(agg1.toXml(""), agg2.toXml(""));
    assertArrayEquals(agg1.serialize(), agg2.serialize());
}
Also used : EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) EscherSpRecord(org.apache.poi.ddf.EscherSpRecord) EscherDgRecord(org.apache.poi.ddf.EscherDgRecord) Test(org.junit.Test)

Example 14 with EscherAggregate

use of org.apache.poi.hssf.record.EscherAggregate 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)

Example 15 with EscherAggregate

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

the class InternalSheet method aggregateDrawingRecords.

/**
     * Finds the DrawingRecord for our sheet, and
     *  attaches it to the DrawingManager (which knows about
     *  the overall DrawingGroup for our workbook).
     * If requested, will create a new DrawRecord
     *  if none currently exist
     * @param drawingManager The DrawingManager2 for our workbook
     * @param createIfMissing Should one be created if missing?
     * @return location of EscherAggregate record. if no EscherAggregate record is found return -1
     */
public int aggregateDrawingRecords(DrawingManager2 drawingManager, boolean createIfMissing) {
    int loc = findFirstRecordLocBySid(DrawingRecord.sid);
    boolean noDrawingRecordsFound = (loc == -1);
    if (noDrawingRecordsFound) {
        if (!createIfMissing) {
            // None found, and not allowed to add in
            return -1;
        }
        EscherAggregate aggregate = new EscherAggregate(true);
        loc = findFirstRecordLocBySid(EscherAggregate.sid);
        if (loc == -1) {
            loc = findFirstRecordLocBySid(WindowTwoRecord.sid);
        } else {
            getRecords().remove(loc);
        }
        getRecords().add(loc, aggregate);
        return loc;
    }
    List<RecordBase> records = getRecords();
    EscherAggregate.createAggregate(records, loc);
    return loc;
}
Also used : EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) RecordBase(org.apache.poi.hssf.record.RecordBase)

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