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

/components/forks/poi/src/loci/poi/hssf/record/formula/ArrayPtg.java

http://github.com/openmicroscopy/bioformats
Java | 270 lines | 182 code | 28 blank | 60 comment | 32 complexity | ae4b2a799caaa5bd816effb2aa28e037 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.formula;
  38. import loci.poi.util.LittleEndian;
  39. import loci.poi.util.BitField;
  40. import loci.poi.util.BitFieldFactory;
  41. import loci.poi.util.StringUtil;
  42. import loci.poi.hssf.util.CellReference;
  43. import loci.poi.hssf.model.Workbook;
  44. import loci.poi.hssf.record.RecordFormatException;
  45. import loci.poi.hssf.record.RecordInputStream;
  46. import loci.poi.hssf.record.SSTRecord;
  47. import loci.poi.hssf.record.UnicodeString;
  48. /**
  49. * ArrayPtg - handles arrays
  50. *
  51. * The ArrayPtg is a little wierd, the size of the Ptg when parsing initially only
  52. * includes the Ptg sid and the reserved bytes. The next Ptg in the expression then follows.
  53. * It is only after the "size" of all the Ptgs is met, that the ArrayPtg data is actually
  54. * held after this. So Ptg.createParsedExpression keeps track of the number of
  55. * ArrayPtg elements and need to parse the data upto the FORMULA record size.
  56. *
  57. * @author Jason Height (jheight at chariot dot net dot au)
  58. */
  59. public class ArrayPtg extends Ptg
  60. {
  61. public final static byte sid = 0x20;
  62. protected byte field_1_reserved;
  63. protected byte field_2_reserved;
  64. protected byte field_3_reserved;
  65. protected byte field_4_reserved;
  66. protected byte field_5_reserved;
  67. protected byte field_6_reserved;
  68. protected byte field_7_reserved;
  69. protected short token_1_columns;
  70. protected short token_2_rows;
  71. protected Object[][] token_3_arrayValues;
  72. protected ArrayPtg() {
  73. //Required for clone methods
  74. }
  75. public ArrayPtg(RecordInputStream in)
  76. {
  77. field_1_reserved = in.readByte();
  78. field_2_reserved = in.readByte();
  79. field_3_reserved = in.readByte();
  80. field_4_reserved = in.readByte();
  81. field_5_reserved = in.readByte();
  82. field_6_reserved = in.readByte();
  83. field_7_reserved = in.readByte();
  84. }
  85. /** Read in the actual token (array) values. This occurs AFTER the last
  86. * Ptg in the expression.
  87. */
  88. public void readTokenValues(RecordInputStream in) {
  89. token_1_columns = (short)(0x00ff & in.readByte());
  90. token_2_rows = in.readShort();
  91. //The token_1_columns and token_2_rows do not follow the documentation.
  92. //The number of physical rows and columns is actually +1 of these values.
  93. //Which is not explicitly documented.
  94. token_1_columns++;
  95. token_2_rows++;
  96. token_3_arrayValues = new Object[token_1_columns][token_2_rows];
  97. for (int x=0;x<token_1_columns;x++) {
  98. for (int y=0;y<token_2_rows;y++) {
  99. byte grbit = in.readByte();
  100. if (grbit == 0x01) {
  101. token_3_arrayValues[x][y] = new Double(in.readDouble());
  102. } else if (grbit == 0x02) {
  103. //Ignore the doco, it is actually a unicode string with all the
  104. //trimmings ie 16 bit size, option byte etc
  105. token_3_arrayValues[x][y] = in.readUnicodeString();
  106. } else throw new RecordFormatException("Unknown grbit '"+grbit+"'");
  107. }
  108. }
  109. }
  110. public String toString()
  111. {
  112. StringBuffer buffer = new StringBuffer("[ArrayPtg]\n");
  113. buffer.append("columns = ").append(getColumnCount()).append("\n");
  114. buffer.append("rows = ").append(getRowCount()).append("\n");
  115. for (int x=0;x<getColumnCount();x++) {
  116. for (int y=0;y<getRowCount();y++) {
  117. Object o = token_3_arrayValues[x][y];
  118. buffer.append("[").append(x).append("][").append(y).append("] = ").append(o).append("\n");
  119. }
  120. }
  121. return buffer.toString();
  122. }
  123. public void writeBytes(byte [] array, int offset)
  124. {
  125. array[offset++] = (byte) (sid + ptgClass);
  126. array[offset++] = field_1_reserved;
  127. array[offset++] = field_2_reserved;
  128. array[offset++] = field_3_reserved;
  129. array[offset++] = field_4_reserved;
  130. array[offset++] = field_5_reserved;
  131. array[offset++] = field_6_reserved;
  132. array[offset++] = field_7_reserved;
  133. }
  134. public int writeTokenValueBytes(byte [] array, int offset) {
  135. int pos = 0;
  136. array[pos + offset] = (byte)(token_1_columns-1);
  137. pos++;
  138. LittleEndian.putShort(array, pos+offset, (short)(token_2_rows-1));
  139. pos += 2;
  140. for (int x=0;x<getColumnCount();x++) {
  141. for (int y=0;y<getRowCount();y++) {
  142. Object o = token_3_arrayValues[x][y];
  143. if (o instanceof Double) {
  144. array[pos+offset] = 0x01;
  145. pos++;
  146. LittleEndian.putDouble(array, pos+offset, ((Double)o).doubleValue());
  147. pos+=8;
  148. } else if (o instanceof UnicodeString) {
  149. array[pos+offset] = 0x02;
  150. pos++;
  151. UnicodeString s = (UnicodeString)o;
  152. //JMH TBD Handle string continuation. Id do it now but its 4am.
  153. UnicodeString.UnicodeRecordStats stats = new UnicodeString.UnicodeRecordStats();
  154. s.serialize(stats, pos + offset, array);
  155. pos += stats.recordSize;
  156. } else throw new RuntimeException("Coding error");
  157. }
  158. }
  159. return pos;
  160. }
  161. public void setRowCount(short row)
  162. {
  163. token_2_rows = row;
  164. }
  165. public short getRowCount()
  166. {
  167. return token_2_rows;
  168. }
  169. public void setColumnCount(short col)
  170. {
  171. token_1_columns = (byte)col;
  172. }
  173. public short getColumnCount()
  174. {
  175. return token_1_columns;
  176. }
  177. /** This size includes the size of the array Ptg plus the Array Ptg Token value size*/
  178. public int getSize()
  179. {
  180. int size = 1+7+1+2;
  181. for (int x=0;x<getColumnCount();x++) {
  182. for (int y=0;y<getRowCount();y++) {
  183. Object o = token_3_arrayValues[x][y];
  184. if (o instanceof UnicodeString) {
  185. size++;
  186. UnicodeString.UnicodeRecordStats rs = new UnicodeString.UnicodeRecordStats();
  187. ((UnicodeString)o).getRecordSize(rs);
  188. size += rs.recordSize;
  189. } else if (o instanceof Double) {
  190. size += 9;
  191. }
  192. }
  193. }
  194. return size;
  195. }
  196. public String toFormulaString(Workbook book)
  197. {
  198. StringBuffer b = new StringBuffer();
  199. b.append("{");
  200. for (int x=0;x<getColumnCount();x++) {
  201. for (int y=0;y<getRowCount();y++) {
  202. Object o = token_3_arrayValues[x][y];
  203. if (o instanceof String) {
  204. b.append((String)o);
  205. } else if (o instanceof Double) {
  206. b.append(((Double)o).doubleValue());
  207. }
  208. if (y != getRowCount())
  209. b.append(",");
  210. }
  211. if (x != getColumnCount())
  212. b.append(";");
  213. }
  214. b.append("}");
  215. return b.toString();
  216. }
  217. public byte getDefaultOperandClass() {
  218. return Ptg.CLASS_ARRAY;
  219. }
  220. public Object clone() {
  221. ArrayPtg ptg = new ArrayPtg();
  222. ptg.field_1_reserved = field_1_reserved;
  223. ptg.field_2_reserved = field_2_reserved;
  224. ptg.field_3_reserved = field_3_reserved;
  225. ptg.field_4_reserved = field_4_reserved;
  226. ptg.field_5_reserved = field_5_reserved;
  227. ptg.field_6_reserved = field_6_reserved;
  228. ptg.field_7_reserved = field_7_reserved;
  229. ptg.token_1_columns = token_1_columns;
  230. ptg.token_2_rows = token_2_rows;
  231. ptg.token_3_arrayValues = new Object[getColumnCount()][getRowCount()];
  232. for (int x=0;x<getColumnCount();x++) {
  233. for (int y=0;y<getRowCount();y++) {
  234. ptg.token_3_arrayValues[x][y] = token_3_arrayValues[x][y];
  235. }
  236. }
  237. ptg.setClass(ptgClass);
  238. return ptg;
  239. }
  240. }