]> source.dussan.org Git - poi.git/commitdiff
[bug-62736] Relations on XSLFPictureShape are removed unconditionally. Thanks to...
authorPJ Fanning <fanningpj@apache.org>
Wed, 26 Sep 2018 21:42:21 +0000 (21:42 +0000)
committerPJ Fanning <fanningpj@apache.org>
Wed, 26 Sep 2018 21:42:21 +0000 (21:42 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1842055 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java
src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java
test-data/slideshow/bug62736.pptx [new file with mode: 0644]

index 2dbc39bff461e60b926fb8aef91505c55ba288c1..cee875a8319c2b09ea9f7550e3b9beac5a102bae 100644 (file)
@@ -692,10 +692,23 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
     /**
      * Helper method for sheet and group shapes
      *
-     * @param pictureShape the picture shapes whose relation is to be removed
+     * @param pictureShape the picture shapes whose relation is to be removed,
+     *                     only if there are no more relations on its sheet to that picture
      */
     void removePictureRelation(XSLFPictureShape pictureShape) {
-        removeRelation(pictureShape.getBlipId());
+        int numberOfRelations = 0;
+        String targetBlipId = pictureShape.getBlipId();
+        for (XSLFShape shape : pictureShape.getSheet().getShapes()) {
+            if (shape instanceof XSLFPictureShape) {
+                XSLFPictureShape currentPictureShape = ((XSLFPictureShape) shape);
+                if (currentPictureShape.getBlipId().equals(targetBlipId)) {
+                    numberOfRelations++;
+                }
+            }
+        }
+        if (numberOfRelations <= 1) {
+            removeRelation(pictureShape.getBlipId());
+        }
     }
 
 
index c594280654a20ee652a48837f67f9d9839db0242..48b08169938a49f81bc5d6af7825a185309ac3c9 100644 (file)
@@ -93,6 +93,87 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
 public class TestXSLFBugs {
     private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
 
+    @Test
+    public void bug62736() throws Exception {
+        XMLSlideShow ss1 = XSLFTestDataSamples.openSampleDocument("bug62736.pptx");
+
+        assertEquals(1, ss1.getSlides().size());
+
+        XSLFSlide slide0 = ss1.getSlides().get(0);
+
+        assertEquals(slide0.getShapes().size(), 4);
+
+        assertRelation(slide0, "/ppt/slides/slide1.xml", null);
+        assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
+        assertRelation(slide0, "/ppt/media/image1.png", "rId2");
+        assertEquals(slide0.getRelations().size(), 2);
+
+        List<XSLFPictureShape> pictures = new ArrayList<>();
+        for (XSLFShape shape : slide0.getShapes()) {
+            if (shape instanceof XSLFPictureShape) {
+                pictures.add((XSLFPictureShape) shape);
+            }
+        }
+
+        assertEquals(pictures.size(), 2);
+        assertEquals(pictures.get(0).getPictureData().getFileName(), "image1.png");
+        assertEquals(pictures.get(1).getPictureData().getFileName(), "image1.png");
+        // blipId is rId2 of both pictures
+
+        // remove just the first picture
+        slide0.removeShape(pictures.get(0));
+
+        assertEquals(slide0.getShapes().size(), 3);
+
+        assertRelation(slide0, "/ppt/slides/slide1.xml", null);
+        assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
+        // the bug is that the following relation is gone
+        assertRelation(slide0, "/ppt/media/image1.png", "rId2");
+        assertEquals(slide0.getRelations().size(), 2);
+
+        // Save and re-load
+        XMLSlideShow ss2 = XSLFTestDataSamples.writeOutAndReadBack(ss1);
+        ss1.close();
+        assertEquals(1, ss2.getSlides().size());
+
+        slide0 = ss2.getSlides().get(0);
+
+        assertRelation(slide0, "/ppt/slides/slide1.xml", null);
+        assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
+        assertRelation(slide0, "/ppt/media/image1.png", "rId2");
+        assertEquals(slide0.getRelations().size(), 2);
+
+        pictures.clear();
+        for (XSLFShape shape : slide0.getShapes()) {
+            if (shape instanceof XSLFPictureShape) {
+                pictures.add((XSLFPictureShape) shape);
+            }
+        }
+
+        assertEquals(pictures.size(), 1);
+        assertEquals(pictures.get(0).getPictureData().getFileName(), "image1.png");
+
+        slide0.removeShape(pictures.get(0));
+
+        assertEquals(slide0.getShapes().size(), 2);
+
+        assertRelation(slide0, "/ppt/slides/slide1.xml", null);
+        assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
+        assertNull(slide0.getRelationById("rId2"));
+        assertEquals(slide0.getRelations().size(), 1);
+
+        // Save and re-load
+        XMLSlideShow ss3 = XSLFTestDataSamples.writeOutAndReadBack(ss2);
+        ss2.close();
+        assertEquals(1, ss3.getSlides().size());
+
+        slide0 = ss3.getSlides().get(0);
+
+        assertRelation(slide0, "/ppt/slides/slide1.xml", null);
+        assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
+        assertEquals(slide0.getShapes().size(), 2);
+        ss3.close();
+    }
 
     @Test
     public void bug61589() throws IOException {
diff --git a/test-data/slideshow/bug62736.pptx b/test-data/slideshow/bug62736.pptx
new file mode 100644 (file)
index 0000000..5593bff
Binary files /dev/null and b/test-data/slideshow/bug62736.pptx differ