PageRenderTime 93ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/SharedFormulaRecord.java

http://github.com/openmicroscopy/bioformats
Java | 304 lines | 193 code | 38 blank | 73 comment | 23 complexity | 0194853a122e58e181b7c54e5cfbf881 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.record;
  38. import java.util.Stack;
  39. import java.util.List;
  40. import loci.poi.hssf.record.formula.*;
  41. import loci.poi.util.LittleEndian;
  42. /**
  43. * Title: SharedFormulaRecord
  44. * Description: Primarily used as an excel optimization so that multiple similar formulas
  45. * are not written out too many times. We should recognize this record and
  46. * serialize as is since this is used when reading templates.
  47. * <p>
  48. * Note: the documentation says that the SID is BC where biffviewer reports 4BC. The hex dump shows
  49. * that the two byte sid representation to be 'BC 04' that is consistent with the other high byte
  50. * record types.
  51. * @author Danny Mui at apache dot org
  52. */
  53. public class SharedFormulaRecord
  54. extends Record
  55. {
  56. public final static short sid = 0x4BC;
  57. private int field_1_first_row;
  58. private int field_2_last_row;
  59. private short field_3_first_column;
  60. private short field_4_last_column;
  61. private int field_5_reserved;
  62. private short field_6_expression_len;
  63. private Stack field_7_parsed_expr;
  64. public SharedFormulaRecord()
  65. {
  66. }
  67. /**
  68. * @param in the RecordInputstream to read the record from
  69. */
  70. public SharedFormulaRecord(RecordInputStream in)
  71. {
  72. super(in);
  73. }
  74. protected void validateSid(short id)
  75. {
  76. if (id != this.sid)
  77. {
  78. throw new RecordFormatException("Not a valid SharedFormula");
  79. }
  80. }
  81. public int getFirstRow() {
  82. return field_1_first_row;
  83. }
  84. public int getLastRow() {
  85. return field_2_last_row;
  86. }
  87. public short getFirstColumn() {
  88. return field_3_first_column;
  89. }
  90. public short getLastColumn() {
  91. return field_4_last_column;
  92. }
  93. public short getExpressionLength()
  94. {
  95. return field_6_expression_len;
  96. }
  97. /**
  98. * spit the record out AS IS. no interperatation or identification
  99. */
  100. public int serialize(int offset, byte [] data)
  101. {
  102. //Because this record is converted to individual Formula records, this method is not required.
  103. throw new UnsupportedOperationException("Cannot serialize a SharedFormulaRecord");
  104. }
  105. public int getRecordSize()
  106. {
  107. //Because this record is converted to individual Formula records, this method is not required.
  108. throw new UnsupportedOperationException("Cannot get the size for a SharedFormulaRecord");
  109. }
  110. /**
  111. * print a sort of string representation ([SHARED FORMULA RECORD] id = x [/SHARED FORMULA RECORD])
  112. */
  113. public String toString()
  114. {
  115. StringBuffer buffer = new StringBuffer();
  116. buffer.append("[SHARED FORMULA RECORD:" + Integer.toHexString(sid) + "]\n");
  117. buffer.append(" .id = ").append(Integer.toHexString(sid))
  118. .append("\n");
  119. buffer.append(" .first_row = ")
  120. .append(Integer.toHexString(getFirstRow())).append("\n");
  121. buffer.append(" .last_row = ")
  122. .append(Integer.toHexString(getLastRow()))
  123. .append("\n");
  124. buffer.append(" .first_column = ")
  125. .append(Integer.toHexString(getFirstColumn())).append("\n");
  126. buffer.append(" .last_column = ")
  127. .append(Integer.toHexString(getLastColumn()))
  128. .append("\n");
  129. buffer.append(" .reserved = ")
  130. .append(Integer.toHexString(field_5_reserved))
  131. .append("\n");
  132. buffer.append(" .expressionlength= ").append(getExpressionLength())
  133. .append("\n");
  134. buffer.append(" .numptgsinarray = ").append(field_7_parsed_expr.size())
  135. .append("\n");
  136. for (int k = 0; k < field_7_parsed_expr.size(); k++ ) {
  137. buffer.append("Formula ")
  138. .append(k)
  139. .append("\n")
  140. .append(field_7_parsed_expr.get(k).toString())
  141. .append("\n");
  142. }
  143. buffer.append("[/SHARED FORMULA RECORD]\n");
  144. return buffer.toString();
  145. }
  146. public short getSid()
  147. {
  148. return sid;
  149. }
  150. /**
  151. * Shared formulas are to treated like unknown records, and as a result d
  152. */
  153. protected void fillFields(RecordInputStream in)
  154. {
  155. field_1_first_row = in.readShort();
  156. field_2_last_row = in.readShort();
  157. field_3_first_column = in.readByte();
  158. field_4_last_column = in.readByte();
  159. field_5_reserved = in.readShort();
  160. field_6_expression_len = in.readShort();
  161. field_7_parsed_expr = getParsedExpressionTokens(in);
  162. }
  163. private Stack getParsedExpressionTokens(RecordInputStream in)
  164. {
  165. Stack stack = new Stack();
  166. while (in.remaining() != 0) {
  167. Ptg ptg = Ptg.createPtg(in);
  168. stack.push(ptg);
  169. }
  170. return stack;
  171. }
  172. public boolean isFormulaInShared(FormulaRecord formula) {
  173. final int formulaRow = formula.getRow();
  174. final int formulaColumn = formula.getColumn();
  175. return ((getFirstRow() <= formulaRow) && (getLastRow() >= formulaRow) &&
  176. (getFirstColumn() <= formulaColumn) && (getLastColumn() >= formulaColumn));
  177. }
  178. /** Creates a non shared formula from the shared formula counter part*/
  179. public void convertSharedFormulaRecord(FormulaRecord formula) {
  180. //Sanity checks
  181. final int formulaRow = formula.getRow();
  182. final int formulaColumn = formula.getColumn();
  183. if (isFormulaInShared(formula)) {
  184. formula.setExpressionLength(getExpressionLength());
  185. Stack newPtgStack = new Stack();
  186. if (field_7_parsed_expr != null)
  187. for (int k = 0; k < field_7_parsed_expr.size(); k++) {
  188. Ptg ptg = (Ptg) field_7_parsed_expr.get(k);
  189. if (ptg instanceof RefNPtg) {
  190. RefNPtg refNPtg = (RefNPtg)ptg;
  191. ptg = new ReferencePtg( (short)(formulaRow + refNPtg.getRow()),
  192. (byte)(formulaColumn + refNPtg.getColumn()),
  193. refNPtg.isRowRelative(),
  194. refNPtg.isColRelative());
  195. } else if (ptg instanceof RefNVPtg) {
  196. RefNVPtg refNVPtg = (RefNVPtg)ptg;
  197. ptg = new RefVPtg( (short)(formulaRow + refNVPtg.getRow()),
  198. (byte)(formulaColumn + refNVPtg.getColumn()),
  199. refNVPtg.isRowRelative(),
  200. refNVPtg.isColRelative());
  201. } else if (ptg instanceof RefNAPtg) {
  202. RefNAPtg refNAPtg = (RefNAPtg)ptg;
  203. ptg = new RefAPtg( (short)(formulaRow + refNAPtg.getRow()),
  204. (byte)(formulaColumn + refNAPtg.getColumn()),
  205. refNAPtg.isRowRelative(),
  206. refNAPtg.isColRelative());
  207. } else if (ptg instanceof AreaNPtg) {
  208. AreaNPtg areaNPtg = (AreaNPtg)ptg;
  209. ptg = new AreaPtg((short)(formulaRow + areaNPtg.getFirstRow()),
  210. (short)(formulaRow + areaNPtg.getLastRow()),
  211. (short)(formulaColumn + areaNPtg.getFirstColumn()),
  212. (short)(formulaColumn + areaNPtg.getLastColumn()),
  213. areaNPtg.isFirstRowRelative(),
  214. areaNPtg.isLastRowRelative(),
  215. areaNPtg.isFirstColRelative(),
  216. areaNPtg.isLastColRelative());
  217. } else if (ptg instanceof AreaNVPtg) {
  218. AreaNVPtg areaNVPtg = (AreaNVPtg)ptg;
  219. ptg = new AreaVPtg((short)(formulaRow + areaNVPtg.getFirstRow()),
  220. (short)(formulaRow + areaNVPtg.getLastRow()),
  221. (short)(formulaColumn + areaNVPtg.getFirstColumn()),
  222. (short)(formulaColumn + areaNVPtg.getLastColumn()),
  223. areaNVPtg.isFirstRowRelative(),
  224. areaNVPtg.isLastRowRelative(),
  225. areaNVPtg.isFirstColRelative(),
  226. areaNVPtg.isLastColRelative());
  227. } else if (ptg instanceof AreaNAPtg) {
  228. AreaNAPtg areaNAPtg = (AreaNAPtg)ptg;
  229. ptg = new AreaAPtg((short)(formulaRow + areaNAPtg.getFirstRow()),
  230. (short)(formulaRow + areaNAPtg.getLastRow()),
  231. (short)(formulaColumn + areaNAPtg.getFirstColumn()),
  232. (short)(formulaColumn + areaNAPtg.getLastColumn()),
  233. areaNAPtg.isFirstRowRelative(),
  234. areaNAPtg.isLastRowRelative(),
  235. areaNAPtg.isFirstColRelative(),
  236. areaNAPtg.isLastColRelative());
  237. }
  238. newPtgStack.add(ptg);
  239. }
  240. formula.setParsedExpression(newPtgStack);
  241. //Now its not shared!
  242. formula.setSharedFormula(false);
  243. } else {
  244. throw new RuntimeException("Shared Formula Conversion: Coding Error");
  245. }
  246. }
  247. /**
  248. * Mirroring formula records so it is registered in the ValueRecordsAggregate
  249. */
  250. public boolean isInValueSection()
  251. {
  252. return true;
  253. }
  254. /**
  255. * Register it in the ValueRecordsAggregate so it can go into the FormulaRecordAggregate
  256. */
  257. public boolean isValue() {
  258. return true;
  259. }
  260. public Object clone() {
  261. //Because this record is converted to individual Formula records, this method is not required.
  262. throw new UnsupportedOperationException("Cannot clone a SharedFormulaRecord");
  263. }
  264. }