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 7.3KB

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