use of org.apache.poi.POIXMLDocumentPart.RelationPart in project poi by apache.
the class TestPOIXMLDocument method traverse.
/**
* Recursively traverse a OOXML document and assert that same logical parts have the same physical instances
*/
private static void traverse(RelationPart rp, HashMap<String, POIXMLDocumentPart> context) throws IOException {
POIXMLDocumentPart dp = rp.getDocumentPart();
assertEquals(rp.getRelationship().getTargetURI().toString(), dp.getPackagePart().getPartName().getName());
context.put(dp.getPackagePart().getPartName().getName(), dp);
for (RelationPart p : dp.getRelationParts()) {
assertNotNull(p.getRelationship().toString());
String uri = p.getDocumentPart().getPackagePart().getPartName().getURI().toString();
assertEquals(uri, p.getRelationship().getTargetURI().toString());
if (!context.containsKey(uri)) {
traverse(p, context);
} else {
POIXMLDocumentPart prev = context.get(uri);
assertSame("Duplicate POIXMLDocumentPart instance for targetURI=" + uri, prev, p.getDocumentPart());
}
}
}
use of org.apache.poi.POIXMLDocumentPart.RelationPart in project poi by apache.
the class TestXSSFDrawing method testXSSFSAddPicture.
@Test
public void testXSSFSAddPicture() throws Exception {
XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFSheet sheet = wb1.createSheet();
//multiple calls of createDrawingPatriarch should return the same instance of XSSFDrawing
XSSFDrawing dr1 = sheet.createDrawingPatriarch();
XSSFDrawing dr2 = sheet.createDrawingPatriarch();
assertSame(dr1, dr2);
List<RelationPart> rels = sheet.getRelationParts();
assertEquals(1, rels.size());
RelationPart rp = rels.get(0);
assertTrue(rp.getDocumentPart() instanceof XSSFDrawing);
assertEquals(0, rp.getDocumentPart().getRelations().size());
XSSFDrawing drawing = rp.getDocumentPart();
String drawingId = rp.getRelationship().getId();
//there should be a relation to this drawing in the worksheet
assertTrue(sheet.getCTWorksheet().isSetDrawing());
assertEquals(drawingId, sheet.getCTWorksheet().getDrawing().getId());
byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("45829.png");
ClientAnchor anchor = wb1.getCreationHelper().createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(1);
drawing.createPicture(anchor, wb1.addPicture(pictureData, Workbook.PICTURE_TYPE_JPEG));
final int pictureIndex = wb1.addPicture(pictureData, Workbook.PICTURE_TYPE_JPEG);
drawing.createPicture(anchor, pictureIndex);
drawing.createPicture(anchor, pictureIndex);
// repeated additions of same share package relationship
assertEquals(2, rp.getDocumentPart().getPackagePart().getRelationships().size());
List<XSSFShape> shapes = drawing.getShapes();
assertEquals(3, shapes.size());
assertTrue(shapes.get(0) instanceof XSSFPicture);
assertTrue(shapes.get(1) instanceof XSSFPicture);
// Save and re-load it
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
sheet = wb2.getSheetAt(0);
// Check
dr1 = sheet.createDrawingPatriarch();
CTDrawing ctDrawing = dr1.getCTDrawing();
// Connector, shapes and text boxes are all two cell anchors
assertEquals(0, ctDrawing.sizeOfAbsoluteAnchorArray());
assertEquals(0, ctDrawing.sizeOfOneCellAnchorArray());
assertEquals(3, ctDrawing.sizeOfTwoCellAnchorArray());
shapes = dr1.getShapes();
assertEquals(3, shapes.size());
assertTrue(shapes.get(0) instanceof XSSFPicture);
assertTrue(shapes.get(1) instanceof XSSFPicture);
checkRewrite(wb2);
wb2.close();
}
use of org.apache.poi.POIXMLDocumentPart.RelationPart in project poi by apache.
the class TestXSSFDrawing method testRead.
@Test
public void testRead() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
//the sheet has one relationship and it is XSSFDrawing
List<RelationPart> rels = sheet.getRelationParts();
assertEquals(1, rels.size());
RelationPart rp = rels.get(0);
assertTrue(rp.getDocumentPart() instanceof XSSFDrawing);
XSSFDrawing drawing = (XSSFDrawing) rp.getDocumentPart();
//sheet.createDrawingPatriarch() should return the same instance of XSSFDrawing
assertSame(drawing, sheet.createDrawingPatriarch());
String drawingId = rp.getRelationship().getId();
//there should be a relation to this drawing in the worksheet
assertTrue(sheet.getCTWorksheet().isSetDrawing());
assertEquals(drawingId, sheet.getCTWorksheet().getDrawing().getId());
List<XSSFShape> shapes = drawing.getShapes();
assertEquals(6, shapes.size());
assertTrue(shapes.get(0) instanceof XSSFPicture);
assertTrue(shapes.get(1) instanceof XSSFPicture);
assertTrue(shapes.get(2) instanceof XSSFPicture);
assertTrue(shapes.get(3) instanceof XSSFPicture);
assertTrue(shapes.get(4) instanceof XSSFSimpleShape);
assertTrue(shapes.get(5) instanceof XSSFPicture);
for (XSSFShape sh : shapes) assertNotNull(sh.getAnchor());
checkRewrite(wb);
wb.close();
}
use of org.apache.poi.POIXMLDocumentPart.RelationPart in project poi by apache.
the class TestXSSFDrawing method testReadTextBox.
/**
* Test reading text from a textbox in an existing file
*/
@Test
public void testReadTextBox() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
//the sheet has one relationship and it is XSSFDrawing
List<RelationPart> rels = sheet.getRelationParts();
assertEquals(1, rels.size());
RelationPart rp = rels.get(0);
assertTrue(rp.getDocumentPart() instanceof XSSFDrawing);
XSSFDrawing drawing = rp.getDocumentPart();
//sheet.createDrawingPatriarch() should return the same instance of XSSFDrawing
assertSame(drawing, sheet.createDrawingPatriarch());
String drawingId = rp.getRelationship().getId();
//there should be a relation to this drawing in the worksheet
assertTrue(sheet.getCTWorksheet().isSetDrawing());
assertEquals(drawingId, sheet.getCTWorksheet().getDrawing().getId());
List<XSSFShape> shapes = drawing.getShapes();
assertEquals(6, shapes.size());
assertTrue(shapes.get(4) instanceof XSSFSimpleShape);
XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(4);
assertEquals("Sheet with various pictures\n(jpeg, png, wmf, emf and pict)", textbox.getText());
checkRewrite(wb);
wb.close();
}
use of org.apache.poi.POIXMLDocumentPart.RelationPart in project poi by apache.
the class TestXSSFBugs method bug51470.
@Test
public void bug51470() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51470.xlsx");
XSSFSheet sh0 = wb.getSheetAt(0);
XSSFSheet sh1 = wb.cloneSheet(0);
List<RelationPart> rels0 = sh0.getRelationParts();
List<RelationPart> rels1 = sh1.getRelationParts();
assertEquals(1, rels0.size());
assertEquals(1, rels1.size());
PackageRelationship pr0 = rels0.get(0).getRelationship();
PackageRelationship pr1 = rels1.get(0).getRelationship();
assertEquals(pr0.getTargetMode(), pr1.getTargetMode());
assertEquals(pr0.getTargetURI(), pr1.getTargetURI());
POIXMLDocumentPart doc0 = rels0.get(0).getDocumentPart();
POIXMLDocumentPart doc1 = rels1.get(0).getDocumentPart();
assertEquals(doc0, doc1);
wb.close();
}
Aggregations