KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > eventusermodel > HSSFRequest


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17
18
19 package org.apache.poi.hssf.eventusermodel;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 import org.apache.poi.hssf.record.Record;
26 import org.apache.poi.hssf.record.RecordFactory;
27
28 /**
29  * An HSSFRequest object should be constructed registering an instance or multiple
30  * instances of HSSFListener with each Record.sid you wish to listen for.
31  *
32  * @see org.apache.poi.hssf.eventusermodel.HSSFEventFactory
33  * @see org.apache.poi.hssf.eventusermodel.HSSFListener
34  * @see org.apache.poi.hssf.dev.EFHSSF
35  * @see org.apache.poi.hssf.eventusermodel.HSSFUserException
36  * @author Andrew C. Oliver (acoliver at apache dot org)
37  * @author Carey Sublette (careysub@earthling.net)
38  */

39
40 public class HSSFRequest
41 {
42     private HashMap JavaDoc records;
43
44     /** Creates a new instance of HSSFRequest */
45
46     public HSSFRequest()
47     {
48         records =
49             new HashMap JavaDoc(50); // most folks won't listen for too many of these
50
}
51
52     /**
53      * add an event listener for a particular record type. The trick is you have to know
54      * what the records are for or just start with our examples and build on them. Alternatively,
55      * you CAN call addListenerForAllRecords and you'll recieve ALL record events in one listener,
56      * but if you like to squeeze every last byte of efficiency out of life you my not like this.
57      * (its sure as heck what I plan to do)
58      *
59      * @see #addListenerForAllRecords(HSSFListener)
60      *
61      * @param lsnr for the event
62      * @param sid identifier for the record type this is the .sid static member on the individual records
63      * for example req.addListener(myListener, BOFRecord.sid)
64      */

65
66     public void addListener(HSSFListener lsnr, short sid)
67     {
68         List JavaDoc list = null;
69         Object JavaDoc obj = records.get(new Short JavaDoc(sid));
70
71         if (obj != null)
72         {
73             list = ( List JavaDoc ) obj;
74         }
75         else
76         {
77             list = new ArrayList JavaDoc(
78                 1); // probably most people will use one listener
79
list.add(lsnr);
80             records.put(new Short JavaDoc(sid), list);
81         }
82     }
83
84     /**
85      * This is the equivilent of calling addListener(myListener, sid) for EVERY
86      * record in the org.apache.poi.hssf.record package. This is for lazy
87      * people like me. You can call this more than once with more than one listener, but
88      * that seems like a bad thing to do from a practice-perspective unless you have a
89      * compelling reason to do so (like maybe you send the event two places or log it or
90      * something?).
91      *
92      * @param lsnr a single listener to associate with ALL records
93      */

94
95     public void addListenerForAllRecords(HSSFListener lsnr)
96     {
97         short[] rectypes = RecordFactory.getAllKnownRecordSIDs();
98
99         for (int k = 0; k < rectypes.length; k++)
100         {
101             addListener(lsnr, rectypes[ k ]);
102         }
103     }
104
105     /**
106      * Called by HSSFEventFactory, passes the Record to each listener associated with
107      * a record.sid.
108      *
109      * Exception and return value added 2002-04-19 by Carey Sublette
110      *
111      * @return numeric user-specified result code. If zero continue processing.
112      * @throws HSSFUserException User exception condition
113      */

114
115     protected short processRecord(Record rec) throws HSSFUserException
116     {
117         Object JavaDoc obj = records.get(new Short JavaDoc(rec.getSid()));
118         short userCode = 0;
119
120         if (obj != null)
121         {
122             List JavaDoc listeners = ( List JavaDoc ) obj;
123
124             for (int k = 0; k < listeners.size(); k++)
125             {
126                 Object JavaDoc listenObj = listeners.get(k);
127                 if (listenObj instanceof AbortableHSSFListener)
128                 {
129                     AbortableHSSFListener listener = ( AbortableHSSFListener ) listenObj;
130                     userCode = listener.abortableProcessRecord(rec);
131                     if (userCode!=0) break;
132                 }
133                 else
134                 {
135                     HSSFListener listener = ( HSSFListener ) listenObj;
136                     listener.processRecord(rec);
137                 }
138             }
139         }
140         return userCode;
141     }
142 }
143
Popular Tags