PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/eventusermodel/HSSFRequest.java

http://github.com/openmicroscopy/bioformats
Java | 165 lines | 64 code | 19 blank | 82 comment | 8 complexity | b121d0bf53bbf50e1642cf6873264092 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.eventusermodel;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. import java.util.ArrayList;
  41. import loci.poi.hssf.record.Record;
  42. import loci.poi.hssf.record.RecordFactory;
  43. /**
  44. * An HSSFRequest object should be constructed registering an instance or multiple
  45. * instances of HSSFListener with each Record.sid you wish to listen for.
  46. *
  47. * @see loci.poi.hssf.eventusermodel.HSSFEventFactory
  48. * @see loci.poi.hssf.eventusermodel.HSSFListener
  49. * @see loci.poi.hssf.dev.EFHSSF
  50. * @see loci.poi.hssf.eventusermodel.HSSFUserException
  51. * @author Andrew C. Oliver (acoliver at apache dot org)
  52. * @author Carey Sublette (careysub@earthling.net)
  53. */
  54. public class HSSFRequest
  55. {
  56. private HashMap records;
  57. /** Creates a new instance of HSSFRequest */
  58. public HSSFRequest()
  59. {
  60. records =
  61. new HashMap(50); // most folks won't listen for too many of these
  62. }
  63. /**
  64. * add an event listener for a particular record type. The trick is you have to know
  65. * what the records are for or just start with our examples and build on them. Alternatively,
  66. * you CAN call addListenerForAllRecords and you'll recieve ALL record events in one listener,
  67. * but if you like to squeeze every last byte of efficiency out of life you my not like this.
  68. * (its sure as heck what I plan to do)
  69. *
  70. * @see #addListenerForAllRecords(HSSFListener)
  71. *
  72. * @param lsnr for the event
  73. * @param sid identifier for the record type this is the .sid static member on the individual records
  74. * for example req.addListener(myListener, BOFRecord.sid)
  75. */
  76. public void addListener(HSSFListener lsnr, short sid)
  77. {
  78. List list = null;
  79. Object obj = records.get(new Short(sid));
  80. if (obj != null)
  81. {
  82. list = ( List ) obj;
  83. }
  84. else
  85. {
  86. list = new ArrayList(
  87. 1); // probably most people will use one listener
  88. list.add(lsnr);
  89. records.put(new Short(sid), list);
  90. }
  91. }
  92. /**
  93. * This is the equivilent of calling addListener(myListener, sid) for EVERY
  94. * record in the loci.poi.hssf.record package. This is for lazy
  95. * people like me. You can call this more than once with more than one listener, but
  96. * that seems like a bad thing to do from a practice-perspective unless you have a
  97. * compelling reason to do so (like maybe you send the event two places or log it or
  98. * something?).
  99. *
  100. * @param lsnr a single listener to associate with ALL records
  101. */
  102. public void addListenerForAllRecords(HSSFListener lsnr)
  103. {
  104. short[] rectypes = RecordFactory.getAllKnownRecordSIDs();
  105. for (int k = 0; k < rectypes.length; k++)
  106. {
  107. addListener(lsnr, rectypes[ k ]);
  108. }
  109. }
  110. /**
  111. * Called by HSSFEventFactory, passes the Record to each listener associated with
  112. * a record.sid.
  113. *
  114. * Exception and return value added 2002-04-19 by Carey Sublette
  115. *
  116. * @return numeric user-specified result code. If zero continue processing.
  117. * @throws HSSFUserException User exception condition
  118. */
  119. protected short processRecord(Record rec) throws HSSFUserException
  120. {
  121. Object obj = records.get(new Short(rec.getSid()));
  122. short userCode = 0;
  123. if (obj != null)
  124. {
  125. List listeners = ( List ) obj;
  126. for (int k = 0; k < listeners.size(); k++)
  127. {
  128. Object listenObj = listeners.get(k);
  129. if (listenObj instanceof AbortableHSSFListener)
  130. {
  131. AbortableHSSFListener listener = ( AbortableHSSFListener ) listenObj;
  132. userCode = listener.abortableProcessRecord(rec);
  133. if (userCode!=0) break;
  134. }
  135. else
  136. {
  137. HSSFListener listener = ( HSSFListener ) listenObj;
  138. listener.processRecord(rec);
  139. }
  140. }
  141. }
  142. return userCode;
  143. }
  144. }