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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  59. * input format
  60. */
  61. public static Workbook create(boolean xssf) throws IOException {
  62. return wp(xssf ? FileMagic.OOXML : FileMagic.OLE2, WorkbookProvider::create);
  63. }
  64. /**
  65. * Creates a Workbook from the given POIFSFileSystem.
  66. *
  67. * <p>Note that in order to properly release resources the
  68. * Workbook should be closed after use.
  69. *
  70. * @param fs The {@link POIFSFileSystem} to read the document from
  71. *
  72. * @return The created workbook
  73. *
  74. * @throws IOException if an error occurs while reading the data
  75. * @throws RuntimeException a number of runtime exceptions can be thrown, especially if there are problems with the
  76. * input format
  77. */
  78. public static Workbook create(POIFSFileSystem fs) throws IOException {
  79. return create(fs, null);
  80. }
  81. /**
  82. * Creates a Workbook from the given POIFSFileSystem, which may
  83. * be password protected.
  84. *
  85. * <p>Note that in order to properly release resources the
  86. * Workbook should be closed after use.
  87. *
  88. * @param fs The {@link POIFSFileSystem} to read the document from
  89. * @param password The password that should be used or null if no password is necessary.
  90. *
  91. * @return The created Workbook
  92. *
  93. * @throws IOException if an error occurs while reading the data
  94. * @throws RuntimeException a number of runtime exceptions can be thrown, especially if there are problems with the
  95. * input format
  96. */
  97. private static Workbook create(final POIFSFileSystem fs, String password) throws IOException {
  98. return create(fs.getRoot(), password);
  99. }
  100. /**
  101. * Creates a Workbook from the given DirectoryNode.
  102. *
  103. * <p>Note that in order to properly release resources the
  104. * Workbook should be closed after use.
  105. *
  106. * @param root The {@link DirectoryNode} to start reading the document from
  107. *
  108. * @return The created Workbook
  109. *
  110. * @throws IOException if an error occurs while reading the data
  111. * @throws RuntimeException a number of other exceptions can be thrown, especially if there are problems with the
  112. * input format
  113. */
  114. public static Workbook create(final DirectoryNode root) throws IOException {
  115. return create(root, null);
  116. }
  117. /**
  118. * Creates a Workbook from the given DirectoryNode, which may
  119. * be password protected.
  120. *
  121. * <p>Note that in order to properly release resources the
  122. * Workbook should be closed after use.
  123. *
  124. * @param root The {@link DirectoryNode} to start reading the document from
  125. * @param password The password that should be used or null if no password is necessary.
  126. *
  127. * @return The created Workbook
  128. *
  129. * @throws IOException if an error occurs while reading the data
  130. * @throws RuntimeException a number of runtime exceptions can be thrown, especially if there are problems with the
  131. * input format
  132. */
  133. public static Workbook create(final DirectoryNode root, String password) throws IOException {
  134. // Encrypted OOXML files go inside OLE2 containers, is this one?
  135. if (root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE)) {
  136. return wp(FileMagic.OOXML, w -> w.create(root, password));
  137. } else {
  138. return wp(FileMagic.OLE2, w -> w.create(root, password));
  139. }
  140. }
  141. /**
  142. * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
  143. * the given InputStream.
  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. * Workbook 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.
  154. *
  155. * @param inp The {@link InputStream} to read data from.
  156. *
  157. * @return The created Workbook
  158. *
  159. * @throws IOException if an error occurs while reading the data
  160. * @throws EncryptedDocumentException If the Workbook given is password protected
  161. * @throws EmptyFileException If the given data is empty
  162. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  163. * input format
  164. */
  165. public static Workbook create(InputStream inp) throws IOException, EncryptedDocumentException {
  166. return create(inp, null);
  167. }
  168. /**
  169. * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
  170. * the given InputStream, which may be password protected.
  171. *
  172. * <p>Your input stream MUST either support mark/reset, or
  173. * be wrapped as a {@link BufferedInputStream}!
  174. * Note that using an {@link InputStream} has a higher memory footprint
  175. * than using a {@link File}.</p>
  176. *
  177. * <p>Note that in order to properly release resources the
  178. * Workbook should be closed after use. Note also that loading
  179. * from an InputStream requires more memory than loading
  180. * from a File, so prefer {@link #create(File)} where possible.</p>
  181. *
  182. * @param inp The {@link InputStream} to read data from.
  183. * @param password The password that should be used or null if no password is necessary.
  184. *
  185. * @return The created Workbook
  186. *
  187. * @throws IOException if an error occurs while reading the data
  188. * @throws EncryptedDocumentException If the wrong password is given for a protected file
  189. * @throws EmptyFileException If the given data is empty
  190. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  191. * input format
  192. */
  193. public static Workbook create(InputStream inp, String password) throws IOException, EncryptedDocumentException {
  194. InputStream is = FileMagic.prepareToCheckMagic(inp);
  195. byte[] emptyFileCheck = new byte[1];
  196. is.mark(emptyFileCheck.length);
  197. if (is.read(emptyFileCheck) < emptyFileCheck.length) {
  198. throw new EmptyFileException();
  199. }
  200. is.reset();
  201. final FileMagic fm = FileMagic.valueOf(is);
  202. if (FileMagic.OOXML == fm) {
  203. return wp(fm, w -> w.create(is));
  204. }
  205. if (FileMagic.OLE2 != fm) {
  206. throw new IOException("Can't open workbook - unsupported file type: "+fm);
  207. }
  208. POIFSFileSystem poifs = new POIFSFileSystem(is);
  209. DirectoryNode root = poifs.getRoot();
  210. boolean isOOXML = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
  211. return wp(isOOXML ? FileMagic.OOXML : fm, w -> w.create(root, password));
  212. }
  213. /**
  214. * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
  215. * the given File, which must exist and be readable.
  216. * <p>Note that in order to properly release resources the
  217. * Workbook should be closed after use.
  218. *
  219. * @param file The file to read data from.
  220. *
  221. * @return The created Workbook
  222. *
  223. * @throws IOException if an error occurs while reading the data
  224. * @throws EncryptedDocumentException If the Workbook given is password protected
  225. * @throws EmptyFileException If the given data is empty
  226. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  227. * input format
  228. */
  229. public static Workbook create(File file) throws IOException, EncryptedDocumentException {
  230. return create(file, null);
  231. }
  232. /**
  233. * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
  234. * the given File, which must exist and be readable, and
  235. * may be password protected
  236. * <p>Note that in order to properly release resources the
  237. * Workbook should be closed after use.
  238. *
  239. * @param file The file to read data from.
  240. * @param password The password that should be used or null if no password is necessary.
  241. *
  242. * @return The created Workbook
  243. *
  244. * @throws IOException if an error occurs while reading the data
  245. * @throws EncryptedDocumentException If the wrong password is given for a protected file
  246. * @throws EmptyFileException If the given data is empty
  247. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  248. * input format
  249. */
  250. public static Workbook create(File file, String password) throws IOException, EncryptedDocumentException {
  251. return create(file, password, false);
  252. }
  253. /**
  254. * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
  255. * the given File, which must exist and be readable, and
  256. * may be password protected
  257. * <p>Note that in order to properly release resources the
  258. * Workbook should be closed after use.
  259. *
  260. * @param file The file to read data from.
  261. * @param password The password that should be used or null if no password is necessary.
  262. * @param readOnly If the Workbook should be opened in read-only mode to avoid writing back
  263. * changes when the document is closed.
  264. *
  265. * @return The created Workbook
  266. *
  267. * @throws IOException if an error occurs while reading the data
  268. * @throws EncryptedDocumentException If the wrong password is given for a protected file
  269. * @throws EmptyFileException If the given data is empty
  270. * @throws RuntimeException a number of other runtime exceptions can be thrown, especially if there are problems with the
  271. * input format
  272. */
  273. public static Workbook create(File file, String password, boolean readOnly) throws IOException, EncryptedDocumentException {
  274. if (!file.exists()) {
  275. throw new FileNotFoundException(file.toString());
  276. }
  277. if (file.length() == 0) {
  278. throw new EmptyFileException(file);
  279. }
  280. FileMagic fm = FileMagic.valueOf(file);
  281. if (fm == FileMagic.OOXML) {
  282. return wp(fm, w -> w.create(file, password, readOnly));
  283. } else if (fm == FileMagic.OLE2) {
  284. final boolean ooxmlEnc;
  285. try (POIFSFileSystem fs = new POIFSFileSystem(file, true)) {
  286. DirectoryNode root = fs.getRoot();
  287. ooxmlEnc = root.hasEntry(DEFAULT_POIFS_ENTRY) || root.hasEntry(OOXML_PACKAGE);
  288. }
  289. return wp(ooxmlEnc ? FileMagic.OOXML : fm, w -> w.create(file, password, readOnly));
  290. } else {
  291. throw new IOException("Can't open workbook - unsupported file type: "+fm);
  292. }
  293. }
  294. private static Workbook wp(FileMagic fm, ProviderMethod fun) throws IOException {
  295. for (WorkbookProvider prov : Singleton.INSTANCE.provider) {
  296. if (prov.accepts(fm)) {
  297. return fun.create(prov);
  298. }
  299. }
  300. throw new IOException("Your InputStream was neither an OLE2 stream, nor an OOXML stream " +
  301. "or you haven't provide the poi-ooxml*.jar in the classpath/modulepath - FileMagic: " + fm +
  302. ", having providers: " + Singleton.INSTANCE.provider);
  303. }
  304. public static void addProvider(WorkbookProvider provider){
  305. Singleton.INSTANCE.provider.add(provider);
  306. }
  307. public static void removeProvider(Class<? extends WorkbookProvider> provider){
  308. Singleton.INSTANCE.provider.removeIf(p -> p.getClass().isAssignableFrom(provider));
  309. }
  310. }