PageRenderTime 119ms CodeModel.GetById 18ms RepoModel.GetById 3ms app.codeStats 0ms

/pptx/src/org/apache/poi/xslf/XSLFSlideShow.java

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