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();
}
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();
}
}
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());
}
}
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));
}
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;
}
Aggregations