PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/aggregates/RowRecordsAggregate.java

http://github.com/openmicroscopy/bioformats
Java | 488 lines | 296 code | 70 blank | 122 comment | 51 complexity | e2636fd1db0e4f81da83b828da4e76a6 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.aggregates;
  38. import loci.poi.hssf.record.DBCellRecord;
  39. import loci.poi.hssf.record.Record;
  40. import loci.poi.hssf.record.RecordInputStream;
  41. import loci.poi.hssf.record.RowRecord;
  42. import java.util.Iterator;
  43. import java.util.Map;
  44. import java.util.TreeMap;
  45. /**
  46. *
  47. * @author andy
  48. * @author Jason Height (jheight at chariot dot net dot au)
  49. */
  50. public class RowRecordsAggregate
  51. extends Record
  52. {
  53. int firstrow = -1;
  54. int lastrow = -1;
  55. Map records = null;
  56. int size = 0;
  57. /** Creates a new instance of ValueRecordsAggregate */
  58. public RowRecordsAggregate()
  59. {
  60. records = new TreeMap();
  61. }
  62. public void insertRow(RowRecord row)
  63. {
  64. size += row.getRecordSize();
  65. // Integer integer = new Integer(row.getRowNumber());
  66. records.put(row, row);
  67. if ((row.getRowNumber() < firstrow) || (firstrow == -1))
  68. {
  69. firstrow = row.getRowNumber();
  70. }
  71. if ((row.getRowNumber() > lastrow) || (lastrow == -1))
  72. {
  73. lastrow = row.getRowNumber();
  74. }
  75. }
  76. public void removeRow(RowRecord row)
  77. {
  78. size -= row.getRecordSize();
  79. // Integer integer = new Integer(row.getRowNumber());
  80. records.remove(row);
  81. }
  82. public RowRecord getRow(int rownum)
  83. {
  84. // Row must be between 0 and 65535
  85. if(rownum < 0 || rownum > 65535) {
  86. throw new IllegalArgumentException("The row number must be between 0 and 65535");
  87. }
  88. RowRecord row = new RowRecord();
  89. row.setRowNumber(rownum);
  90. return ( RowRecord ) records.get(row);
  91. }
  92. public int getPhysicalNumberOfRows()
  93. {
  94. return records.size();
  95. }
  96. public int getFirstRowNum()
  97. {
  98. return firstrow;
  99. }
  100. public int getLastRowNum()
  101. {
  102. return lastrow;
  103. }
  104. /** Returns the number of row blocks.
  105. * <p/>The row blocks are goupings of rows that contain the DBCell record
  106. * after them
  107. */
  108. public int getRowBlockCount() {
  109. int size = records.size()/DBCellRecord.BLOCK_SIZE;
  110. if ((records.size() % DBCellRecord.BLOCK_SIZE) != 0)
  111. size++;
  112. return size;
  113. }
  114. public int getRowBlockSize(int block) {
  115. return 20 * getRowCountForBlock(block);
  116. }
  117. /** Returns the number of physical rows within a block*/
  118. public int getRowCountForBlock(int block) {
  119. int startIndex = block * DBCellRecord.BLOCK_SIZE;
  120. int endIndex = startIndex + DBCellRecord.BLOCK_SIZE - 1;
  121. if (endIndex >= records.size())
  122. endIndex = records.size()-1;
  123. return endIndex-startIndex+1;
  124. }
  125. /** Returns the physical row number of the first row in a block*/
  126. public int getStartRowNumberForBlock(int block) {
  127. //Given that we basically iterate through the rows in order,
  128. //For a performance improvement, it would be better to return an instance of
  129. //an iterator and use that instance throughout, rather than recreating one and
  130. //having to move it to the right position.
  131. int startIndex = block * DBCellRecord.BLOCK_SIZE;
  132. Iterator rowIter = records.values().iterator();
  133. RowRecord row = null;
  134. //Position the iterator at the start of the block
  135. for (int i=0; i<=startIndex;i++) {
  136. row = (RowRecord)rowIter.next();
  137. }
  138. return row.getRowNumber();
  139. }
  140. /** Returns the physical row number of the end row in a block*/
  141. public int getEndRowNumberForBlock(int block) {
  142. int endIndex = ((block + 1)*DBCellRecord.BLOCK_SIZE)-1;
  143. if (endIndex >= records.size())
  144. endIndex = records.size()-1;
  145. Iterator rowIter = records.values().iterator();
  146. RowRecord row = null;
  147. for (int i=0; i<=endIndex;i++) {
  148. row = (RowRecord)rowIter.next();
  149. }
  150. return row.getRowNumber();
  151. }
  152. /** Serializes a block of the rows */
  153. private int serializeRowBlock(final int block, final int offset, byte[] data) {
  154. final int startIndex = block*DBCellRecord.BLOCK_SIZE;
  155. final int endIndex = startIndex + DBCellRecord.BLOCK_SIZE;
  156. Iterator rowIterator = records.values().iterator();
  157. int pos = offset;
  158. //Given that we basically iterate through the rows in order,
  159. //For a performance improvement, it would be better to return an instance of
  160. //an iterator and use that instance throughout, rather than recreating one and
  161. //having to move it to the right position.
  162. int i=0;
  163. for (;i<startIndex;i++)
  164. rowIterator.next();
  165. while(rowIterator.hasNext() && (i++ < endIndex)) {
  166. RowRecord row = (RowRecord)rowIterator.next();
  167. pos += row.serialize(pos, data);
  168. }
  169. return pos - offset;
  170. }
  171. public int serialize(int offset, byte [] data) {
  172. throw new RuntimeException("The serialize method that passes in cells should be used");
  173. }
  174. /**
  175. * called by the class that is responsible for writing this sucker.
  176. * Subclasses should implement this so that their data is passed back in a
  177. * byte array.
  178. *
  179. * @param offset offset to begin writing at
  180. * @param data byte array containing instance data
  181. * @return number of bytes written
  182. */
  183. public int serialize(int offset, byte [] data, ValueRecordsAggregate cells)
  184. {
  185. int pos = offset;
  186. //DBCells are serialized before row records.
  187. final int blockCount = getRowBlockCount();
  188. for (int block=0;block<blockCount;block++) {
  189. //Serialize a block of rows.
  190. //Hold onto the position of the first row in the block
  191. final int rowStartPos = pos;
  192. //Hold onto the size of this block that was serialized
  193. final int rowBlockSize = serializeRowBlock(block, pos, data);
  194. pos += rowBlockSize;
  195. //Serialize a block of cells for those rows
  196. final int startRowNumber = getStartRowNumberForBlock(block);
  197. final int endRowNumber = getEndRowNumberForBlock(block);
  198. DBCellRecord cellRecord = new DBCellRecord();
  199. //Note: Cell references start from the second row...
  200. int cellRefOffset = (rowBlockSize-20);
  201. for (int row=startRowNumber;row<=endRowNumber;row++) {
  202. if (null != cells && cells.rowHasCells(row)) {
  203. final int rowCellSize = cells.serializeCellRow(row, pos, data);
  204. pos += rowCellSize;
  205. //Add the offset to the first cell for the row into the DBCellRecord.
  206. cellRecord.addCellOffset((short)cellRefOffset);
  207. cellRefOffset = rowCellSize;
  208. }
  209. }
  210. //Calculate Offset from the start of a DBCellRecord to the first Row
  211. cellRecord.setRowOffset(pos - rowStartPos);
  212. pos += cellRecord.serialize(pos, data);
  213. }
  214. return pos - offset;
  215. }
  216. /**
  217. * You never fill an aggregate
  218. */
  219. protected void fillFields(RecordInputStream in)
  220. {
  221. }
  222. /**
  223. * called by constructor, should throw runtime exception in the event of a
  224. * record passed with a differing ID.
  225. *
  226. * @param id alleged id for this record
  227. */
  228. protected void validateSid(short id)
  229. {
  230. }
  231. /**
  232. * return the non static version of the id for this record.
  233. */
  234. public short getSid()
  235. {
  236. return -1000;
  237. }
  238. public int getRecordSize()
  239. {
  240. return size;
  241. }
  242. public Iterator getIterator()
  243. {
  244. return records.values().iterator();
  245. }
  246. /**
  247. * Performs a deep clone of the record
  248. */
  249. public Object clone()
  250. {
  251. RowRecordsAggregate rec = new RowRecordsAggregate();
  252. for ( Iterator rowIter = getIterator(); rowIter.hasNext(); )
  253. {
  254. //return the cloned Row Record & insert
  255. RowRecord row = (RowRecord) ( (RowRecord) rowIter.next() ).clone();
  256. rec.insertRow( row );
  257. }
  258. return rec;
  259. }
  260. public int findStartOfRowOutlineGroup(int row)
  261. {
  262. // Find the start of the group.
  263. RowRecord rowRecord = this.getRow( row );
  264. int level = rowRecord.getOutlineLevel();
  265. int currentRow = row;
  266. while (this.getRow( currentRow ) != null)
  267. {
  268. rowRecord = this.getRow( currentRow );
  269. if (rowRecord.getOutlineLevel() < level)
  270. return currentRow + 1;
  271. currentRow--;
  272. }
  273. return currentRow + 1;
  274. }
  275. public int findEndOfRowOutlineGroup( int row )
  276. {
  277. int level = getRow( row ).getOutlineLevel();
  278. int currentRow;
  279. for (currentRow = row; currentRow < this.getLastRowNum(); currentRow++)
  280. {
  281. if (getRow(currentRow) == null || getRow(currentRow).getOutlineLevel() < level)
  282. {
  283. break;
  284. }
  285. }
  286. return currentRow-1;
  287. }
  288. public int writeHidden( RowRecord rowRecord, int row, boolean hidden )
  289. {
  290. int level = rowRecord.getOutlineLevel();
  291. while (rowRecord != null && this.getRow(row).getOutlineLevel() >= level)
  292. {
  293. rowRecord.setZeroHeight( hidden );
  294. row++;
  295. rowRecord = this.getRow( row );
  296. }
  297. return row - 1;
  298. }
  299. public void collapseRow( int rowNumber )
  300. {
  301. // Find the start of the group.
  302. int startRow = findStartOfRowOutlineGroup( rowNumber );
  303. RowRecord rowRecord = (RowRecord) getRow( startRow );
  304. // Hide all the columns until the end of the group
  305. int lastRow = writeHidden( rowRecord, startRow, true );
  306. // Write collapse field
  307. if (getRow(lastRow + 1) != null)
  308. {
  309. getRow(lastRow + 1).setColapsed( true );
  310. }
  311. else
  312. {
  313. RowRecord row = createRow( lastRow + 1);
  314. row.setColapsed( true );
  315. insertRow( row );
  316. }
  317. }
  318. /**
  319. * Create a row record.
  320. *
  321. * @param row number
  322. * @return RowRecord created for the passed in row number
  323. * @see loci.poi.hssf.record.RowRecord
  324. */
  325. public static RowRecord createRow(int row)
  326. {
  327. RowRecord rowrec = new RowRecord();
  328. //rowrec.setRowNumber(( short ) row);
  329. rowrec.setRowNumber(row);
  330. rowrec.setHeight(( short ) 0xff);
  331. rowrec.setOptimize(( short ) 0x0);
  332. rowrec.setOptionFlags(( short ) 0x100); // seems necessary for outlining
  333. rowrec.setXFIndex(( short ) 0xf);
  334. return rowrec;
  335. }
  336. public boolean isRowGroupCollapsed( int row )
  337. {
  338. int collapseRow = findEndOfRowOutlineGroup( row ) + 1;
  339. if (getRow(collapseRow) == null)
  340. return false;
  341. else
  342. return getRow( collapseRow ).getColapsed();
  343. }
  344. public void expandRow( int rowNumber )
  345. {
  346. int idx = rowNumber;
  347. if (idx == -1)
  348. return;
  349. // If it is already expanded do nothing.
  350. if (!isRowGroupCollapsed(idx))
  351. return;
  352. // Find the start of the group.
  353. int startIdx = findStartOfRowOutlineGroup( idx );
  354. RowRecord row = getRow( startIdx );
  355. // Find the end of the group.
  356. int endIdx = findEndOfRowOutlineGroup( idx );
  357. // expand:
  358. // colapsed bit must be unset
  359. // hidden bit gets unset _if_ surrounding groups are expanded you can determine
  360. // this by looking at the hidden bit of the enclosing group. You will have
  361. // to look at the start and the end of the current group to determine which
  362. // is the enclosing group
  363. // hidden bit only is altered for this outline level. ie. don't uncollapse contained groups
  364. if ( !isRowGroupHiddenByParent( idx ) )
  365. {
  366. for ( int i = startIdx; i <= endIdx; i++ )
  367. {
  368. if ( row.getOutlineLevel() == getRow( i ).getOutlineLevel() )
  369. getRow( i ).setZeroHeight( false );
  370. else if (!isRowGroupCollapsed(i))
  371. getRow( i ).setZeroHeight( false );
  372. }
  373. }
  374. // Write collapse field
  375. getRow( endIdx + 1 ).setColapsed( false );
  376. }
  377. public boolean isRowGroupHiddenByParent( int row )
  378. {
  379. // Look out outline details of end
  380. int endLevel;
  381. boolean endHidden;
  382. int endOfOutlineGroupIdx = findEndOfRowOutlineGroup( row );
  383. if (getRow( endOfOutlineGroupIdx + 1 ) == null)
  384. {
  385. endLevel = 0;
  386. endHidden = false;
  387. }
  388. else
  389. {
  390. endLevel = getRow( endOfOutlineGroupIdx + 1).getOutlineLevel();
  391. endHidden = getRow( endOfOutlineGroupIdx + 1).getZeroHeight();
  392. }
  393. // Look out outline details of start
  394. int startLevel;
  395. boolean startHidden;
  396. int startOfOutlineGroupIdx = findStartOfRowOutlineGroup( row );
  397. if (startOfOutlineGroupIdx - 1 < 0 || getRow(startOfOutlineGroupIdx - 1) == null)
  398. {
  399. startLevel = 0;
  400. startHidden = false;
  401. }
  402. else
  403. {
  404. startLevel = getRow( startOfOutlineGroupIdx - 1).getOutlineLevel();
  405. startHidden = getRow( startOfOutlineGroupIdx - 1 ).getZeroHeight();
  406. }
  407. if (endLevel > startLevel)
  408. {
  409. return endHidden;
  410. }
  411. else
  412. {
  413. return startHidden;
  414. }
  415. }
  416. }