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.

XSLFPowerPointExtractor.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 java.io.IOException;
  17. import org.apache.poi.POIXMLTextExtractor;
  18. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  19. import org.apache.poi.openxml4j.opc.OPCPackage;
  20. import org.apache.poi.sl.extractor.SlideShowExtractor;
  21. import org.apache.poi.util.Removal;
  22. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  23. import org.apache.poi.xslf.usermodel.XSLFRelation;
  24. import org.apache.poi.xslf.usermodel.XSLFShape;
  25. import org.apache.poi.xslf.usermodel.XSLFSlide;
  26. import org.apache.poi.xslf.usermodel.XSLFSlideShow;
  27. import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
  28. import org.apache.xmlbeans.XmlException;
  29. /**
  30. * Extractor for XSLF SlideShows
  31. *
  32. * @deprecated use {@link SlideShowExtractor}
  33. */
  34. @Deprecated
  35. @Removal(version="4.2.0")
  36. public class XSLFPowerPointExtractor extends POIXMLTextExtractor {
  37. public static final XSLFRelation[] SUPPORTED_TYPES = new XSLFRelation[]{
  38. XSLFRelation.MAIN, XSLFRelation.MACRO, XSLFRelation.MACRO_TEMPLATE,
  39. XSLFRelation.PRESENTATIONML, XSLFRelation.PRESENTATIONML_TEMPLATE,
  40. XSLFRelation.PRESENTATION_MACRO
  41. };
  42. private final SlideShowExtractor<XSLFShape, XSLFTextParagraph> delegate;
  43. private boolean slidesByDefault = true;
  44. private boolean notesByDefault;
  45. private boolean commentsByDefault;
  46. private boolean masterByDefault;
  47. @SuppressWarnings("WeakerAccess")
  48. public XSLFPowerPointExtractor(XMLSlideShow slideShow) {
  49. super(slideShow);
  50. delegate = new SlideShowExtractor<>(slideShow);
  51. }
  52. public XSLFPowerPointExtractor(XSLFSlideShow slideShow) {
  53. this(new XMLSlideShow(slideShow.getPackage()));
  54. }
  55. public XSLFPowerPointExtractor(OPCPackage container) throws XmlException, OpenXML4JException, IOException {
  56. this(new XSLFSlideShow(container));
  57. }
  58. public static void main(String[] args) throws Exception {
  59. if (args.length < 1) {
  60. System.err.println("Use:");
  61. System.err.println(" XSLFPowerPointExtractor <filename.pptx>");
  62. System.exit(1);
  63. }
  64. POIXMLTextExtractor extractor =
  65. new XSLFPowerPointExtractor(
  66. new XSLFSlideShow(args[0]));
  67. System.out.println(extractor.getText());
  68. extractor.close();
  69. }
  70. /**
  71. * Should a call to getText() return slide text?
  72. * Default is yes
  73. */
  74. @SuppressWarnings("WeakerAccess")
  75. public void setSlidesByDefault(final boolean slidesByDefault) {
  76. this.slidesByDefault = slidesByDefault;
  77. delegate.setSlidesByDefault(slidesByDefault);
  78. }
  79. /**
  80. * Should a call to getText() return notes text?
  81. * Default is no
  82. */
  83. @SuppressWarnings("WeakerAccess")
  84. public void setNotesByDefault(final boolean notesByDefault) {
  85. this.notesByDefault = notesByDefault;
  86. delegate.setNotesByDefault(notesByDefault);
  87. }
  88. /**
  89. * Should a call to getText() return comments text? Default is no
  90. */
  91. @SuppressWarnings({"WeakerAccess", "unused"})
  92. public void setCommentsByDefault(final boolean commentsByDefault) {
  93. this.commentsByDefault = commentsByDefault;
  94. delegate.setCommentsByDefault(commentsByDefault);
  95. }
  96. /**
  97. * Should a call to getText() return text from master? Default is no
  98. */
  99. @SuppressWarnings("WeakerAccess")
  100. public void setMasterByDefault(final boolean masterByDefault) {
  101. this.masterByDefault = masterByDefault;
  102. delegate.setMasterByDefault(masterByDefault);
  103. }
  104. /**
  105. * Gets the slide text, but not the notes text
  106. */
  107. @Override
  108. public String getText() {
  109. return delegate.getText();
  110. }
  111. /**
  112. * Gets the requested text from the file
  113. *
  114. * @param slideText Should we retrieve text from slides?
  115. * @param notesText Should we retrieve text from notes?
  116. */
  117. public String getText(final boolean slideText, final boolean notesText) {
  118. return getText(slideText, notesText, commentsByDefault, masterByDefault);
  119. }
  120. /**
  121. * Gets the requested text from the file
  122. *
  123. * @param slideText Should we retrieve text from slides?
  124. * @param notesText Should we retrieve text from notes?
  125. * @param masterText Should we retrieve text from master slides?
  126. * @return the extracted text
  127. */
  128. public String getText(boolean slideText, boolean notesText, boolean masterText) {
  129. return getText(slideText, notesText, commentsByDefault, masterText);
  130. }
  131. /**
  132. * Gets the requested text from the file
  133. *
  134. * @param slideText Should we retrieve text from slides?
  135. * @param notesText Should we retrieve text from notes?
  136. * @param commentText Should we retrieve text from comments?
  137. * @param masterText Should we retrieve text from master slides?
  138. * @return the extracted text
  139. */
  140. @SuppressWarnings("Duplicates")
  141. public String getText(boolean slideText, boolean notesText, boolean commentText, boolean masterText) {
  142. delegate.setSlidesByDefault(slideText);
  143. delegate.setNotesByDefault(notesText);
  144. delegate.setCommentsByDefault(commentText);
  145. delegate.setMasterByDefault(masterText);
  146. try {
  147. return delegate.getText();
  148. } finally {
  149. delegate.setSlidesByDefault(slidesByDefault);
  150. delegate.setNotesByDefault(notesByDefault);
  151. delegate.setCommentsByDefault(commentsByDefault);
  152. delegate.setMasterByDefault(masterByDefault);
  153. }
  154. }
  155. /**
  156. * Gets the requested text from the slide
  157. *
  158. * @param slide the slide to retrieve the text from
  159. * @param slideText Should we retrieve text from slides?
  160. * @param notesText Should we retrieve text from notes?
  161. * @param masterText Should we retrieve text from master slides?
  162. * @return the extracted text
  163. */
  164. public static String getText(XSLFSlide slide, boolean slideText, boolean notesText, boolean masterText) {
  165. return getText(slide, slideText, notesText, false, masterText);
  166. }
  167. /**
  168. * Gets the requested text from the slide
  169. *
  170. * @param slide the slide to retrieve the text from
  171. * @param slideText Should we retrieve text from slides?
  172. * @param notesText Should we retrieve text from notes?
  173. * @param commentText Should we retrieve text from comments?
  174. * @param masterText Should we retrieve text from master slides?
  175. * @return the extracted text
  176. */
  177. public static String getText(XSLFSlide slide, boolean slideText, boolean notesText, boolean commentText, boolean masterText) {
  178. final SlideShowExtractor<XSLFShape, XSLFTextParagraph> ex = new SlideShowExtractor<>(slide.getSlideShow());
  179. ex.setSlidesByDefault(slideText);
  180. ex.setNotesByDefault(notesText);
  181. ex.setCommentsByDefault(commentText);
  182. ex.setMasterByDefault(masterText);
  183. return ex.getText(slide);
  184. }
  185. }