1 2 /* ==================================================================== 3 * The Apache Software License, Version 1.1 4 * 5 * Copyright (c) 2002 The Apache Software Foundation. All rights 6 * reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. The end-user documentation included with the redistribution, 21 * if any, must include the following acknowledgment: 22 * "This product includes software developed by the 23 * Apache Software Foundation (http://www.apache.org/)." 24 * Alternately, this acknowledgment may appear in the software itself, 25 * if and wherever such third-party acknowledgments normally appear. 26 * 27 * 4. The names "Apache" and "Apache Software Foundation" and 28 * "Apache POI" must not be used to endorse or promote products 29 * derived from this software without prior written permission. For 30 * written permission, please contact apache@apache.org. 31 * 32 * 5. Products derived from this software may not be called "Apache", 33 * "Apache POI", nor may "Apache" appear in their name, without 34 * prior written permission of the Apache Software Foundation. 35 * 36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * ==================================================================== 49 * 50 * This software consists of voluntary contributions made by many 51 * individuals on behalf of the Apache Software Foundation. For more 52 * information on the Apache Software Foundation, please see 53 * <http://www.apache.org/>. 54 */ 55 56 package org.apache.poi.hssf.eventmodel; 57 58 import java.io.InputStream; 59 import java.io.IOException; 60 61 import org.apache.poi.util.LittleEndian; 62 import org.apache.poi.hssf.record.RecordFormatException; 63 import org.apache.poi.hssf.record.Record; 64 import org.apache.poi.hssf.record.RecordFactory; 65 import org.apache.poi.hssf.record.ContinueRecord; 66 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 67 68 /** 69 * Low level event based HSSF reader. Pass either a DocumentInputStream to 70 * process events along with a request object or pass a POIFS POIFSFileSystem to 71 * processWorkbookEvents along with a request. 72 * 73 * This will cause your file to be processed a record at a time. Each record with 74 * a static id matching one that you have registed in your HSSFRequest will be passed 75 * to your associated HSSFListener. 76 * 77 * @see org.apache.poi.hssf.dev.EFHSSF 78 * 79 * @author andy 80 */ 81 82 public class HSSFEventFactory 83 { 84 85 /** Creates a new instance of HSSFEventFactory */ 86 87 public HSSFEventFactory() 88 { 89 } 90 91 /** 92 * Processes a file into essentially record events. 93 * 94 * @param req an Instance of HSSFRequest which has your registered listeners 95 * @param fs a POIFS filesystem containing your workbook 96 */ 97 98 public void processWorkbookEvents(HSSFRequest req, POIFSFileSystem fs) 99 throws IOException 100 { 101 InputStream in = fs.createDocumentInputStream("Workbook"); 102 103 processEvents(req, in); 104 } 105 106 /** 107 * Processes a DocumentInputStream into essentially Record events. 108 * 109 * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String) 110 * @param req an Instance of HSSFRequest which has your registered listeners 111 * @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object 112 */ 113 114 public void processEvents(HSSFRequest req, InputStream in) 115 throws IOException 116 { 117 try 118 { 119 byte[] sidbytes = new byte[ 2 ]; 120 int bytesread = in.read(sidbytes); 121 Record rec = null; 122 123 while (bytesread > 0) 124 { 125 short sid = 0; 126 127 sid = LittleEndian.getShort(sidbytes); 128 if ((rec != null) && (sid != ContinueRecord.sid)) 129 { 130 req.processRecord(rec); 131 } 132 if (sid != ContinueRecord.sid) 133 { 134 short size = LittleEndian.readShort(in); 135 byte[] data = new byte[ size ]; 136 137 if (data.length > 0) 138 { 139 in.read(data); 140 } 141 Record[] recs = RecordFactory.createRecord(sid, size, 142 data); 143 144 if (recs.length > 1) 145 { // we know that the multiple 146 for (int k = 0; k < (recs.length - 1); k++) 147 { // record situations do not 148 req.processRecord( 149 recs[ k ]); // contain continue records 150 } 151 } 152 rec = recs[ recs.length - 1 ]; // regardless we'll process 153 154 // the last record as though 155 // it might be continued 156 // if there is only one 157 // records, it will go here too. 158 } 159 else 160 { // we do have a continue record 161 short size = LittleEndian.readShort(in); 162 byte[] data = new byte[ size ]; 163 164 if (data.length > 0) 165 { 166 in.read(data); 167 } 168 rec.processContinueRecord(data); 169 } 170 bytesread = in.read(sidbytes); // read next record sid 171 } 172 if (rec != null) 173 { 174 req.processRecord(rec); 175 } 176 } 177 catch (IOException e) 178 { 179 throw new RecordFormatException("Error reading bytes"); 180 } 181 182 // Record[] retval = new Record[ records.size() ]; 183 // retval = ( Record [] ) records.toArray(retval); 184 // return null; 185 } 186 } 187