Search in sources :

Example 11 with ObjRecord

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

the class TestText method testResultEqualsToNonExistingAbstractShape.

@Test
public void testResultEqualsToNonExistingAbstractShape() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sh = wb.createSheet();
    HSSFPatriarch patriarch = sh.createDrawingPatriarch();
    HSSFTextbox textbox = patriarch.createTextbox(new HSSFClientAnchor());
    assertEquals(textbox.getEscherContainer().getChildRecords().size(), 5);
    //sp record
    byte[] expected = decompress("H4sIAAAAAAAAAFvEw/WBg4GBgZEFSHAxMAAA9gX7nhAAAAA=");
    byte[] actual = textbox.getEscherContainer().getChild(0).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    expected = decompress("H4sIAAAAAAAAAGNgEPggxIANAABK4+laGgAAAA==");
    actual = textbox.getEscherContainer().getChild(2).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    expected = decompress("H4sIAAAAAAAAAGNgEPzAAAQACl6c5QgAAAA=");
    actual = textbox.getEscherContainer().getChild(3).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    expected = decompress("H4sIAAAAAAAAAGNg4P3AAAQA6pyIkQgAAAA=");
    actual = textbox.getEscherContainer().getChild(4).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    ObjRecord obj = textbox.getObjRecord();
    expected = decompress("H4sIAAAAAAAAAItlkGIQZRBiYGNgZBBMYEADAOdCLuweAAAA");
    actual = obj.serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    TextObjectRecord tor = textbox.getTextObjectRecord();
    expected = decompress("H4sIAAAAAAAAANvGKMQgxMSABgBGi8T+FgAAAA==");
    actual = tor.serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    wb.close();
}
Also used : TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) ObjRecord(org.apache.poi.hssf.record.ObjRecord) Test(org.junit.Test)

Example 12 with ObjRecord

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

the class HSSFShapeFactory method createShapeTree.

/**
     * build shape tree from escher container
     * @param container root escher container from which escher records must be taken
     * @param agg - EscherAggregate
     * @param out - shape container to which shapes must be added
     * @param root - node to create HSSFObjectData shapes
     */
public static void createShapeTree(EscherContainerRecord container, EscherAggregate agg, HSSFShapeContainer out, DirectoryNode root) {
    if (container.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
        ObjRecord obj = null;
        EscherClientDataRecord clientData = ((EscherContainerRecord) container.getChild(0)).getChildById(EscherClientDataRecord.RECORD_ID);
        if (null != clientData) {
            obj = (ObjRecord) agg.getShapeToObjMapping().get(clientData);
        }
        HSSFShapeGroup group = new HSSFShapeGroup(container, obj);
        List<EscherContainerRecord> children = container.getChildContainers();
        // skip the first child record, it is group descriptor
        for (int i = 0; i < children.size(); i++) {
            EscherContainerRecord spContainer = children.get(i);
            if (i != 0) {
                createShapeTree(spContainer, agg, group, root);
            }
        }
        out.addShape(group);
    } else if (container.getRecordId() == EscherContainerRecord.SP_CONTAINER) {
        Map<EscherRecord, Record> shapeToObj = agg.getShapeToObjMapping();
        ObjRecord objRecord = null;
        TextObjectRecord txtRecord = null;
        for (EscherRecord record : container) {
            switch(record.getRecordId()) {
                case EscherClientDataRecord.RECORD_ID:
                    objRecord = (ObjRecord) shapeToObj.get(record);
                    break;
                case EscherTextboxRecord.RECORD_ID:
                    txtRecord = (TextObjectRecord) shapeToObj.get(record);
                    break;
                default:
                    break;
            }
        }
        if (objRecord == null) {
            throw new RecordFormatException("EscherClientDataRecord can't be found.");
        }
        if (isEmbeddedObject(objRecord)) {
            HSSFObjectData objectData = new HSSFObjectData(container, objRecord, root);
            out.addShape(objectData);
            return;
        }
        CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) objRecord.getSubRecords().get(0);
        final HSSFShape shape;
        switch(cmo.getObjectType()) {
            case CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE:
                shape = new HSSFPicture(container, objRecord);
                break;
            case CommonObjectDataSubRecord.OBJECT_TYPE_RECTANGLE:
                shape = new HSSFSimpleShape(container, objRecord, txtRecord);
                break;
            case CommonObjectDataSubRecord.OBJECT_TYPE_LINE:
                shape = new HSSFSimpleShape(container, objRecord);
                break;
            case CommonObjectDataSubRecord.OBJECT_TYPE_COMBO_BOX:
                shape = new HSSFCombobox(container, objRecord);
                break;
            case CommonObjectDataSubRecord.OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING:
                EscherOptRecord optRecord = container.getChildById(EscherOptRecord.RECORD_ID);
                if (optRecord == null) {
                    shape = new HSSFSimpleShape(container, objRecord, txtRecord);
                } else {
                    EscherProperty property = optRecord.lookup(EscherProperties.GEOMETRY__VERTICES);
                    if (null != property) {
                        shape = new HSSFPolygon(container, objRecord, txtRecord);
                    } else {
                        shape = new HSSFSimpleShape(container, objRecord, txtRecord);
                    }
                }
                break;
            case CommonObjectDataSubRecord.OBJECT_TYPE_TEXT:
                shape = new HSSFTextbox(container, objRecord, txtRecord);
                break;
            case CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT:
                shape = new HSSFComment(container, objRecord, txtRecord, agg.getNoteRecordByObj(objRecord));
                break;
            default:
                shape = new HSSFSimpleShape(container, objRecord, txtRecord);
        }
        out.addShape(shape);
    }
}
Also used : TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) CommonObjectDataSubRecord(org.apache.poi.hssf.record.CommonObjectDataSubRecord) EscherRecord(org.apache.poi.ddf.EscherRecord) EscherOptRecord(org.apache.poi.ddf.EscherOptRecord) EscherProperty(org.apache.poi.ddf.EscherProperty) RecordFormatException(org.apache.poi.util.RecordFormatException) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EscherClientDataRecord(org.apache.poi.ddf.EscherClientDataRecord) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) Map(java.util.Map)

Example 13 with ObjRecord

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

the class TestDrawingAggregate method test45129.

/**
     * test reading drawing aggregate from a test file from Bugzilla 45129
     */
@Test
public void test45129() throws IOException {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("45129.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", 394, 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, 389);
    // collect drawing records into a byte buffer.
    byte[] dgBytes = toByteArray(dgRecords);
    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);
    }
    // the first record after the drawing block
    assertTrue("records.get(389) is expected to be Window2", records.get(389) instanceof WindowTwoRecord);
    // aggregate drawing records.
    // The subrange [19, 388] 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", 25, 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 14 with ObjRecord

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

the class TestDrawingShapes method testDefaultSettingsWithEmptyContainer.

/**
     * No NullPointerException should appear
     */
@Test
public void testDefaultSettingsWithEmptyContainer() {
    EscherContainerRecord container = new EscherContainerRecord();
    EscherOptRecord opt = new EscherOptRecord();
    opt.setRecordId(EscherOptRecord.RECORD_ID);
    container.addChildRecord(opt);
    ObjRecord obj = new ObjRecord();
    CommonObjectDataSubRecord cod = new CommonObjectDataSubRecord();
    cod.setObjectType(HSSFSimpleShape.OBJECT_TYPE_PICTURE);
    obj.addSubRecord(cod);
    HSSFPicture picture = new HSSFPicture(container, obj);
    assertEquals(picture.getLineWidth(), HSSFShape.LINEWIDTH_DEFAULT);
    assertEquals(picture.getFillColor(), HSSFShape.FILL__FILLCOLOR_DEFAULT);
    assertEquals(picture.getLineStyle(), HSSFShape.LINESTYLE_DEFAULT);
    assertEquals(picture.getLineStyleColor(), HSSFShape.LINESTYLE__COLOR_DEFAULT);
    assertEquals(picture.isNoFill(), HSSFShape.NO_FILL_DEFAULT);
    //not set yet
    assertEquals(picture.getPictureIndex(), -1);
}
Also used : CommonObjectDataSubRecord(org.apache.poi.hssf.record.CommonObjectDataSubRecord) ObjRecord(org.apache.poi.hssf.record.ObjRecord) HSSFPicture(org.apache.poi.hssf.usermodel.HSSFPicture) Test(org.junit.Test)

Example 15 with ObjRecord

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

the class TestPolygon method testResultEqualsToAbstractShape.

@Test
public void testResultEqualsToAbstractShape() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sh = wb.createSheet();
    HSSFPatriarch patriarch = sh.createDrawingPatriarch();
    HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());
    polygon.setPolygonDrawArea(100, 100);
    polygon.setPoints(new int[] { 0, 90, 50 }, new int[] { 5, 5, 44 });
    polygon.setShapeId(1024);
    assertEquals(polygon.getEscherContainer().getChildRecords().size(), 4);
    //sp record
    byte[] expected = decompress("H4sIAAAAAAAAAGNi4PrAwQAELEDMxcAAAAU6ZlwQAAAA");
    byte[] actual = polygon.getEscherContainer().getChild(0).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    expected = decompress("H4sIAAAAAAAAAGNgEPggxIANAABK4+laGgAAAA==");
    actual = polygon.getEscherContainer().getChild(2).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    expected = decompress("H4sIAAAAAAAAAGNgEPzAAAQACl6c5QgAAAA=");
    actual = polygon.getEscherContainer().getChild(3).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    ObjRecord obj = polygon.getObjRecord();
    expected = decompress("H4sIAAAAAAAAAItlkGIQZRBikGNgYBBMYEADAOAV/ZkeAAAA");
    actual = obj.serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    wb.close();
}
Also used : ObjRecord(org.apache.poi.hssf.record.ObjRecord) Test(org.junit.Test)

Aggregations

ObjRecord (org.apache.poi.hssf.record.ObjRecord)17 TextObjectRecord (org.apache.poi.hssf.record.TextObjectRecord)10 Test (org.junit.Test)10 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)9 NoteRecord (org.apache.poi.hssf.record.NoteRecord)7 CommonObjectDataSubRecord (org.apache.poi.hssf.record.CommonObjectDataSubRecord)6 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)5 EscherRecord (org.apache.poi.ddf.EscherRecord)5 DrawingRecord (org.apache.poi.hssf.record.DrawingRecord)5 EOFRecord (org.apache.poi.hssf.record.EOFRecord)5 Record (org.apache.poi.hssf.record.Record)5 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)5 ContinueRecord (org.apache.poi.hssf.record.ContinueRecord)4 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)4 RecordBase (org.apache.poi.hssf.record.RecordBase)4 RowRecordsAggregate (org.apache.poi.hssf.record.aggregates.RowRecordsAggregate)4 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)4 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)4 DefaultEscherRecordFactory (org.apache.poi.ddf.DefaultEscherRecordFactory)3 EndSubRecord (org.apache.poi.hssf.record.EndSubRecord)3