FileDocCategorySizeDatePackage
RecordFactory.javaAPI DocApache Poi 3.0.111594Mon Jan 01 18:59:10 GMT 2007org.apache.poi.hssf.record

RecordFactory

public class RecordFactory extends Object
Title: Record Factory

Description: Takes a stream and outputs an array of Record objects.

deprecated
use {@link org.apache.poi.hssf.eventmodel.EventRecordFactory} instead
see
org.apache.poi.hssf.eventmodel.EventRecordFactory
author
Andrew C. Oliver (acoliver at apache dot org)
author
Marc Johnson (mjohnson at apache dot org)
author
Glen Stampoultzis (glens at apache.org)
author
Csaba Nagy (ncsaba at yahoo dot com)

Fields Summary
private static int
NUM_RECORDS
private static final Class[]
records
private static Map
recordsMap
Constructors Summary
Methods Summary
public static org.apache.poi.hssf.record.Record[]createRecord(org.apache.poi.hssf.record.RecordInputStream in)

        Record   retval;
        Record[] realretval = null;

        try
        {
            Constructor constructor =
                ( Constructor ) recordsMap.get(new Short(in.getSid()));

            if (constructor != null)
            {
                retval = ( Record ) constructor.newInstance(new Object[]
                {
                    in
                });
            }
            else
            {
                retval = new UnknownRecord(in);
            }
        }
        catch (Exception introspectionException)
        {
            throw new RecordFormatException("Unable to construct record instance",introspectionException);
        }
        if (retval instanceof RKRecord)
        {
            RKRecord     rk  = ( RKRecord ) retval;
            NumberRecord num = new NumberRecord();

            num.setColumn(rk.getColumn());
            num.setRow(rk.getRow());
            num.setXFIndex(rk.getXFIndex());
            num.setValue(rk.getRKNumber());
            retval = num;
        }
        else if (retval instanceof DBCellRecord)
        {
            retval = null;
        }
        else if (retval instanceof MulRKRecord)
        {
            MulRKRecord mrk = ( MulRKRecord ) retval;

            realretval = new Record[ mrk.getNumColumns() ];
            for (int k = 0; k < mrk.getNumColumns(); k++)
            {
                NumberRecord nr = new NumberRecord();

                nr.setColumn(( short ) (k + mrk.getFirstColumn()));
                nr.setRow(mrk.getRow());
                nr.setXFIndex(mrk.getXFAt(k));
                nr.setValue(mrk.getRKNumberAt(k));
                realretval[ k ] = nr;
            }
        }
        else if (retval instanceof MulBlankRecord)
        {
            MulBlankRecord mb = ( MulBlankRecord ) retval;

            realretval = new Record[ mb.getNumColumns() ];
            for (int k = 0; k < mb.getNumColumns(); k++)
            {
                BlankRecord br = new BlankRecord();

                br.setColumn(( short ) (k + mb.getFirstColumn()));
                br.setRow(mb.getRow());
                br.setXFIndex(mb.getXFAt(k));
                realretval[ k ] = br;
            }
        }
        if (realretval == null)
        {
            realretval      = new Record[ 1 ];
            realretval[ 0 ] = retval;
        }
        return realretval;
    
public static java.util.ListcreateRecords(java.io.InputStream in)
Create an array of records from an input stream

param
in the InputStream from which the records will be obtained
return
an array of Records created from the InputStream
exception
RecordFormatException on error processing the InputStream

        ArrayList records     = new ArrayList(NUM_RECORDS);

        RecordInputStream recStream = new RecordInputStream(in);
            DrawingRecord lastDrawingRecord = new DrawingRecord( );
        Record lastRecord = null;
        while (recStream.hasNextRecord()) {
          recStream.nextRecord();
          if (recStream.getSid() != 0)
            {
              Record[] recs = createRecord(recStream);   // handle MulRK records

                    if (recs.length > 1)
                    {
                        for (int k = 0; k < recs.length; k++)
                        {
                            records.add(
                                recs[ k ]);               // these will be number records
                  }
                    }
                    else
                    {
                        Record record = recs[ 0 ];

                        if (record != null)
                        {
                        if (record.getSid() == DrawingGroupRecord.sid
                            && lastRecord instanceof DrawingGroupRecord)
                            {
                            DrawingGroupRecord lastDGRecord = (DrawingGroupRecord) lastRecord;
                                lastDGRecord.join((AbstractEscherHolderRecord) record);
                            }
                        else if (record.getSid() == ContinueRecord.sid &&
                                 ((lastRecord instanceof ObjRecord) || (lastRecord instanceof TextObjectRecord))) {
                          // Drawing records have a very strange continue behaviour.
                          //There can actually be OBJ records mixed between the continues.
                          lastDrawingRecord.processContinueRecord( ((ContinueRecord)record).getData() );
                        } else if (record.getSid() == ContinueRecord.sid &&
                                   (lastRecord instanceof DrawingGroupRecord)) {
                            ((DrawingGroupRecord)lastRecord).processContinueRecord(((ContinueRecord)record).getData());
                        } else if (record.getSid() == ContinueRecord.sid) {
                          if (lastRecord instanceof UnknownRecord) {
                            //Gracefully handle records that we dont know about,
                            //that happen to be continued
                            records.add(record);
                          } else 
                        	  throw new RecordFormatException("Unhandled Continue Record");
                            }
                        else {
                            lastRecord = record;
                                if (record instanceof DrawingRecord)
                                    lastDrawingRecord = (DrawingRecord) record;
                                records.add(record);
                            }
                        }
                    }
                }
            }

        return records;
    
public static short[]getAllKnownRecordSIDs()

        short[] results = new short[ recordsMap.size() ];
        int     i       = 0;

        for (Iterator iterator = recordsMap.keySet().iterator();
                iterator.hasNext(); )
        {
            Short sid = ( Short ) iterator.next();

            results[ i++ ] = sid.shortValue();
        }
        return results;
    
private static java.util.MaprecordsToMap(java.lang.Class[] records)

        Map         result = new HashMap();
        Constructor constructor;

        for (int i = 0; i < records.length; i++)
        {
            Class record;
            short sid;

            record = records[ i ];
            try
            {
                sid         = record.getField("sid").getShort(null);
                constructor = record.getConstructor(new Class[]
                {
                    RecordInputStream.class
                });
            }
            catch (Exception illegalArgumentException)
            {
                throw new RecordFormatException(
                    "Unable to determine record types", illegalArgumentException);
            }
            result.put(new Short(sid), constructor);
        }
        return result;
    
public static voidsetCapacity(int capacity)
changes the default capacity (10000) to handle larger files


                  

        
    
        NUM_RECORDS = capacity;