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.

XSLFFileHandler.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.stress;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotNull;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.InputStream;
  21. import org.apache.poi.extractor.ExtractorFactory;
  22. import org.apache.poi.ooxml.POIXMLException;
  23. import org.apache.poi.sl.extractor.SlideShowExtractor;
  24. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  25. import org.apache.poi.xslf.usermodel.XSLFSlideShow;
  26. import org.junit.jupiter.api.Test;
  27. class XSLFFileHandler extends SlideShowHandler {
  28. @Override
  29. public void handleFile(InputStream stream, String path) throws Exception {
  30. try (XMLSlideShow slide = new XMLSlideShow(stream);
  31. XSLFSlideShow slideInner = new XSLFSlideShow(slide.getPackage())) {
  32. ;
  33. assertNotNull(slideInner.getPresentation());
  34. assertNotNull(slideInner.getSlideMasterReferences());
  35. assertNotNull(slideInner.getSlideReferences());
  36. new POIXMLDocumentHandler().handlePOIXMLDocument(slide);
  37. POIXMLDocumentHandler.cursorRecursive(slide.getCTPresentation());
  38. handleSlideShow(slide);
  39. } catch (POIXMLException e) {
  40. Exception cause = (Exception)e.getCause();
  41. throw cause == null ? e : cause;
  42. }
  43. }
  44. @Override
  45. public void handleExtracting(File file) throws Exception {
  46. super.handleExtracting(file);
  47. // additionally try the other getText() methods
  48. try (SlideShowExtractor<?,?> extractor = (SlideShowExtractor<?, ?>) ExtractorFactory.createExtractor(file)) {
  49. assertNotNull(extractor);
  50. extractor.setSlidesByDefault(true);
  51. extractor.setNotesByDefault(true);
  52. extractor.setMasterByDefault(true);
  53. assertNotNull(extractor.getText());
  54. extractor.setSlidesByDefault(false);
  55. extractor.setNotesByDefault(false);
  56. extractor.setMasterByDefault(false);
  57. assertEquals("", extractor.getText(), "With all options disabled we should not get text");
  58. }
  59. }
  60. // a test-case to test this locally without executing the full TestAllFiles
  61. @Override
  62. @Test
  63. void test() throws Exception {
  64. File file = new File("test-data/slideshow/ca.ubc.cs.people_~emhill_presentations_HowWeRefactor.pptx");
  65. try (InputStream stream = new FileInputStream(file)) {
  66. handleFile(stream, file.getPath());
  67. }
  68. handleExtracting(file);
  69. }
  70. }