FileDocCategorySizeDatePackage
ValueRecordsAggregate.javaAPI DocApache Poi 3.0.110962Sun Mar 11 12:59:08 GMT 2007org.apache.poi.hssf.record.aggregates

ValueRecordsAggregate

public class ValueRecordsAggregate extends Record
Aggregate value records together. Things are easier to handle that way.
author
andy
author
Glen Stampoultzis (glens at apache.org)
author
Jason Height (jheight at chariot dot net dot au)

Fields Summary
public static final short
sid
int
firstcell
int
lastcell
CellValueRecordInterface[]
records
Constructors Summary
public ValueRecordsAggregate()
Creates a new instance of ValueRecordsAggregate


           

     
    
    records = new CellValueRecordInterface[30][]; // We start with 30 Rows.
    
Methods Summary
public java.lang.Objectclone()
Performs a deep clone of the record

      ValueRecordsAggregate rec = new ValueRecordsAggregate();
      for (Iterator valIter = getIterator(); valIter.hasNext();) {
        CellValueRecordInterface val = (CellValueRecordInterface)((CellValueRecordInterface)valIter.next()).clone();
        rec.insertCell(val);
      }
      return rec;
    
public intconstruct(int offset, java.util.List records)

        int k = 0;

        FormulaRecordAggregate lastFormulaAggregate = null;
        
        List sharedFormulas = new java.util.ArrayList();

        for (k = offset; k < records.size(); k++)
        {
            Record rec = ( Record ) records.get(k);

            if (rec instanceof StringRecord == false && !rec.isInValueSection() && !(rec instanceof UnknownRecord))
            {
                break;
            } else if (rec instanceof SharedFormulaRecord) {
            	sharedFormulas.add(rec);
            } else if (rec instanceof FormulaRecord)
            {
              FormulaRecord formula = (FormulaRecord)rec;
              if (formula.isSharedFormula()) {
                Record nextRecord = (Record) records.get(k + 1);
                if (nextRecord instanceof SharedFormulaRecord) {
                	sharedFormulas.add(nextRecord);
                	k++;
                }
                //traverse the list of shared formulas in reverse order, and try to find the correct one
                //for us
                boolean found = false;
                for (int i=sharedFormulas.size()-1;i>=0;i--) {
                	SharedFormulaRecord shrd = (SharedFormulaRecord)sharedFormulas.get(i);
                	if (shrd.isFormulaInShared(formula)) {
                		shrd.convertSharedFormulaRecord(formula);
                		found = true;
                		break;
                	}
                }
                if (!found) {
                	//Sometimes the shared formula flag "seems" to be errornously set,
                	//cant really do much about that.
                	//throw new RecordFormatException("Could not find appropriate shared formula");
                }
              }
            	
              lastFormulaAggregate = new FormulaRecordAggregate((FormulaRecord)rec, null);
              insertCell( lastFormulaAggregate );
            }
            else if (rec instanceof StringRecord)
            {
                lastFormulaAggregate.setStringRecord((StringRecord)rec);
            }
            else if (rec.isValue())
            {
                insertCell(( CellValueRecordInterface ) rec);
            }
        }
        return k;
    
protected voidfillFields(org.apache.poi.hssf.record.RecordInputStream in)
called by the constructor, should set class level fields. Should throw runtime exception for bad/icomplete data.

param
data raw data
param
size size of data
param
offset of the record's data (provided a big array of the file)

    
public intgetFirstCellNum()

        return firstcell;
    
public java.util.IteratorgetIterator()

    return new MyIterator();
    
public intgetLastCellNum()

        return lastcell;
    
public intgetPhysicalNumberOfCells()

    int count=0;
    for(int r=0;r<records.length;r++) {
      CellValueRecordInterface[] rowCells=records[r];
      if (rowCells != null)
        for(short c=0;c<rowCells.length;c++) {
          if(rowCells[c]!=null) count++;
        }
    }
    return count;
    
public intgetRecordSize()

    
        int size = 0;
    Iterator irecs = this.getIterator();
        
        while (irecs.hasNext()) {
                size += (( Record ) irecs.next()).getRecordSize();
        }

        return size;
    
public intgetRowCellBlockSize(int startRow, int endRow)
Tallies a count of the size of the cell records that are attached to the rows in the range specified.

      MyIterator itr = new MyIterator(startRow, endRow);
      int size = 0;
      while (itr.hasNext()) {
        CellValueRecordInterface cell = (CellValueRecordInterface)itr.next();
        int row = cell.getRow();
        if (row > endRow)
          break;
        if ((row >=startRow) && (row <= endRow))
          size += ((Record)cell).getRecordSize();
      }
      return size;
    
public shortgetSid()
return the non static version of the id for this record.

        return sid;
    
public voidinsertCell(org.apache.poi.hssf.record.CellValueRecordInterface cell)

    short column = cell.getColumn();
    int row = cell.getRow();
    if (row >= records.length) {
      CellValueRecordInterface[][] oldRecords = records;
      int newSize = oldRecords.length * 2;
      if(newSize<row+1) newSize=row+1;
      records = new CellValueRecordInterface[newSize][];
      System.arraycopy(oldRecords, 0, records, 0, oldRecords.length);
    }
    CellValueRecordInterface[] rowCells = records[row];
    if (rowCells == null) {
      int newSize = column + 1;
      if(newSize<10) newSize=10;
      rowCells = new CellValueRecordInterface[newSize];
      records[row] = rowCells;
    }
    if (column >= rowCells.length) {
      CellValueRecordInterface[] oldRowCells = rowCells;
      int newSize = oldRowCells.length * 2;
      if(newSize<column+1) newSize=column+1;
      // if(newSize>257) newSize=257; // activate?
      rowCells = new CellValueRecordInterface[newSize];
      System.arraycopy(oldRowCells, 0, rowCells, 0, oldRowCells.length);
      records[row] = rowCells;
    }
    rowCells[column] = cell;

    if ((column < firstcell) || (firstcell == -1)) {
      firstcell = column;
    }
    if ((column > lastcell) || (lastcell == -1)) {
      lastcell = column;
    }
  
public voidremoveCell(org.apache.poi.hssf.record.CellValueRecordInterface cell)

    	if (cell != null) {
          short column = cell.getColumn();
          int row = cell.getRow();
          if(row>=records.length) return;
          CellValueRecordInterface[] rowCells=records[row];
          if(rowCells==null) return;
          if(column>=rowCells.length) return;
          rowCells[column]=null;
    	}
    
public booleanrowHasCells(int row)
Returns true if the row has cells attached to it

    	if (row > records.length-1) //previously this said row > records.length which means if 
    		return false;  // if records.length == 60 and I pass "60" here I get array out of bounds
      CellValueRecordInterface[] rowCells=records[row]; //because a 60 length array has the last index = 59
      if(rowCells==null) return false;
      for(int col=0;col<rowCells.length;col++) {
        if(rowCells[col]!=null) return true;
      }
      return false;
    
public intserialize(int offset, byte[] data)
called by the class that is responsible for writing this sucker. Subclasses should implement this so that their data is passed back in a byte array.

param
offset to begin writing at
param
data byte array containing instance data
return
number of bytes written

      throw new RuntimeException("This method shouldnt be called. ValueRecordsAggregate.serializeCellRow() should be called from RowRecordsAggregate.");
    
public intserializeCellRow(int row, int offset, byte[] data)
Serializes the cells that are allocated to a certain row range

      MyIterator itr = new MyIterator(row, row);
        int      pos = offset;

        while (itr.hasNext())
        {
            CellValueRecordInterface cell = (CellValueRecordInterface)itr.next();
            if (cell.getRow() != row)
              break;
            pos += (( Record ) cell).serialize(pos, data);
        }
        return pos - offset;
    
protected voidvalidateSid(short id)
called by constructor, should throw runtime exception in the event of a record passed with a differing ID.

param
id alleged id for this record