looks like I had stuff locally that I forgot to commit (oops) . Thsi patch from Carey Sublette

Makes the event based API abortable.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352497 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew C. Oliver 2002-04-27 13:33:04 +00:00
parent ced81e122b
commit 6212e135de
4 changed files with 401 additions and 73 deletions

View File

@ -0,0 +1,106 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache POI" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache POI", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.poi.hssf.eventmodel;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.HSSFUserException;
/**
* Interface for use with the HSSFRequest and HSSFEventFactory. Users should create
* a listener supporting this interface and register it with the HSSFRequest (associating
* it with Record SID's).
*
* @see org.apache.poi.hssf.eventmodel.HSSFEventFactory
* @see org.apache.poi.hssf.eventmodel.HSSFRequest
* @see org.apache.poi.hssf.HSSFUserException
*
* @author Carey Sublette (careysub@earthling.net)
*
*/
public abstract class AbortableHSSFListener implements HSSFListener
{
/**
* This method, inherited from HSSFListener is implemented as a stub.
* It is never called by HSSFEventFActory or HSSFRequest.
*
*/
public void processRecord(Record record)
{
}
/**
* Process an HSSF Record. Called when a record occurs in an HSSF file.
* Provides two options for halting the processing of the HSSF file.
*
* The return value provides a means of non-error termination with a
* user-defined result code. A value of zero must be returned to
* continue processing, any other value will halt processing by
* <code>HSSFEventFactory</code> with the code being passed back by
* its abortable process events methods.
*
* Error termination can be done by throwing the HSSFUserException.
*
* Note that HSSFEventFactory will not call the inherited process
*
* @return result code of zero for continued processing.
*
* @throws HSSFUserException User code can throw this to abort
* file processing by HSSFEventFactory and return diagnostic information.
*/
public abstract short abortableProcessRecord(Record record) throws HSSFUserException;
}

View File

@ -59,6 +59,7 @@ import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.hssf.HSSFUserException;
import org.apache.poi.hssf.record.RecordFormatException; import org.apache.poi.hssf.record.RecordFormatException;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordFactory; import org.apache.poi.hssf.record.RecordFactory;
@ -76,12 +77,12 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
* *
* @see org.apache.poi.hssf.dev.EFHSSF * @see org.apache.poi.hssf.dev.EFHSSF
* *
* @author andy * @author Andrew C. Oliver (acoliver at apache dot org)
* @authro Carey Sublette (careysub@earthling.net)
*/ */
public class HSSFEventFactory public class HSSFEventFactory
{ {
/** Creates a new instance of HSSFEventFactory */ /** Creates a new instance of HSSFEventFactory */
public HSSFEventFactory() public HSSFEventFactory()
@ -103,9 +104,28 @@ public class HSSFEventFactory
processEvents(req, in); processEvents(req, in);
} }
/**
* Processes a file into essentially record events.
*
* @param req an Instance of HSSFRequest which has your registered listeners
* @param fs a POIFS filesystem containing your workbook
* @return numeric user-specified result code.
*/
public short abortableProcessWorkbookEvents(HSSFRequest req, POIFSFileSystem fs)
throws IOException, HSSFUserException
{
InputStream in = fs.createDocumentInputStream("Workbook");
return abortableProcessEvents(req, in);
}
/** /**
* Processes a DocumentInputStream into essentially Record events. * Processes a DocumentInputStream into essentially Record events.
* *
* If an <code>AbortableHSSFListener</code> causes a halt to processing during this call
* the method will return just as with <code>abortableProcessEvents</code>, but no
* user code or <code>HSSFUserException</code> will be passed back.
*
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String) * @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
* @param req an Instance of HSSFRequest which has your registered listeners * @param req an Instance of HSSFRequest which has your registered listeners
* @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object * @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
@ -113,74 +133,117 @@ public class HSSFEventFactory
public void processEvents(HSSFRequest req, InputStream in) public void processEvents(HSSFRequest req, InputStream in)
throws IOException throws IOException
{
try
{
genericProcessEvents(req, in);
}
catch (HSSFUserException hue)
{/*If an HSSFUserException user exception is thrown, ignore it.*/ }
}
/**
* Processes a DocumentInputStream into essentially Record events.
*
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
* @param req an Instance of HSSFRequest which has your registered listeners
* @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
* @return numeric user-specified result code.
*/
public short abortableProcessEvents(HSSFRequest req, InputStream in)
throws IOException, HSSFUserException
{ {
try return genericProcessEvents(req, in);
{ }
byte[] sidbytes = new byte[ 2 ];
int bytesread = in.read(sidbytes);
Record rec = null;
while (bytesread > 0) /**
{ * Processes a DocumentInputStream into essentially Record events.
short sid = 0; *
* @see org.apache.poi.poifs.filesystem.POIFSFileSystem#createDocumentInputStream(String)
* @param req an Instance of HSSFRequest which has your registered listeners
* @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
* @param in a DocumentInputStream obtained from POIFS's POIFSFileSystem object
* @return numeric user-specified result code.
*/
sid = LittleEndian.getShort(sidbytes); protected short genericProcessEvents(HSSFRequest req, InputStream in)
if ((rec != null) && (sid != ContinueRecord.sid)) throws IOException, HSSFUserException
{ {
req.processRecord(rec); short userCode = 0;
} process:
if (sid != ContinueRecord.sid) try
{ {
short size = LittleEndian.readShort(in); byte[] sidbytes = new byte[ 2 ];
byte[] data = new byte[ size ]; int bytesread = in.read(sidbytes);
Record rec = null;
if (data.length > 0) while (bytesread > 0)
{ {
in.read(data); short sid = 0;
}
Record[] recs = RecordFactory.createRecord(sid, size,
data);
if (recs.length > 1) sid = LittleEndian.getShort(sidbytes);
{ // we know that the multiple if ((rec != null) && (sid != ContinueRecord.sid))
for (int k = 0; k < (recs.length - 1); k++) {
{ // record situations do not userCode = req.processRecord(rec);
req.processRecord( if (userCode != 0) break process;
recs[ k ]); // contain continue records }
} if (sid != ContinueRecord.sid)
} {
rec = recs[ recs.length - 1 ]; // regardless we'll process short size = LittleEndian.readShort(in);
byte[] data = new byte[ size ];
// the last record as though if (data.length > 0)
// it might be continued {
// if there is only one in.read(data);
// records, it will go here too. }
} Record[] recs = RecordFactory.createRecord(sid, size,
else data);
{ // we do have a continue record
short size = LittleEndian.readShort(in);
byte[] data = new byte[ size ];
if (data.length > 0) if (recs.length > 1)
{ { // we know that the multiple
in.read(data); for (int k = 0; k < (recs.length - 1); k++)
} { // record situations do not
rec.processContinueRecord(data); userCode = req.processRecord(
} recs[ k ]); // contain continue records
bytesread = in.read(sidbytes); // read next record sid if (userCode != 0) break process;
} }
if (rec != null) }
{ rec = recs[ recs.length - 1 ]; // regardless we'll process
req.processRecord(rec);
}
}
catch (IOException e)
{
throw new RecordFormatException("Error reading bytes");
}
// Record[] retval = new Record[ records.size() ]; // the last record as though
// retval = ( Record [] ) records.toArray(retval); // it might be continued
// return null; // if there is only one
// records, it will go here too.
}
else
{ // we do have a continue record
short size = LittleEndian.readShort(in);
byte[] data = new byte[ size ];
if (data.length > 0)
{
in.read(data);
}
rec.processContinueRecord(data);
}
bytesread = in.read(sidbytes); // read next record sid
}
if (rec != null)
{
userCode = req.processRecord(rec);
if (userCode != 0) break process;
}
}
catch (IOException e)
{
throw new RecordFormatException("Error reading bytes");
}
return userCode;
// Record[] retval = new Record[ records.size() ];
// retval = ( Record [] ) records.toArray(retval);
// return null;
} }
} }

View File

@ -9,7 +9,7 @@
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* *
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source userCode must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
@ -59,6 +59,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import org.apache.poi.hssf.HSSFUserException;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordFactory; import org.apache.poi.hssf.record.RecordFactory;
@ -69,7 +70,9 @@ import org.apache.poi.hssf.record.RecordFactory;
* @see org.apache.poi.hssf.eventmodel.HSSFEventFactory * @see org.apache.poi.hssf.eventmodel.HSSFEventFactory
* @see org.apache.poi.hssf.eventmodel.HSSFListener * @see org.apache.poi.hssf.eventmodel.HSSFListener
* @see org.apache.poi.hssf.dev.EFHSSF * @see org.apache.poi.hssf.dev.EFHSSF
* @author andy * @see org.apache.poi.hssf.HSSFUserException
* @author Andrew C. Oliver (acoliver at apache dot org)
* @author Carey Sublette (careysub@earthling.net)
*/ */
public class HSSFRequest public class HSSFRequest
@ -137,14 +140,20 @@ public class HSSFRequest
} }
} }
/** /**
* called by HSSFEventFactory, passes the Record to each listener associated with * Called by HSSFEventFactory, passes the Record to each listener associated with
* a record.sid. * a record.sid.
*/ *
* Exception and return value added 2002-04-19 by Carey Sublette
*
* @return numeric user-specified result code. If zero continue processing.
* @throws HSSFUserException User exception condition
*/
protected void processRecord(Record rec) protected short processRecord(Record rec) throws HSSFUserException
{ {
Object obj = records.get(new Short(rec.getSid())); Object obj = records.get(new Short(rec.getSid()));
short userCode = 0;
if (obj != null) if (obj != null)
{ {
@ -152,10 +161,20 @@ public class HSSFRequest
for (int k = 0; k < listeners.size(); k++) for (int k = 0; k < listeners.size(); k++)
{ {
HSSFListener listener = ( HSSFListener ) listeners.get(k); Object listenObj = listeners.get(k);
if (listenObj instanceof AbortableHSSFListener)
listener.processRecord(rec); {
AbortableHSSFListener listener = ( AbortableHSSFListener ) listenObj;
userCode = listener.abortableProcessRecord(rec);
if (userCode!=0) break;
}
else
{
HSSFListener listener = ( HSSFListener ) listenObj;
listener.processRecord(rec);
}
} }
} }
return userCode;
} }
} }

View File

@ -0,0 +1,140 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
package org.apache.poi.hssf;
/**
* <p>This exception is provided as a way for API users to throw
* exceptions from their event handling code. By doing so they
* abort file processing by the HSSFEventFactory and by
* catching it from outside the HSSFEventFactory.processEvents
* method they can diagnose the cause for the abort.</p>
*
* <p>The HSSFUserException supports a nested "reason"
* throwable, i.e. an exception that caused this one to be thrown.</p>
*
* <p>The HSSF package does not itself throw any of these
* exceptions.</p>
*
* @author Rainer Klute (klute@rainer-klute.de)
* @author Carey Sublette (careysub@earthling.net)
* @version HSSFUserException.java,v 1.0
* @since 2002-04-19
*/
public class HSSFUserException extends Exception
{
private Throwable reason;
/**
* <p>Creates a new {@link HSSFUserException}.</p>
*/
public HSSFUserException()
{
super();
}
/**
* <p>Creates a new {@link HSSFUserException} with a message
* string.</p>
*/
public HSSFUserException(final String msg)
{
super(msg);
}
/**
* <p>Creates a new {@link HSSFUserException} with a reason.</p>
*/
public HSSFUserException(final Throwable reason)
{
super();
this.reason = reason;
}
/**
* <p>Creates a new {@link HSSFUserException} with a message string
* and a reason.</p>
*/
public HSSFUserException(final String msg, final Throwable reason)
{
super(msg);
this.reason = reason;
}
/**
* <p>Returns the {@link Throwable} that caused this exception to
* be thrown or <code>null</code> if there was no such {@link
* Throwable}.</p>
*/
public Throwable getReason()
{
return reason;
}
}