From 56a5f076e58029c46e7684a8e75a1372c635ad2b Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 13 Mar 2016 21:30:27 +0000 Subject: [PATCH] Bug 55791: Avoid using an existing file-name when creating a new slide, it could still be left over from previous partial removal git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734862 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xslf/usermodel/XMLSlideShow.java | 32 +++++++++++++++++-- .../org/apache/poi/xslf/TestXSLFBugs.java | 22 +++++++++++-- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java index 2084031b07..64177bcb26 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java @@ -103,7 +103,7 @@ implements SlideShow { 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 { 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 { 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(); diff --git a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java index ca46f295e5..42f6543c55 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java @@ -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()); + } } -- 2.39.5