Search in sources :

Example 6 with TextObjectRecord

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

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

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

the class TestHSSFComment method resultEqualsToNonExistingAbstractShape.

@Test
public void resultEqualsToNonExistingAbstractShape() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sh = wb.createSheet();
    HSSFPatriarch patriarch = sh.createDrawingPatriarch();
    HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());
    HSSFRow row = sh.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellComment(comment);
    assertEquals(comment.getEscherContainer().getChildRecords().size(), 5);
    //sp record
    byte[] expected = decompress("H4sIAAAAAAAAAFvEw/WBg4GBgZEFSHAxMAAA9gX7nhAAAAA=");
    byte[] actual = comment.getEscherContainer().getChild(0).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    expected = decompress("H4sIAAAAAAAAAGNgEPggxIANAABK4+laGgAAAA==");
    actual = comment.getEscherContainer().getChild(2).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    expected = decompress("H4sIAAAAAAAAAGNgEPzAAAQACl6c5QgAAAA=");
    actual = comment.getEscherContainer().getChild(3).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    expected = decompress("H4sIAAAAAAAAAGNg4P3AAAQA6pyIkQgAAAA=");
    actual = comment.getEscherContainer().getChild(4).serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    ObjRecord obj = comment.getObjRecord();
    expected = decompress("H4sIAAAAAAAAAItlMGEQZRBikGRgZBF0YEACvAxiDLgBAJZsuoU4AAAA");
    actual = obj.serialize();
    assertEquals(expected.length, actual.length);
    //assertArrayEquals(expected, actual);
    TextObjectRecord tor = comment.getTextObjectRecord();
    expected = decompress("H4sIAAAAAAAAANvGKMQgxMSABgBGi8T+FgAAAA==");
    actual = tor.serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    NoteRecord note = comment.getNoteRecord();
    expected = decompress("H4sIAAAAAAAAAJNh4GGAAEYWEAkAS0KXuRAAAAA=");
    actual = note.serialize();
    assertEquals(expected.length, actual.length);
    assertArrayEquals(expected, actual);
    wb.close();
}
Also used : TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) ObjRecord(org.apache.poi.hssf.record.ObjRecord) Test(org.junit.Test)

Example 9 with TextObjectRecord

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

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

Aggregations

TextObjectRecord (org.apache.poi.hssf.record.TextObjectRecord)12 ObjRecord (org.apache.poi.hssf.record.ObjRecord)10 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)7 NoteRecord (org.apache.poi.hssf.record.NoteRecord)7 Test (org.junit.Test)7 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)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1