KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /* ====================================================================
3    Copyright 2003-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 package org.apache.poi.hssf.eventusermodel;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import org.apache.poi.util.LittleEndian;
24 import org.apache.poi.hssf.eventusermodel.HSSFUserException;
25 import org.apache.poi.hssf.record.RecordFormatException;
26 import org.apache.poi.hssf.record.Record;
27 import org.apache.poi.hssf.record.RecordFactory;
28 import org.apache.poi.hssf.record.ContinueRecord;
29 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
30
31 /**
32  * Low level event based HSSF reader. Pass either a DocumentInputStream to
33  * process events along with a request object or pass a POIFS POIFSFileSystem to
34  * processWorkbookEvents along with a request.
35  *
36  * This will cause your file to be processed a record at a time. Each record with
37  * a static id matching one that you have registed in your HSSFRequest will be passed
38  * to your associated HSSFListener.
39  *
40  * @see org.apache.poi.hssf.dev.EFHSSF
41  *
42  * @author Andrew C. Oliver (acoliver at apache dot org)
43  * @author Carey Sublette (careysub@earthling.net)
44  */

45
46 public class HSSFEventFactory
47 {
48     /** Creates a new instance of HSSFEventFactory */
49
50     public HSSFEventFactory()
51     {
52     }
53
54     /**
55      * Processes a file into essentially record events.
56      *
57      * @param req an Instance of HSSFRequest which has your registered listeners
58      * @param fs a POIFS filesystem containing your workbook
59      */

60
61     public void processWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
62         throws IOException JavaDoc
63     {
64         InputStream JavaDoc in = fs.createDocumentInputStream("Workbook");
65
66         processEvents(req, in);
67     }
68
69     /**
70      * Processes a file into essentially record events.
71      *
72      * @param req an Instance of HSSFRequest which has your registered listeners
73      * @param fs a POIFS filesystem containing your workbook
74      * @return numeric user-specified result code.
75      */

76
77     public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
78         throws IOException JavaDoc, HSSFUserException
79     {
80         InputStream JavaDoc in = fs.createDocumentInputStream("Workbook");
81         return abortableProcessEvents(req, in);
82     }
83
84     /**
85      * Processes a DocumentInputStream into essentially Record events.
86      *
87      * If an <code>AbortableHSSFListener</code> causes a halt to processing during this call
88      * the method will return just as with <code>abortableProcessEvents</code>, but no
89      * user code or <code>HSSFUserException</code> will be passed back.
90      *
91      * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
92      * @param req an Instance of HSSFRequest which has your registered listeners
93      * @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
94      */

95
96     public void processEvents(HSSFRequest req, InputStream JavaDoc in)
97         throws IOException JavaDoc
98     {
99         try
100         {
101             genericProcessEvents(req, in);
102         }
103         catch (HSSFUserException hue)
104         {/*If an HSSFUserException user exception is thrown, ignore it.*/ }
105     }
106
107
108     /**
109      * Processes a DocumentInputStream into essentially Record events.
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      * @return numeric user-specified result code.
115      */

116
117     public short abortableProcessEvents(HSSFRequest req, InputStream JavaDoc in)
118         throws IOException JavaDoc, HSSFUserException
119     {
120         return genericProcessEvents(req, in);
121     }
122
123      /**
124      * Processes a DocumentInputStream into essentially Record events.
125      *
126      * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
127      * @param req an Instance of HSSFRequest which has your registered listeners
128      * @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
129      * @return numeric user-specified result code.
130      */

131
132     protected short genericProcessEvents(HSSFRequest req, InputStream JavaDoc in)
133         throws IOException JavaDoc, HSSFUserException
134     {
135         short userCode = 0;
136
137         short sid = 0;
138         process:
139         try
140         {
141             byte[] sidbytes = new byte[ 2 ];
142             int bytesread = in.read(sidbytes);
143             Record rec = null;
144
145             while (bytesread > 0)
146             {
147
148                 sid = LittleEndian.getShort(sidbytes);
149                 
150                 //
151
// for some reasons we have to make the workbook to be at least 4096 bytes
152
// but if we have such workbook we fill the end of it with zeros (many zeros)
153
//
154
// it is not good:
155
// if the length( all zero records ) % 4 = 1
156
// e.g.: any zero record would be readed as 4 bytes at once ( 2 - id and 2 - size ).
157
// And the last 1 byte will be readed WRONG ( the id must be 2 bytes )
158
//
159
// So we should better to check if the sid is zero and not to read more data
160
// The zero sid shows us that rest of the stream data is a fake to make workbook
161
// certain size
162
//
163
if ( sid == 0 )
164                     break;
165
166
167                 if ((rec != null) && (sid != ContinueRecord.sid))
168                 {
169                     userCode = req.processRecord(rec);
170                     if (userCode != 0) break process;
171                 }
172                 if (sid != ContinueRecord.sid)
173                 {
174                     short size = LittleEndian.readShort(in);
175                     byte[] data = new byte[ size ];
176
177                     if (data.length > 0)
178                     {
179                         in.read(data);
180                     }
181                                         //System.out.println("creating "+sid);
182
Record[] recs = RecordFactory.createRecord(sid, size,
183                                                                data);
184
185                     if (recs.length > 1)
186                     { // we know that the multiple
187
for (int k = 0; k < (recs.length - 1); k++)
188                         { // record situations do not
189
userCode = req.processRecord(
190                                 recs[ k ]); // contain continue records
191
if (userCode != 0) break process;
192                         }
193                     }
194                     rec = recs[ recs.length - 1 ]; // regardless we'll process
195

196                     // the last record as though
197
// it might be continued
198
// if there is only one
199
// records, it will go here too.
200
}
201                 else
202                 { // we do have a continue record
203
short size = LittleEndian.readShort(in);
204                     byte[] data = new byte[ size ];
205
206                     if (data.length > 0)
207                     {
208                         in.read(data);
209                     }
210                     rec.processContinueRecord(data);
211                 }
212                 bytesread = in.read(sidbytes); // read next record sid
213
}
214             if (rec != null)
215             {
216                 userCode = req.processRecord(rec);
217                 if (userCode != 0) break process;
218             }
219         }
220         catch (IOException JavaDoc e)
221         {
222             throw new RecordFormatException("Error reading bytes" +
223                         "while processing record sid="+sid);
224         }
225         return userCode;
226
227         // Record[] retval = new Record[ records.size() ];
228
// retval = ( Record [] ) records.toArray(retval);
229
// return null;
230
}
231 }
232
Popular Tags