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.

XSLFSlideShow.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.usermodel;
  16. import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import org.apache.poi.ooxml.POIXMLDocument;
  22. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  23. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  24. import org.apache.poi.openxml4j.opc.OPCPackage;
  25. import org.apache.poi.openxml4j.opc.PackagePart;
  26. import org.apache.poi.openxml4j.opc.PackageRelationship;
  27. import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
  28. import org.apache.poi.openxml4j.opc.TargetMode;
  29. import org.apache.poi.util.Internal;
  30. import org.apache.xmlbeans.XmlException;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentList;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesSlide;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTPresentation;
  34. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
  35. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdList;
  36. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
  37. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMaster;
  38. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdList;
  39. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListEntry;
  40. import org.openxmlformats.schemas.presentationml.x2006.main.CmLstDocument;
  41. import org.openxmlformats.schemas.presentationml.x2006.main.NotesDocument;
  42. import org.openxmlformats.schemas.presentationml.x2006.main.PresentationDocument;
  43. import org.openxmlformats.schemas.presentationml.x2006.main.SldDocument;
  44. import org.openxmlformats.schemas.presentationml.x2006.main.SldMasterDocument;
  45. /**
  46. * Experimental class to do low level processing of pptx files.
  47. *
  48. * Most users should use the higher level {@link XMLSlideShow} instead.
  49. *
  50. * If you are using these low level classes, then you
  51. * will almost certainly need to refer to the OOXML
  52. * specifications from
  53. * https://www.ecma-international.org/publications/standards/Ecma-376.htm
  54. *
  55. * WARNING - APIs expected to change rapidly
  56. */
  57. public class XSLFSlideShow extends POIXMLDocument {
  58. private final PresentationDocument presentationDoc;
  59. /**
  60. * The embedded OLE2 files in the OPC package
  61. */
  62. private final List<PackagePart> embeddedParts;
  63. public XSLFSlideShow(OPCPackage container) throws OpenXML4JException, IOException, XmlException {
  64. super(container);
  65. if(getCorePart().getContentType().equals(XSLFRelation.THEME_MANAGER.getContentType())) {
  66. rebase(getPackage());
  67. }
  68. try (InputStream stream = getCorePart().getInputStream()) {
  69. presentationDoc =
  70. PresentationDocument.Factory.parse(stream, DEFAULT_XML_OPTIONS);
  71. }
  72. embeddedParts = new LinkedList<>();
  73. for (CTSlideIdListEntry ctSlide : getSlideReferences().getSldIdArray()) {
  74. PackagePart corePart = getCorePart();
  75. PackagePart slidePart = corePart.getRelatedPart(corePart.getRelationship(ctSlide.getId2()));
  76. for(PackageRelationship rel : slidePart.getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
  77. if (TargetMode.EXTERNAL == rel.getTargetMode()) {
  78. continue;
  79. }
  80. // TODO: Add this reference to each slide as well
  81. embeddedParts.add(slidePart.getRelatedPart(rel));
  82. }
  83. for (PackageRelationship rel : slidePart.getRelationshipsByType(PACK_OBJECT_REL_TYPE)) {
  84. embeddedParts.add(slidePart.getRelatedPart(rel));
  85. }
  86. }
  87. }
  88. public XSLFSlideShow(String file) throws OpenXML4JException, IOException, XmlException {
  89. this(openPackage(file));
  90. }
  91. /**
  92. * Returns the low level presentation base object
  93. */
  94. @Internal
  95. public CTPresentation getPresentation() {
  96. return presentationDoc.getPresentation();
  97. }
  98. /**
  99. * Returns the references from the presentation to its
  100. * slides.
  101. * You'll need these to figure out the slide ordering,
  102. * and to get at the actual slides themselves
  103. */
  104. @Internal
  105. public CTSlideIdList getSlideReferences() {
  106. if(! getPresentation().isSetSldIdLst()) {
  107. getPresentation().setSldIdLst(CTSlideIdList.Factory.newInstance());
  108. }
  109. return getPresentation().getSldIdLst();
  110. }
  111. /**
  112. * Returns the references from the presentation to its
  113. * slide masters.
  114. * You'll need these to get at the actual slide
  115. * masters themselves
  116. */
  117. @Internal
  118. public CTSlideMasterIdList getSlideMasterReferences() {
  119. return getPresentation().getSldMasterIdLst();
  120. }
  121. public PackagePart getSlideMasterPart(CTSlideMasterIdListEntry master) throws IOException, XmlException {
  122. try {
  123. PackagePart corePart = getCorePart();
  124. return corePart.getRelatedPart(
  125. corePart.getRelationship(master.getId2())
  126. );
  127. } catch(InvalidFormatException e) {
  128. throw new XmlException(e);
  129. }
  130. }
  131. /**
  132. * Returns the low level slide master object from
  133. * the supplied slide master reference
  134. */
  135. @Internal
  136. public CTSlideMaster getSlideMaster(CTSlideMasterIdListEntry master) throws IOException, XmlException {
  137. PackagePart masterPart = getSlideMasterPart(master);
  138. try (InputStream stream = masterPart.getInputStream()) {
  139. SldMasterDocument masterDoc =
  140. SldMasterDocument.Factory.parse(stream, DEFAULT_XML_OPTIONS);
  141. return masterDoc.getSldMaster();
  142. }
  143. }
  144. public PackagePart getSlidePart(CTSlideIdListEntry slide) throws IOException, XmlException {
  145. try {
  146. PackagePart corePart = getCorePart();
  147. return corePart.getRelatedPart(corePart.getRelationship(slide.getId2()));
  148. } catch(InvalidFormatException e) {
  149. throw new XmlException(e);
  150. }
  151. }
  152. /**
  153. * Returns the low level slide object from
  154. * the supplied slide reference
  155. */
  156. @Internal
  157. public CTSlide getSlide(CTSlideIdListEntry slide) throws IOException, XmlException {
  158. PackagePart slidePart = getSlidePart(slide);
  159. try (InputStream stream = slidePart.getInputStream()) {
  160. SldDocument slideDoc = SldDocument.Factory.parse(stream, DEFAULT_XML_OPTIONS);
  161. return slideDoc.getSld();
  162. }
  163. }
  164. /**
  165. * Gets the PackagePart of the notes for the
  166. * given slide, or null if there isn't one.
  167. */
  168. public PackagePart getNodesPart(CTSlideIdListEntry parentSlide) throws IOException, XmlException {
  169. PackageRelationshipCollection notes;
  170. PackagePart slidePart = getSlidePart(parentSlide);
  171. try {
  172. notes = slidePart.getRelationshipsByType(XSLFRelation.NOTES.getRelation());
  173. } catch(InvalidFormatException e) {
  174. throw new IOException(e);
  175. }
  176. if(notes.isEmpty()) {
  177. // No notes for this slide
  178. return null;
  179. }
  180. if(notes.size() > 1) {
  181. throw new IOException("Expecting 0 or 1 notes for a slide, but found " + notes.size());
  182. }
  183. try {
  184. return slidePart.getRelatedPart(notes.getRelationship(0));
  185. } catch(InvalidFormatException e) {
  186. throw new IllegalStateException(e);
  187. }
  188. }
  189. /**
  190. * Returns the low level notes object for the given
  191. * slide, as found from the supplied slide reference
  192. */
  193. @Internal
  194. public CTNotesSlide getNotes(CTSlideIdListEntry slide) throws IOException, XmlException {
  195. PackagePart notesPart = getNodesPart(slide);
  196. if(notesPart == null)
  197. return null;
  198. try (InputStream stream = notesPart.getInputStream()) {
  199. NotesDocument notesDoc = NotesDocument.Factory.parse(stream, DEFAULT_XML_OPTIONS);
  200. return notesDoc.getNotes();
  201. }
  202. }
  203. /**
  204. * Returns all the comments for the given slide
  205. */
  206. @Internal
  207. public CTCommentList getSlideComments(CTSlideIdListEntry slide) throws IOException, XmlException {
  208. PackageRelationshipCollection commentRels;
  209. PackagePart slidePart = getSlidePart(slide);
  210. try {
  211. commentRels = slidePart.getRelationshipsByType(XSLFRelation.COMMENTS.getRelation());
  212. } catch(InvalidFormatException e) {
  213. throw new IOException(e);
  214. }
  215. if(commentRels.isEmpty()) {
  216. // No comments for this slide
  217. return null;
  218. }
  219. if(commentRels.size() > 1) {
  220. throw new IOException("Expecting 0 or 1 comments for a slide, but found " + commentRels.size());
  221. }
  222. try {
  223. PackagePart cPart = slidePart.getRelatedPart(
  224. commentRels.getRelationship(0)
  225. );
  226. try (InputStream stream = cPart.getInputStream()) {
  227. CmLstDocument commDoc = CmLstDocument.Factory.parse(stream, DEFAULT_XML_OPTIONS);
  228. return commDoc.getCmLst();
  229. }
  230. } catch(InvalidFormatException e) {
  231. throw new IOException(e);
  232. }
  233. }
  234. /**
  235. * Get the document's embedded files.
  236. */
  237. @Override
  238. public List<PackagePart> getAllEmbeddedParts() throws OpenXML4JException {
  239. return embeddedParts;
  240. }
  241. }