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
This commit is contained in:
Dominik Stadler 2016-03-13 21:30:27 +00:00
parent 0a5dcbec1b
commit 3c1052e375
2 changed files with 48 additions and 6 deletions

View 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();

View 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());
}
}