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.

TestFormulaViewer.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 java.io.File;
  17. import java.io.InputStream;
  18. import java.io.PrintWriter;
  19. import java.util.Locale;
  20. import java.util.Map;
  21. import java.util.Objects;
  22. import java.util.function.Function;
  23. import org.apache.commons.io.output.NullWriter;
  24. import org.apache.poi.EncryptedDocumentException;
  25. import org.apache.poi.hssf.model.HSSFFormulaParser;
  26. import org.apache.poi.hssf.record.FormulaRecord;
  27. import org.apache.poi.hssf.record.RecordFactory;
  28. import org.apache.poi.hssf.record.RecordInputStream;
  29. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  30. import org.apache.poi.ss.formula.ptg.FuncPtg;
  31. import org.apache.poi.ss.formula.ptg.Ptg;
  32. class TestFormulaViewer extends BaseTestIteratingXLS {
  33. @Override
  34. protected Map<String, Class<? extends Throwable>> getExcludes() {
  35. Map<String, Class<? extends Throwable>> excludes = super.getExcludes();
  36. excludes.put("35897-type4.xls", EncryptedDocumentException.class); // unsupported crypto api header
  37. excludes.put("51832.xls", EncryptedDocumentException.class);
  38. excludes.put("xor-encryption-abc.xls", EncryptedDocumentException.class);
  39. excludes.put("password.xls", EncryptedDocumentException.class);
  40. excludes.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well
  41. excludes.put("44958_1.xls", RecordInputStream.LeftoverDataException.class);
  42. excludes.put("protected_66115.xls", EncryptedDocumentException.class);
  43. return excludes;
  44. }
  45. private final boolean doListFormula = true;
  46. @Override
  47. void runOneFile(File fileIn) throws Exception {
  48. // replace with System.out for manual tests
  49. PrintWriter out = new PrintWriter(NullWriter.INSTANCE);
  50. final Function<FormulaRecord, String> lister = (doListFormula) ? this::listFormula : this::parseFormulaRecord;
  51. try (POIFSFileSystem fs = new POIFSFileSystem(fileIn, true);
  52. InputStream is = BiffViewer.getPOIFSInputStream(fs)) {
  53. RecordFactory.createRecords(is).stream()
  54. .filter(r -> r.getSid() == FormulaRecord.sid)
  55. .map(FormulaRecord.class::cast)
  56. .map(lister)
  57. .map(Objects::nonNull)
  58. .forEach(out::println);
  59. }
  60. }
  61. private String listFormula(FormulaRecord record) {
  62. Ptg[] tokens = record.getParsedExpression();
  63. int numptgs = tokens.length;
  64. final Ptg lastToken = tokens[numptgs - 1];
  65. String fmlStr;
  66. try {
  67. fmlStr = lastToken.toFormulaString();
  68. } catch (Exception ignored) {
  69. return null;
  70. }
  71. return String.join("~",
  72. fmlStr,
  73. mapToken(lastToken),
  74. (numptgs > 1 ? mapToken(tokens[numptgs - 2]) : "VALUE"),
  75. String.valueOf(lastToken instanceof FuncPtg ? numptgs - 1 : -1)
  76. );
  77. }
  78. private static String mapToken(Ptg token) {
  79. switch (token.getPtgClass()) {
  80. case Ptg.CLASS_REF:
  81. return "REF";
  82. case Ptg.CLASS_VALUE:
  83. return "VALUE";
  84. case Ptg.CLASS_ARRAY:
  85. return "ARRAY";
  86. default:
  87. throwInvalidRVAToken(token);
  88. return "";
  89. }
  90. }
  91. /**
  92. * Method parseFormulaRecord
  93. *
  94. * @param record the record to be parsed
  95. */
  96. public String parseFormulaRecord(FormulaRecord record) {
  97. return String.format(Locale.ROOT,
  98. "==============================\n" +
  99. "row = %d, col = %d\n" +
  100. "value = %f\n" +
  101. "xf = %d, number of ptgs = %d, options = %d\n" +
  102. "RPN List = %s\n" +
  103. "Formula text = %s",
  104. record.getRow(), record.getColumn(), record.getValue(), record.getXFIndex(),
  105. record.getParsedExpression().length, record.getOptions(),
  106. formulaString(record), composeFormula(record));
  107. }
  108. private String formulaString(FormulaRecord record) {
  109. StringBuilder buf = new StringBuilder();
  110. Ptg[] tokens = record.getParsedExpression();
  111. for (Ptg token : tokens) {
  112. buf.append(token.toFormulaString());
  113. switch (token.getPtgClass()) {
  114. case Ptg.CLASS_REF:
  115. buf.append("(R)");
  116. break;
  117. case Ptg.CLASS_VALUE:
  118. buf.append("(V)");
  119. break;
  120. case Ptg.CLASS_ARRAY:
  121. buf.append("(A)");
  122. break;
  123. default:
  124. throwInvalidRVAToken(token);
  125. }
  126. buf.append(' ');
  127. }
  128. return buf.toString();
  129. }
  130. private static void throwInvalidRVAToken(Ptg token) {
  131. throw new IllegalStateException("Invalid RVA type (" + token.getPtgClass() + "). This should never happen.");
  132. }
  133. private static String composeFormula(FormulaRecord record) {
  134. return HSSFFormulaParser.toFormulaString(null, record.getParsedExpression());
  135. }
  136. }