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.

MainExtractorFactory.java 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.extractor;
  16. import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import org.apache.poi.hssf.extractor.EventBasedExcelExtractor;
  21. import org.apache.poi.hssf.extractor.ExcelExtractor;
  22. import org.apache.poi.hssf.extractor.OldExcelExtractor;
  23. import org.apache.poi.hssf.model.InternalWorkbook;
  24. import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
  25. import org.apache.poi.poifs.filesystem.DirectoryNode;
  26. import org.apache.poi.poifs.filesystem.FileMagic;
  27. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  28. /**
  29. * ExtractorFactory for HSSF and Old Excel format
  30. */
  31. public class MainExtractorFactory implements ExtractorProvider {
  32. @Override
  33. public boolean accepts(FileMagic fm) {
  34. return FileMagic.OLE2 == fm;
  35. }
  36. @SuppressWarnings({"java:S2095"})
  37. @Override
  38. public POITextExtractor create(File file, String password) throws IOException {
  39. return create(new POIFSFileSystem(file, true).getRoot(), password);
  40. }
  41. @Override
  42. public POITextExtractor create(InputStream inputStream, String password) throws IOException {
  43. return create(new POIFSFileSystem(inputStream).getRoot(), password);
  44. }
  45. @Override
  46. public POITextExtractor create(DirectoryNode poifsDir, String password) throws IOException {
  47. final String oldPW = Biff8EncryptionKey.getCurrentUserPassword();
  48. try {
  49. Biff8EncryptionKey.setCurrentUserPassword(password);
  50. // Look for certain entries in the stream, to figure it out from
  51. for (String workbookName : WORKBOOK_DIR_ENTRY_NAMES) {
  52. if (poifsDir.hasEntry(workbookName)) {
  53. return ExtractorFactory.getPreferEventExtractor() ? new EventBasedExcelExtractor(poifsDir) : new ExcelExtractor(poifsDir);
  54. }
  55. }
  56. if (poifsDir.hasEntry(InternalWorkbook.OLD_WORKBOOK_DIR_ENTRY_NAME)) {
  57. return new OldExcelExtractor(poifsDir);
  58. }
  59. } finally {
  60. Biff8EncryptionKey.setCurrentUserPassword(oldPW);
  61. }
  62. return null;
  63. }
  64. }