]> source.dussan.org Git - poi.git/commitdiff
Bug 55791: Avoid using an existing file-name when creating a new slide, it could...
authorDominik Stadler <centic@apache.org>
Sun, 13 Mar 2016 21:30:27 +0000 (21:30 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 13 Mar 2016 21:30:27 +0000 (21:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734862 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java

index 2084031b07380296b8b9a5f708390c5ce6dc8f5d..64177bcb263b6a88b789e8dc874e02b4db79f593 100644 (file)
@@ -103,7 +103,7 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
         this(PackageHelper.open(is));
     }
 
-    static final OPCPackage empty() {
+    static OPCPackage empty() {
         InputStream is = XMLSlideShow.class.getResourceAsStream("empty.pptx");
         if (is == null) {
             throw new POIXMLException("Missing resource 'empty.pptx'");
@@ -215,11 +215,37 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
                 slideNumber = (int)Math.max(slideId.getId() + 1, slideNumber);
                 cnt++;
             }
+
+            // Bug 55791: We also need to check that the resulting file name is not already taken
+            // this can happen when removing/adding slides
+            while(true) {
+                String slideName = XSLFRelation.SLIDE.getFileName(cnt);
+                boolean found = false;
+                for (POIXMLDocumentPart relation : getRelations()) {
+                    if (relation.getPackagePart() != null &&
+                            slideName.equals(relation.getPackagePart().getPartName().getName())) {
+                        // name is taken => try next one
+                        found = true;
+                        break;
+                    }
+                }
+
+                if(!found &&
+                        getPackage().getPartsByName(Pattern.compile(Pattern.quote(slideName))).size() > 0) {
+                    // name is taken => try next one
+                    found = true;
+                }
+
+                if (!found) {
+                    break;
+                }
+                cnt++;
+            }
         }
 
         RelationPart rp = createRelationship(
                 XSLFRelation.SLIDE, XSLFFactory.getInstance(), cnt, false);
-        XSLFSlide slide = (XSLFSlide)rp.getDocumentPart();
+        XSLFSlide slide = rp.getDocumentPart();
 
         CTSlideIdListEntry slideId = slideList.addNewSldId();
         slideId.setId(slideNumber);
@@ -286,7 +312,7 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
     public void createNotesMaster() {
         RelationPart rp = createRelationship
             (XSLFRelation.NOTES_MASTER, XSLFFactory.getInstance(), 1, false);
-        _notesMaster = (XSLFNotesMaster)rp.getDocumentPart();
+        _notesMaster = rp.getDocumentPart();
         
         CTNotesMasterIdList notesMasterIdList = _presentation.addNewNotesMasterIdLst();
         CTNotesMasterIdListEntry notesMasterId = notesMasterIdList.addNewNotesMasterId();
index ca46f295e50b417eaefaf5d0930c9ef0b90d31e9..42f6543c550eb3b7626052386847f66cb4d5ab6e 100644 (file)
@@ -18,9 +18,7 @@ package org.apache.poi.xslf;
 
 import static junit.framework.TestCase.assertEquals;
 import static org.apache.poi.POITestCase.assertContains;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
 
 import java.awt.Color;
 import java.awt.Dimension;
@@ -412,4 +410,22 @@ public class TestXSLFBugs {
         
         ss.close();
     }
+
+    @Test
+    public void bug55791a() {
+        XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("45541_Footer.pptx");
+        removeAndCreateSlide(ppt);
+    }
+
+    @Test
+    public void bug55791b() {
+        XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("SampleShow.pptx");
+        removeAndCreateSlide(ppt);
+    }
+
+    private void removeAndCreateSlide(XMLSlideShow ppt) {
+        assertTrue(ppt.getSlides().size() > 1);
+        ppt.removeSlide(1);
+        assertNotNull(ppt.createSlide());
+    }
 }