naruto-lgtm commented on code in PR #1571:
URL: https://github.com/apache/poi/pull/1571#discussion_r4662931227
##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java:
##########
@@ -456,6 +456,8 @@ public List<XSLFSlide> getSlides() {
* Return all the charts in the slideshow
*/
public List<XSLFChart> getCharts() {
+ // charts can be removed together with their shapes
+ _charts.removeIf(c ->
!getPackage().containPart(c.getPackagePart().getPartName()));
Review Comment:
Agreed, the getter was the wrong place for this. The chart is now dropped
from the list in removeUnusedRelations, right when its part is removed, so a
chart created afterwards with the reused part name can't be mistaken for it.
getCharts is a plain getter again, and testCreateChartAfterRemoveGraphicFrame
covers the createChart case.
##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java:
##########
@@ -727,6 +732,48 @@ void removePictureRelation(XSLFPictureShape pictureShape) {
}
}
+ /**
+ * Helper method for sheet and group shapes
+ *
+ * @param frame the graphic frame of a chart, an embedded object, a
diagram, ...
+ * @return the ids of the relationships of this sheet, which the frame uses
+ */
+ Set<String> getRelationIds(XSLFGraphicFrame frame) {
+ Set<String> ids = getRelationIds(frame.getXmlObject());
+ if (frame instanceof XSLFDiagram diagram &&
diagram.getDiagramDrawing() != null) {
+ // the diagram drawing is referred to by the diagram data and not
by the frame
Review Comment:
Good catch. The drawing id isn't collected with the frame's ids anymore.
After those are released, the drawing is only released if the frame's data
relationship (r:dm) is gone; XSLFDiagram now keeps the id it resolved the
drawing from. testRemoveGraphicFrameKeepsSharedParts has a duplicated SmartArt
frame case: the drawing and the other diagram parts survive the first removal
and go with the second.
##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java:
##########
@@ -382,7 +385,9 @@ public boolean removeShape(XSLFShape xShape) {
} else if (obj instanceof CTConnector) {
removeElement(spTree.getCxnSpArray(), obj, spTree::removeCxnSp);
} else if (obj instanceof CTGraphicalObjectFrame) {
+ Set<String> relIds = getRelationIds((XSLFGraphicFrame)xShape);
removeElement(spTree.getGraphicFrameArray(), obj,
spTree::removeGraphicFrame);
+ removeUnusedRelations(relIds);
} else if (obj instanceof CTPicture) {
XSLFPictureShape ps = (XSLFPictureShape)xShape;
removePictureRelation(ps);
Review Comment:
I'd keep this PR to frames and do pictures as a follow-up. getRelationIds
and removeUnusedRelations are package-private, so routing XSLFPictureShape
through them (blip, a:videoFile/a:audioFile r:link, p14:media) and dropping the
top-level-only count is a small change on top of this.
##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java:
##########
@@ -727,6 +732,48 @@ void removePictureRelation(XSLFPictureShape pictureShape) {
}
}
+ /**
+ * Helper method for sheet and group shapes
+ *
+ * @param frame the graphic frame of a chart, an embedded object, a
diagram, ...
+ * @return the ids of the relationships of this sheet, which the frame uses
+ */
+ Set<String> getRelationIds(XSLFGraphicFrame frame) {
+ Set<String> ids = getRelationIds(frame.getXmlObject());
+ if (frame instanceof XSLFDiagram diagram &&
diagram.getDiagramDrawing() != null) {
+ // the diagram drawing is referred to by the diagram data and not
by the frame
+ String drawingId = getRelationId(diagram.getDiagramDrawing());
+ if (drawingId != null) {
+ ids.add(drawingId);
+ }
+ }
+ return ids;
+ }
+
+ private static Set<String> getRelationIds(XmlObject xml) {
+ Set<String> ids = new HashSet<>();
+ try (XmlCursor cur = xml.newCursor(); XmlCursor end = xml.newCursor())
{
+ end.toEndToken();
+ while (cur.toNextToken() != XmlCursor.TokenType.NONE &&
cur.isLeftOf(end)) {
Review Comment:
Done, both namespaces are matched now.
##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSheet.java:
##########
@@ -727,6 +732,48 @@ void removePictureRelation(XSLFPictureShape pictureShape) {
}
}
+ /**
+ * Helper method for sheet and group shapes
+ *
+ * @param frame the graphic frame of a chart, an embedded object, a
diagram, ...
+ * @return the ids of the relationships of this sheet, which the frame uses
+ */
+ Set<String> getRelationIds(XSLFGraphicFrame frame) {
+ Set<String> ids = getRelationIds(frame.getXmlObject());
+ if (frame instanceof XSLFDiagram diagram &&
diagram.getDiagramDrawing() != null) {
+ // the diagram drawing is referred to by the diagram data and not
by the frame
+ String drawingId = getRelationId(diagram.getDiagramDrawing());
+ if (drawingId != null) {
+ ids.add(drawingId);
+ }
+ }
+ return ids;
+ }
+
+ private static Set<String> getRelationIds(XmlObject xml) {
+ Set<String> ids = new HashSet<>();
+ try (XmlCursor cur = xml.newCursor(); XmlCursor end = xml.newCursor())
{
+ end.toEndToken();
+ while (cur.toNextToken() != XmlCursor.TokenType.NONE &&
cur.isLeftOf(end)) {
+ if (cur.isAttr() &&
RELATIONSHIPS_NS.equals(cur.getName().getNamespaceURI())) {
+ ids.add(cur.getTextValue());
+ }
+ }
+ }
+ return ids;
+ }
+
+ /**
+ * Helper method for sheet and group shapes
+ *
+ * @param relIds the relationship ids of a removed shape, whose relations
are to be removed,
+ * only if nothing else on this sheet refers to them
+ */
+ void removeUnusedRelations(Set<String> relIds) {
Review Comment:
Added the early return for an empty id set, which covers the common case of
tables without hyperlinks. Collecting the remaining ids once per batch would
need removeShape to know about the batch, so I left that out.
##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFGroupShape.java:
##########
@@ -183,7 +184,12 @@ public boolean removeShape(XSLFShape xShape) {
} else if (obj instanceof CTConnector){
XSLFSheet.removeElement(grpSp.getCxnSpArray(), obj,
grpSp::removeCxnSp);
} else if (obj instanceof CTGraphicalObjectFrame) {
+ XSLFSheet sh = getSheet();
+ Set<String> relIds = (sh != null) ?
sh.getRelationIds((XSLFGraphicFrame)xShape) : null;
Review Comment:
Done, it mirrors XSLFSheet.removeShape now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]