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.

Formula.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.Arrays;
  17. import org.apache.poi.ss.formula.ptg.ExpPtg;
  18. import org.apache.poi.ss.formula.ptg.Ptg;
  19. import org.apache.poi.ss.formula.ptg.TblPtg;
  20. import org.apache.poi.ss.util.CellReference;
  21. import org.apache.poi.util.IOUtils;
  22. import org.apache.poi.util.LittleEndian;
  23. import org.apache.poi.util.LittleEndianByteArrayInputStream;
  24. import org.apache.poi.util.LittleEndianInput;
  25. import org.apache.poi.util.LittleEndianOutput;
  26. /**
  27. * Encapsulates an encoded formula token array.
  28. *
  29. * @author Josh Micich
  30. */
  31. public class Formula {
  32. //Arbitrarily set. May need to increase.
  33. private static final int MAX_ENCODED_LEN = 100000;
  34. private static final Formula EMPTY = new Formula(new byte[0], 0);
  35. /** immutable */
  36. private final byte[] _byteEncoding;
  37. private final int _encodedTokenLen;
  38. private Formula(byte[] byteEncoding, int encodedTokenLen) {
  39. _byteEncoding = byteEncoding.clone();
  40. _encodedTokenLen = encodedTokenLen;
  41. // TODO - this seems to occur when IntersectionPtg is present
  42. // This example file "IntersectionPtg.xls"
  43. // used by test: TestIntersectionPtg.testReading()
  44. // has 10 bytes unused at the end of the formula
  45. // 10 extra bytes are just 0x01 and 0x00
  46. // LittleEndianByteArrayInputStream in = new LittleEndianByteArrayInputStream(byteEncoding);
  47. // Ptg.readTokens(encodedTokenLen, in);
  48. // int nUnusedBytes = _byteEncoding.length - in.getReadIndex();
  49. }
  50. /**
  51. * Convenience method for {@link #read(int, LittleEndianInput, int)}
  52. */
  53. public static Formula read(int encodedTokenLen, LittleEndianInput in) {
  54. return read(encodedTokenLen, in, encodedTokenLen);
  55. }
  56. /**
  57. * When there are no array constants present, <tt>encodedTokenLen</tt>==<tt>totalEncodedLen</tt>
  58. * @param encodedTokenLen number of bytes in the stream taken by the plain formula tokens
  59. * @param totalEncodedLen the total number of bytes in the formula (includes trailing encoding
  60. * for array constants, but does not include 2 bytes for initial <tt>ushort encodedTokenLen</tt> field.
  61. * @return A new formula object as read from the stream. Possibly empty, never <code>null</code>.
  62. */
  63. public static Formula read(int encodedTokenLen, LittleEndianInput in, int totalEncodedLen) {
  64. byte[] byteEncoding = IOUtils.safelyAllocate(totalEncodedLen, MAX_ENCODED_LEN);
  65. in.readFully(byteEncoding);
  66. return new Formula(byteEncoding, encodedTokenLen);
  67. }
  68. public Ptg[] getTokens() {
  69. LittleEndianInput in = new LittleEndianByteArrayInputStream(_byteEncoding);
  70. return Ptg.readTokens(_encodedTokenLen, in);
  71. }
  72. /**
  73. * Writes The formula encoding is includes:
  74. * <ul>
  75. * <li>ushort tokenDataLen</li>
  76. * <li>tokenData</li>
  77. * <li>arrayConstantData (if present)</li>
  78. * </ul>
  79. */
  80. public void serialize(LittleEndianOutput out) {
  81. out.writeShort(_encodedTokenLen);
  82. out.write(_byteEncoding);
  83. }
  84. public void serializeTokens(LittleEndianOutput out) {
  85. out.write(_byteEncoding, 0, _encodedTokenLen);
  86. }
  87. public void serializeArrayConstantData(LittleEndianOutput out) {
  88. int len = _byteEncoding.length-_encodedTokenLen;
  89. out.write(_byteEncoding, _encodedTokenLen, len);
  90. }
  91. /**
  92. * @return total formula encoding length. The formula encoding includes:
  93. * <ul>
  94. * <li>ushort tokenDataLen</li>
  95. * <li>tokenData</li>
  96. * <li>arrayConstantData (optional)</li>
  97. * </ul>
  98. * Note - this value is different to <tt>tokenDataLength</tt>
  99. */
  100. public int getEncodedSize() {
  101. return 2 + _byteEncoding.length;
  102. }
  103. /**
  104. * This method is often used when the formula length does not appear immediately before
  105. * the encoded token data.
  106. *
  107. * @return the encoded length of the plain formula tokens. This does <em>not</em> include
  108. * the leading ushort field, nor any trailing array constant data.
  109. */
  110. public int getEncodedTokenSize() {
  111. return _encodedTokenLen;
  112. }
  113. /**
  114. * Creates a {@link Formula} object from a supplied {@link Ptg} array.
  115. * Handles <code>null</code>s OK.
  116. * @param ptgs may be <code>null</code>
  117. * @return Never <code>null</code> (Possibly empty if the supplied <tt>ptgs</tt> is <code>null</code>)
  118. */
  119. public static Formula create(Ptg[] ptgs) {
  120. if (ptgs == null || ptgs.length < 1) {
  121. return EMPTY;
  122. }
  123. int totalSize = Ptg.getEncodedSize(ptgs);
  124. byte[] encodedData = new byte[totalSize];
  125. Ptg.serializePtgs(ptgs, encodedData, 0);
  126. int encodedTokenLen = Ptg.getEncodedSizeWithoutArrayData(ptgs);
  127. return new Formula(encodedData, encodedTokenLen);
  128. }
  129. /**
  130. * Gets the {@link Ptg} array from the supplied {@link Formula}.
  131. * Handles <code>null</code>s OK.
  132. *
  133. * @param formula may be <code>null</code>
  134. * @return possibly <code>null</code> (if the supplied <tt>formula</tt> is <code>null</code>)
  135. */
  136. public static Ptg[] getTokens(Formula formula) {
  137. if (formula == null) {
  138. return null;
  139. }
  140. return formula.getTokens();
  141. }
  142. public Formula copy() {
  143. // OK to return this because immutable
  144. return this;
  145. }
  146. /**
  147. * Gets the locator for the corresponding {@link org.apache.poi.hssf.record.SharedFormulaRecord},
  148. * {@link org.apache.poi.hssf.record.ArrayRecord} or {@link org.apache.poi.hssf.record.TableRecord}
  149. * if this formula belongs to such a grouping. The {@link CellReference}
  150. * returned by this method will match the top left corner of the range of that grouping.
  151. * The return value is usually not the same as the location of the cell containing this formula.
  152. *
  153. * @return the firstRow &amp; firstColumn of an array formula or shared formula that this formula
  154. * belongs to. <code>null</code> if this formula is not part of an array or shared formula.
  155. */
  156. public CellReference getExpReference() {
  157. byte[] data = _byteEncoding;
  158. if (data.length != 5) {
  159. // tExp and tTbl are always 5 bytes long, and the only ptg in the formula
  160. return null;
  161. }
  162. switch (data[0]) {
  163. case ExpPtg.sid:
  164. break;
  165. case TblPtg.sid:
  166. break;
  167. default:
  168. return null;
  169. }
  170. int firstRow = LittleEndian.getUShort(data, 1);
  171. int firstColumn = LittleEndian.getUShort(data, 3);
  172. return new CellReference(firstRow, firstColumn);
  173. }
  174. public boolean isSame(Formula other) {
  175. return Arrays.equals(_byteEncoding, other._byteEncoding);
  176. }
  177. }