You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSLFPowerPointExtractor.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xslf.extractor;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.apache.poi.POITestCase.assertNotContained;
  18. import static org.junit.Assert.assertEquals;
  19. import static org.junit.Assert.assertFalse;
  20. import static org.junit.Assert.assertNotNull;
  21. import static org.junit.Assert.assertTrue;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import org.apache.poi.POIDataSamples;
  25. import org.apache.poi.POITextExtractor;
  26. import org.apache.poi.extractor.ExtractorFactory;
  27. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  28. import org.apache.poi.openxml4j.opc.OPCPackage;
  29. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  30. import org.apache.xmlbeans.XmlException;
  31. import org.junit.Test;
  32. /**
  33. * Tests for XSLFPowerPointExtractor
  34. */
  35. public class TestXSLFPowerPointExtractor {
  36. private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  37. /**
  38. * Get text out of the simple file
  39. * @throws XmlException
  40. * @throws OpenXML4JException
  41. */
  42. @Test
  43. public void testGetSimpleText()
  44. throws IOException, XmlException, OpenXML4JException {
  45. XMLSlideShow xmlA = openPPTX("sample.pptx");
  46. @SuppressWarnings("resource")
  47. OPCPackage pkg = xmlA.getPackage();
  48. new XSLFPowerPointExtractor(xmlA).close();
  49. new XSLFPowerPointExtractor(pkg).close();
  50. XSLFPowerPointExtractor extractor =
  51. new XSLFPowerPointExtractor(xmlA);
  52. extractor.getText();
  53. String text = extractor.getText();
  54. assertTrue(text.length() > 0);
  55. // Check Basics
  56. assertTrue(text.startsWith("Lorem ipsum dolor sit amet\n"));
  57. assertContains(text, "amet\n\n");
  58. // Our placeholder master text
  59. // This shouldn't show up in the output
  60. // String masterText =
  61. // "Click to edit Master title style\n" +
  62. // "Click to edit Master subtitle style\n" +
  63. // "\n\n\n\n\n\n" +
  64. // "Click to edit Master title style\n" +
  65. // "Click to edit Master text styles\n" +
  66. // "Second level\n" +
  67. // "Third level\n" +
  68. // "Fourth level\n" +
  69. // "Fifth level\n";
  70. // Just slides, no notes
  71. text = extractor.getText(true, false, false);
  72. String slideText =
  73. "Lorem ipsum dolor sit amet\n" +
  74. "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
  75. "\n" +
  76. "Lorem ipsum dolor sit amet\n" +
  77. "Lorem\n" +
  78. "ipsum\n" +
  79. "dolor\n" +
  80. "sit\n" +
  81. "amet\n" +
  82. "\n";
  83. assertEquals(slideText, text);
  84. // Just notes, no slides
  85. text = extractor.getText(false, true);
  86. assertEquals("\n\n\n\n", text);
  87. // Both
  88. text = extractor.getText(true, true, false);
  89. String bothText =
  90. "Lorem ipsum dolor sit amet\n" +
  91. "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
  92. "\n\n\n" +
  93. "Lorem ipsum dolor sit amet\n" +
  94. "Lorem\n" +
  95. "ipsum\n" +
  96. "dolor\n" +
  97. "sit\n" +
  98. "amet\n" +
  99. "\n\n\n";
  100. assertEquals(bothText, text);
  101. // With Slides and Master Text
  102. text = extractor.getText(true, false, true);
  103. String smText =
  104. "Lorem ipsum dolor sit amet\n" +
  105. "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
  106. "\n" +
  107. "Lorem ipsum dolor sit amet\n" +
  108. "Lorem\n" +
  109. "ipsum\n" +
  110. "dolor\n" +
  111. "sit\n" +
  112. "amet\n" +
  113. "\n";
  114. assertEquals(smText, text);
  115. // With Slides, Notes and Master Text
  116. text = extractor.getText(true, true, true);
  117. String snmText =
  118. "Lorem ipsum dolor sit amet\n" +
  119. "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
  120. "\n" +
  121. "\n\n" +
  122. "Lorem ipsum dolor sit amet\n" +
  123. "Lorem\n" +
  124. "ipsum\n" +
  125. "dolor\n" +
  126. "sit\n" +
  127. "amet\n" +
  128. "\n\n\n";
  129. assertEquals(snmText, text);
  130. // Via set defaults
  131. extractor.setSlidesByDefault(false);
  132. extractor.setNotesByDefault(true);
  133. text = extractor.getText();
  134. assertEquals("\n\n\n\n", text);
  135. extractor.close();
  136. xmlA.close();
  137. }
  138. public void testGetComments() throws IOException {
  139. XMLSlideShow xml = openPPTX("45545_Comment.pptx");
  140. XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
  141. String text = extractor.getText();
  142. assertTrue(text.length() > 0);
  143. // Check comments are there
  144. assertContains(text, "testdoc");
  145. assertContains(text, "test phrase");
  146. // Check the authors came through too
  147. assertContains(text, "XPVMWARE01");
  148. extractor.close();
  149. xml.close();
  150. }
  151. public void testGetMasterText() throws Exception {
  152. XMLSlideShow xml = openPPTX("WithMaster.pptx");
  153. XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
  154. extractor.setSlidesByDefault(true);
  155. extractor.setNotesByDefault(false);
  156. extractor.setMasterByDefault(true);
  157. String text = extractor.getText();
  158. assertTrue(text.length() > 0);
  159. // Check master text is there
  160. assertContains(text, "Footer from the master slide");
  161. // Theme text shouldn't show up
  162. // String themeText =
  163. // "Theme Master Title\n" +
  164. // "Theme Master first level\n" +
  165. // "And the 2nd level\n" +
  166. // "Our 3rd level goes here\n" +
  167. // "And onto the 4th, such fun....\n" +
  168. // "Finally is the Fifth level\n";
  169. // Check the whole text
  170. String wholeText =
  171. "First page title\n" +
  172. "First page subtitle\n" +
  173. "This is the Master Title\n" +
  174. "This text comes from the Master Slide\n" +
  175. "\n" +
  176. // TODO Detect we didn't have a title, and include the master one
  177. "2nd page subtitle\n" +
  178. "Footer from the master slide\n" +
  179. "This is the Master Title\n" +
  180. "This text comes from the Master Slide\n";
  181. assertEquals(wholeText, text);
  182. extractor.close();
  183. xml.close();
  184. }
  185. @Test
  186. public void testTable() throws Exception {
  187. XMLSlideShow xml = openPPTX("present1.pptx");
  188. XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
  189. String text = extractor.getText();
  190. assertTrue(text.length() > 0);
  191. // Check comments are there
  192. assertTrue("Unable to find expected word in text\n" + text, text.contains("TEST"));
  193. extractor.close();
  194. xml.close();
  195. }
  196. /**
  197. * Test that we can get the text from macro enabled,
  198. * template, theme, slide enabled etc formats, as
  199. * well as from the normal file
  200. */
  201. @Test
  202. public void testDifferentSubformats() throws Exception {
  203. String[] extensions = new String[] {
  204. "pptx", "pptm", "ppsm", "ppsx", "thmx",
  205. // "xps" - Doesn't have a core document
  206. };
  207. for(String extension : extensions) {
  208. String filename = "testPPT." + extension;
  209. XMLSlideShow xml = openPPTX(filename);
  210. XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
  211. String text = extractor.getText();
  212. if (extension.equals("thmx")) {
  213. // Theme file doesn't have any textual content
  214. assertEquals(0, text.length());
  215. continue;
  216. }
  217. assertTrue(text.length() > 0);
  218. assertTrue(
  219. "Text missing for " + filename + "\n" + text,
  220. text.contains("Attachment Test")
  221. );
  222. assertTrue(
  223. "Text missing for " + filename + "\n" + text,
  224. text.contains("This is a test file data with the same content")
  225. );
  226. assertTrue(
  227. "Text missing for " + filename + "\n" + text,
  228. text.contains("content parsing")
  229. );
  230. assertTrue(
  231. "Text missing for " + filename + "\n" + text,
  232. text.contains("Different words to test against")
  233. );
  234. assertTrue(
  235. "Text missing for " + filename + "\n" + text,
  236. text.contains("Mystery")
  237. );
  238. extractor.close();
  239. xml.close();
  240. }
  241. }
  242. @Test
  243. public void test45541() throws Exception {
  244. // extract text from a powerpoint that has a header in the notes-element
  245. POITextExtractor extr = ExtractorFactory.createExtractor(
  246. slTests.getFile("45541_Header.pptx"));
  247. String text = extr.getText();
  248. assertNotNull(text);
  249. assertFalse("Had: " + text, text.contains("testdoc"));
  250. text = ((XSLFPowerPointExtractor)extr).getText(false, true);
  251. assertContains(text, "testdoc");
  252. extr.close();
  253. assertNotNull(text);
  254. // extract text from a powerpoint that has a footer in the master-slide
  255. extr = ExtractorFactory.createExtractor(
  256. slTests.getFile("45541_Footer.pptx"));
  257. text = extr.getText();
  258. assertNotContained(text, "testdoc");
  259. text = ((XSLFPowerPointExtractor)extr).getText(false, true);
  260. assertNotContained(text, "testdoc");
  261. text = ((XSLFPowerPointExtractor)extr).getText(false, false, true);
  262. assertNotContained(text, "testdoc");
  263. extr.close();
  264. }
  265. @Test
  266. public void bug54570() throws IOException {
  267. XMLSlideShow xml = openPPTX("bug54570.pptx");
  268. XSLFPowerPointExtractor extractor = new XSLFPowerPointExtractor(xml);
  269. String text = extractor.getText();
  270. assertNotNull(text);
  271. extractor.close();
  272. xml.close();
  273. }
  274. private XMLSlideShow openPPTX(String file) throws IOException {
  275. InputStream is = slTests.openResourceAsStream(file);
  276. try {
  277. return new XMLSlideShow(is);
  278. } finally {
  279. is.close();
  280. }
  281. }
  282. }