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.

SlideShowFactory.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.sl.usermodel;
  16. import static org.apache.poi.extractor.ExtractorFactory.OOXML_PACKAGE;
  17. import static org.apache.poi.poifs.crypt.EncryptionInfo.ENCRYPTION_INFO_ENTRY;
  18. import java.io.BufferedInputStream;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.ServiceLoader;
  26. import org.apache.poi.EmptyFileException;
  27. import org.apache.poi.EncryptedDocumentException;
  28. import org.apache.poi.poifs.crypt.Decryptor;
  29. import org.apache.poi.poifs.filesystem.DirectoryNode;
  30. import org.apache.poi.poifs.filesystem.FileMagic;
  31. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  32. public final class SlideShowFactory {
  33. private static class Singleton {
  34. private static final SlideShowFactory INSTANCE = new SlideShowFactory();
  35. }
  36. private interface ProviderMethod {
  37. SlideShow<?,?> create(SlideShowProvider<?,?> prov) throws IOException;
  38. }
  39. private final List<SlideShowProvider<?,?>> provider = new ArrayList<>();
  40. private SlideShowFactory() {
  41. ClassLoader cl = SlideShowFactory.class.getClassLoader();
  42. ServiceLoader.load(SlideShowProvider.class, cl).forEach(provider::add);
  43. }
  44. /**
  45. * Create a new empty SlideShow, either XSLF or HSLF depending
  46. * on the parameter
  47. *
  48. * @param XSLF If an XSLFSlideShow or a HSLFSlideShow should be created
  49. *
  50. * @return The created SlideShow
  51. *
  52. * @throws IOException if an error occurs while creating the objects
  53. */
  54. public static SlideShow<?,?> create(boolean XSLF) throws IOException {
  55. return wp(XSLF ? FileMagic.OOXML : FileMagic.OLE2, SlideShowProvider::create);
  56. }
  57. /**
  58. * Creates a HSLFSlideShow from the given POIFSFileSystem<p>
  59. *
  60. * Note that in order to properly release resources the
  61. * SlideShow should be closed after use.
  62. *
  63. * @param fs The {@link POIFSFileSystem} to read the document from
  64. *
  65. * @return The created SlideShow
  66. *
  67. * @throws IOException if an error occurs while reading the data
  68. */
  69. public static SlideShow<?,?> create(POIFSFileSystem fs) throws IOException {
  70. return create(fs, null);
  71. }
  72. /**
  73. * Creates a SlideShow from the given POIFSFileSystem, which may
  74. * be password protected
  75. *
  76. * @param fs The {@link POIFSFileSystem} to read the document from
  77. * @param password The password that should be used or null if no password is necessary.
  78. *
  79. * @return The created SlideShow
  80. *
  81. * @throws IOException if an error occurs while reading the data
  82. */
  83. private static SlideShow<?,?> create(final POIFSFileSystem fs, String password) throws IOException {
  84. return create(fs.getRoot(), password);
  85. }
  86. /**
  87. * Creates a SlideShow from the given DirectoryNode.
  88. *
  89. * @param root The {@link DirectoryNode} to start reading the document from
  90. *
  91. * @return The created SlideShow
  92. *
  93. * @throws IOException if an error occurs while reading the data
  94. */
  95. public static SlideShow<?,?> create(final DirectoryNode root) throws IOException {
  96. return create(root, null);
  97. }
  98. /**
  99. * Creates a SlideShow from the given DirectoryNode, which may
  100. * be password protected
  101. *
  102. * @param root The {@link DirectoryNode} to start reading the document from
  103. * @param password The password that should be used or null if no password is necessary.
  104. *
  105. * @return The created SlideShow
  106. *
  107. * @throws IOException if an error occurs while reading the data
  108. */
  109. public static SlideShow<?,?> create(final DirectoryNode root, String password) throws IOException {
  110. // Encrypted OOXML files go inside OLE2 containers, is this one?
  111. if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE)) {
  112. return wp(FileMagic.OOXML, w -> w.create(root, password));
  113. } else {
  114. return wp(FileMagic.OLE2, w -> w.create(root, password));
  115. }
  116. }
  117. /**
  118. * Creates the appropriate HSLFSlideShow / XSLFSlideShow from
  119. * the given InputStream.
  120. *
  121. * <p>Your input stream MUST either support mark/reset, or
  122. * be wrapped as a {@link BufferedInputStream}!
  123. * Note that using an {@link InputStream} has a higher memory footprint
  124. * than using a {@link File}.</p>
  125. *
  126. * <p>Note that in order to properly release resources the
  127. * SlideShow should be closed after use. Note also that loading
  128. * from an InputStream requires more memory than loading
  129. * from a File, so prefer {@link #create(File)} where possible.
  130. *
  131. * @param inp The {@link InputStream} to read data from.
  132. *
  133. * @return The created SlideShow
  134. *
  135. * @throws IOException if an error occurs while reading the data
  136. * @throws EncryptedDocumentException If the SlideShow<?,?> given is password protected
  137. */
  138. public static SlideShow<?,?> create(InputStream inp) throws IOException, EncryptedDocumentException {
  139. return create(inp, null);
  140. }
  141. /**
  142. * Creates the appropriate HSLFSlideShow / XSLFSlideShow from
  143. * the given InputStream, which may be password protected.
  144. *
  145. * <p>Your input stream MUST either support mark/reset, or
  146. * be wrapped as a {@link BufferedInputStream}!
  147. * Note that using an {@link InputStream} has a higher memory footprint
  148. * than using a {@link File}.</p>
  149. *
  150. * <p>Note that in order to properly release resources the
  151. * SlideShow should be closed after use. Note also that loading
  152. * from an InputStream requires more memory than loading
  153. * from a File, so prefer {@link #create(File)} where possible.</p>
  154. *
  155. * @param inp The {@link InputStream} to read data from.
  156. * @param password The password that should be used or null if no password is necessary.
  157. *
  158. * @return The created SlideShow
  159. *
  160. * @throws IOException if an error occurs while reading the data
  161. * @throws EncryptedDocumentException If the wrong password is given for a protected file
  162. */
  163. public static SlideShow<?,?> create(InputStream inp, String password) throws IOException, EncryptedDocumentException {
  164. InputStream is = FileMagic.prepareToCheckMagic(inp);
  165. byte[] emptyFileCheck = new byte[1];
  166. is.mark(emptyFileCheck.length);
  167. if (is.read(emptyFileCheck) < emptyFileCheck.length) {
  168. throw new EmptyFileException();
  169. }
  170. is.reset();
  171. final FileMagic fm = FileMagic.valueOf(is);
  172. if (FileMagic.OOXML == fm) {
  173. return wp(fm, w -> w.create(is));
  174. }
  175. if (FileMagic.OLE2 != fm) {
  176. throw new IOException("Can't open SlideShow - unsupported file type: "+fm);
  177. }
  178. POIFSFileSystem poifs = new POIFSFileSystem(is);
  179. boolean isOOXML = poifs.getRoot().hasEntry(ENCRYPTION_INFO_ENTRY);
  180. return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(poifs.getRoot(), password));
  181. }
  182. /**
  183. * Creates the appropriate HSLFSlideShow / XSLFSlideShow from
  184. * the given File, which must exist and be readable.
  185. * <p>Note that in order to properly release resources the
  186. * SlideShow should be closed after use.
  187. *
  188. * @param file The file to read data from.
  189. *
  190. * @return The created SlideShow
  191. *
  192. * @throws IOException if an error occurs while reading the data
  193. * @throws EncryptedDocumentException If the SlideShow given is password protected
  194. */
  195. public static SlideShow<?,?> create(File file) throws IOException, EncryptedDocumentException {
  196. return create(file, null);
  197. }
  198. /**
  199. * Creates the appropriate HSLFSlideShow / XSLFSlideShow from
  200. * the given File, which must exist and be readable, and
  201. * may be password protected
  202. * <p>Note that in order to properly release resources the
  203. * SlideShow should be closed after use.
  204. *
  205. * @param file The file to read data from.
  206. * @param password The password that should be used or null if no password is necessary.
  207. *
  208. * @return The created SlideShow
  209. *
  210. * @throws IOException if an error occurs while reading the data
  211. * @throws EncryptedDocumentException If the wrong password is given for a protected file
  212. */
  213. public static SlideShow<?,?> create(File file, String password) throws IOException, EncryptedDocumentException {
  214. return create(file, password, false);
  215. }
  216. /**
  217. * Creates the appropriate HSLFSlideShow / XSLFSlideShow from
  218. * the given File, which must exist and be readable, and
  219. * may be password protected
  220. * <p>Note that in order to properly release resources the
  221. * SlideShow should be closed after use.
  222. *
  223. * @param file The file to read data from.
  224. * @param password The password that should be used or null if no password is necessary.
  225. * @param readOnly If the SlideShow should be opened in read-only mode to avoid writing back
  226. * changes when the document is closed.
  227. *
  228. * @return The created SlideShow
  229. *
  230. * @throws IOException if an error occurs while reading the data
  231. * @throws EncryptedDocumentException If the wrong password is given for a protected file
  232. */
  233. public static SlideShow<?,?> create(File file, String password, boolean readOnly) throws IOException, EncryptedDocumentException {
  234. if (!file.exists()) {
  235. throw new FileNotFoundException(file.toString());
  236. }
  237. if (file.length() == 0) {
  238. throw new EmptyFileException();
  239. }
  240. FileMagic fm = FileMagic.valueOf(file);
  241. if (fm == FileMagic.OOXML) {
  242. return wp(fm, w -> w.create(file, password, readOnly));
  243. } else if (fm == FileMagic.OLE2) {
  244. final boolean ooxmlEnc;
  245. try (POIFSFileSystem fs = new POIFSFileSystem(file, true)) {
  246. DirectoryNode root = fs.getRoot();
  247. ooxmlEnc = root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
  248. }
  249. return wp(ooxmlEnc ? FileMagic.OOXML : fm, w -> w.create(file, password, readOnly));
  250. }
  251. return null;
  252. }
  253. private static SlideShow<?,?> wp(FileMagic fm, SlideShowFactory.ProviderMethod fun) throws IOException {
  254. for (SlideShowProvider<?,?> prov : SlideShowFactory.Singleton.INSTANCE.provider) {
  255. if (prov.accepts(fm)) {
  256. return fun.create(prov);
  257. }
  258. }
  259. throw new IOException("Your InputStream was neither an OLE2 stream, nor an OOXML stream " +
  260. "or you haven't provide the poi-ooxml*.jar in the classpath/modulepath - FileMagic: "+fm);
  261. }
  262. public static void addProvider(SlideShowProvider<?,?> provider){
  263. Singleton.INSTANCE.provider.add(provider);
  264. }
  265. public static void removeProvider(Class<? extends SlideShowProvider> provider){
  266. Singleton.INSTANCE.provider.removeIf(p -> p.getClass().getName().equals(provider.getName()));
  267. }
  268. }