You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RowRecordsAggregate.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record.aggregates;
  16. import java.util.ArrayList;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Spliterator;
  21. import java.util.TreeMap;
  22. import org.apache.poi.hssf.model.RecordStream;
  23. import org.apache.poi.hssf.record.ArrayRecord;
  24. import org.apache.poi.hssf.record.CellValueRecordInterface;
  25. import org.apache.poi.hssf.record.ContinueRecord;
  26. import org.apache.poi.hssf.record.DBCellRecord;
  27. import org.apache.poi.hssf.record.DConRefRecord;
  28. import org.apache.poi.hssf.record.DimensionsRecord;
  29. import org.apache.poi.hssf.record.FormulaRecord;
  30. import org.apache.poi.hssf.record.IndexRecord;
  31. import org.apache.poi.hssf.record.MergeCellsRecord;
  32. import org.apache.poi.hssf.record.MulBlankRecord;
  33. import org.apache.poi.hssf.record.Record;
  34. import org.apache.poi.hssf.record.RowRecord;
  35. import org.apache.poi.hssf.record.SharedFormulaRecord;
  36. import org.apache.poi.hssf.record.TableRecord;
  37. import org.apache.poi.hssf.record.UnknownRecord;
  38. import org.apache.poi.ss.SpreadsheetVersion;
  39. import org.apache.poi.ss.formula.FormulaShifter;
  40. public final class RowRecordsAggregate extends RecordAggregate {
  41. private int _firstrow = -1;
  42. private int _lastrow = -1;
  43. private final Map<Integer, RowRecord> _rowRecords;
  44. private final ValueRecordsAggregate _valuesAgg;
  45. private final List<org.apache.poi.hssf.record.Record> _unknownRecords;
  46. private final SharedValueManager _sharedValueManager;
  47. // Cache values to speed up performance of
  48. // getStartRowNumberForBlock / getEndRowNumberForBlock, see Bugzilla 47405
  49. private RowRecord[] _rowRecordValues;
  50. /** Creates a new instance of ValueRecordsAggregate */
  51. public RowRecordsAggregate() {
  52. this(SharedValueManager.createEmpty());
  53. }
  54. private RowRecordsAggregate(SharedValueManager svm) {
  55. if (svm == null) {
  56. throw new IllegalArgumentException("SharedValueManager must be provided.");
  57. }
  58. _rowRecords = new TreeMap<>();
  59. _valuesAgg = new ValueRecordsAggregate();
  60. _unknownRecords = new ArrayList<>();
  61. _sharedValueManager = svm;
  62. }
  63. /**
  64. * @param rs record stream with all {@link SharedFormulaRecord}
  65. * {@link ArrayRecord}, {@link TableRecord} {@link MergeCellsRecord} Records removed
  66. * @param svm an initialised {@link SharedValueManager} (from the shared formula, array
  67. * and table records of the current sheet). Never <code>null</code>.
  68. */
  69. public RowRecordsAggregate(RecordStream rs, SharedValueManager svm) {
  70. this(svm);
  71. while(rs.hasNext()) {
  72. Record rec = rs.getNext();
  73. switch (rec.getSid()) {
  74. case RowRecord.sid:
  75. insertRow((RowRecord) rec);
  76. continue;
  77. case DConRefRecord.sid:
  78. addUnknownRecord(rec);
  79. continue;
  80. case DBCellRecord.sid:
  81. // end of 'Row Block'. Should only occur after cell records
  82. // ignore DBCELL records because POI generates them upon re-serialization
  83. continue;
  84. }
  85. if (rec instanceof UnknownRecord) {
  86. // might need to keep track of where exactly these belong
  87. addUnknownRecord(rec);
  88. while (rs.peekNextSid() == ContinueRecord.sid) {
  89. addUnknownRecord(rs.getNext());
  90. }
  91. continue;
  92. }
  93. if (rec instanceof MulBlankRecord) {
  94. _valuesAgg.addMultipleBlanks((MulBlankRecord) rec);
  95. continue;
  96. }
  97. if (!(rec instanceof CellValueRecordInterface)) {
  98. throw new RuntimeException("Unexpected record type (" + rec.getClass().getName() + ")");
  99. }
  100. _valuesAgg.construct((CellValueRecordInterface)rec, rs, svm);
  101. }
  102. }
  103. /**
  104. * Handles UnknownRecords which appear within the row/cell records
  105. */
  106. private void addUnknownRecord(org.apache.poi.hssf.record.Record rec) {
  107. // ony a few distinct record IDs are encountered by the existing POI test cases:
  108. // 0x1065 // many
  109. // 0x01C2 // several
  110. // 0x0034 // few
  111. // No documentation could be found for these
  112. // keep the unknown records for re-serialization
  113. _unknownRecords.add(rec);
  114. }
  115. public void insertRow(RowRecord row) {
  116. // Integer integer = Integer.valueOf(row.getRowNumber());
  117. _rowRecords.put(Integer.valueOf(row.getRowNumber()), row);
  118. // Clear the cached values
  119. _rowRecordValues = null;
  120. if ((row.getRowNumber() < _firstrow) || (_firstrow == -1)) {
  121. _firstrow = row.getRowNumber();
  122. }
  123. if ((row.getRowNumber() > _lastrow) || (_lastrow == -1)) {
  124. _lastrow = row.getRowNumber();
  125. }
  126. }
  127. public void removeRow(RowRecord row) {
  128. int rowIndex = row.getRowNumber();
  129. _valuesAgg.removeAllCellsValuesForRow(rowIndex);
  130. Integer key = Integer.valueOf(rowIndex);
  131. RowRecord rr = _rowRecords.remove(key);
  132. if (rr == null) {
  133. throw new RuntimeException("Invalid row index (" + key.intValue() + ")");
  134. }
  135. if (row != rr) {
  136. _rowRecords.put(key, rr);
  137. throw new RuntimeException("Attempt to remove row that does not belong to this sheet");
  138. }
  139. // Clear the cached values
  140. _rowRecordValues = null;
  141. }
  142. public RowRecord getRow(int rowIndex) {
  143. int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex();
  144. if (rowIndex < 0 || rowIndex > maxrow) {
  145. throw new IllegalArgumentException("The row number must be between 0 and " + maxrow + ", but had: " + rowIndex);
  146. }
  147. return _rowRecords.get(Integer.valueOf(rowIndex));
  148. }
  149. public int getPhysicalNumberOfRows()
  150. {
  151. return _rowRecords.size();
  152. }
  153. public int getFirstRowNum()
  154. {
  155. return _firstrow;
  156. }
  157. public int getLastRowNum()
  158. {
  159. return _lastrow;
  160. }
  161. /** Returns the number of row blocks.
  162. * <p>The row blocks are goupings of rows that contain the DBCell record
  163. * after them
  164. */
  165. public int getRowBlockCount() {
  166. int size = _rowRecords.size()/DBCellRecord.BLOCK_SIZE;
  167. if ((_rowRecords.size() % DBCellRecord.BLOCK_SIZE) != 0)
  168. size++;
  169. return size;
  170. }
  171. private int getRowBlockSize(int block) {
  172. return RowRecord.ENCODED_SIZE * getRowCountForBlock(block);
  173. }
  174. /** Returns the number of physical rows within a block*/
  175. public int getRowCountForBlock(int block) {
  176. int startIndex = block * DBCellRecord.BLOCK_SIZE;
  177. int endIndex = startIndex + DBCellRecord.BLOCK_SIZE - 1;
  178. if (endIndex >= _rowRecords.size())
  179. endIndex = _rowRecords.size()-1;
  180. return endIndex-startIndex+1;
  181. }
  182. /** Returns the physical row number of the first row in a block*/
  183. private int getStartRowNumberForBlock(int block) {
  184. int startIndex = block * DBCellRecord.BLOCK_SIZE;
  185. if (_rowRecordValues == null) {
  186. _rowRecordValues = _rowRecords.values().toArray(new RowRecord[0]);
  187. }
  188. try {
  189. return _rowRecordValues[startIndex].getRowNumber();
  190. } catch(ArrayIndexOutOfBoundsException e) {
  191. throw new RuntimeException("Did not find start row for block " + block);
  192. }
  193. }
  194. /** Returns the physical row number of the end row in a block*/
  195. private int getEndRowNumberForBlock(int block) {
  196. int endIndex = ((block + 1)*DBCellRecord.BLOCK_SIZE)-1;
  197. if (endIndex >= _rowRecords.size())
  198. endIndex = _rowRecords.size()-1;
  199. if (_rowRecordValues == null){
  200. _rowRecordValues = _rowRecords.values().toArray(new RowRecord[0]);
  201. }
  202. try {
  203. return _rowRecordValues[endIndex].getRowNumber();
  204. } catch(ArrayIndexOutOfBoundsException e) {
  205. throw new RuntimeException("Did not find end row for block " + block);
  206. }
  207. }
  208. private int visitRowRecordsForBlock(int blockIndex, RecordVisitor rv) {
  209. final int startIndex = blockIndex*DBCellRecord.BLOCK_SIZE;
  210. final int endIndex = startIndex + DBCellRecord.BLOCK_SIZE;
  211. Iterator<RowRecord> rowIterator = _rowRecords.values().iterator();
  212. //Given that we basically iterate through the rows in order,
  213. //For a performance improvement, it would be better to return an instance of
  214. //an iterator and use that instance throughout, rather than recreating one and
  215. //having to move it to the right position.
  216. int i=0;
  217. for (;i<startIndex;i++)
  218. rowIterator.next();
  219. int result = 0;
  220. while(rowIterator.hasNext() && (i++ < endIndex)) {
  221. Record rec = rowIterator.next();
  222. result += rec.getRecordSize();
  223. rv.visitRecord(rec);
  224. }
  225. return result;
  226. }
  227. @Override
  228. public void visitContainedRecords(RecordVisitor rv) {
  229. PositionTrackingVisitor stv = new PositionTrackingVisitor(rv, 0);
  230. //DBCells are serialized before row records.
  231. final int blockCount = getRowBlockCount();
  232. for (int blockIndex = 0; blockIndex < blockCount; blockIndex++) {
  233. // Serialize a block of rows.
  234. // Hold onto the position of the first row in the block
  235. int pos=0;
  236. // Hold onto the size of this block that was serialized
  237. final int rowBlockSize = visitRowRecordsForBlock(blockIndex, rv);
  238. pos += rowBlockSize;
  239. // Serialize a block of cells for those rows
  240. final int startRowNumber = getStartRowNumberForBlock(blockIndex);
  241. final int endRowNumber = getEndRowNumberForBlock(blockIndex);
  242. final List<Short> cellOffsets = new ArrayList<>();
  243. // Note: Cell references start from the second row...
  244. int cellRefOffset = (rowBlockSize - RowRecord.ENCODED_SIZE);
  245. for (int row = startRowNumber; row <= endRowNumber; row++) {
  246. if (_valuesAgg.rowHasCells(row)) {
  247. stv.setPosition(0);
  248. _valuesAgg.visitCellsForRow(row, stv);
  249. int rowCellSize = stv.getPosition();
  250. pos += rowCellSize;
  251. // Add the offset to the first cell for the row into the
  252. // DBCellRecord.
  253. cellOffsets.add((short)cellRefOffset);
  254. cellRefOffset = rowCellSize;
  255. }
  256. }
  257. // Calculate Offset from the start of a DBCellRecord to the first Row
  258. rv.visitRecord(new DBCellRecord(pos, shortListToArray(cellOffsets)));
  259. }
  260. // Potentially breaking the file here since we don't know exactly where to write these records
  261. _unknownRecords.forEach(rv::visitRecord);
  262. }
  263. private static short[] shortListToArray(List<Short> list) {
  264. final short[] arr = new short[list.size()];
  265. int idx = 0;
  266. for (Short s : list) {
  267. arr[idx++] = s;
  268. }
  269. return arr;
  270. }
  271. public Iterator<RowRecord> getIterator() {
  272. return _rowRecords.values().iterator();
  273. }
  274. /**
  275. * @since POI 5.2.0
  276. */
  277. public Spliterator<RowRecord> getSpliterator() {
  278. return _rowRecords.values().spliterator();
  279. }
  280. public int findStartOfRowOutlineGroup(int row) {
  281. // Find the start of the group.
  282. RowRecord rowRecord = this.getRow( row );
  283. int level = rowRecord.getOutlineLevel();
  284. int currentRow = row;
  285. while (currentRow >= 0 && this.getRow( currentRow ) != null) {
  286. rowRecord = this.getRow( currentRow );
  287. if (rowRecord.getOutlineLevel() < level) {
  288. return currentRow + 1;
  289. }
  290. currentRow--;
  291. }
  292. return currentRow + 1;
  293. }
  294. public int findEndOfRowOutlineGroup(int row) {
  295. int level = getRow( row ).getOutlineLevel();
  296. int currentRow;
  297. for (currentRow = row; currentRow < getLastRowNum(); currentRow++) {
  298. if (getRow(currentRow) == null || getRow(currentRow).getOutlineLevel() < level) {
  299. break;
  300. }
  301. }
  302. return currentRow-1;
  303. }
  304. /**
  305. * Hide all rows at or below the current outline level
  306. * @return index of the <em>next<em> row after the last row that gets hidden
  307. */
  308. private int writeHidden(RowRecord pRowRecord, int row) {
  309. int rowIx = row;
  310. RowRecord rowRecord = pRowRecord;
  311. int level = rowRecord.getOutlineLevel();
  312. while (rowRecord != null && getRow(rowIx).getOutlineLevel() >= level) {
  313. rowRecord.setZeroHeight(true);
  314. rowIx++;
  315. rowRecord = getRow(rowIx);
  316. }
  317. return rowIx;
  318. }
  319. public void collapseRow(int rowNumber) {
  320. // Find the start of the group.
  321. int startRow = findStartOfRowOutlineGroup(rowNumber);
  322. RowRecord rowRecord = getRow(startRow);
  323. // Hide all the columns until the end of the group
  324. int nextRowIx = writeHidden(rowRecord, startRow);
  325. RowRecord row = getRow(nextRowIx);
  326. if (row == null) {
  327. row = createRow(nextRowIx);
  328. insertRow(row);
  329. }
  330. // Write collapse field
  331. row.setColapsed(true);
  332. }
  333. /**
  334. * Create a row record.
  335. *
  336. * @param rowNumber row number
  337. * @return RowRecord created for the passed in row number
  338. * @see org.apache.poi.hssf.record.RowRecord
  339. */
  340. public static RowRecord createRow(int rowNumber) {
  341. return new RowRecord(rowNumber);
  342. }
  343. public boolean isRowGroupCollapsed(int row) {
  344. int collapseRow = findEndOfRowOutlineGroup(row) + 1;
  345. return getRow(collapseRow) != null && getRow(collapseRow).getColapsed();
  346. }
  347. public void expandRow(int rowNumber) {
  348. if (rowNumber == -1)
  349. return;
  350. // If it is already expanded do nothing.
  351. if (!isRowGroupCollapsed(rowNumber)) {
  352. return;
  353. }
  354. // Find the start of the group.
  355. int startIdx = findStartOfRowOutlineGroup(rowNumber);
  356. RowRecord row = getRow(startIdx);
  357. // Find the end of the group.
  358. int endIdx = findEndOfRowOutlineGroup(rowNumber);
  359. // expand:
  360. // collapsed bit must be unset
  361. // hidden bit gets unset _if_ surrounding groups are expanded you can determine
  362. // this by looking at the hidden bit of the enclosing group. You will have
  363. // to look at the start and the end of the current group to determine which
  364. // is the enclosing group
  365. // hidden bit only is altered for this outline level. ie. don't un-collapse contained groups
  366. if (!isRowGroupHiddenByParent(rowNumber)) {
  367. for (int i = startIdx; i <= endIdx; i++) {
  368. RowRecord otherRow = getRow(i);
  369. if (row.getOutlineLevel() == otherRow.getOutlineLevel() || !isRowGroupCollapsed(i)) {
  370. otherRow.setZeroHeight(false);
  371. }
  372. }
  373. }
  374. // Write collapse field
  375. getRow(endIdx + 1).setColapsed(false);
  376. }
  377. public boolean isRowGroupHiddenByParent(int row) {
  378. // Look out outline details of end
  379. int endLevel;
  380. boolean endHidden;
  381. int endOfOutlineGroupIdx = findEndOfRowOutlineGroup(row);
  382. if (getRow(endOfOutlineGroupIdx + 1) == null) {
  383. endLevel = 0;
  384. endHidden = false;
  385. } else {
  386. endLevel = getRow(endOfOutlineGroupIdx + 1).getOutlineLevel();
  387. endHidden = getRow(endOfOutlineGroupIdx + 1).getZeroHeight();
  388. }
  389. // Look out outline details of start
  390. int startLevel;
  391. boolean startHidden;
  392. int startOfOutlineGroupIdx = findStartOfRowOutlineGroup( row );
  393. if (startOfOutlineGroupIdx - 1 < 0 || getRow(startOfOutlineGroupIdx - 1) == null) {
  394. startLevel = 0;
  395. startHidden = false;
  396. } else {
  397. startLevel = getRow(startOfOutlineGroupIdx - 1).getOutlineLevel();
  398. startHidden = getRow(startOfOutlineGroupIdx - 1).getZeroHeight();
  399. }
  400. if (endLevel > startLevel) {
  401. return endHidden;
  402. }
  403. return startHidden;
  404. }
  405. /**
  406. * Returns an iterator for the cell values
  407. */
  408. public Iterator<CellValueRecordInterface> getCellValueIterator() {
  409. return _valuesAgg.iterator();
  410. }
  411. /**
  412. * Returns a spliterator for the cell values
  413. *
  414. * @since POI 5.2.0
  415. */
  416. public Spliterator<CellValueRecordInterface> getCellValueSpliterator() {
  417. return _valuesAgg.spliterator();
  418. }
  419. public IndexRecord createIndexRecord(int indexRecordOffset, int sizeOfInitialSheetRecords) {
  420. IndexRecord result = new IndexRecord();
  421. result.setFirstRow(_firstrow);
  422. result.setLastRowAdd1(_lastrow + 1);
  423. // Calculate the size of the records from the end of the BOF
  424. // and up to the RowRecordsAggregate...
  425. // Add the references to the DBCells in the IndexRecord (one for each block)
  426. // Note: The offsets are relative to the Workbook BOF. Assume that this is
  427. // 0 for now.....
  428. int blockCount = getRowBlockCount();
  429. // Calculate the size of this IndexRecord
  430. int indexRecSize = IndexRecord.getRecordSizeForBlockCount(blockCount);
  431. int currentOffset = indexRecordOffset + indexRecSize + sizeOfInitialSheetRecords;
  432. for (int block = 0; block < blockCount; block++) {
  433. // each row-block has a DBCELL record.
  434. // The offset of each DBCELL record needs to be updated in the INDEX record
  435. // account for row records in this row-block
  436. currentOffset += getRowBlockSize(block);
  437. // account for cell value records after those
  438. currentOffset += _valuesAgg.getRowCellBlockSize(
  439. getStartRowNumberForBlock(block), getEndRowNumberForBlock(block));
  440. // currentOffset is now the location of the DBCELL record for this row-block
  441. result.addDbcell(currentOffset);
  442. // Add space required to write the DBCELL record (whose reference was just added).
  443. currentOffset += (8 + (getRowCountForBlock(block) * 2));
  444. }
  445. return result;
  446. }
  447. public void insertCell(CellValueRecordInterface cvRec) {
  448. _valuesAgg.insertCell(cvRec);
  449. }
  450. public void removeCell(CellValueRecordInterface cvRec) {
  451. if (cvRec instanceof FormulaRecordAggregate) {
  452. ((FormulaRecordAggregate)cvRec).notifyFormulaChanging();
  453. }
  454. _valuesAgg.removeCell(cvRec);
  455. }
  456. public FormulaRecordAggregate createFormula(int row, int col) {
  457. FormulaRecord fr = new FormulaRecord();
  458. fr.setRow(row);
  459. fr.setColumn((short) col);
  460. return new FormulaRecordAggregate(fr, null, _sharedValueManager);
  461. }
  462. public void updateFormulasAfterRowShift(FormulaShifter formulaShifter, int currentExternSheetIndex) {
  463. _valuesAgg.updateFormulasAfterRowShift(formulaShifter, currentExternSheetIndex);
  464. }
  465. public DimensionsRecord createDimensions() {
  466. DimensionsRecord result = new DimensionsRecord();
  467. result.setFirstRow(_firstrow);
  468. result.setLastRow(_lastrow);
  469. result.setFirstCol((short) _valuesAgg.getFirstCellNum());
  470. result.setLastCol((short) _valuesAgg.getLastCellNum());
  471. return result;
  472. }
  473. }