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.

TestBiffDrawingToXml.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.hssf.dev;
  16. import static org.apache.commons.io.output.NullOutputStream.NULL_OUTPUT_STREAM;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27. import org.apache.poi.EncryptedDocumentException;
  28. import org.apache.poi.ddf.EscherRecord;
  29. import org.apache.poi.hssf.model.InternalWorkbook;
  30. import org.apache.poi.hssf.record.DrawingGroupRecord;
  31. import org.apache.poi.hssf.record.RecordInputStream;
  32. import org.apache.poi.hssf.usermodel.HSSFPatriarch;
  33. import org.apache.poi.hssf.usermodel.HSSFSheet;
  34. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  35. import org.apache.poi.ss.usermodel.Sheet;
  36. import org.apache.poi.util.StringUtil;
  37. class TestBiffDrawingToXml extends BaseTestIteratingXLS {
  38. @Override
  39. protected Map<String, Class<? extends Throwable>> getExcludes() {
  40. Map<String, Class<? extends Throwable>> excludes = super.getExcludes();
  41. // unsupported crypto api header
  42. excludes.put("35897-type4.xls", EncryptedDocumentException.class);
  43. excludes.put("51832.xls", EncryptedDocumentException.class);
  44. excludes.put("xor-encryption-abc.xls", EncryptedDocumentException.class);
  45. excludes.put("password.xls", EncryptedDocumentException.class);
  46. // HSSFWorkbook cannot open it as well
  47. excludes.put("43493.xls", RecordInputStream.LeftoverDataException.class);
  48. excludes.put("44958_1.xls", RecordInputStream.LeftoverDataException.class);
  49. excludes.put("protected_66115.xls", EncryptedDocumentException.class);
  50. excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5285517825277952.xls", IllegalArgumentException.class);
  51. excludes.put("clusterfuzz-testcase-minimized-POIHSSFFuzzer-5436547081830400.xls", IllegalArgumentException.class);
  52. return excludes;
  53. }
  54. // output sheets with specified name
  55. private static final String[] SHEET_NAMES = {};
  56. // output sheets with specified indexes
  57. private static final int[] SHEET_IDX = {};
  58. // exclude workbook-level records
  59. private static final boolean EXCLUDE_WORKBOOK = false;
  60. @Override
  61. void runOneFile(File pFile) throws Exception {
  62. try (InputStream inp = new FileInputStream(pFile);
  63. OutputStream outputStream = NULL_OUTPUT_STREAM) {
  64. writeToFile(outputStream, inp);
  65. }
  66. }
  67. public static void writeToFile(OutputStream fos, InputStream xlsWorkbook) throws IOException {
  68. try (HSSFWorkbook workbook = new HSSFWorkbook(xlsWorkbook)) {
  69. InternalWorkbook internalWorkbook = workbook.getInternalWorkbook();
  70. DrawingGroupRecord r = (DrawingGroupRecord) internalWorkbook.findFirstRecordBySid(DrawingGroupRecord.sid);
  71. StringBuilder builder = new StringBuilder();
  72. builder.append("<workbook>\n");
  73. String tab = "\t";
  74. if (!EXCLUDE_WORKBOOK && r != null) {
  75. r.decode();
  76. List<EscherRecord> escherRecords = r.getEscherRecords();
  77. for (EscherRecord record : escherRecords) {
  78. builder.append(record.toXml(tab));
  79. }
  80. }
  81. int i = 0;
  82. for (HSSFSheet sheet : getSheets(workbook)) {
  83. HSSFPatriarch p = sheet.getDrawingPatriarch();
  84. if (p != null) {
  85. builder.append(tab).append("<sheet").append(i).append(">\n");
  86. builder.append(p.getBoundAggregate().toXml(tab + "\t"));
  87. builder.append(tab).append("</sheet").append(i).append(">\n");
  88. i++;
  89. }
  90. }
  91. builder.append("</workbook>\n");
  92. fos.write(builder.toString().getBytes(StringUtil.UTF8));
  93. }
  94. }
  95. private static List<HSSFSheet> getSheets(HSSFWorkbook workbook) {
  96. List<Integer> sheetIdx = Arrays.stream(SHEET_IDX).boxed().collect(Collectors.toList());
  97. List<String> sheetNms = Arrays.stream(SHEET_NAMES).collect(Collectors.toList());
  98. List<HSSFSheet> list = new ArrayList<>();
  99. for (Sheet sheet : workbook) {
  100. if ((sheetIdx.isEmpty() && sheetNms.isEmpty()) ||
  101. sheetIdx.contains(workbook.getSheetIndex(sheet)) ||
  102. sheetNms.contains(sheet.getSheetName())
  103. ) {
  104. list.add((HSSFSheet)sheet);
  105. }
  106. }
  107. return list;
  108. }
  109. }