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.

MissingRecordAwareHSSFListener.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.eventusermodel;
  16. import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
  17. import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;
  18. import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord;
  19. import org.apache.poi.hssf.record.*;
  20. /**
  21. * <p>A HSSFListener which tracks rows and columns, and will
  22. * trigger your HSSFListener for all rows and cells,
  23. * even the ones that aren't actually stored in the file.</p>
  24. * <p>This allows your code to have a more "Excel" like
  25. * view of the data in the file, and not have to worry
  26. * (as much) about if a particular row/cell is in the
  27. * file, or was skipped from being written as it was
  28. * blank.
  29. */
  30. public final class MissingRecordAwareHSSFListener implements HSSFListener {
  31. private HSSFListener childListener;
  32. // Need to have different counters for cell rows and
  33. // row rows, as you sometimes get a RowRecord in the
  34. // middle of some cells, and that'd break everything
  35. private int lastRowRow;
  36. private int lastCellRow;
  37. private int lastCellColumn;
  38. /**
  39. * Constructs a new MissingRecordAwareHSSFListener, which
  40. * will fire processRecord on the supplied child
  41. * HSSFListener for all Records, and missing records.
  42. * @param listener The HSSFListener to pass records on to
  43. */
  44. public MissingRecordAwareHSSFListener(HSSFListener listener) {
  45. resetCounts();
  46. childListener = listener;
  47. }
  48. public void processRecord(Record record) {
  49. int thisRow;
  50. int thisColumn;
  51. CellValueRecordInterface[] expandedRecords = null;
  52. if (record instanceof CellValueRecordInterface) {
  53. CellValueRecordInterface valueRec = (CellValueRecordInterface) record;
  54. thisRow = valueRec.getRow();
  55. thisColumn = valueRec.getColumn();
  56. } else {
  57. if (record instanceof StringRecord){
  58. //it contains only cashed result of the previous FormulaRecord evaluation
  59. childListener.processRecord(record);
  60. return;
  61. }
  62. thisRow = -1;
  63. thisColumn = -1;
  64. switch (record.getSid()) {
  65. // the BOFRecord can represent either the beginning of a sheet or
  66. // the workbook
  67. case BOFRecord.sid:
  68. BOFRecord bof = (BOFRecord) record;
  69. if (bof.getType() == bof.TYPE_WORKBOOK || bof.getType() == bof.TYPE_WORKSHEET) {
  70. // Reset the row and column counts - new workbook / worksheet
  71. resetCounts();
  72. }
  73. break;
  74. case RowRecord.sid:
  75. RowRecord rowrec = (RowRecord) record;
  76. //System.out.println("Row " + rowrec.getRowNumber() + " found, first column at "
  77. // + rowrec.getFirstCol() + " last column at " + rowrec.getLastCol());
  78. // If there's a jump in rows, fire off missing row records
  79. if (lastRowRow + 1 < rowrec.getRowNumber()) {
  80. for (int i = (lastRowRow + 1); i < rowrec.getRowNumber(); i++) {
  81. MissingRowDummyRecord dr = new MissingRowDummyRecord(i);
  82. childListener.processRecord(dr);
  83. }
  84. }
  85. // Record this as the last row we saw
  86. lastRowRow = rowrec.getRowNumber();
  87. break;
  88. case SharedFormulaRecord.sid:
  89. // SharedFormulaRecord occurs after the first FormulaRecord of the cell range.
  90. // There are probably (but not always) more cell records after this
  91. // - so don't fire off the LastCellOfRowDummyRecord yet
  92. childListener.processRecord(record);
  93. return;
  94. case MulBlankRecord.sid:
  95. // These appear in the middle of the cell records, to
  96. // specify that the next bunch are empty but styled
  97. // Expand this out into multiple blank cells
  98. MulBlankRecord mbr = (MulBlankRecord)record;
  99. expandedRecords = RecordFactory.convertBlankRecords(mbr);
  100. break;
  101. case MulRKRecord.sid:
  102. // This is multiple consecutive number cells in one record
  103. // Exand this out into multiple regular number cells
  104. MulRKRecord mrk = (MulRKRecord)record;
  105. expandedRecords = RecordFactory.convertRKRecords(mrk);
  106. break;
  107. case NoteRecord.sid:
  108. NoteRecord nrec = (NoteRecord) record;
  109. thisRow = nrec.getRow();
  110. thisColumn = nrec.getColumn();
  111. break;
  112. }
  113. }
  114. // First part of expanded record handling
  115. if(expandedRecords != null && expandedRecords.length > 0) {
  116. thisRow = expandedRecords[0].getRow();
  117. thisColumn = expandedRecords[0].getColumn();
  118. }
  119. // If we're on cells, and this cell isn't in the same
  120. // row as the last one, then fire the
  121. // dummy end-of-row records
  122. if(thisRow != lastCellRow && lastCellRow > -1) {
  123. for(int i=lastCellRow; i<thisRow; i++) {
  124. int cols = -1;
  125. if(i == lastCellRow) {
  126. cols = lastCellColumn;
  127. }
  128. childListener.processRecord(new LastCellOfRowDummyRecord(i, cols));
  129. }
  130. }
  131. // If we've just finished with the cells, then fire the
  132. // final dummy end-of-row record
  133. if(lastCellRow != -1 && lastCellColumn != -1 && thisRow == -1) {
  134. childListener.processRecord(new LastCellOfRowDummyRecord(lastCellRow, lastCellColumn));
  135. lastCellRow = -1;
  136. lastCellColumn = -1;
  137. }
  138. // If we've moved onto a new row, the ensure we re-set
  139. // the column counter
  140. if(thisRow != lastCellRow) {
  141. lastCellColumn = -1;
  142. }
  143. // If there's a gap in the cells, then fire
  144. // the dummy cell records
  145. if(lastCellColumn != thisColumn-1) {
  146. for(int i=lastCellColumn+1; i<thisColumn; i++) {
  147. childListener.processRecord(new MissingCellDummyRecord(thisRow, i));
  148. }
  149. }
  150. // Next part of expanded record handling
  151. if(expandedRecords != null && expandedRecords.length > 0) {
  152. thisColumn = expandedRecords[expandedRecords.length-1].getColumn();
  153. }
  154. // Update cell and row counts as needed
  155. if(thisColumn != -1) {
  156. lastCellColumn = thisColumn;
  157. lastCellRow = thisRow;
  158. }
  159. // Pass along the record(s)
  160. if(expandedRecords != null && expandedRecords.length > 0) {
  161. for(CellValueRecordInterface r : expandedRecords) {
  162. childListener.processRecord((Record)r);
  163. }
  164. } else {
  165. childListener.processRecord(record);
  166. }
  167. }
  168. private void resetCounts() {
  169. lastRowRow = -1;
  170. lastCellRow = -1;
  171. lastCellColumn = -1;
  172. }
  173. }