#54570 - InvalidFormatException because of Absolute URI forbidden

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1744137 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-05-16 21:04:00 +00:00
parent 33241e821f
commit 61321b4b37
4 changed files with 240 additions and 251 deletions

View File

@ -191,7 +191,7 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/ppt/media/.*?")); List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/ppt/media/.*?"));
_pictures = new ArrayList<XSLFPictureData>(mediaParts.size()); _pictures = new ArrayList<XSLFPictureData>(mediaParts.size());
for(PackagePart part : mediaParts){ for(PackagePart part : mediaParts){
XSLFPictureData pd = new XSLFPictureData(part, null); XSLFPictureData pd = new XSLFPictureData(part);
pd.setIndex(_pictures.size()); pd.setIndex(_pictures.size());
_pictures.add(pd); _pictures.add(pd);
} }

View File

@ -16,36 +16,43 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.xslf.extractor; package org.apache.poi.xslf.extractor;
import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertNotContained;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.POITextExtractor; import org.apache.poi.POITextExtractor;
import org.apache.poi.extractor.ExtractorFactory; import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xslf.usermodel.XSLFSlideShow; import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.xmlbeans.XmlException;
import junit.framework.TestCase; import org.junit.Test;
/** /**
* Tests for HXFPowerPointExtractor * Tests for XSLFPowerPointExtractor
*/ */
public class TestXSLFPowerPointExtractor extends TestCase { public class TestXSLFPowerPointExtractor {
/** private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
* A simple file
*/
private XSLFSlideShow xmlA;
private OPCPackage pkg;
private POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
protected void setUp() throws Exception {
slTests = POIDataSamples.getSlideShowInstance();
pkg = OPCPackage.open(slTests.openResourceAsStream("sample.pptx"));
xmlA = new XSLFSlideShow(pkg);
}
/** /**
* Get text out of the simple file * Get text out of the simple file
* @throws XmlException
* @throws OpenXML4JException
*/ */
public void testGetSimpleText() throws Exception { @Test
public void testGetSimpleText()
throws IOException, XmlException, OpenXML4JException {
XMLSlideShow xmlA = openPPTX("sample.pptx");
@SuppressWarnings("resource")
OPCPackage pkg = xmlA.getPackage();
new XSLFPowerPointExtractor(xmlA).close(); new XSLFPowerPointExtractor(xmlA).close();
new XSLFPowerPointExtractor(pkg).close(); new XSLFPowerPointExtractor(pkg).close();
@ -58,24 +65,24 @@ public class TestXSLFPowerPointExtractor extends TestCase {
// Check Basics // Check Basics
assertTrue(text.startsWith("Lorem ipsum dolor sit amet\n")); assertTrue(text.startsWith("Lorem ipsum dolor sit amet\n"));
assertTrue(text.contains("amet\n\n")); assertContains(text, "amet\n\n");
// Our placeholder master text // Our placeholder master text
// This shouldn't show up in the output // This shouldn't show up in the output
String masterText = // String masterText =
"Click to edit Master title style\n" + // "Click to edit Master title style\n" +
"Click to edit Master subtitle style\n" + // "Click to edit Master subtitle style\n" +
"\n\n\n\n\n\n" + // "\n\n\n\n\n\n" +
"Click to edit Master title style\n" + // "Click to edit Master title style\n" +
"Click to edit Master text styles\n" + // "Click to edit Master text styles\n" +
"Second level\n" + // "Second level\n" +
"Third level\n" + // "Third level\n" +
"Fourth level\n" + // "Fourth level\n" +
"Fifth level\n"; // "Fifth level\n";
// Just slides, no notes // Just slides, no notes
text = extractor.getText(true, false, false); text = extractor.getText(true, false, false);
assertEquals( String slideText =
"Lorem ipsum dolor sit amet\n" + "Lorem ipsum dolor sit amet\n" +
"Nunc at risus vel erat tempus posuere. Aenean non ante.\n" + "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
"\n" + "\n" +
@ -85,19 +92,16 @@ public class TestXSLFPowerPointExtractor extends TestCase {
"dolor\n" + "dolor\n" +
"sit\n" + "sit\n" +
"amet\n" + "amet\n" +
"\n" "\n";
, text assertEquals(slideText, text);
);
// Just notes, no slides // Just notes, no slides
text = extractor.getText(false, true); text = extractor.getText(false, true);
assertEquals( assertEquals("\n\n\n\n", text);
"\n\n\n\n", text
);
// Both // Both
text = extractor.getText(true, true, false); text = extractor.getText(true, true, false);
assertEquals( String bothText =
"Lorem ipsum dolor sit amet\n" + "Lorem ipsum dolor sit amet\n" +
"Nunc at risus vel erat tempus posuere. Aenean non ante.\n" + "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
"\n\n\n" + "\n\n\n" +
@ -107,13 +111,12 @@ public class TestXSLFPowerPointExtractor extends TestCase {
"dolor\n" + "dolor\n" +
"sit\n" + "sit\n" +
"amet\n" + "amet\n" +
"\n\n\n" "\n\n\n";
, text assertEquals(bothText, text);
);
// With Slides and Master Text // With Slides and Master Text
text = extractor.getText(true, false, true); text = extractor.getText(true, false, true);
assertEquals( String smText =
"Lorem ipsum dolor sit amet\n" + "Lorem ipsum dolor sit amet\n" +
"Nunc at risus vel erat tempus posuere. Aenean non ante.\n" + "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
"\n" + "\n" +
@ -123,13 +126,12 @@ public class TestXSLFPowerPointExtractor extends TestCase {
"dolor\n" + "dolor\n" +
"sit\n" + "sit\n" +
"amet\n" + "amet\n" +
"\n" "\n";
, text assertEquals(smText, text);
);
// With Slides, Notes and Master Text // With Slides, Notes and Master Text
text = extractor.getText(true, true, true); text = extractor.getText(true, true, true);
assertEquals( String snmText =
"Lorem ipsum dolor sit amet\n" + "Lorem ipsum dolor sit amet\n" +
"Nunc at risus vel erat tempus posuere. Aenean non ante.\n" + "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
"\n" + "\n" +
@ -140,45 +142,40 @@ public class TestXSLFPowerPointExtractor extends TestCase {
"dolor\n" + "dolor\n" +
"sit\n" + "sit\n" +
"amet\n" + "amet\n" +
"\n\n\n" "\n\n\n";
, text assertEquals(snmText, text);
);
// Via set defaults // Via set defaults
extractor.setSlidesByDefault(false); extractor.setSlidesByDefault(false);
extractor.setNotesByDefault(true); extractor.setNotesByDefault(true);
text = extractor.getText(); text = extractor.getText();
assertEquals( assertEquals("\n\n\n\n", text);
"\n\n\n\n", text
);
extractor.close(); extractor.close();
xmlA.close();
} }
public void testGetComments() throws Exception { public void testGetComments() throws IOException {
XSLFSlideShow xml = XMLSlideShow xml = openPPTX("45545_Comment.pptx");
new XSLFSlideShow(OPCPackage.open(slTests.openResourceAsStream("45545_Comment.pptx"))); XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
XSLFPowerPointExtractor extractor =
new XSLFPowerPointExtractor(xml);
String text = extractor.getText(); String text = extractor.getText();
assertTrue(text.length() > 0); assertTrue(text.length() > 0);
// Check comments are there // Check comments are there
assertTrue("Unable to find expected word in text\n" + text, text.contains("testdoc")); assertContains(text, "testdoc");
assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase")); assertContains(text, "test phrase");
// Check the authors came through too // Check the authors came through too
assertTrue("Unable to find expected word in text\n" + text, text.contains("XPVMWARE01")); assertContains(text, "XPVMWARE01");
extractor.close(); extractor.close();
xml.close();
} }
public void testGetMasterText() throws Exception { public void testGetMasterText() throws Exception {
XSLFSlideShow xml = XMLSlideShow xml = openPPTX("WithMaster.pptx");
new XSLFSlideShow(OPCPackage.open(slTests.openResourceAsStream("WithMaster.pptx"))); XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
XSLFPowerPointExtractor extractor =
new XSLFPowerPointExtractor(xml);
extractor.setSlidesByDefault(true); extractor.setSlidesByDefault(true);
extractor.setNotesByDefault(false); extractor.setNotesByDefault(false);
extractor.setMasterByDefault(true); extractor.setMasterByDefault(true);
@ -187,20 +184,19 @@ public class TestXSLFPowerPointExtractor extends TestCase {
assertTrue(text.length() > 0); assertTrue(text.length() > 0);
// Check master text is there // Check master text is there
assertTrue("Unable to find expected word in text\n" + text, assertContains(text, "Footer from the master slide");
text.contains("Footer from the master slide"));
// Theme text shouldn't show up // Theme text shouldn't show up
String themeText = // String themeText =
"Theme Master Title\n" + // "Theme Master Title\n" +
"Theme Master first level\n" + // "Theme Master first level\n" +
"And the 2nd level\n" + // "And the 2nd level\n" +
"Our 3rd level goes here\n" + // "Our 3rd level goes here\n" +
"And onto the 4th, such fun....\n" + // "And onto the 4th, such fun....\n" +
"Finally is the Fifth level\n"; // "Finally is the Fifth level\n";
// Check the whole text // Check the whole text
assertEquals( String wholeText =
"First page title\n" + "First page title\n" +
"First page subtitle\n" + "First page subtitle\n" +
"This is the Master Title\n" + "This is the Master Title\n" +
@ -210,18 +206,17 @@ public class TestXSLFPowerPointExtractor extends TestCase {
"2nd page subtitle\n" + "2nd page subtitle\n" +
"Footer from the master slide\n" + "Footer from the master slide\n" +
"This is the Master Title\n" + "This is the Master Title\n" +
"This text comes from the Master Slide\n" "This text comes from the Master Slide\n";
, text assertEquals(wholeText, text);
);
extractor.close(); extractor.close();
xml.close();
} }
@Test
public void testTable() throws Exception { public void testTable() throws Exception {
XSLFSlideShow xml = XMLSlideShow xml = openPPTX("present1.pptx");
new XSLFSlideShow(OPCPackage.open(slTests.openResourceAsStream("present1.pptx"))); XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
XSLFPowerPointExtractor extractor =
new XSLFPowerPointExtractor(xml);
String text = extractor.getText(); String text = extractor.getText();
assertTrue(text.length() > 0); assertTrue(text.length() > 0);
@ -230,6 +225,7 @@ public class TestXSLFPowerPointExtractor extends TestCase {
assertTrue("Unable to find expected word in text\n" + text, text.contains("TEST")); assertTrue("Unable to find expected word in text\n" + text, text.contains("TEST"));
extractor.close(); extractor.close();
xml.close();
} }
/** /**
@ -237,18 +233,16 @@ public class TestXSLFPowerPointExtractor extends TestCase {
* template, theme, slide enabled etc formats, as * template, theme, slide enabled etc formats, as
* well as from the normal file * well as from the normal file
*/ */
@Test
public void testDifferentSubformats() throws Exception { public void testDifferentSubformats() throws Exception {
String[] extensions = new String[] { String[] extensions = new String[] {
"pptx", "pptm", "ppsm", "ppsx", "pptx", "pptm", "ppsm", "ppsx", "thmx",
"thmx", // "xps" - Doesn't have a core document
//"xps" // Doesn't have a core document
}; };
for(String extension : extensions) { for(String extension : extensions) {
String filename = "testPPT." + extension; String filename = "testPPT." + extension;
XSLFSlideShow xml = XMLSlideShow xml = openPPTX(filename);
new XSLFSlideShow(OPCPackage.open(slTests.openResourceAsStream(filename))); XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
XSLFPowerPointExtractor extractor =
new XSLFPowerPointExtractor(xml);
String text = extractor.getText(); String text = extractor.getText();
if (extension.equals("thmx")) { if (extension.equals("thmx")) {
@ -280,38 +274,56 @@ public class TestXSLFPowerPointExtractor extends TestCase {
); );
extractor.close(); extractor.close();
xml.close();
} }
} }
@Test
public void test45541() throws Exception { public void test45541() throws Exception {
// extract text from a powerpoint that has a header in the notes-element // extract text from a powerpoint that has a header in the notes-element
POITextExtractor extr = ExtractorFactory.createExtractor(slTests POITextExtractor extr = ExtractorFactory.createExtractor(
.openResourceAsStream("45541_Header.pptx")); slTests.getFile("45541_Header.pptx"));
String text = extr.getText(); String text = extr.getText();
assertNotNull(text); assertNotNull(text);
assertFalse("Had: " + text, text.contains("testdoc")); assertFalse("Had: " + text, text.contains("testdoc"));
text = ((XSLFPowerPointExtractor)extr).getText(false, true); text = ((XSLFPowerPointExtractor)extr).getText(false, true);
assertNotNull(text); assertContains(text, "testdoc");
assertTrue("Had: " + text, text.contains("testdoc"));
extr.close(); extr.close();
assertNotNull(text); assertNotNull(text);
// extract text from a powerpoint that has a footer in the master-slide // extract text from a powerpoint that has a footer in the master-slide
extr = ExtractorFactory.createExtractor(slTests extr = ExtractorFactory.createExtractor(
.openResourceAsStream("45541_Footer.pptx")); slTests.getFile("45541_Footer.pptx"));
text = extr.getText(); text = extr.getText();
assertNotNull(text); assertNotContained(text, "testdoc");
assertFalse("Had " + text, text.contains("testdoc"));
text = ((XSLFPowerPointExtractor)extr).getText(false, true); text = ((XSLFPowerPointExtractor)extr).getText(false, true);
assertNotNull(text); assertNotContained(text, "testdoc");
assertFalse("Had: " + text, text.contains("testdoc"));
text = ((XSLFPowerPointExtractor)extr).getText(false, false, true); text = ((XSLFPowerPointExtractor)extr).getText(false, false, true);
assertNotNull(text); assertNotContained(text, "testdoc");
assertFalse("Had: " + text, text.contains("testdoc"));
extr.close(); extr.close();
} }
@Test
public void bug54570() throws IOException {
XMLSlideShow xml = openPPTX("bug54570.pptx");
XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
String text = extractor.getText();
assertNotNull(text);
extractor.close();
xml.close();
}
private XMLSlideShow openPPTX(String file) throws IOException {
InputStream is = slTests.openResourceAsStream(file);
try {
return new XMLSlideShow(is);
} finally {
is.close();
}
}
} }

View File

@ -18,6 +18,7 @@
package org.apache.poi; package org.apache.poi;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -26,56 +27,30 @@ import java.lang.reflect.Method;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedActionException; import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import org.apache.poi.util.SuppressForbidden; import org.apache.poi.util.SuppressForbidden;
/** /**
* Parent class for POI JUnit TestCases, which provide additional * Util class for POI JUnit TestCases, which provide additional features
* features
*/ */
public class POITestCase { public final class POITestCase {
public static void assertContains(String haystack, String needle) { public static void assertContains(String haystack, String needle) {
assertNotNull(haystack);
assertTrue( assertTrue(
"Unable to find expected text '" + needle + "' in text:\n" + haystack, "Unable to find expected text '" + needle + "' in text:\n" + haystack,
haystack.contains(needle) haystack.contains(needle)
); );
} }
public static void assertNotContained(String haystack, String needle) { public static void assertNotContained(String haystack, String needle) {
assertNotNull(haystack);
assertFalse( assertFalse(
"Unexpectedly found text '" + needle + "' in text:\n" + haystack, "Unexpectedly found text '" + needle + "' in text:\n" + haystack,
haystack.contains(needle) haystack.contains(needle)
); );
} }
public static <T> void assertContains(T needle, T[] haystack)
{
// Check
for (T thing : haystack) {
if (thing.equals(needle)) {
return;
}
}
// Failed, try to build a nice error
StringBuilder sb = new StringBuilder();
sb.append("Unable to find ").append(needle).append(" in [");
for (T thing : haystack) {
sb.append(" ").append(thing.toString()).append(" ,");
}
sb.setCharAt(sb.length()-1, ']');
fail(sb.toString());
}
public static <T> void assertContains(T needle, Collection<T> haystack) {
if (haystack.contains(needle)) {
return;
}
fail("Unable to find " + needle + " in " + haystack);
}
/** /**
* @param map haystack * @param map haystack
* @param key needle * @param key needle
@ -87,7 +62,8 @@ public class POITestCase {
fail("Unable to find " + key + " in " + map); fail("Unable to find " + key + " in " + map);
} }
/** Utility method to get the value of a private/protected field. /**
* Utility method to get the value of a private/protected field.
* Only use this method in test cases!!! * Only use this method in test cases!!!
*/ */
public static <R,T> R getFieldValue(final Class<? super T> clazz, final T instance, final Class<R> fieldType, final String fieldName) { public static <R,T> R getFieldValue(final Class<? super T> clazz, final T instance, final Class<R> fieldType, final String fieldName) {
@ -108,7 +84,8 @@ public class POITestCase {
} }
} }
/** Utility method to call a private/protected method. /**
* Utility method to call a private/protected method.
* Only use this method in test cases!!! * Only use this method in test cases!!!
*/ */
public static <R,T> R callMethod(final Class<? super T> clazz, final T instance, final Class<R> returnType, final String methodName, public static <R,T> R callMethod(final Class<? super T> clazz, final T instance, final Class<R> returnType, final String methodName,

Binary file not shown.