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.

ParseNode.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 org.apache.poi.ss.formula.ptg.ArrayPtg;
  17. import org.apache.poi.ss.formula.ptg.AttrPtg;
  18. import org.apache.poi.ss.formula.ptg.FuncVarPtg;
  19. import org.apache.poi.ss.formula.ptg.MemAreaPtg;
  20. import org.apache.poi.ss.formula.ptg.MemFuncPtg;
  21. import org.apache.poi.ss.formula.ptg.Ptg;
  22. import org.apache.poi.ss.formula.function.FunctionMetadataRegistry;
  23. /**
  24. * Represents a syntactic element from a formula by encapsulating the corresponding <tt>Ptg</tt>
  25. * token. Each <tt>ParseNode</tt> may have child <tt>ParseNode</tt>s in the case when the wrapped
  26. * <tt>Ptg</tt> is non-atomic.
  27. */
  28. final class ParseNode {
  29. public static final ParseNode[] EMPTY_ARRAY = { };
  30. private final Ptg _token;
  31. private final ParseNode[] _children;
  32. private boolean _isIf;
  33. private final int _tokenCount;
  34. public ParseNode(Ptg token, ParseNode[] children) {
  35. if (token == null) {
  36. throw new IllegalArgumentException("token must not be null");
  37. }
  38. _token = token;
  39. _children = children.clone();
  40. _isIf = isIf(token);
  41. int tokenCount = 1;
  42. for (int i = 0; i < children.length; i++) {
  43. tokenCount += children[i].getTokenCount();
  44. }
  45. if (_isIf) {
  46. // there will be 2 or 3 extra tAttr tokens according to whether the false param is present
  47. tokenCount += children.length;
  48. }
  49. _tokenCount = tokenCount;
  50. }
  51. public ParseNode(Ptg token) {
  52. this(token, EMPTY_ARRAY);
  53. }
  54. public ParseNode(Ptg token, ParseNode child0) {
  55. this(token, new ParseNode[] { child0, });
  56. }
  57. public ParseNode(Ptg token, ParseNode child0, ParseNode child1) {
  58. this(token, new ParseNode[] { child0, child1, });
  59. }
  60. private int getTokenCount() {
  61. return _tokenCount;
  62. }
  63. public int getEncodedSize() {
  64. int result = _token instanceof ArrayPtg ? ArrayPtg.PLAIN_TOKEN_SIZE : _token.getSize();
  65. for (int i = 0; i < _children.length; i++) {
  66. result += _children[i].getEncodedSize();
  67. }
  68. return result;
  69. }
  70. /**
  71. * Collects the array of <tt>Ptg</tt> tokens for the specified tree.
  72. */
  73. public static Ptg[] toTokenArray(ParseNode rootNode) {
  74. TokenCollector temp = new TokenCollector(rootNode.getTokenCount());
  75. rootNode.collectPtgs(temp);
  76. return temp.getResult();
  77. }
  78. private void collectPtgs(TokenCollector temp) {
  79. if (isIf(_token)) {
  80. collectIfPtgs(temp);
  81. return;
  82. }
  83. boolean isPreFixOperator = _token instanceof MemFuncPtg || _token instanceof MemAreaPtg;
  84. if (isPreFixOperator) {
  85. temp.add(_token);
  86. }
  87. for (int i=0; i< getChildren().length; i++) {
  88. getChildren()[i].collectPtgs(temp);
  89. }
  90. if (!isPreFixOperator) {
  91. temp.add(_token);
  92. }
  93. }
  94. /**
  95. * The IF() function gets marked up with two or three tAttr tokens.
  96. * Similar logic will be required for CHOOSE() when it is supported
  97. *
  98. * See excelfileformat.pdf sec 3.10.5 "tAttr (19H)
  99. */
  100. private void collectIfPtgs(TokenCollector temp) {
  101. // condition goes first
  102. getChildren()[0].collectPtgs(temp);
  103. // placeholder for tAttrIf
  104. int ifAttrIndex = temp.createPlaceholder();
  105. // true parameter
  106. getChildren()[1].collectPtgs(temp);
  107. // placeholder for first skip attr
  108. int skipAfterTrueParamIndex = temp.createPlaceholder();
  109. int trueParamSize = temp.sumTokenSizes(ifAttrIndex+1, skipAfterTrueParamIndex);
  110. AttrPtg attrIf = AttrPtg.createIf(trueParamSize + 4); // distance to start of false parameter/tFuncVar. +4 for tAttrSkip after true
  111. if (getChildren().length > 2) {
  112. // false param present
  113. // false parameter
  114. getChildren()[2].collectPtgs(temp);
  115. int skipAfterFalseParamIndex = temp.createPlaceholder();
  116. int falseParamSize = temp.sumTokenSizes(skipAfterTrueParamIndex+1, skipAfterFalseParamIndex);
  117. AttrPtg attrSkipAfterTrue = AttrPtg.createSkip(falseParamSize + 4 + 4 - 1); // 1 less than distance to end of if FuncVar(size=4). +4 for attr skip before
  118. AttrPtg attrSkipAfterFalse = AttrPtg.createSkip(4 - 1); // 1 less than distance to end of if FuncVar(size=4).
  119. temp.setPlaceholder(ifAttrIndex, attrIf);
  120. temp.setPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
  121. temp.setPlaceholder(skipAfterFalseParamIndex, attrSkipAfterFalse);
  122. } else {
  123. // false parameter not present
  124. AttrPtg attrSkipAfterTrue = AttrPtg.createSkip(4 - 1); // 1 less than distance to end of if FuncVar(size=4).
  125. temp.setPlaceholder(ifAttrIndex, attrIf);
  126. temp.setPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
  127. }
  128. temp.add(_token);
  129. }
  130. private static boolean isIf(Ptg token) {
  131. if (token instanceof FuncVarPtg) {
  132. FuncVarPtg func = (FuncVarPtg) token;
  133. if (FunctionMetadataRegistry.FUNCTION_NAME_IF.equals(func.getName())) {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. public Ptg getToken() {
  140. return _token;
  141. }
  142. public ParseNode[] getChildren() {
  143. return _children;
  144. }
  145. private static final class TokenCollector {
  146. private final Ptg[] _ptgs;
  147. private int _offset;
  148. public TokenCollector(int tokenCount) {
  149. _ptgs = new Ptg[tokenCount];
  150. _offset = 0;
  151. }
  152. public int sumTokenSizes(int fromIx, int toIx) {
  153. int result = 0;
  154. for (int i=fromIx; i<toIx; i++) {
  155. result += _ptgs[i].getSize();
  156. }
  157. return result;
  158. }
  159. public int createPlaceholder() {
  160. return _offset++;
  161. }
  162. public void add(Ptg token) {
  163. if (token == null) {
  164. throw new IllegalArgumentException("token must not be null");
  165. }
  166. _ptgs[_offset] = token;
  167. _offset++;
  168. }
  169. public void setPlaceholder(int index, Ptg token) {
  170. if (_ptgs[index] != null) {
  171. throw new IllegalStateException("Invalid placeholder index (" + index + ")");
  172. }
  173. _ptgs[index] = token;
  174. }
  175. public Ptg[] getResult() {
  176. return _ptgs;
  177. }
  178. }
  179. }