PageRenderTime 175ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/openmicroscopy/bioformats
Java | 571 lines | 348 code | 84 blank | 139 comment | 58 complexity | 097b310c8bca6adc7d7f77161df42564 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. /*
  38. * FormulaRecord.java
  39. *
  40. * Created on October 28, 2001, 5:44 PM
  41. */
  42. package loci.poi.hssf.record;
  43. import java.util.List;
  44. import java.util.Stack;
  45. import loci.poi.hssf.record.formula.Ptg;
  46. import loci.poi.util.BitField;
  47. import loci.poi.util.BitFieldFactory;
  48. import loci.poi.util.LittleEndian;
  49. /**
  50. * Formula Record.
  51. * REFERENCE: PG 317/444 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  52. * @author Andrew C. Oliver (acoliver at apache dot org)
  53. * @author Jason Height (jheight at chariot dot net dot au)
  54. * @version 2.0-pre
  55. */
  56. public class FormulaRecord
  57. extends Record
  58. implements CellValueRecordInterface, Comparable
  59. {
  60. public static final short sid =
  61. 0x06; // docs say 406...because of a bug Microsoft support site article #Q184647)
  62. //private short field_1_row;
  63. private int field_1_row;
  64. private short field_2_column;
  65. private short field_3_xf;
  66. private double field_4_value;
  67. private short field_5_options;
  68. private BitField alwaysCalc = BitFieldFactory.getInstance(0x0001);
  69. private BitField calcOnLoad = BitFieldFactory.getInstance(0x0002);
  70. private BitField sharedFormula = BitFieldFactory.getInstance(0x0008);
  71. private int field_6_zero;
  72. private short field_7_expression_len;
  73. private Stack field_8_parsed_expr;
  74. /**
  75. * Since the NaN support seems sketchy (different constants) we'll store and spit it out directly
  76. */
  77. private byte[] value_data;
  78. private byte[] all_data; //if formula support is not enabled then
  79. //we'll just store/reserialize
  80. /** Creates new FormulaRecord */
  81. public FormulaRecord()
  82. {
  83. field_8_parsed_expr = new Stack();
  84. }
  85. /**
  86. * Constructs a Formula record and sets its fields appropriately.
  87. * Note - id must be 0x06 (NOT 0x406 see MSKB #Q184647 for an
  88. * "explanation of this bug in the documentation) or an exception
  89. * will be throw upon validation
  90. *
  91. * @param in the RecordInputstream to read the record from
  92. */
  93. public FormulaRecord(RecordInputStream in)
  94. {
  95. super(in);
  96. }
  97. protected void fillFields(RecordInputStream in)
  98. {
  99. try {
  100. field_1_row = in.readUShort();
  101. field_2_column = in.readShort();
  102. field_3_xf = in.readShort();
  103. field_4_value = in.readDouble();
  104. field_5_options = in.readShort();
  105. if (Double.isNaN(field_4_value)) {
  106. value_data = in.getNANData();
  107. }
  108. field_6_zero = in.readInt();
  109. field_7_expression_len = in.readShort();
  110. field_8_parsed_expr = Ptg.createParsedExpressionTokens(field_7_expression_len, in);
  111. } catch (java.lang.UnsupportedOperationException uoe) {
  112. throw new RecordFormatException(uoe);
  113. }
  114. }
  115. //public void setRow(short row)
  116. public void setRow(int row)
  117. {
  118. field_1_row = row;
  119. }
  120. public void setColumn(short column)
  121. {
  122. field_2_column = column;
  123. }
  124. public void setXFIndex(short xf)
  125. {
  126. field_3_xf = xf;
  127. }
  128. /**
  129. * set the calculated value of the formula
  130. *
  131. * @param value calculated value
  132. */
  133. public void setValue(double value)
  134. {
  135. field_4_value = value;
  136. }
  137. /**
  138. * set the option flags
  139. *
  140. * @param options bitmask
  141. */
  142. public void setOptions(short options)
  143. {
  144. field_5_options = options;
  145. }
  146. /**
  147. * set the length (in number of tokens) of the expression
  148. * @param len length
  149. */
  150. public void setExpressionLength(short len)
  151. {
  152. field_7_expression_len = len;
  153. }
  154. //public short getRow()
  155. public int getRow()
  156. {
  157. return field_1_row;
  158. }
  159. public short getColumn()
  160. {
  161. return field_2_column;
  162. }
  163. public short getXFIndex()
  164. {
  165. return field_3_xf;
  166. }
  167. /**
  168. * get the calculated value of the formula
  169. *
  170. * @return calculated value
  171. */
  172. public double getValue()
  173. {
  174. return field_4_value;
  175. }
  176. /**
  177. * get the option flags
  178. *
  179. * @return bitmask
  180. */
  181. public short getOptions()
  182. {
  183. return field_5_options;
  184. }
  185. public boolean isSharedFormula() {
  186. return sharedFormula.isSet(field_5_options);
  187. }
  188. public void setSharedFormula(boolean flag) {
  189. sharedFormula.setBoolean(field_5_options, flag);
  190. }
  191. /**
  192. * get the length (in number of tokens) of the expression
  193. * @return expression length
  194. */
  195. public short getExpressionLength()
  196. {
  197. return field_7_expression_len;
  198. }
  199. /**
  200. * push a token onto the stack
  201. *
  202. * @param ptg the token
  203. */
  204. public void pushExpressionToken(Ptg ptg)
  205. {
  206. field_8_parsed_expr.push(ptg);
  207. }
  208. /**
  209. * pop a token off of the stack
  210. *
  211. * @return Ptg - the token
  212. */
  213. public Ptg popExpressionToken()
  214. {
  215. return ( Ptg ) field_8_parsed_expr.pop();
  216. }
  217. /**
  218. * peek at the token on the top of stack
  219. *
  220. * @return Ptg - the token
  221. */
  222. public Ptg peekExpressionToken()
  223. {
  224. return ( Ptg ) field_8_parsed_expr.peek();
  225. }
  226. /**
  227. * get the size of the stack
  228. * @return size of the stack
  229. */
  230. public int getNumberOfExpressionTokens()
  231. {
  232. if (this.field_8_parsed_expr == null) {
  233. return 0;
  234. } else {
  235. return field_8_parsed_expr.size();
  236. }
  237. }
  238. /**
  239. * get the stack as a list
  240. *
  241. * @return list of tokens (casts stack to a list and returns it!)
  242. * this method can return null is we are unable to create Ptgs from
  243. * existing excel file
  244. * callers should check for null!
  245. */
  246. public List getParsedExpression()
  247. {
  248. return field_8_parsed_expr;
  249. }
  250. public void setParsedExpression(Stack ptgs) {
  251. field_8_parsed_expr = ptgs;
  252. }
  253. /**
  254. * called by constructor, should throw runtime exception in the event of a
  255. * record passed with a differing ID.
  256. *
  257. * @param id alleged id for this record
  258. */
  259. protected void validateSid(short id)
  260. {
  261. if (id != sid)
  262. {
  263. throw new RecordFormatException("NOT A FORMULA RECORD");
  264. }
  265. }
  266. public short getSid()
  267. {
  268. return sid;
  269. }
  270. /**
  271. * called by the class that is responsible for writing this sucker.
  272. * Subclasses should implement this so that their data is passed back in a
  273. * byte array.
  274. *
  275. * @return byte array containing instance data
  276. */
  277. public int serialize(int offset, byte [] data)
  278. {
  279. if (this.field_8_parsed_expr != null) {
  280. int ptgSize = getTotalPtgSize();
  281. LittleEndian.putShort(data, 0 + offset, sid);
  282. LittleEndian.putShort(data, 2 + offset, ( short ) (22 + ptgSize));
  283. //LittleEndian.putShort(data, 4 + offset, getRow());
  284. LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
  285. LittleEndian.putShort(data, 6 + offset, getColumn());
  286. LittleEndian.putShort(data, 8 + offset, getXFIndex());
  287. //only reserialize if the value is still NaN and we have old nan data
  288. if (Double.isNaN(this.getValue()) && value_data != null) {
  289. System.arraycopy(value_data,0,data,10 + offset,value_data.length);
  290. } else {
  291. LittleEndian.putDouble(data, 10 + offset, field_4_value);
  292. }
  293. LittleEndian.putShort(data, 18 + offset, getOptions());
  294. //when writing the chn field (offset 20), it's supposed to be 0 but ignored on read
  295. //Microsoft Excel Developer's Kit Page 318
  296. LittleEndian.putInt(data, 20 + offset, 0);
  297. LittleEndian.putShort(data, 24 + offset, getExpressionLength());
  298. Ptg.serializePtgStack(field_8_parsed_expr, data, 26+offset);
  299. } else {
  300. System.arraycopy(all_data,0,data,offset,all_data.length);
  301. }
  302. return getRecordSize();
  303. }
  304. public int getRecordSize()
  305. {
  306. int retval =0;
  307. if (this.field_8_parsed_expr != null) {
  308. retval = getTotalPtgSize() + 26;
  309. } else {
  310. retval =all_data.length;
  311. }
  312. return retval;
  313. // return getTotalPtgSize() + 28;
  314. }
  315. private int getTotalPtgSize()
  316. {
  317. List list = getParsedExpression();
  318. int retval = 0;
  319. for (int k = 0; k < list.size(); k++)
  320. {
  321. Ptg ptg = ( Ptg ) list.get(k);
  322. retval += ptg.getSize();
  323. }
  324. return retval;
  325. }
  326. public boolean isBefore(CellValueRecordInterface i)
  327. {
  328. if (this.getRow() > i.getRow())
  329. {
  330. return false;
  331. }
  332. if ((this.getRow() == i.getRow())
  333. && (this.getColumn() > i.getColumn()))
  334. {
  335. return false;
  336. }
  337. if ((this.getRow() == i.getRow())
  338. && (this.getColumn() == i.getColumn()))
  339. {
  340. return false;
  341. }
  342. return true;
  343. }
  344. public boolean isAfter(CellValueRecordInterface i)
  345. {
  346. if (this.getRow() < i.getRow())
  347. {
  348. return false;
  349. }
  350. if ((this.getRow() == i.getRow())
  351. && (this.getColumn() < i.getColumn()))
  352. {
  353. return false;
  354. }
  355. if ((this.getRow() == i.getRow())
  356. && (this.getColumn() == i.getColumn()))
  357. {
  358. return false;
  359. }
  360. return true;
  361. }
  362. public boolean isEqual(CellValueRecordInterface i)
  363. {
  364. return ((this.getRow() == i.getRow())
  365. && (this.getColumn() == i.getColumn()));
  366. }
  367. public boolean isInValueSection()
  368. {
  369. return true;
  370. }
  371. public boolean isValue()
  372. {
  373. return true;
  374. }
  375. public int compareTo(Object obj)
  376. {
  377. CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
  378. if ((this.getRow() == loc.getRow())
  379. && (this.getColumn() == loc.getColumn()))
  380. {
  381. return 0;
  382. }
  383. if (this.getRow() < loc.getRow())
  384. {
  385. return -1;
  386. }
  387. if (this.getRow() > loc.getRow())
  388. {
  389. return 1;
  390. }
  391. if (this.getColumn() < loc.getColumn())
  392. {
  393. return -1;
  394. }
  395. if (this.getColumn() > loc.getColumn())
  396. {
  397. return 1;
  398. }
  399. return -1;
  400. }
  401. public boolean equals(Object obj)
  402. {
  403. if (!(obj instanceof CellValueRecordInterface))
  404. {
  405. return false;
  406. }
  407. CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
  408. if ((this.getRow() == loc.getRow())
  409. && (this.getColumn() == loc.getColumn()))
  410. {
  411. return true;
  412. }
  413. return false;
  414. }
  415. public String toString()
  416. {
  417. StringBuffer buffer = new StringBuffer();
  418. buffer.append("[FORMULA]\n");
  419. buffer.append(" .row = ")
  420. .append(Integer.toHexString(getRow())).append("\n");
  421. buffer.append(" .column = ")
  422. .append(Integer.toHexString(getColumn()))
  423. .append("\n");
  424. buffer.append(" .xf = ")
  425. .append(Integer.toHexString(getXFIndex())).append("\n");
  426. if (Double.isNaN(this.getValue()) && value_data != null)
  427. buffer.append(" .value (NaN) = ")
  428. .append(loci.poi.util.HexDump.dump(value_data,0,0))
  429. .append("\n");
  430. else
  431. buffer.append(" .value = ").append(getValue())
  432. .append("\n");
  433. buffer.append(" .options = ").append(getOptions())
  434. .append("\n");
  435. buffer.append(" .alwaysCalc = ").append(alwaysCalc.isSet(getOptions()))
  436. .append("\n");
  437. buffer.append(" .calcOnLoad = ").append(calcOnLoad.isSet(getOptions()))
  438. .append("\n");
  439. buffer.append(" .sharedFormula = ").append(sharedFormula.isSet(getOptions()))
  440. .append("\n");
  441. buffer.append(" .zero = ").append(field_6_zero)
  442. .append("\n");
  443. buffer.append(" .expressionlength= ").append(getExpressionLength())
  444. .append("\n");
  445. if (field_8_parsed_expr != null) {
  446. buffer.append(" .numptgsinarray = ").append(field_8_parsed_expr.size())
  447. .append("\n");
  448. for (int k = 0; k < field_8_parsed_expr.size(); k++ ) {
  449. buffer.append(" Ptg(")
  450. .append(k)
  451. .append(")=")
  452. .append(field_8_parsed_expr.get(k).toString())
  453. .append("\n")
  454. .append(((Ptg)field_8_parsed_expr.get(k)).toDebugString())
  455. .append("\n");
  456. }
  457. }else {
  458. buffer.append("Formula full data \n")
  459. .append(loci.poi.util.HexDump.dump(this.all_data,0,0));
  460. }
  461. buffer.append("[/FORMULA]\n");
  462. return buffer.toString();
  463. }
  464. public Object clone() {
  465. FormulaRecord rec = new FormulaRecord();
  466. rec.field_1_row = field_1_row;
  467. rec.field_2_column = field_2_column;
  468. rec.field_3_xf = field_3_xf;
  469. rec.field_4_value = field_4_value;
  470. rec.field_5_options = field_5_options;
  471. rec.field_6_zero = field_6_zero;
  472. rec.field_7_expression_len = field_7_expression_len;
  473. rec.field_8_parsed_expr = new Stack();
  474. int size = 0;
  475. if (field_8_parsed_expr != null)
  476. size = field_8_parsed_expr.size();
  477. for (int i=0; i< size; i++) {
  478. Ptg ptg = (Ptg)((Ptg)field_8_parsed_expr.get(i)).clone();
  479. rec.field_8_parsed_expr.add(i, ptg);
  480. }
  481. rec.value_data = value_data;
  482. rec.all_data = all_data;
  483. return rec;
  484. }
  485. }