use of org.apache.poi.hssf.record.EscherAggregate 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();
}
use of org.apache.poi.hssf.record.EscherAggregate 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();
}
use of org.apache.poi.hssf.record.EscherAggregate 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();
}
use of org.apache.poi.hssf.record.EscherAggregate in project poi by apache.
the class TestDrawingShapes method testTextForSimpleShape.
@Test
public void testTextForSimpleShape() throws IOException {
HSSFWorkbook wb1 = new HSSFWorkbook();
HSSFSheet sheet = wb1.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor());
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch);
assertEquals(agg.getShapeToObjMapping().size(), 2);
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
shape = (HSSFSimpleShape) patriarch.getChildren().get(0);
agg = HSSFTestHelper.getEscherAggregate(patriarch);
assertEquals(agg.getShapeToObjMapping().size(), 2);
shape.setString(new HSSFRichTextString("string1"));
assertEquals(shape.getString().getString(), "string1");
assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID));
assertEquals(agg.getShapeToObjMapping().size(), 2);
HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
wb2.close();
HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
wb3.close();
sheet = wb4.getSheetAt(0);
patriarch = sheet.getDrawingPatriarch();
shape = (HSSFSimpleShape) patriarch.getChildren().get(0);
assertNotNull(HSSFTestHelper.getTextObjRecord(shape));
assertEquals(shape.getString().getString(), "string1");
assertNotNull(HSSFTestHelper.getEscherContainer(shape).getChildById(EscherTextboxRecord.RECORD_ID));
assertEquals(agg.getShapeToObjMapping().size(), 2);
wb4.close();
}
use of org.apache.poi.hssf.record.EscherAggregate in project poi by apache.
the class TestDrawingShapes method testDgRecordNumShapes.
@Test
public void testDgRecordNumShapes() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
EscherAggregate aggregate = HSSFTestHelper.getEscherAggregate(patriarch);
EscherDgRecord dgRecord = (EscherDgRecord) aggregate.getEscherRecord(0).getChild(0);
assertEquals(dgRecord.getNumShapes(), 1);
wb.close();
}
Aggregations