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.

EventRecordFactory.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.eventmodel;
  16. import java.io.InputStream;
  17. import java.util.Arrays;
  18. import org.apache.poi.hssf.record.ContinueRecord;
  19. import org.apache.poi.hssf.record.Record;
  20. import org.apache.poi.hssf.record.RecordFactory;
  21. import org.apache.poi.hssf.record.RecordInputStream;
  22. import org.apache.poi.util.RecordFormatException;
  23. /**
  24. * Event-based record factory. As opposed to RecordFactory
  25. * this version sends {@link ERFListener#processRecord(org.apache.poi.hssf.record.Record) } messages to
  26. * the supplied listener. Record notifications are sent one record behind
  27. * to ensure that {@link ContinueRecord}s are processed first.
  28. */
  29. public final class EventRecordFactory {
  30. private final ERFListener _listener;
  31. private final short[] _sids;
  32. /**
  33. * Create an EventRecordFactory
  34. *
  35. * @param listener the listener to be informed about events
  36. * @param sids an array of Record.sid values identifying the records
  37. * the listener will work with. Alternatively if this is "null" then
  38. * all records are passed. For all 'known' record types use {@link RecordFactory#getAllKnownRecordSIDs()}
  39. */
  40. public EventRecordFactory(ERFListener listener, short[] sids) {
  41. _listener = listener;
  42. if (sids == null) {
  43. _sids = null;
  44. } else {
  45. _sids = sids.clone();
  46. Arrays.sort(_sids); // for faster binary search
  47. }
  48. }
  49. private boolean isSidIncluded(short sid) {
  50. if (_sids == null) {
  51. return true;
  52. }
  53. return Arrays.binarySearch(_sids, sid) >= 0;
  54. }
  55. /**
  56. * sends the record event to all registered listeners.
  57. * @param record the record to be thrown.
  58. * @return <code>false</code> to abort. This aborts
  59. * out of the event loop should the listener return false
  60. */
  61. private boolean processRecord(org.apache.poi.hssf.record.Record record) {
  62. if (!isSidIncluded(record.getSid())) {
  63. return true;
  64. }
  65. return _listener.processRecord(record);
  66. }
  67. /**
  68. * Create an array of records from an input stream
  69. *
  70. * @param in the InputStream from which the records will be
  71. * obtained
  72. *
  73. * @throws RecordFormatException on error processing the
  74. * InputStream
  75. */
  76. public void processRecords(InputStream in) throws RecordFormatException {
  77. Record last_record = null;
  78. RecordInputStream recStream = new RecordInputStream(in);
  79. while (recStream.hasNextRecord()) {
  80. recStream.nextRecord();
  81. Record[] recs = RecordFactory.createRecord(recStream); // handle MulRK records
  82. if (recs.length > 1) {
  83. for (org.apache.poi.hssf.record.Record rec : recs) {
  84. if ( last_record != null ) {
  85. if (!processRecord(last_record)) {
  86. return;
  87. }
  88. }
  89. last_record = rec; // do to keep the algorithm homogeneous...you can't
  90. } // actually continue a number record anyhow.
  91. } else {
  92. Record record = recs[ 0 ];
  93. if (record != null) {
  94. if (last_record != null) {
  95. if (!processRecord(last_record)) {
  96. return;
  97. }
  98. }
  99. last_record = record;
  100. }
  101. }
  102. }
  103. if (last_record != null) {
  104. processRecord(last_record);
  105. }
  106. }
  107. }