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.

WorkbookFactory.java 12KB

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