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

XWPFFileHandler.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 java.io.BufferedInputStream;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.InputStream;
  20. import java.util.Set;
  21. import org.apache.commons.io.output.NullOutputStream;
  22. import org.apache.poi.ooxml.POIXMLException;
  23. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  24. import org.junit.jupiter.api.Test;
  25. public class XWPFFileHandler extends AbstractFileHandler {
  26. private static final Set<String> EXPECTED_FAILURES = StressTestUtils.unmodifiableHashSet(
  27. "document/truncated62886.docx", "document/ExternalEntityInText.docx"
  28. );
  29. @Override
  30. public void handleFile(InputStream stream, String path) throws Exception {
  31. // ignore password protected files
  32. if (POIXMLDocumentHandler.isEncrypted(stream)) return;
  33. if (StressTestUtils.excludeFile(path, EXPECTED_FAILURES)) return;
  34. try (XWPFDocument doc = new XWPFDocument(stream)) {
  35. new POIXMLDocumentHandler().handlePOIXMLDocument(doc);
  36. POIXMLDocumentHandler.cursorRecursive(doc.getDocument());
  37. doc.write(NullOutputStream.INSTANCE);
  38. } catch (POIXMLException e) {
  39. Exception cause = (Exception)e.getCause();
  40. throw cause == null ? e : cause;
  41. }
  42. }
  43. // a test-case to test this locally without executing the full TestAllFiles
  44. @Test
  45. @SuppressWarnings("java:S2699")
  46. void test() throws Exception {
  47. File file = new File("test-data/document/51921-Word-Crash067.docx");
  48. try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) {
  49. handleFile(stream, file.getPath());
  50. }
  51. handleExtracting(file);
  52. handleAdditional(file);
  53. }
  54. }