Save This Page
Home » poi-src-3.2-FINAL-20081019 » org.apache » poi » hssf » eventusermodel » [javadoc | source]
    1   
    2   /* ====================================================================
    3      Licensed to the Apache Software Foundation (ASF) under one or more
    4      contributor license agreements.  See the NOTICE file distributed with
    5      this work for additional information regarding copyright ownership.
    6      The ASF licenses this file to You under the Apache License, Version 2.0
    7      (the "License"); you may not use this file except in compliance with
    8      the License.  You may obtain a copy of the License at
    9   
   10          http://www.apache.org/licenses/LICENSE-2.0
   11   
   12      Unless required by applicable law or agreed to in writing, software
   13      distributed under the License is distributed on an "AS IS" BASIS,
   14      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15      See the License for the specific language governing permissions and
   16      limitations under the License.
   17   ==================================================================== */
   18   
   19   package org.apache.poi.hssf.eventusermodel;
   20   
   21   import java.io.InputStream;
   22   import java.io.IOException;
   23   
   24   import org.apache.poi.hssf.eventusermodel.HSSFUserException;
   25   import org.apache.poi.hssf.record;
   26   import org.apache.poi.poifs.filesystem.POIFSFileSystem;
   27   
   28   /**
   29    * Low level event based HSSF reader.  Pass either a DocumentInputStream to
   30    * process events along with a request object or pass a POIFS POIFSFileSystem to
   31    * processWorkbookEvents along with a request.
   32    *
   33    * This will cause your file to be processed a record at a time.  Each record with
   34    * a static id matching one that you have registed in your HSSFRequest will be passed
   35    * to your associated HSSFListener.
   36    *
   37    * @see org.apache.poi.hssf.dev.EFHSSF
   38    *
   39    * @author Andrew C. Oliver (acoliver at apache dot org)
   40    * @author Carey Sublette  (careysub@earthling.net)
   41    */
   42   
   43   public class HSSFEventFactory
   44   {
   45       /** Creates a new instance of HSSFEventFactory */
   46   
   47       public HSSFEventFactory()
   48       {
   49       }
   50   
   51       /**
   52        * Processes a file into essentially record events.
   53        *
   54        * @param req       an Instance of HSSFRequest which has your registered listeners
   55        * @param fs        a POIFS filesystem containing your workbook
   56        */
   57   
   58       public void processWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
   59           throws IOException
   60       {
   61           InputStream in = fs.createDocumentInputStream("Workbook");
   62   
   63           processEvents(req, in);
   64       }
   65   
   66       /**
   67   	 * Processes a file into essentially record events.
   68   	 *
   69   	 * @param req       an Instance of HSSFRequest which has your registered listeners
   70   	 * @param fs        a POIFS filesystem containing your workbook
   71   	 * @return 			numeric user-specified result code.
   72   	 */
   73   
   74   	public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
   75   		throws IOException, HSSFUserException
   76   	{
   77   		InputStream in = fs.createDocumentInputStream("Workbook");
   78   		return abortableProcessEvents(req, in);
   79       }
   80   
   81       /**
   82        * Processes a DocumentInputStream into essentially Record events.
   83        *
   84        * If an <code>AbortableHSSFListener</code> causes a halt to processing during this call
   85        * the method will return just as with <code>abortableProcessEvents</code>, but no
   86        * user code or <code>HSSFUserException</code> will be passed back.
   87        *
   88        * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
   89        * @param req       an Instance of HSSFRequest which has your registered listeners
   90        * @param in        a DocumentInputStream obtained from POIFS's POIFSFileSystem object
   91        */
   92   
   93       public void processEvents(HSSFRequest req, InputStream in)
   94           throws IOException
   95   	{
   96   		try
   97   		{
   98   			genericProcessEvents(req, new RecordInputStream(in));
   99   		}
  100   		catch (HSSFUserException hue)
  101   		{/*If an HSSFUserException user exception is thrown, ignore it.*/ }
  102   	}
  103   
  104   
  105       /**
  106        * Processes a DocumentInputStream into essentially Record events.
  107        *
  108        * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
  109        * @param req       an Instance of HSSFRequest which has your registered listeners
  110        * @param in        a DocumentInputStream obtained from POIFS's POIFSFileSystem object
  111   	 * @return 			numeric user-specified result code.
  112        */
  113   
  114       public short abortableProcessEvents(HSSFRequest req, InputStream in)
  115           throws IOException, HSSFUserException
  116       {
  117   		return genericProcessEvents(req, new RecordInputStream(in));
  118       }
  119   
  120        /**
  121   	 * Processes a DocumentInputStream into essentially Record events.
  122   	 *
  123   	 * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
  124   	 * @param req       an Instance of HSSFRequest which has your registered listeners
  125   	 * @param in        a DocumentInputStream obtained from POIFS's POIFSFileSystem object
  126   	 * @return 			numeric user-specified result code.
  127   	 */
  128   
  129   	protected short genericProcessEvents(HSSFRequest req, RecordInputStream in)
  130   		throws IOException, HSSFUserException
  131   	{
  132   		boolean going = true;
  133   		short userCode = 0;
  134   		Record r = null;
  135   		
  136   		// Create a new RecordStream and use that
  137   		HSSFRecordStream recordStream = new HSSFRecordStream(in);
  138   		
  139   		// Process each record as they come in
  140   		while(going) {
  141   			r = recordStream.nextRecord();
  142   			if(r != null) {
  143   				userCode = req.processRecord(r);
  144   				if (userCode != 0) break;
  145   			} else {
  146   				going = false;
  147   			}
  148   		}
  149   		
  150   		// All done, return our last code
  151   		return userCode;
  152       }
  153   }

Save This Page
Home » poi-src-3.2-FINAL-20081019 » org.apache » poi » hssf » eventusermodel » [javadoc | source]