PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/poi-3.6/src/java/org/apache/poi/ss/formula/ParseNode.java

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