Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestBiffDrawingToXml.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. return excludes;
  52. }
  53. // output sheets with specified name
  54. private static final String[] SHEET_NAMES = {};
  55. // output sheets with specified indexes
  56. private static final int[] SHEET_IDX = {};
  57. // exclude workbook-level records
  58. private static final boolean EXCLUDE_WORKBOOK = false;
  59. @Override
  60. void runOneFile(File pFile) throws Exception {
  61. try (InputStream inp = new FileInputStream(pFile);
  62. OutputStream outputStream = NULL_OUTPUT_STREAM) {
  63. writeToFile(outputStream, inp);
  64. }
  65. }
  66. public static void writeToFile(OutputStream fos, InputStream xlsWorkbook) throws IOException {
  67. try (HSSFWorkbook workbook = new HSSFWorkbook(xlsWorkbook)) {
  68. InternalWorkbook internalWorkbook = workbook.getInternalWorkbook();
  69. DrawingGroupRecord r = (DrawingGroupRecord) internalWorkbook.findFirstRecordBySid(DrawingGroupRecord.sid);
  70. StringBuilder builder = new StringBuilder();
  71. builder.append("<workbook>\n");
  72. String tab = "\t";
  73. if (!EXCLUDE_WORKBOOK && r != null) {
  74. r.decode();
  75. List<EscherRecord> escherRecords = r.getEscherRecords();
  76. for (EscherRecord record : escherRecords) {
  77. builder.append(record.toXml(tab));
  78. }
  79. }
  80. int i = 0;
  81. for (HSSFSheet sheet : getSheets(workbook)) {
  82. HSSFPatriarch p = sheet.getDrawingPatriarch();
  83. if (p != null) {
  84. builder.append(tab).append("<sheet").append(i).append(">\n");
  85. builder.append(p.getBoundAggregate().toXml(tab + "\t"));
  86. builder.append(tab).append("</sheet").append(i).append(">\n");
  87. i++;
  88. }
  89. }
  90. builder.append("</workbook>\n");
  91. fos.write(builder.toString().getBytes(StringUtil.UTF8));
  92. }
  93. }
  94. private static List<HSSFSheet> getSheets(HSSFWorkbook workbook) {
  95. List<Integer> sheetIdx = Arrays.stream(SHEET_IDX).boxed().collect(Collectors.toList());
  96. List<String> sheetNms = Arrays.stream(SHEET_NAMES).collect(Collectors.toList());
  97. List<HSSFSheet> list = new ArrayList<>();
  98. for (Sheet sheet : workbook) {
  99. if ((sheetIdx.isEmpty() && sheetNms.isEmpty()) ||
  100. sheetIdx.contains(workbook.getSheetIndex(sheet)) ||
  101. sheetNms.contains(sheet.getSheetName())
  102. ) {
  103. list.add((HSSFSheet)sheet);
  104. }
  105. }
  106. return list;
  107. }
  108. }