While trying to traverse the inner contents of the ShapeContainer, I wanted to differentiate between XSSFPictures
which are children of XSSFShapeGroup
and which are not by checking it's parent:
private void traverseShapeContainer(ShapeContainer<XSSFShape> container) {
for (XSSFShape shape : container) {
// Other types of XSSFShapes have not been mentioned here.
if (shape instanceof XSSFPicture) {
XSSFPicture picture = (XSSFPicture) shape;
System.out.println(shape.getParent() instanceof XSSFShapeGroup); // Always outputs false
System.out.println(Objects.isNull(shape.getParent())); // Always outputs true
} else if (shape instanceof XSSFShapeGroup) {
XSSFShapeGroup shapeGroup = (XSSFShapeGroup) shape;
// accessing inner contents of XSSFShapeGroup
traverseShapeContainer(shapeGroup);
}
}
}
This was the same for every case whether an XSSFPicture
was a child of a XSSFShapeGroup
or not.
This seemed particularly strange after I performed these two tests to check for it's parent.
- Test 1: Checking whether the ShapeContainer is an instance of XSSFShapeGroup or not.
System.out.println(Objects.isNull(container instanceof XSSFShapeGroup)); // Output: false
- Test 2: Looking for the parent tag after visiting the child node.
if (shape instanceof XSSFPicture) {
XSSFPicture picture = (XSSFPicture) shape;
// "xdr:grpSp" is the tag for XSSFShapeGroup
System.out.println(picture.getCTPicture().getDomNode().getParentNode().getNodeName()
.equals("xdr:grpSp")); // Output: true
}
This clearly shows that the parent does exist and also allows us to check for the parent in the process.
I haven't checked for other types of XSSFShapes
such as XSSFSimpleShape
or XSSFConnector
yet. However, since they all inherit the same class,i.e, XSSFShape
I guess the results wouldn't be much different.
So what might be wrong with XSSFShape.getParent()
or is my perception of the problem incorrect?