/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.hssf.record.aggregates; import org.apache.poi.hssf.record.*; import java.util.Iterator; import java.util.List; /** * * 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) */ public class ValueRecordsAggregate extends Record { public final static short sid = -1000; int firstcell = -1; int lastcell = -1; CellValueRecordInterface[][] records; /** Creates a new instance of ValueRecordsAggregate */ public ValueRecordsAggregate() { records = new CellValueRecordInterface[30][]; // We start with 30 Rows. } public void insertCell(CellValueRecordInterface cell) { short column = cell.getColumn(); int row = cell.getRow(); if (row >= records.length) { CellValueRecordInterface[][] oldRecords = records; int newSize = oldRecords.length * 2; if(newSize= rowCells.length) { CellValueRecordInterface[] oldRowCells = rowCells; int newSize = oldRowCells.length * 2; if(newSize257) 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 void removeCell(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 int getPhysicalNumberOfCells() { int count=0; for(int r=0;r=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; } /** * 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 */ public int serialize(int offset, byte [] data) { throw new RuntimeException("This method shouldnt be called. ValueRecordsAggregate.serializeCellRow() should be called from RowRecordsAggregate."); } /** Tallies a count of the size of the cell records * that are attached to the rows in the range specified. */ public int getRowCellBlockSize(int startRow, int endRow) { 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; } /** Returns true if the row has cells attached to it */ public boolean rowHasCells(int row) { 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