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 8.8KB

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