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.

HSSFEventFactory.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 java.io.InputStream;
  17. import java.io.IOException;
  18. import java.util.Set;
  19. import org.apache.poi.hssf.record.*;
  20. import org.apache.poi.poifs.filesystem.DirectoryNode;
  21. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  22. import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAMES;
  23. /**
  24. * Low level event based HSSF reader. Pass either a DocumentInputStream to
  25. * process events along with a request object or pass a POIFS POIFSFileSystem to
  26. * processWorkbookEvents along with a request.
  27. *
  28. * This will cause your file to be processed a record at a time. Each record with
  29. * a static id matching one that you have registered in your HSSFRequest will be passed
  30. * to your associated HSSFListener.
  31. */
  32. public class HSSFEventFactory {
  33. /** Creates a new instance of HSSFEventFactory */
  34. public HSSFEventFactory() {
  35. // no instance fields
  36. }
  37. /**
  38. * Processes a file into essentially record events.
  39. *
  40. * @param req an Instance of HSSFRequest which has your registered listeners
  41. * @param fs a POIFS filesystem containing your workbook
  42. *
  43. * @throws IOException if the workbook contained errors
  44. */
  45. public void processWorkbookEvents(HSSFRequest req, POIFSFileSystem fs) throws IOException {
  46. processWorkbookEvents(req, fs.getRoot());
  47. }
  48. /**
  49. * Processes a file into essentially record events.
  50. *
  51. * @param req an Instance of HSSFRequest which has your registered listeners
  52. * @param dir a DirectoryNode containing your workbook
  53. *
  54. * @throws IOException if the workbook contained errors
  55. */
  56. public void processWorkbookEvents(HSSFRequest req, DirectoryNode dir) throws IOException {
  57. // some old documents have "WORKBOOK" or "BOOK"
  58. String name = null;
  59. Set<String> entryNames = dir.getEntryNames();
  60. for (String potentialName : WORKBOOK_DIR_ENTRY_NAMES) {
  61. if (entryNames.contains(potentialName)) {
  62. name = potentialName;
  63. break;
  64. }
  65. }
  66. // If in doubt, go for the default
  67. if (name == null) {
  68. name = WORKBOOK_DIR_ENTRY_NAMES[0];
  69. }
  70. try (InputStream in = dir.createDocumentInputStream(name)) {
  71. processEvents(req, in);
  72. }
  73. }
  74. /**
  75. * Processes a file into essentially record events.
  76. *
  77. * @param req an Instance of HSSFRequest which has your registered listeners
  78. * @param fs a POIFS filesystem containing your workbook
  79. * @return numeric user-specified result code.
  80. *
  81. * @throws HSSFUserException if the processing should be aborted
  82. * @throws IOException if the workbook contained errors
  83. */
  84. public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
  85. throws IOException, HSSFUserException {
  86. return abortableProcessWorkbookEvents(req, fs.getRoot());
  87. }
  88. /**
  89. * Processes a file into essentially record events.
  90. *
  91. * @param req an Instance of HSSFRequest which has your registered listeners
  92. * @param dir a DirectoryNode containing your workbook
  93. * @return numeric user-specified result code.
  94. *
  95. * @throws HSSFUserException if the processing should be aborted
  96. * @throws IOException if the workbook contained errors
  97. */
  98. public short abortableProcessWorkbookEvents(HSSFRequest req, DirectoryNode dir)
  99. throws IOException, HSSFUserException {
  100. try (InputStream in = dir.createDocumentInputStream("Workbook")) {
  101. return abortableProcessEvents(req, in);
  102. }
  103. }
  104. /**
  105. * Processes a DocumentInputStream into essentially Record events.
  106. *
  107. * If an <code>AbortableHSSFListener</code> causes a halt to processing during this call
  108. * the method will return just as with <code>abortableProcessEvents</code>, but no
  109. * user code or <code>HSSFUserException</code> will be passed back.
  110. *
  111. * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
  112. * @param req an Instance of HSSFRequest which has your registered listeners
  113. * @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
  114. */
  115. public void processEvents(HSSFRequest req, InputStream in) {
  116. try {
  117. genericProcessEvents(req, in);
  118. } catch (HSSFUserException hue) {
  119. /*If an HSSFUserException user exception is thrown, ignore it.*/
  120. }
  121. }
  122. /**
  123. * Processes a DocumentInputStream into essentially Record events.
  124. *
  125. * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
  126. * @param req an Instance of HSSFRequest which has your registered listeners
  127. * @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
  128. * @return numeric user-specified result code.
  129. *
  130. * @throws HSSFUserException if the processing should be aborted
  131. */
  132. public short abortableProcessEvents(HSSFRequest req, InputStream in)
  133. throws HSSFUserException {
  134. return genericProcessEvents(req, in);
  135. }
  136. /**
  137. * Processes a DocumentInputStream into essentially Record events.
  138. *
  139. * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
  140. * @param req an Instance of HSSFRequest which has your registered listeners
  141. * @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
  142. * @return numeric user-specified result code.
  143. */
  144. private short genericProcessEvents(HSSFRequest req, InputStream in)
  145. throws HSSFUserException {
  146. short userCode = 0;
  147. // Create a new RecordStream and use that
  148. RecordFactoryInputStream recordStream = new RecordFactoryInputStream(in, false);
  149. // Process each record as they come in
  150. while(true) {
  151. org.apache.poi.hssf.record.Record r = recordStream.nextRecord();
  152. if(r == null) {
  153. break;
  154. }
  155. userCode = req.processRecord(r);
  156. if (userCode != 0) {
  157. break;
  158. }
  159. }
  160. // All done, return our last code
  161. return userCode;
  162. }
  163. }