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.

EvaluationTracker.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.ArrayList;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Set;
  20. import org.apache.poi.hssf.record.formula.eval.BlankEval;
  21. import org.apache.poi.hssf.record.formula.eval.ErrorEval;
  22. import org.apache.poi.hssf.record.formula.eval.ValueEval;
  23. import org.apache.poi.hssf.usermodel.HSSFCell;
  24. /**
  25. * Instances of this class keep track of multiple dependent cell evaluations due
  26. * to recursive calls to {@link WorkbookEvaluator#evaluate(HSSFCell)}
  27. * The main purpose of this class is to detect an attempt to evaluate a cell
  28. * that is already being evaluated. In other words, it detects circular
  29. * references in spreadsheet formulas.
  30. *
  31. * @author Josh Micich
  32. */
  33. final class EvaluationTracker {
  34. // TODO - consider deleting this class and letting CellEvaluationFrame take care of itself
  35. private final List<CellEvaluationFrame> _evaluationFrames;
  36. private final Set<FormulaCellCacheEntry> _currentlyEvaluatingCells;
  37. private final EvaluationCache _cache;
  38. public EvaluationTracker(EvaluationCache cache) {
  39. _cache = cache;
  40. _evaluationFrames = new ArrayList<CellEvaluationFrame>();
  41. _currentlyEvaluatingCells = new HashSet<FormulaCellCacheEntry>();
  42. }
  43. /**
  44. * Notifies this evaluation tracker that evaluation of the specified cell is
  45. * about to start.<br/>
  46. *
  47. * In the case of a <code>true</code> return code, the caller should
  48. * continue evaluation of the specified cell, and also be sure to call
  49. * <tt>endEvaluate()</tt> when complete.<br/>
  50. *
  51. * In the case of a <code>null</code> return code, the caller should
  52. * return an evaluation result of
  53. * <tt>ErrorEval.CIRCULAR_REF_ERROR<tt>, and not call <tt>endEvaluate()</tt>.
  54. * <br/>
  55. * @return <code>false</code> if the specified cell is already being evaluated
  56. */
  57. public boolean startEvaluate(FormulaCellCacheEntry cce) {
  58. if (cce == null) {
  59. throw new IllegalArgumentException("cellLoc must not be null");
  60. }
  61. if (_currentlyEvaluatingCells.contains(cce)) {
  62. return false;
  63. }
  64. _currentlyEvaluatingCells.add(cce);
  65. _evaluationFrames.add(new CellEvaluationFrame(cce));
  66. return true;
  67. }
  68. public void updateCacheResult(ValueEval result) {
  69. int nFrames = _evaluationFrames.size();
  70. if (nFrames < 1) {
  71. throw new IllegalStateException("Call to endEvaluate without matching call to startEvaluate");
  72. }
  73. CellEvaluationFrame frame = _evaluationFrames.get(nFrames-1);
  74. if (result == ErrorEval.CIRCULAR_REF_ERROR && nFrames > 1) {
  75. // Don't cache a circular ref error result if this cell is not the top evaluated cell.
  76. // A true circular ref error will propagate all the way around the loop. However, it's
  77. // possible to have parts of the formula tree (/ parts of the loop) to evaluate to
  78. // CIRCULAR_REF_ERROR, and that value not get used in the final cell result (see the
  79. // unit tests for a simple example). Thus, the only CIRCULAR_REF_ERROR result that can
  80. // safely be cached is that of the top evaluated cell.
  81. return;
  82. }
  83. frame.updateFormulaResult(result);
  84. }
  85. /**
  86. * Notifies this evaluation tracker that the evaluation of the specified cell is complete. <p/>
  87. *
  88. * Every successful call to <tt>startEvaluate</tt> must be followed by a call to <tt>endEvaluate</tt> (recommended in a finally block) to enable
  89. * proper tracking of which cells are being evaluated at any point in time.<p/>
  90. *
  91. * Assuming a well behaved client, parameters to this method would not be
  92. * required. However, they have been included to assert correct behaviour,
  93. * and form more meaningful error messages.
  94. */
  95. public void endEvaluate(CellCacheEntry cce) {
  96. int nFrames = _evaluationFrames.size();
  97. if (nFrames < 1) {
  98. throw new IllegalStateException("Call to endEvaluate without matching call to startEvaluate");
  99. }
  100. nFrames--;
  101. CellEvaluationFrame frame = _evaluationFrames.get(nFrames);
  102. if (cce != frame.getCCE()) {
  103. throw new IllegalStateException("Wrong cell specified. ");
  104. }
  105. // else - no problems so pop current frame
  106. _evaluationFrames.remove(nFrames);
  107. _currentlyEvaluatingCells.remove(cce);
  108. }
  109. public void acceptFormulaDependency(CellCacheEntry cce) {
  110. // Tell the currently evaluating cell frame that it has a dependency on the specified
  111. int prevFrameIndex = _evaluationFrames.size()-1;
  112. if (prevFrameIndex < 0) {
  113. // Top level frame, there is no 'cell' above this frame that is using the current cell
  114. } else {
  115. CellEvaluationFrame consumingFrame = _evaluationFrames.get(prevFrameIndex);
  116. consumingFrame.addSensitiveInputCell(cce);
  117. }
  118. }
  119. public void acceptPlainValueDependency(int bookIndex, int sheetIndex,
  120. int rowIndex, int columnIndex, ValueEval value) {
  121. // Tell the currently evaluating cell frame that it has a dependency on the specified
  122. int prevFrameIndex = _evaluationFrames.size() - 1;
  123. if (prevFrameIndex < 0) {
  124. // Top level frame, there is no 'cell' above this frame that is using the current cell
  125. } else {
  126. CellEvaluationFrame consumingFrame = _evaluationFrames.get(prevFrameIndex);
  127. if (value == BlankEval.INSTANCE) {
  128. consumingFrame.addUsedBlankCell(bookIndex, sheetIndex, rowIndex, columnIndex);
  129. } else {
  130. PlainValueCellCacheEntry cce = _cache.getPlainValueEntry(bookIndex, sheetIndex,
  131. rowIndex, columnIndex, value);
  132. consumingFrame.addSensitiveInputCell(cce);
  133. }
  134. }
  135. }
  136. }