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.

FormulaRenderer.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.ss.formula;
  16. import java.util.Stack;
  17. import org.apache.poi.ss.formula.ptg.AttrPtg;
  18. import org.apache.poi.ss.formula.ptg.MemAreaPtg;
  19. import org.apache.poi.ss.formula.ptg.MemErrPtg;
  20. import org.apache.poi.ss.formula.ptg.MemFuncPtg;
  21. import org.apache.poi.ss.formula.ptg.OperationPtg;
  22. import org.apache.poi.ss.formula.ptg.ParenthesisPtg;
  23. import org.apache.poi.ss.formula.ptg.Ptg;
  24. /**
  25. * Common logic for rendering formulas.<br>
  26. *
  27. * For POI internal use only
  28. *
  29. * @author Josh Micich
  30. */
  31. public class FormulaRenderer {
  32. /**
  33. * Static method to convert an array of {@link Ptg}s in RPN order
  34. * to a human readable string format in infix mode.
  35. * @param book used for defined names and 3D references
  36. * @param ptgs must not be <code>null</code>
  37. * @return a human readable String
  38. */
  39. public static String toFormulaString(FormulaRenderingWorkbook book, Ptg[] ptgs) {
  40. if (ptgs == null || ptgs.length == 0) {
  41. throw new IllegalArgumentException("ptgs must not be null");
  42. }
  43. Stack<String> stack = new Stack<>();
  44. for (Ptg ptg : ptgs) {
  45. // TODO - what about MemNoMemPtg?
  46. if(ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) {
  47. // marks the start of a list of area expressions which will be naturally combined
  48. // by their trailing operators (e.g. UnionPtg)
  49. // TODO - put comment and throw exception in toFormulaString() of these classes
  50. continue;
  51. }
  52. if (ptg instanceof ParenthesisPtg) {
  53. String contents = stack.pop();
  54. stack.push ("(" + contents + ")");
  55. continue;
  56. }
  57. if (ptg instanceof AttrPtg) {
  58. AttrPtg attrPtg = ((AttrPtg) ptg);
  59. if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isSkip()) {
  60. continue;
  61. }
  62. if (attrPtg.isSpace()) {
  63. // POI currently doesn't render spaces in formulas
  64. continue;
  65. // but if it ever did, care must be taken:
  66. // tAttrSpace comes *before* the operand it applies to, which may be consistent
  67. // with how the formula text appears but is against the RPN ordering assumed here
  68. }
  69. if (attrPtg.isSemiVolatile()) {
  70. // similar to tAttrSpace - RPN is violated
  71. continue;
  72. }
  73. if (attrPtg.isSum()) {
  74. String[] operands = getOperands(stack, attrPtg.getNumberOfOperands());
  75. stack.push(attrPtg.toFormulaString(operands));
  76. continue;
  77. }
  78. throw new RuntimeException("Unexpected tAttr: " + attrPtg);
  79. }
  80. if (ptg instanceof WorkbookDependentFormula) {
  81. WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg;
  82. stack.push(optg.toFormulaString(book));
  83. continue;
  84. }
  85. if (! (ptg instanceof OperationPtg)) {
  86. stack.push(ptg.toFormulaString());
  87. continue;
  88. }
  89. OperationPtg o = (OperationPtg) ptg;
  90. String[] operands = getOperands(stack, o.getNumberOfOperands());
  91. stack.push(o.toFormulaString(operands));
  92. }
  93. if(stack.isEmpty()) {
  94. // inspection of the code above reveals that every stack.pop() is followed by a
  95. // stack.push(). So this is either an internal error or impossible.
  96. throw new IllegalStateException("Stack underflow");
  97. }
  98. String result = stack.pop();
  99. if(!stack.isEmpty()) {
  100. // Might be caused by some tokens like AttrPtg and Mem*Ptg, which really shouldn't
  101. // put anything on the stack
  102. throw new IllegalStateException("too much stuff left on the stack");
  103. }
  104. return result;
  105. }
  106. private static String[] getOperands(Stack<String> stack, int nOperands) {
  107. String[] operands = new String[nOperands];
  108. for (int j = nOperands-1; j >= 0; j--) { // reverse iteration because args were pushed in-order
  109. if(stack.isEmpty()) {
  110. String msg = "Too few arguments supplied to operation. Expected (" + nOperands
  111. + ") operands but got (" + (nOperands - j - 1) + ")";
  112. throw new IllegalStateException(msg);
  113. }
  114. operands[j] = stack.pop();
  115. }
  116. return operands;
  117. }
  118. }