PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/openmicroscopy/bioformats
Java | 505 lines | 251 code | 79 blank | 175 comment | 11 complexity | e20024a2a3d58e395cd362f6ec951727 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 loci.poi.util.BitField;
  39. import loci.poi.util.BitFieldFactory;
  40. import loci.poi.util.LittleEndian;
  41. /**
  42. * Title: Row Record<P>
  43. * Description: stores the row information for the sheet. <P>
  44. * REFERENCE: PG 379 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  45. * @author Andrew C. Oliver (acoliver at apache dot org)
  46. * @author Jason Height (jheight at chariot dot net dot au)
  47. * @version 2.0-pre
  48. */
  49. public class RowRecord
  50. extends Record
  51. implements Comparable
  52. {
  53. public final static short sid = 0x208;
  54. /** The maximum row number that excel can handle (zero bazed) ie 65536 rows is
  55. * max number of rows.
  56. */
  57. public final static int MAX_ROW_NUMBER = 65535;
  58. //private short field_1_row_number;
  59. private int field_1_row_number;
  60. private short field_2_first_col;
  61. private short field_3_last_col; // plus 1
  62. private short field_4_height;
  63. private short field_5_optimize; // hint field for gui, can/should be set to zero
  64. // for generated sheets.
  65. private short field_6_reserved;
  66. private short field_7_option_flags;
  67. private static final BitField outlineLevel = BitFieldFactory.getInstance(0x07);
  68. // bit 3 reserved
  69. private static final BitField colapsed = BitFieldFactory.getInstance(0x10);
  70. private static final BitField zeroHeight = BitFieldFactory.getInstance(0x20);
  71. private static final BitField badFontHeight = BitFieldFactory.getInstance(0x40);
  72. private static final BitField formatted = BitFieldFactory.getInstance(0x80);
  73. private short field_8_xf_index; // only if isFormatted
  74. public RowRecord()
  75. {
  76. }
  77. /**
  78. * Constructs a Row record and sets its fields appropriately.
  79. * @param in the RecordInputstream to read the record from
  80. */
  81. public RowRecord(RecordInputStream in)
  82. {
  83. super(in);
  84. }
  85. protected void validateSid(short id)
  86. {
  87. if (id != sid)
  88. {
  89. throw new RecordFormatException("NOT A valid ROW RECORD");
  90. }
  91. }
  92. protected void fillFields(RecordInputStream in)
  93. {
  94. //field_1_row_number = LittleEndian.getShort(data, 0 + offset);
  95. field_1_row_number = in.readUShort();
  96. field_2_first_col = in.readShort();
  97. field_3_last_col = in.readShort();
  98. field_4_height = in.readShort();
  99. field_5_optimize = in.readShort();
  100. field_6_reserved = in.readShort();
  101. field_7_option_flags = in.readShort();
  102. field_8_xf_index = in.readShort();
  103. }
  104. /**
  105. * set the logical row number for this row (0 based index)
  106. * @param row - the row number
  107. */
  108. //public void setRowNumber(short row)
  109. public void setRowNumber(int row)
  110. {
  111. field_1_row_number = row;
  112. }
  113. /**
  114. * set the logical col number for the first cell this row (0 based index)
  115. * @param col - the col number
  116. */
  117. public void setFirstCol(short col)
  118. {
  119. field_2_first_col = col;
  120. }
  121. /**
  122. * set the logical col number for the last cell this row (0 based index)
  123. * @param col - the col number
  124. */
  125. public void setLastCol(short col)
  126. {
  127. field_3_last_col = col;
  128. }
  129. /**
  130. * set the height of the row
  131. * @param height of the row
  132. */
  133. public void setHeight(short height)
  134. {
  135. field_4_height = height;
  136. }
  137. /**
  138. * set whether to optimize or not (set to 0)
  139. * @param optimize (set to 0)
  140. */
  141. public void setOptimize(short optimize)
  142. {
  143. field_5_optimize = optimize;
  144. }
  145. /**
  146. * sets the option bitmask. (use the individual bit setters that refer to this
  147. * method)
  148. * @param options - the bitmask
  149. */
  150. public void setOptionFlags(short options)
  151. {
  152. field_7_option_flags = options;
  153. }
  154. // option bitfields
  155. /**
  156. * set the outline level of this row
  157. * @param ol - the outline level
  158. * @see #setOptionFlags(short)
  159. */
  160. public void setOutlineLevel(short ol)
  161. {
  162. field_7_option_flags =
  163. outlineLevel.setShortValue(field_7_option_flags, ol);
  164. }
  165. /**
  166. * set whether or not to colapse this row
  167. * @param c - colapse or not
  168. * @see #setOptionFlags(short)
  169. */
  170. public void setColapsed(boolean c)
  171. {
  172. field_7_option_flags = colapsed.setShortBoolean(field_7_option_flags,
  173. c);
  174. }
  175. /**
  176. * set whether or not to display this row with 0 height
  177. * @param z height is zero or not.
  178. * @see #setOptionFlags(short)
  179. */
  180. public void setZeroHeight(boolean z)
  181. {
  182. field_7_option_flags =
  183. zeroHeight.setShortBoolean(field_7_option_flags, z);
  184. }
  185. /**
  186. * set whether the font and row height are not compatible
  187. * @param f true if they aren't compatible (damn not logic)
  188. * @see #setOptionFlags(short)
  189. */
  190. public void setBadFontHeight(boolean f)
  191. {
  192. field_7_option_flags =
  193. badFontHeight.setShortBoolean(field_7_option_flags, f);
  194. }
  195. /**
  196. * set whether the row has been formatted (even if its got all blank cells)
  197. * @param f formatted or not
  198. * @see #setOptionFlags(short)
  199. */
  200. public void setFormatted(boolean f)
  201. {
  202. field_7_option_flags = formatted.setShortBoolean(field_7_option_flags,
  203. f);
  204. }
  205. // end bitfields
  206. /**
  207. * if the row is formatted then this is the index to the extended format record
  208. * @see loci.poi.hssf.record.ExtendedFormatRecord
  209. * @param index to the XF record
  210. */
  211. public void setXFIndex(short index)
  212. {
  213. field_8_xf_index = index;
  214. }
  215. /**
  216. * get the logical row number for this row (0 based index)
  217. * @return row - the row number
  218. */
  219. //public short getRowNumber()
  220. public int getRowNumber()
  221. {
  222. return field_1_row_number;
  223. }
  224. /**
  225. * get the logical col number for the first cell this row (0 based index)
  226. * @return col - the col number
  227. */
  228. public short getFirstCol()
  229. {
  230. return field_2_first_col;
  231. }
  232. /**
  233. * get the logical col number for the last cell this row plus one (0 based index)
  234. * @return col - the last col number + 1
  235. */
  236. public short getLastCol()
  237. {
  238. return field_3_last_col;
  239. }
  240. /**
  241. * get the height of the row
  242. * @return height of the row
  243. */
  244. public short getHeight()
  245. {
  246. return field_4_height;
  247. }
  248. /**
  249. * get whether to optimize or not (set to 0)
  250. * @return optimize (set to 0)
  251. */
  252. public short getOptimize()
  253. {
  254. return field_5_optimize;
  255. }
  256. /**
  257. * gets the option bitmask. (use the individual bit setters that refer to this
  258. * method)
  259. * @return options - the bitmask
  260. */
  261. public short getOptionFlags()
  262. {
  263. return field_7_option_flags;
  264. }
  265. // option bitfields
  266. /**
  267. * get the outline level of this row
  268. * @return ol - the outline level
  269. * @see #getOptionFlags()
  270. */
  271. public short getOutlineLevel()
  272. {
  273. return outlineLevel.getShortValue(field_7_option_flags);
  274. }
  275. /**
  276. * get whether or not to colapse this row
  277. * @return c - colapse or not
  278. * @see #getOptionFlags()
  279. */
  280. public boolean getColapsed()
  281. {
  282. return (colapsed.isSet(field_7_option_flags));
  283. }
  284. /**
  285. * get whether or not to display this row with 0 height
  286. * @return - z height is zero or not.
  287. * @see #getOptionFlags()
  288. */
  289. public boolean getZeroHeight()
  290. {
  291. return zeroHeight.isSet(field_7_option_flags);
  292. }
  293. /**
  294. * get whether the font and row height are not compatible
  295. * @return - f -true if they aren't compatible (damn not logic)
  296. * @see #getOptionFlags()
  297. */
  298. public boolean getBadFontHeight()
  299. {
  300. return badFontHeight.isSet(field_7_option_flags);
  301. }
  302. /**
  303. * get whether the row has been formatted (even if its got all blank cells)
  304. * @return formatted or not
  305. * @see #getOptionFlags()
  306. */
  307. public boolean getFormatted()
  308. {
  309. return formatted.isSet(field_7_option_flags);
  310. }
  311. // end bitfields
  312. /**
  313. * if the row is formatted then this is the index to the extended format record
  314. * @see loci.poi.hssf.record.ExtendedFormatRecord
  315. * @return index to the XF record or bogus value (undefined) if isn't formatted
  316. */
  317. public short getXFIndex()
  318. {
  319. return field_8_xf_index;
  320. }
  321. public boolean isInValueSection()
  322. {
  323. return true;
  324. }
  325. public String toString()
  326. {
  327. StringBuffer buffer = new StringBuffer();
  328. buffer.append("[ROW]\n");
  329. buffer.append(" .rownumber = ")
  330. .append(Integer.toHexString(getRowNumber())).append("\n");
  331. buffer.append(" .firstcol = ")
  332. .append(Integer.toHexString(getFirstCol())).append("\n");
  333. buffer.append(" .lastcol = ")
  334. .append(Integer.toHexString(getLastCol())).append("\n");
  335. buffer.append(" .height = ")
  336. .append(Integer.toHexString(getHeight())).append("\n");
  337. buffer.append(" .optimize = ")
  338. .append(Integer.toHexString(getOptimize())).append("\n");
  339. buffer.append(" .reserved = ")
  340. .append(Integer.toHexString(field_6_reserved)).append("\n");
  341. buffer.append(" .optionflags = ")
  342. .append(Integer.toHexString(getOptionFlags())).append("\n");
  343. buffer.append(" .outlinelvl = ")
  344. .append(Integer.toHexString(getOutlineLevel())).append("\n");
  345. buffer.append(" .colapsed = ").append(getColapsed())
  346. .append("\n");
  347. buffer.append(" .zeroheight = ").append(getZeroHeight())
  348. .append("\n");
  349. buffer.append(" .badfontheig= ").append(getBadFontHeight())
  350. .append("\n");
  351. buffer.append(" .formatted = ").append(getFormatted())
  352. .append("\n");
  353. buffer.append(" .xfindex = ")
  354. .append(Integer.toHexString(getXFIndex())).append("\n");
  355. buffer.append("[/ROW]\n");
  356. return buffer.toString();
  357. }
  358. public int serialize(int offset, byte [] data)
  359. {
  360. LittleEndian.putShort(data, 0 + offset, sid);
  361. LittleEndian.putShort(data, 2 + offset, ( short ) 16);
  362. //LittleEndian.putShort(data, 4 + offset, getRowNumber());
  363. LittleEndian.putShort(data, 4 + offset, ( short ) getRowNumber());
  364. LittleEndian.putShort(data, 6 + offset, getFirstCol() == -1 ? (short)0 : getFirstCol());
  365. LittleEndian.putShort(data, 8 + offset, getLastCol() == -1 ? (short)0 : getLastCol());
  366. LittleEndian.putShort(data, 10 + offset, getHeight());
  367. LittleEndian.putShort(data, 12 + offset, getOptimize());
  368. LittleEndian.putShort(data, 14 + offset, field_6_reserved);
  369. LittleEndian.putShort(data, 16 + offset, getOptionFlags());
  370. // LittleEndian.putShort(data,18,getOutlineLevel());
  371. LittleEndian.putShort(data, 18 + offset, getXFIndex());
  372. return getRecordSize();
  373. }
  374. public int getRecordSize()
  375. {
  376. return 20;
  377. }
  378. public short getSid()
  379. {
  380. return sid;
  381. }
  382. public int compareTo(Object obj)
  383. {
  384. RowRecord loc = ( RowRecord ) obj;
  385. if (this.getRowNumber() == loc.getRowNumber())
  386. {
  387. return 0;
  388. }
  389. if (this.getRowNumber() < loc.getRowNumber())
  390. {
  391. return -1;
  392. }
  393. if (this.getRowNumber() > loc.getRowNumber())
  394. {
  395. return 1;
  396. }
  397. return -1;
  398. }
  399. public boolean equals(Object obj)
  400. {
  401. if (!(obj instanceof RowRecord))
  402. {
  403. return false;
  404. }
  405. RowRecord loc = ( RowRecord ) obj;
  406. if (this.getRowNumber() == loc.getRowNumber())
  407. {
  408. return true;
  409. }
  410. return false;
  411. }
  412. public Object clone() {
  413. RowRecord rec = new RowRecord();
  414. rec.field_1_row_number = field_1_row_number;
  415. rec.field_2_first_col = field_2_first_col;
  416. rec.field_3_last_col = field_3_last_col;
  417. rec.field_4_height = field_4_height;
  418. rec.field_5_optimize = field_5_optimize;
  419. rec.field_6_reserved = field_6_reserved;
  420. rec.field_7_option_flags = field_7_option_flags;
  421. rec.field_8_xf_index = field_8_xf_index;
  422. return rec;
  423. }
  424. }