1 2 17 18 package org.apache.poi.hssf.eventusermodel; 19 20 import java.io.InputStream ; 21 import java.io.IOException ; 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 45 46 public class HSSFEventFactory 47 { 48 49 50 public HSSFEventFactory() 51 { 52 } 53 54 60 61 public void processWorkbookEvents(HSSFRequest req, POIFSFileSystem fs) 62 throws IOException 63 { 64 InputStream in = fs.createDocumentInputStream("Workbook"); 65 66 processEvents(req, in); 67 } 68 69 76 77 public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs) 78 throws IOException , HSSFUserException 79 { 80 InputStream in = fs.createDocumentInputStream("Workbook"); 81 return abortableProcessEvents(req, in); 82 } 83 84 95 96 public void processEvents(HSSFRequest req, InputStream in) 97 throws IOException 98 { 99 try 100 { 101 genericProcessEvents(req, in); 102 } 103 catch (HSSFUserException hue) 104 { } 105 } 106 107 108 116 117 public short abortableProcessEvents(HSSFRequest req, InputStream in) 118 throws IOException , HSSFUserException 119 { 120 return genericProcessEvents(req, in); 121 } 122 123 131 132 protected short genericProcessEvents(HSSFRequest req, InputStream in) 133 throws IOException , 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 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 Record[] recs = RecordFactory.createRecord(sid, size, 183 data); 184 185 if (recs.length > 1) 186 { for (int k = 0; k < (recs.length - 1); k++) 188 { userCode = req.processRecord( 190 recs[ k ]); if (userCode != 0) break process; 192 } 193 } 194 rec = recs[ recs.length - 1 ]; 196 } 201 else 202 { 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); } 214 if (rec != null) 215 { 216 userCode = req.processRecord(rec); 217 if (userCode != 0) break process; 218 } 219 } 220 catch (IOException e) 221 { 222 throw new RecordFormatException("Error reading bytes" + 223 "while processing record sid="+sid); 224 } 225 return userCode; 226 227 } 231 } 232 | Popular Tags |