您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractFileHandler.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.stress;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assumptions.assumeFalse;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.Arrays;
  25. import java.util.HashSet;
  26. import java.util.Set;
  27. import org.apache.poi.EncryptedDocumentException;
  28. import org.apache.poi.extractor.ExtractorFactory;
  29. import org.apache.poi.extractor.POIOLE2TextExtractor;
  30. import org.apache.poi.extractor.POITextExtractor;
  31. import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor;
  32. import org.apache.poi.hssf.extractor.EventBasedExcelExtractor;
  33. import org.apache.poi.ooxml.POIXMLException;
  34. import org.apache.poi.ss.extractor.ExcelExtractor;
  35. /**
  36. * Base class with things that can be run for any supported file handler
  37. * in the integration tests, mostly text-extraction related at the moment.
  38. */
  39. public abstract class AbstractFileHandler implements FileHandler {
  40. // some FileHandlers extend this list!?!
  41. protected static final Set<String> EXPECTED_EXTRACTOR_FAILURES = new HashSet<>(Arrays.asList(
  42. // password protected files without password
  43. // ... currently none ...
  44. // unsupported file-types, no supported OLE2 parts
  45. "hmef/quick-winmail.dat",
  46. "hmef/winmail-sample1.dat",
  47. "hmef/bug52400-winmail-simple.dat",
  48. "hmef/bug52400-winmail-with-attachments.dat",
  49. "hmef/bug63955-winmail.dat",
  50. "hpsf/Test0313rur.adm",
  51. "poifs/Notes.ole2",
  52. "poifs/64322.ole2",
  53. //commons-compress 1.21 no longer supports truncated files
  54. "document/truncated62886.docx"
  55. ));
  56. @Override
  57. public void handleExtracting(File file) throws Exception {
  58. boolean before = ExtractorFactory.getThreadPrefersEventExtractors();
  59. try {
  60. ExtractorFactory.setThreadPrefersEventExtractors(true);
  61. handleExtractingInternal(file);
  62. ExtractorFactory.setThreadPrefersEventExtractors(false);
  63. handleExtractingInternal(file);
  64. } finally {
  65. ExtractorFactory.setThreadPrefersEventExtractors(before);
  66. }
  67. /* Did fail for some documents with special XML contents...
  68. try {
  69. OOXMLPrettyPrint.main(new String[] { file.getAbsolutePath(),
  70. "/tmp/pretty-" + file.getName() });
  71. } catch (ZipException e) {
  72. // ignore, not a Zip/OOXML file
  73. }*/
  74. }
  75. private void handleExtractingInternal(File file) throws Exception {
  76. long length = file.length();
  77. long modified = file.lastModified();
  78. String fileAndParentName = file.getParentFile().getName() + "/" + file.getName();
  79. try {
  80. handleExtractingAsStream(file);
  81. // fix windows absolute paths for exception message tracking
  82. String relPath = file.getPath().replaceAll(".*test-data", "test-data").replace('\\', '/');
  83. try (POITextExtractor extractor = ExtractorFactory.createExtractor(file)) {
  84. assertNotNull(extractor, "Should get a POITextExtractor but had none for file " + relPath);
  85. assertNotNull(extractor.getText(), "Should get some text but had none for file " + relPath);
  86. // also try metadata
  87. POITextExtractor metadataExtractor = extractor.getMetadataTextExtractor();
  88. assertNotNull(metadataExtractor.getText());
  89. assertFalse(EXPECTED_EXTRACTOR_FAILURES.contains(fileAndParentName),
  90. "Expected Extraction to fail for file " + relPath + " and handler " + this + ", but did not fail!");
  91. assertEquals(length, file.length(), "File should not be modified by extractor");
  92. assertEquals(modified, file.lastModified(), "File should not be modified by extractor");
  93. if (extractor instanceof POIOLE2TextExtractor) {
  94. POIOLE2TextExtractor ole2Extractor = (POIOLE2TextExtractor) extractor;
  95. ole2Extractor.getRoot();
  96. if (!(ole2Extractor instanceof EventBasedExcelExtractor)) {
  97. ole2Extractor.getSummaryInformation();
  98. ole2Extractor.getDocSummaryInformation();
  99. }
  100. try (HPSFPropertiesExtractor hpsfExtractor = new HPSFPropertiesExtractor(ole2Extractor)) {
  101. assertNotNull(hpsfExtractor.getDocumentSummaryInformationText());
  102. assertNotNull(hpsfExtractor.getSummaryInformationText());
  103. String text = hpsfExtractor.getText();
  104. //System.out.println(text);
  105. assertNotNull(text);
  106. }
  107. if (ole2Extractor.getRoot() != null && !Boolean.getBoolean("scratchpad.ignore")) {
  108. POITextExtractor[] embedded = ExtractorFactory.getEmbeddedDocsTextExtractors(ole2Extractor);
  109. try {
  110. for (POITextExtractor poiTextExtractor : embedded) {
  111. poiTextExtractor.getText();
  112. poiTextExtractor.getDocument();
  113. poiTextExtractor.getFilesystem();
  114. POITextExtractor metaData = poiTextExtractor.getMetadataTextExtractor();
  115. metaData.getFilesystem();
  116. metaData.getText();
  117. }
  118. } finally {
  119. for (POITextExtractor embeddedExtractor : embedded) {
  120. embeddedExtractor.close();
  121. }
  122. }
  123. }
  124. }
  125. // test again with including formulas and cell-comments as this caused some bugs
  126. if (extractor instanceof ExcelExtractor &&
  127. // comment-extraction and formula extraction are not well supported in event based extraction
  128. !(extractor instanceof EventBasedExcelExtractor)) {
  129. ((ExcelExtractor) extractor).setFormulasNotResults(true);
  130. String text = extractor.getText();
  131. assertNotNull(text);
  132. // */
  133. ((ExcelExtractor) extractor).setIncludeCellComments(true);
  134. text = extractor.getText();
  135. assertNotNull(text);
  136. }
  137. }
  138. } catch (IOException | POIXMLException e) {
  139. Exception prevE = e;
  140. Throwable cause;
  141. while ((cause = prevE.getCause()) instanceof Exception) {
  142. if (cause instanceof IOException || cause instanceof POIXMLException) {
  143. prevE = (Exception)cause;
  144. } else {
  145. throw (Exception)cause;
  146. }
  147. }
  148. throw e;
  149. } catch (IllegalArgumentException e) {
  150. if(!EXPECTED_EXTRACTOR_FAILURES.contains(fileAndParentName)) {
  151. throw e;
  152. }
  153. } catch (EncryptedDocumentException e) {
  154. String msg = "org.apache.poi.EncryptedDocumentException: Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files";
  155. assumeFalse(msg.equals(e.getMessage()));
  156. throw e;
  157. } catch (IllegalStateException e) {
  158. if (!e.getMessage().contains("POI Scratchpad jar missing") || !Boolean.getBoolean("scratchpad.ignore")) {
  159. throw e;
  160. }
  161. }
  162. }
  163. private void handleExtractingAsStream(File file) throws IOException {
  164. try (InputStream stream = new FileInputStream(file)) {
  165. try (POITextExtractor streamExtractor = ExtractorFactory.createExtractor(stream)) {
  166. assertNotNull(streamExtractor);
  167. assertNotNull(streamExtractor.getText());
  168. assertNotNull(streamExtractor.getMetadataTextExtractor());
  169. }
  170. }
  171. }
  172. @Override
  173. public void handleAdditional(File file) throws Exception {
  174. // by default we do nothing here
  175. }
  176. }