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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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.hslf.extractor;
  16. import java.io.InputStream;
  17. import java.util.List;
  18. import junit.framework.TestCase;
  19. import org.apache.poi.POIDataSamples;
  20. import org.apache.poi.hslf.HSLFSlideShow;
  21. import org.apache.poi.hslf.model.OLEShape;
  22. import org.apache.poi.hslf.usermodel.SlideShow;
  23. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  24. import org.apache.poi.hwpf.HWPFDocument;
  25. import org.apache.poi.poifs.filesystem.DirectoryNode;
  26. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  27. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  28. /**
  29. * Tests that the extractor correctly gets the text out of our sample file
  30. *
  31. * @author Nick Burch (nick at torchbox dot com)
  32. */
  33. public final class TestExtractor extends TestCase {
  34. /** Extractor primed on the 2 page basic test data */
  35. private PowerPointExtractor ppe;
  36. private static final String expectText = "This is a test title\nThis is a test subtitle\nThis is on page 1\nThis is the title on page 2\nThis is page two\nIt has several blocks of text\nNone of them have formatting\n";
  37. /** Extractor primed on the 1 page but text-box'd test data */
  38. private PowerPointExtractor ppe2;
  39. private static final String expectText2 = "Hello, World!!!\nI am just a poor boy\nThis is Times New Roman\nPlain Text \n";
  40. /** Where our embeded files live */
  41. //private String pdirname;
  42. private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  43. //private String pdirname;
  44. protected void setUp() throws Exception {
  45. ppe = new PowerPointExtractor(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
  46. ppe2 = new PowerPointExtractor(slTests.openResourceAsStream("with_textbox.ppt"));
  47. }
  48. private static void assertContains(String haystack, String needle) {
  49. assertContains(
  50. "Unable to find expected text '" + needle + "' in text:\n" + haystack,
  51. haystack, needle
  52. );
  53. }
  54. private static void assertContains(String reason, String haystack, String needle) {
  55. assertTrue(reason, haystack.contains(needle));
  56. }
  57. public void testReadSheetText() {
  58. // Basic 2 page example
  59. String sheetText = ppe.getText();
  60. ensureTwoStringsTheSame(expectText, sheetText);
  61. // 1 page example with text boxes
  62. sheetText = ppe2.getText();
  63. ensureTwoStringsTheSame(expectText2, sheetText);
  64. }
  65. public void testReadNoteText() {
  66. // Basic 2 page example
  67. String notesText = ppe.getNotes();
  68. String expectText = "These are the notes for page 1\nThese are the notes on page two, again lacking formatting\n";
  69. ensureTwoStringsTheSame(expectText, notesText);
  70. // Other one doesn't have notes
  71. notesText = ppe2.getNotes();
  72. expectText = "";
  73. ensureTwoStringsTheSame(expectText, notesText);
  74. }
  75. public void testReadBoth() {
  76. String[] slText = new String[] {
  77. "This is a test title\nThis is a test subtitle\nThis is on page 1\n",
  78. "This is the title on page 2\nThis is page two\nIt has several blocks of text\nNone of them have formatting\n"
  79. };
  80. String[] ntText = new String[] {
  81. "These are the notes for page 1\n",
  82. "These are the notes on page two, again lacking formatting\n"
  83. };
  84. ppe.setSlidesByDefault(true);
  85. ppe.setNotesByDefault(false);
  86. assertEquals(slText[0]+slText[1], ppe.getText());
  87. ppe.setSlidesByDefault(false);
  88. ppe.setNotesByDefault(true);
  89. assertEquals(ntText[0]+ntText[1], ppe.getText());
  90. ppe.setSlidesByDefault(true);
  91. ppe.setNotesByDefault(true);
  92. assertEquals(slText[0]+slText[1]+"\n"+ntText[0]+ntText[1], ppe.getText());
  93. }
  94. /**
  95. * Test that when presented with a PPT file missing the odd
  96. * core record, we can still get the rest of the text out
  97. * @throws Exception
  98. */
  99. public void testMissingCoreRecords() throws Exception {
  100. ppe = new PowerPointExtractor(slTests.openResourceAsStream("missing_core_records.ppt"));
  101. String text = ppe.getText(true, false);
  102. String nText = ppe.getNotes();
  103. assertNotNull(text);
  104. assertNotNull(nText);
  105. // Notes record were corrupt, so don't expect any
  106. assertEquals(nText.length(), 0);
  107. // Slide records were fine
  108. assertTrue(text.startsWith("Using Disease Surveillance and Response"));
  109. }
  110. private void ensureTwoStringsTheSame(String exp, String act) {
  111. assertEquals(exp.length(),act.length());
  112. char[] expC = exp.toCharArray();
  113. char[] actC = act.toCharArray();
  114. for(int i=0; i<expC.length; i++) {
  115. assertEquals("Char " + i, expC[i], actC[i]);
  116. }
  117. assertEquals(exp,act);
  118. }
  119. public void testExtractFromEmbeded() throws Exception {
  120. POIFSFileSystem fs = new POIFSFileSystem(
  121. POIDataSamples.getSpreadSheetInstance().openResourceAsStream("excel_with_embeded.xls")
  122. );
  123. HSLFSlideShow ss;
  124. DirectoryNode dirA = (DirectoryNode)
  125. fs.getRoot().getEntry("MBD0000A3B6");
  126. DirectoryNode dirB = (DirectoryNode)
  127. fs.getRoot().getEntry("MBD0000A3B3");
  128. assertNotNull(dirA.getEntry("PowerPoint Document"));
  129. assertNotNull(dirB.getEntry("PowerPoint Document"));
  130. // Check the first file
  131. ss = new HSLFSlideShow(dirA);
  132. ppe = new PowerPointExtractor(ss);
  133. assertEquals("Sample PowerPoint file\nThis is the 1st file\nNot much too it\n",
  134. ppe.getText(true, false)
  135. );
  136. // And the second
  137. ss = new HSLFSlideShow(dirB);
  138. ppe = new PowerPointExtractor(ss);
  139. assertEquals("Sample PowerPoint file\nThis is the 2nd file\nNot much too it either\n",
  140. ppe.getText(true, false)
  141. );
  142. }
  143. /**
  144. * A powerpoint file with embeded powerpoint files
  145. */
  146. public void testExtractFromOwnEmbeded() throws Exception {
  147. String path = "ppt_with_embeded.ppt";
  148. ppe = new PowerPointExtractor(POIDataSamples.getSlideShowInstance().openResourceAsStream(path));
  149. List<OLEShape> shapes = ppe.getOLEShapes();
  150. assertEquals("Expected 6 ole shapes in " + path, 6, shapes.size());
  151. int num_ppt = 0, num_doc = 0, num_xls = 0;
  152. for(OLEShape ole : shapes) {
  153. String name = ole.getInstanceName();
  154. InputStream data = ole.getObjectData().getData();
  155. if ("Worksheet".equals(name)) {
  156. HSSFWorkbook wb = new HSSFWorkbook(data);
  157. num_xls++;
  158. } else if ("Document".equals(name)) {
  159. HWPFDocument doc = new HWPFDocument(data);
  160. num_doc++;
  161. } else if ("Presentation".equals(name)) {
  162. num_ppt++;
  163. SlideShow ppt = new SlideShow(data);
  164. }
  165. }
  166. assertEquals("Expected 2 embedded Word Documents", 2, num_doc);
  167. assertEquals("Expected 2 embedded Excel Spreadsheets", 2, num_xls);
  168. assertEquals("Expected 2 embedded PowerPoint Presentations", 2, num_ppt);
  169. }
  170. /**
  171. * From bug #45543
  172. */
  173. public void testWithComments() throws Exception {
  174. ppe = new PowerPointExtractor(slTests.openResourceAsStream("WithComments.ppt"));
  175. String text = ppe.getText();
  176. assertFalse("Comments not in by default", text.contains("This is a test comment"));
  177. ppe.setCommentsByDefault(true);
  178. text = ppe.getText();
  179. assertContains(text, "This is a test comment");
  180. // And another file
  181. ppe = new PowerPointExtractor(slTests.openResourceAsStream("45543.ppt"));
  182. text = ppe.getText();
  183. assertFalse("Comments not in by default", text.contains("testdoc"));
  184. ppe.setCommentsByDefault(true);
  185. text = ppe.getText();
  186. assertContains(text, "testdoc");
  187. }
  188. /**
  189. * From bug #45537
  190. */
  191. public void testHeaderFooter() throws Exception {
  192. String text;
  193. // With a header on the notes
  194. HSLFSlideShow hslf = new HSLFSlideShow(slTests.openResourceAsStream("45537_Header.ppt"));
  195. SlideShow ss = new SlideShow(hslf);
  196. assertNotNull(ss.getNotesHeadersFooters());
  197. assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getHeaderText());
  198. ppe = new PowerPointExtractor(hslf);
  199. text = ppe.getText();
  200. assertFalse("Header shouldn't be there by default\n" + text, text.contains("testdoc"));
  201. assertFalse("Header shouldn't be there by default\n" + text, text.contains("test phrase"));
  202. ppe.setNotesByDefault(true);
  203. text = ppe.getText();
  204. assertContains(text, "testdoc");
  205. assertContains(text, "test phrase");
  206. // And with a footer, also on notes
  207. hslf = new HSLFSlideShow(slTests.openResourceAsStream("45537_Footer.ppt"));
  208. ss = new SlideShow(hslf);
  209. assertNotNull(ss.getNotesHeadersFooters());
  210. assertEquals("testdoc test phrase", ss.getNotesHeadersFooters().getFooterText());
  211. ppe = new PowerPointExtractor(slTests.openResourceAsStream("45537_Footer.ppt"));
  212. text = ppe.getText();
  213. assertFalse("Header shouldn't be there by default\n" + text, text.contains("testdoc"));
  214. assertFalse("Header shouldn't be there by default\n" + text, text.contains("test phrase"));
  215. ppe.setNotesByDefault(true);
  216. text = ppe.getText();
  217. assertContains(text, "testdoc");
  218. assertContains(text, "test phrase");
  219. }
  220. public void testSlideMasterText() throws Exception {
  221. String masterTitleText = "This is the Master Title";
  222. String masterRandomText = "This text comes from the Master Slide";
  223. String masterFooterText = "Footer from the master slide";
  224. HSLFSlideShow hslf = new HSLFSlideShow(slTests.openResourceAsStream("WithMaster.ppt"));
  225. ppe = new PowerPointExtractor(hslf);
  226. String text = ppe.getText();
  227. //assertContains(text, masterTitleText); // TODO Is this available in PPT?
  228. //assertContains(text, masterRandomText); // TODO Extract
  229. assertContains(text, masterFooterText);
  230. }
  231. public void testMasterText() throws Exception {
  232. ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt"));
  233. // Initially not there
  234. String text = ppe.getText();
  235. assertFalse(text.contains("Master Header Text"));
  236. // Enable, shows up
  237. ppe.setMasterByDefault(true);
  238. text = ppe.getText();
  239. assertTrue(text.contains("Master Header Text"));
  240. // Now with another file only containing master text
  241. // Will always show up
  242. String masterText = "Footer from the master slide";
  243. HSLFSlideShow hslf = new HSLFSlideShow(slTests.openResourceAsStream("WithMaster.ppt"));
  244. ppe = new PowerPointExtractor(hslf);
  245. text = ppe.getText();
  246. assertContains(text.toLowerCase(), "master");
  247. assertContains(text, masterText);
  248. }
  249. /**
  250. * Tests that we can work with both {@link POIFSFileSystem}
  251. * and {@link NPOIFSFileSystem}
  252. */
  253. public void testDifferentPOIFS() throws Exception {
  254. // Open the two filesystems
  255. DirectoryNode[] files = new DirectoryNode[2];
  256. files[0] = (new POIFSFileSystem(slTests.openResourceAsStream("basic_test_ppt_file.ppt"))).getRoot();
  257. files[1] = (new NPOIFSFileSystem(slTests.getFile("basic_test_ppt_file.ppt"))).getRoot();
  258. // Open directly
  259. for(DirectoryNode dir : files) {
  260. PowerPointExtractor extractor = new PowerPointExtractor(dir, null);
  261. assertEquals(expectText, extractor.getText());
  262. }
  263. // Open via a HWPFDocument
  264. for(DirectoryNode dir : files) {
  265. HSLFSlideShow slideshow = new HSLFSlideShow(dir);
  266. PowerPointExtractor extractor = new PowerPointExtractor(slideshow);
  267. assertEquals(expectText, extractor.getText());
  268. }
  269. }
  270. }