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.

HSSFRequest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.util.HashMap;
  17. import java.util.List;
  18. import java.util.ArrayList;
  19. import java.util.Map;
  20. import org.apache.poi.hssf.record.Record;
  21. import org.apache.poi.hssf.record.RecordFactory;
  22. /**
  23. * An HSSFRequest object should be constructed registering an instance or multiple
  24. * instances of HSSFListener with each Record.sid you wish to listen for.
  25. *
  26. * @see org.apache.poi.hssf.eventusermodel.HSSFEventFactory
  27. * @see org.apache.poi.hssf.eventusermodel.HSSFListener
  28. * @see org.apache.poi.hssf.eventusermodel.HSSFUserException
  29. */
  30. public class HSSFRequest {
  31. private final Map<Short, List<HSSFListener>> _records;
  32. /** Creates a new instance of HSSFRequest */
  33. public HSSFRequest() {
  34. _records = new HashMap<>(50); // most folks won't listen for too many of these
  35. }
  36. /**
  37. * add an event listener for a particular record type. The trick is you have to know
  38. * what the records are for or just start with our examples and build on them. Alternatively,
  39. * you CAN call addListenerForAllRecords and you'll receive ALL record events in one listener,
  40. * but if you like to squeeze every last byte of efficiency out of life you my not like this.
  41. * (its sure as heck what I plan to do)
  42. *
  43. * @see #addListenerForAllRecords(HSSFListener)
  44. *
  45. * @param lsnr for the event
  46. * @param sid identifier for the record type this is the .sid static member on the individual records
  47. * for example req.addListener(myListener, BOFRecord.sid)
  48. */
  49. public void addListener(HSSFListener lsnr, short sid) {
  50. List<HSSFListener> list = _records.computeIfAbsent(Short.valueOf(sid), k -> new ArrayList<>(1));
  51. // probably most people will use one listener
  52. list.add(lsnr);
  53. }
  54. /**
  55. * This is the equivalent of calling addListener(myListener, sid) for EVERY
  56. * record in the org.apache.poi.hssf.record package. This is for lazy
  57. * people like me. You can call this more than once with more than one listener, but
  58. * that seems like a bad thing to do from a practice-perspective unless you have a
  59. * compelling reason to do so (like maybe you send the event two places or log it or
  60. * something?).
  61. *
  62. * @param lsnr a single listener to associate with ALL records
  63. */
  64. public void addListenerForAllRecords(HSSFListener lsnr) {
  65. short[] rectypes = RecordFactory.getAllKnownRecordSIDs();
  66. for (short rectype : rectypes) {
  67. addListener(lsnr, rectype);
  68. }
  69. }
  70. /**
  71. * Called by HSSFEventFactory, passes the Record to each listener associated with
  72. * a record.sid.
  73. *
  74. * @param rec the record to be processed
  75. *
  76. * @return numeric user-specified result code. If zero continue processing.
  77. * @throws HSSFUserException User exception condition
  78. */
  79. protected short processRecord(org.apache.poi.hssf.record.Record rec) throws HSSFUserException {
  80. List<HSSFListener> listeners = _records.get(Short.valueOf(rec.getSid()));
  81. short userCode = 0;
  82. if (listeners != null) {
  83. for (int k = 0; k < listeners.size(); k++) {
  84. Object listenObj = listeners.get(k);
  85. if (listenObj instanceof AbortableHSSFListener) {
  86. AbortableHSSFListener listener = (AbortableHSSFListener) listenObj;
  87. userCode = listener.abortableProcessRecord(rec);
  88. if (userCode != 0)
  89. break;
  90. } else {
  91. HSSFListener listener = (HSSFListener) listenObj;
  92. listener.processRecord(rec);
  93. }
  94. }
  95. }
  96. return userCode;
  97. }
  98. }