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.

XSSFBSheetHandler.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.xssf.binary;
  16. import java.io.InputStream;
  17. import java.util.Queue;
  18. import org.apache.poi.ss.usermodel.DataFormatter;
  19. import org.apache.poi.ss.util.CellAddress;
  20. import org.apache.poi.util.Internal;
  21. import org.apache.poi.util.LittleEndian;
  22. import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
  23. import org.apache.poi.xssf.usermodel.XSSFComment;
  24. import org.apache.poi.xssf.usermodel.XSSFRichTextString;
  25. @Internal
  26. public class XSSFBSheetHandler extends XSSFBParser {
  27. private final static int CHECK_ALL_ROWS = -1;
  28. private final XSSFBSharedStringsTable stringsTable;
  29. private final XSSFSheetXMLHandler.SheetContentsHandler handler;
  30. private final XSSFBStylesTable styles;
  31. private final XSSFBCommentsTable comments;
  32. private final DataFormatter dataFormatter;
  33. private final boolean formulasNotResults;//TODO: implement this
  34. private int lastEndedRow = -1;
  35. private int lastStartedRow = -1;
  36. private int currentRow = 0;
  37. private byte[] rkBuffer = new byte[8];
  38. private XSSFBCellRange hyperlinkCellRange = null;
  39. private StringBuilder xlWideStringBuffer = new StringBuilder();
  40. private final XSSFBCellHeader cellBuffer = new XSSFBCellHeader();
  41. public XSSFBSheetHandler(InputStream is,
  42. XSSFBStylesTable styles,
  43. XSSFBCommentsTable comments,
  44. XSSFBSharedStringsTable strings,
  45. XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler,
  46. DataFormatter dataFormatter,
  47. boolean formulasNotResults) {
  48. super(is);
  49. this.styles = styles;
  50. this.comments = comments;
  51. this.stringsTable = strings;
  52. this.handler = sheetContentsHandler;
  53. this.dataFormatter = dataFormatter;
  54. this.formulasNotResults = formulasNotResults;
  55. }
  56. @Override
  57. public void handleRecord(int id, byte[] data) throws XSSFBParseException {
  58. XSSFBRecordType type = XSSFBRecordType.lookup(id);
  59. switch(type) {
  60. case BrtRowHdr:
  61. long rw = LittleEndian.getUInt(data, 0);
  62. if (rw > 0x00100000L) {//could make sure this is larger than currentRow, according to spec?
  63. throw new XSSFBParseException("Row number beyond allowable range: "+rw);
  64. }
  65. currentRow = (int)rw;
  66. checkMissedComments(currentRow);
  67. startRow(currentRow);
  68. break;
  69. case BrtCellIsst:
  70. handleBrtCellIsst(data);
  71. break;
  72. case BrtCellSt: //TODO: needs test
  73. handleCellSt(data);
  74. break;
  75. case BrtCellRk:
  76. handleCellRk(data);
  77. break;
  78. case BrtCellReal:
  79. handleCellReal(data);
  80. break;
  81. case BrtCellBool:
  82. handleBoolean(data);
  83. break;
  84. case BrtCellError:
  85. handleCellError(data);
  86. break;
  87. case BrtCellBlank:
  88. beforeCellValue(data);//read cell info and check for missing comments
  89. break;
  90. case BrtFmlaString:
  91. handleFmlaString(data);
  92. break;
  93. case BrtFmlaNum:
  94. handleFmlaNum(data);
  95. break;
  96. case BrtFmlaError:
  97. handleFmlaError(data);
  98. break;
  99. //TODO: All the PCDI and PCDIA
  100. case BrtEndSheetData:
  101. checkMissedComments(CHECK_ALL_ROWS);
  102. endRow(lastStartedRow);
  103. break;
  104. case BrtBeginHeaderFooter:
  105. handleHeaderFooter(data);
  106. break;
  107. }
  108. }
  109. private void beforeCellValue(byte[] data) {
  110. XSSFBCellHeader.parse(data, 0, currentRow, cellBuffer);
  111. checkMissedComments(currentRow, cellBuffer.getColNum());
  112. }
  113. private void handleCellValue(String formattedValue) {
  114. CellAddress cellAddress = new CellAddress(currentRow, cellBuffer.getColNum());
  115. XSSFBComment comment = null;
  116. if (comments != null) {
  117. comment = comments.get(cellAddress);
  118. }
  119. handler.cell(cellAddress.formatAsString(), formattedValue, comment);
  120. }
  121. private void handleFmlaNum(byte[] data) {
  122. beforeCellValue(data);
  123. //xNum
  124. double val = LittleEndian.getDouble(data, XSSFBCellHeader.length);
  125. String formatString = styles.getNumberFormatString(cellBuffer.getStyleIdx());
  126. String formattedVal = dataFormatter.formatRawCellContents(val, cellBuffer.getStyleIdx(), formatString);
  127. handleCellValue(formattedVal);
  128. }
  129. private void handleCellSt(byte[] data) {
  130. beforeCellValue(data);
  131. xlWideStringBuffer.setLength(0);
  132. XSSFBUtils.readXLWideString(data, XSSFBCellHeader.length, xlWideStringBuffer);
  133. handleCellValue(xlWideStringBuffer.toString());
  134. }
  135. private void handleFmlaString(byte[] data) {
  136. beforeCellValue(data);
  137. xlWideStringBuffer.setLength(0);
  138. XSSFBUtils.readXLWideString(data, XSSFBCellHeader.length, xlWideStringBuffer);
  139. handleCellValue(xlWideStringBuffer.toString());
  140. }
  141. private void handleCellError(byte[] data) {
  142. beforeCellValue(data);
  143. //TODO, read byte to figure out the type of error
  144. handleCellValue("ERROR");
  145. }
  146. private void handleFmlaError(byte[] data) {
  147. beforeCellValue(data);
  148. //TODO, read byte to figure out the type of error
  149. handleCellValue("ERROR");
  150. }
  151. private void handleBoolean(byte[] data) {
  152. beforeCellValue(data);
  153. String formattedVal = (data[XSSFBCellHeader.length] == 1) ? "TRUE" : "FALSE";
  154. handleCellValue(formattedVal);
  155. }
  156. private void handleCellReal(byte[] data) {
  157. beforeCellValue(data);
  158. //xNum
  159. double val = LittleEndian.getDouble(data, XSSFBCellHeader.length);
  160. String formatString = styles.getNumberFormatString(cellBuffer.getStyleIdx());
  161. String formattedVal = dataFormatter.formatRawCellContents(val, cellBuffer.getStyleIdx(), formatString);
  162. handleCellValue(formattedVal);
  163. }
  164. private void handleCellRk(byte[] data) {
  165. beforeCellValue(data);
  166. double val = rkNumber(data, XSSFBCellHeader.length);
  167. String formatString = styles.getNumberFormatString(cellBuffer.getStyleIdx());
  168. String formattedVal = dataFormatter.formatRawCellContents(val, cellBuffer.getStyleIdx(), formatString);
  169. handleCellValue(formattedVal);
  170. }
  171. private void handleBrtCellIsst(byte[] data) {
  172. beforeCellValue(data);
  173. long idx = LittleEndian.getUInt(data, XSSFBCellHeader.length);
  174. //check for out of range, buffer overflow
  175. XSSFRichTextString rtss = new XSSFRichTextString(stringsTable.getEntryAt((int)idx));
  176. handleCellValue(rtss.getString());
  177. }
  178. private void handleHeaderFooter(byte[] data) {
  179. XSSFBHeaderFooters headerFooter = XSSFBHeaderFooters.parse(data);
  180. outputHeaderFooter(headerFooter.getHeader());
  181. outputHeaderFooter(headerFooter.getFooter());
  182. outputHeaderFooter(headerFooter.getHeaderEven());
  183. outputHeaderFooter(headerFooter.getFooterEven());
  184. outputHeaderFooter(headerFooter.getHeaderFirst());
  185. outputHeaderFooter(headerFooter.getFooterFirst());
  186. }
  187. private void outputHeaderFooter(XSSFBHeaderFooter headerFooter) {
  188. String text = headerFooter.getString();
  189. if (text != null && text.trim().length() > 0) {
  190. handler.headerFooter(text, headerFooter.isHeader(), headerFooter.getHeaderFooterTypeLabel());
  191. }
  192. }
  193. //at start of next cell or end of row, return the cellAddress if it equals currentRow and col
  194. private void checkMissedComments(int currentRow, int colNum) {
  195. if (comments == null) {
  196. return;
  197. }
  198. Queue<CellAddress> queue = comments.getAddresses();
  199. while (queue.size() > 0) {
  200. CellAddress cellAddress = queue.peek();
  201. if (cellAddress.getRow() == currentRow && cellAddress.getColumn() < colNum) {
  202. cellAddress = queue.remove();
  203. dumpEmptyCellComment(cellAddress, comments.get(cellAddress));
  204. } else if (cellAddress.getRow() == currentRow && cellAddress.getColumn() == colNum) {
  205. queue.remove();
  206. return;
  207. } else if (cellAddress.getRow() == currentRow && cellAddress.getColumn() > colNum) {
  208. return;
  209. } else if (cellAddress.getRow() > currentRow) {
  210. return;
  211. }
  212. }
  213. }
  214. //check for anything from rows before
  215. private void checkMissedComments(int currentRow) {
  216. if (comments == null) {
  217. return;
  218. }
  219. Queue<CellAddress> queue = comments.getAddresses();
  220. int lastInterpolatedRow = -1;
  221. while (queue.size() > 0) {
  222. CellAddress cellAddress = queue.peek();
  223. if (currentRow == CHECK_ALL_ROWS || cellAddress.getRow() < currentRow) {
  224. cellAddress = queue.remove();
  225. if (cellAddress.getRow() != lastInterpolatedRow) {
  226. startRow(cellAddress.getRow());
  227. }
  228. dumpEmptyCellComment(cellAddress, comments.get(cellAddress));
  229. lastInterpolatedRow = cellAddress.getRow();
  230. } else {
  231. break;
  232. }
  233. }
  234. }
  235. private void startRow(int row) {
  236. if (row == lastStartedRow) {
  237. return;
  238. }
  239. if (lastStartedRow != lastEndedRow) {
  240. endRow(lastStartedRow);
  241. }
  242. handler.startRow(row);
  243. lastStartedRow = row;
  244. }
  245. private void endRow(int row) {
  246. if (lastEndedRow == row) {
  247. return;
  248. }
  249. handler.endRow(row);
  250. lastEndedRow = row;
  251. }
  252. private void dumpEmptyCellComment(CellAddress cellAddress, XSSFBComment comment) {
  253. handler.cell(cellAddress.formatAsString(), null, comment);
  254. }
  255. private double rkNumber(byte[] data, int offset) {
  256. //see 2.5.122 for this abomination
  257. byte b0 = data[offset];
  258. String s = Integer.toString(b0, 2);
  259. boolean numDivBy100 = ((b0 & 1) == 1); // else as is
  260. boolean floatingPoint = ((b0 >> 1 & 1) == 0); // else signed integer
  261. //unset highest 2 bits
  262. b0 &= ~1;
  263. b0 &= ~(1<<1);
  264. rkBuffer[4] = b0;
  265. for (int i = 1; i < 4; i++) {
  266. rkBuffer[i+4] = data[offset+i];
  267. }
  268. double d = 0.0;
  269. if (floatingPoint) {
  270. d = LittleEndian.getDouble(rkBuffer);
  271. } else {
  272. d = LittleEndian.getInt(rkBuffer);
  273. }
  274. d = (numDivBy100) ? d/100 : d;
  275. return d;
  276. }
  277. /**
  278. * You need to implement this to handle the results
  279. * of the sheet parsing.
  280. */
  281. public interface SheetContentsHandler extends XSSFSheetXMLHandler.SheetContentsHandler {
  282. /**
  283. * A cell, with the given formatted value (may be null),
  284. * a url (may be null), a toolTip (may be null)
  285. * and possibly a comment (may be null), was encountered */
  286. void hyperlinkCell(String cellReference, String formattedValue, String url, String toolTip, XSSFComment comment);
  287. }
  288. }