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.

HSLFFileHandler.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.assertNotNull;
  17. import static org.junit.jupiter.api.Assertions.assertTrue;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.InputStream;
  21. import org.apache.logging.log4j.LogManager;
  22. import org.apache.logging.log4j.Logger;
  23. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  24. import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
  25. import org.junit.jupiter.api.Test;
  26. class HSLFFileHandler extends SlideShowHandler {
  27. private static final Logger LOGGER = LogManager.getLogger(HSLFFileHandler.class);
  28. @Override
  29. public void handleFile(InputStream stream, String path) throws Exception {
  30. HSLFSlideShowImpl slide = new HSLFSlideShowImpl(stream);
  31. assertNotNull(slide.getCurrentUserAtom());
  32. assertNotNull(slide.getEmbeddedObjects());
  33. assertNotNull(slide.getUnderlyingBytes());
  34. assertNotNull(slide.getPictureData());
  35. org.apache.poi.hslf.record.Record[] records = slide.getRecords();
  36. assertNotNull(records);
  37. for(org.apache.poi.hslf.record.Record record : records) {
  38. assertNotNull( record, "Found a record which was null" );
  39. assertTrue(record.getRecordType() >= 0);
  40. }
  41. handlePOIDocument(slide);
  42. HSLFSlideShow ss = new HSLFSlideShow(slide);
  43. handleSlideShow(ss);
  44. }
  45. @Test
  46. void testOne() throws Exception {
  47. testOneFile(new File("test-data/slideshow/54880_chinese.ppt"));
  48. }
  49. // a test-case to test all .ppt files without executing the full TestAllFiles
  50. @Override
  51. @Test
  52. void test() throws Exception {
  53. File[] files = new File("test-data/slideshow/").listFiles((dir, name) -> name.endsWith(".ppt"));
  54. assertNotNull(files);
  55. System.out.println("Testing " + files.length + " files");
  56. for(File file : files) {
  57. try {
  58. testOneFile(file);
  59. } catch (Throwable e) {
  60. LOGGER.atWarn().withThrowable(e).log("Failed to handle file {}", file);
  61. }
  62. }
  63. }
  64. private void testOneFile(File file) throws Exception {
  65. System.out.println(file);
  66. try (InputStream stream = new FileInputStream(file)) {
  67. handleFile(stream, file.getPath());
  68. }
  69. handleExtracting(file);
  70. }
  71. public static void main(String[] args) throws Exception {
  72. try (InputStream stream = new FileInputStream(args[0])) {
  73. new HSLFFileHandler().handleFile(stream, args[0]);
  74. }
  75. }
  76. }