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.

HPSFFileHandler.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.Assert.assertEquals;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.PrintStream;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Arrays;
  25. import java.util.Collections;
  26. import java.util.HashSet;
  27. import java.util.Set;
  28. import org.apache.poi.hpsf.DocumentSummaryInformation;
  29. import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument;
  30. import org.apache.poi.hpsf.MarkUnsupportedException;
  31. import org.apache.poi.hpsf.PropertySet;
  32. import org.apache.poi.hpsf.SummaryInformation;
  33. import org.apache.poi.hpsf.examples.CopyCompare;
  34. import org.apache.poi.poifs.filesystem.DirectoryNode;
  35. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  36. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  37. import org.apache.poi.util.TempFile;
  38. import org.junit.Assume;
  39. import org.junit.Test;
  40. public class HPSFFileHandler extends POIFSFileHandler {
  41. private static final String NL = System.getProperty("line.separator");
  42. private static File copyOutput;
  43. static final Set<String> EXCLUDES_HANDLE_ADD = unmodifiableHashSet(
  44. "spreadsheet/45290.xls",
  45. "spreadsheet/46904.xls",
  46. "spreadsheet/55982.xls",
  47. "spreadsheet/testEXCEL_3.xls",
  48. "spreadsheet/testEXCEL_4.xls",
  49. "hpsf/Test_Humor-Generation.ppt",
  50. "document/word2.doc"
  51. );
  52. static final Set<String> EXCLUDES_HANDLE_FILE = unmodifiableHashSet(
  53. "hpsf/Test_Humor-Generation.ppt",
  54. "slideshow/missing-moveto.ppt" // POIFS properties corrupted
  55. );
  56. private static Set<String> unmodifiableHashSet(String... a) {
  57. return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(a)));
  58. }
  59. @Override
  60. public void handleFile(InputStream stream, String path) throws Exception {
  61. Assume.assumeFalse(EXCLUDES_HANDLE_FILE.contains(path));
  62. POIFSFileSystem poifs = new POIFSFileSystem(stream);
  63. HPSFPropertiesOnlyDocument hpsf = new HPSFPropertiesOnlyDocument(poifs);
  64. DocumentSummaryInformation dsi = hpsf.getDocumentSummaryInformation();
  65. SummaryInformation si = hpsf.getSummaryInformation();
  66. boolean hasDSI = hasPropertyStream(poifs, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
  67. boolean hasSI = hasPropertyStream(poifs, SummaryInformation.DEFAULT_STREAM_NAME);
  68. assertEquals(hasDSI, dsi != null);
  69. assertEquals(hasSI, si != null);
  70. handlePOIDocument(hpsf);
  71. }
  72. private static boolean hasPropertyStream(POIFSFileSystem poifs, String streamName) throws IOException, MarkUnsupportedException {
  73. DirectoryNode root = poifs.getRoot();
  74. if (!root.hasEntry(streamName)) {
  75. return false;
  76. }
  77. try (DocumentInputStream dis = root.createDocumentInputStream(streamName)) {
  78. return PropertySet.isPropertySetStream(dis);
  79. }
  80. }
  81. @Override
  82. public void handleAdditional(File file) throws Exception {
  83. Assume.assumeFalse(EXCLUDES_HANDLE_ADD.contains(file.getParentFile().getName()+"/"+file.getName()));
  84. if (copyOutput == null) {
  85. copyOutput = TempFile.createTempFile("hpsfCopy", "out");
  86. copyOutput.deleteOnExit();
  87. }
  88. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  89. PrintStream psNew = new PrintStream(bos);
  90. PrintStream ps = System.out;
  91. try {
  92. System.setOut(psNew);
  93. CopyCompare.main(new String[]{file.getAbsolutePath(), copyOutput.getAbsolutePath()});
  94. assertEquals("Equal" + NL, new String(bos.toByteArray(), StandardCharsets.UTF_8));
  95. } finally {
  96. System.setOut(ps);
  97. }
  98. }
  99. // a test-case to test this locally without executing the full TestAllFiles
  100. @Override
  101. @Test
  102. @SuppressWarnings("java:S2699")
  103. public void test() throws Exception {
  104. String path = "test-data/hpsf/Test0313rur.adm";
  105. try (InputStream stream = new FileInputStream(path)) {
  106. handleFile(stream, path);
  107. }
  108. }
  109. // a test-case to test this locally without executing the full TestAllFiles
  110. @Test
  111. public void testExtractor() throws Exception {
  112. handleExtracting(new File("test-data/hpsf/TestBug44375.xls"));
  113. }
  114. }