2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/* ====================================================================
|
2004-04-09 09:05:39 -04:00
|
|
|
Copyright 2002-2004 Apache Software Foundation
|
|
|
|
|
|
|
|
Licensed 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.
|
|
|
|
==================================================================== */
|
2004-08-23 04:52:54 -04:00
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
package org.apache.poi.hssf.record.aggregates;
|
|
|
|
|
2002-08-21 07:56:49 -04:00
|
|
|
import org.apache.poi.hssf.record.*;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2004-10-06 23:37:16 -04:00
|
|
|
import java.util.SortedMap;
|
2004-08-23 04:52:54 -04:00
|
|
|
import java.util.TreeMap;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
2004-10-06 23:37:16 -04:00
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Aggregate value records together. Things are easier to handle that way.
|
|
|
|
*
|
2004-08-23 04:52:54 -04:00
|
|
|
* @author andy
|
2002-02-10 23:23:11 -05:00
|
|
|
* @author Glen Stampoultzis (glens at apache.org)
|
2002-09-04 20:26:28 -04:00
|
|
|
* @author Jason Height (jheight at chariot dot net dot au)
|
2002-01-30 21:22:28 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class ValueRecordsAggregate
|
|
|
|
extends Record
|
|
|
|
{
|
|
|
|
public final static short sid = -1000;
|
2004-08-23 04:52:54 -04:00
|
|
|
int firstcell = -1;
|
|
|
|
int lastcell = -1;
|
|
|
|
TreeMap records = null;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
2004-10-06 23:37:16 -04:00
|
|
|
/** This class is used to find a row in the TreeMap.
|
|
|
|
*
|
|
|
|
* This instance of which is used by the rowHasCells method as the key.
|
|
|
|
*/
|
|
|
|
private class RowComparator implements CellValueRecordInterface, Comparable {
|
|
|
|
private int row;
|
|
|
|
|
|
|
|
public void setRow(int row) {
|
|
|
|
this.row = row;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int compareTo(Object obj) {
|
|
|
|
CellValueRecordInterface cell = (CellValueRecordInterface) obj;
|
|
|
|
|
|
|
|
if (row == cell.getRow()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (row < cell.getRow()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (row > cell.getRow()){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
public int getRow() { return row;}
|
|
|
|
public short getColumn() { return 0;}
|
|
|
|
public void setColumn(short col){}
|
|
|
|
public void setXFIndex(short xf){}
|
|
|
|
public short getXFIndex(){return 0;}
|
|
|
|
public boolean isBefore(CellValueRecordInterface i){ return false; }
|
|
|
|
public boolean isAfter(CellValueRecordInterface i){ return false; }
|
|
|
|
public boolean isEqual(CellValueRecordInterface i){ return false; }
|
|
|
|
public Object clone(){ return null;}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterates the cell records that exist between the startRow and endRow (inclusive).
|
|
|
|
*
|
|
|
|
* User must ensure that hasNext & next are called insequence for correct
|
|
|
|
* operation. Could fix, but since this is only used internally to the
|
|
|
|
* ValueRecordsAggregate class there doesnt seem much point.
|
|
|
|
*/
|
|
|
|
private class RowCellIterator implements Iterator {
|
|
|
|
private int startRow;
|
|
|
|
private int endRow;
|
|
|
|
private Iterator internalIterator;
|
|
|
|
private CellValueRecordInterface atCell;
|
|
|
|
|
|
|
|
public class RowCellComparator extends RowComparator {
|
|
|
|
public int compareTo(Object obj) {
|
|
|
|
CellValueRecordInterface cell = (CellValueRecordInterface) obj;
|
|
|
|
|
|
|
|
if (getRow() == cell.getRow() && cell.getColumn() == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (getRow() < cell.getRow()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (getRow() > cell.getRow()){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (cell.getColumn() > 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (cell.getColumn() < 0)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private RowCellComparator rowCellCompare;
|
|
|
|
|
|
|
|
|
|
|
|
public RowCellIterator(int startRow, int endRow) {
|
|
|
|
this.startRow = startRow;
|
|
|
|
this.endRow = endRow;
|
|
|
|
rowCellCompare = new RowCellComparator();
|
|
|
|
rowCellCompare.setRow(startRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
if (internalIterator == null) {
|
|
|
|
internalIterator = records.tailMap(rowCellCompare).values().iterator();
|
|
|
|
}
|
|
|
|
if (internalIterator.hasNext()) {
|
|
|
|
atCell = (CellValueRecordInterface) internalIterator.next();
|
|
|
|
return (atCell.getRow() <= endRow);
|
|
|
|
} else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object next() {
|
|
|
|
return atCell;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void remove() {
|
|
|
|
//Do Nothing (Not called)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Only need a single instance of this class, but the row fields
|
|
|
|
//will probably change each use. Instance is only used in the rowHasCells method.
|
|
|
|
public final RowComparator compareRow = new RowComparator();
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
/** Creates a new instance of ValueRecordsAggregate */
|
2003-07-18 22:48:17 -04:00
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public ValueRecordsAggregate()
|
|
|
|
{
|
|
|
|
records = new TreeMap();
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public void insertCell(CellValueRecordInterface cell)
|
|
|
|
{
|
|
|
|
Object o = records.put(cell, cell);
|
2002-01-30 21:22:28 -05:00
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
if ((cell.getColumn() < firstcell) || (firstcell == -1))
|
|
|
|
{
|
|
|
|
firstcell = cell.getColumn();
|
|
|
|
}
|
|
|
|
if ((cell.getColumn() > lastcell) || (lastcell == -1))
|
|
|
|
{
|
|
|
|
lastcell = cell.getColumn();
|
|
|
|
}
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public void removeCell(CellValueRecordInterface cell)
|
|
|
|
{
|
|
|
|
records.remove(cell);
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public int getPhysicalNumberOfCells()
|
|
|
|
{
|
|
|
|
return records.size();
|
2003-09-17 22:10:50 -04:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public int getFirstCellNum()
|
2004-04-18 09:02:48 -04:00
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
return firstcell;
|
2003-09-17 22:10:50 -04:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public int getLastCellNum()
|
2003-09-17 22:10:50 -04:00
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
return lastcell;
|
2003-09-17 22:10:50 -04:00
|
|
|
}
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
public int construct(int offset, List records)
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
int k = 0;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
2002-08-21 07:56:49 -04:00
|
|
|
FormulaRecordAggregate lastFormulaAggregate = null;
|
|
|
|
|
2002-01-30 21:22:28 -05:00
|
|
|
for (k = offset; k < records.size(); k++)
|
|
|
|
{
|
|
|
|
Record rec = ( Record ) records.get(k);
|
|
|
|
|
2002-08-21 07:56:49 -04:00
|
|
|
if (rec instanceof StringRecord == false && !rec.isInValueSection() && !(rec instanceof UnknownRecord))
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2002-08-21 07:56:49 -04:00
|
|
|
if (rec instanceof FormulaRecord)
|
|
|
|
{
|
|
|
|
lastFormulaAggregate = new FormulaRecordAggregate((FormulaRecord)rec, null);
|
|
|
|
insertCell( lastFormulaAggregate );
|
|
|
|
}
|
|
|
|
else if (rec instanceof StringRecord)
|
|
|
|
{
|
|
|
|
lastFormulaAggregate.setStringRecord((StringRecord)rec);
|
2004-08-23 04:52:54 -04:00
|
|
|
}
|
|
|
|
else if (rec instanceof SharedFormulaRecord)
|
|
|
|
{
|
|
|
|
//these follow the first formula in a group
|
2003-07-19 10:56:36 -04:00
|
|
|
lastFormulaAggregate.setSharedFormulaRecord((SharedFormulaRecord)rec);
|
2002-08-21 07:56:49 -04:00
|
|
|
}
|
|
|
|
else if (rec.isValue())
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
insertCell(( CellValueRecordInterface ) rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2003-07-18 22:48:17 -04:00
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public int serialize(int offset, byte [] data)
|
2004-04-18 09:02:48 -04:00
|
|
|
{
|
2004-10-06 23:37:16 -04:00
|
|
|
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) {
|
|
|
|
RowCellIterator itr = new RowCellIterator(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) {
|
|
|
|
compareRow.setRow(row);
|
|
|
|
return records.containsKey(compareRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Serializes the cells that are allocated to a certain row range*/
|
|
|
|
public int serializeCellRow(final int row, int offset, byte [] data)
|
|
|
|
{
|
|
|
|
RowCellIterator itr = new RowCellIterator(row, row);
|
2004-08-23 04:52:54 -04:00
|
|
|
int pos = offset;
|
2003-07-18 22:48:17 -04:00
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
while (itr.hasNext())
|
2004-04-18 09:02:48 -04:00
|
|
|
{
|
2004-10-06 23:37:16 -04:00
|
|
|
CellValueRecordInterface cell = (CellValueRecordInterface)itr.next();
|
|
|
|
if (cell.getRow() != row)
|
|
|
|
break;
|
|
|
|
pos += (( Record ) cell).serialize(pos, data);
|
2003-07-18 22:48:17 -04:00
|
|
|
}
|
2004-08-23 04:52:54 -04:00
|
|
|
return pos - offset;
|
2003-07-18 22:48:17 -04:00
|
|
|
}
|
2004-10-06 23:37:16 -04:00
|
|
|
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2003-07-18 22:48:17 -04:00
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
protected void fillFields(byte [] data, short size, int offset)
|
2002-01-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-04-18 09:02:48 -04:00
|
|
|
/**
|
2004-08-23 04:52:54 -04:00
|
|
|
* called by constructor, should throw runtime exception in the event of a
|
|
|
|
* record passed with a differing ID.
|
2004-04-18 09:02:48 -04:00
|
|
|
*
|
2004-08-23 04:52:54 -04:00
|
|
|
* @param id alleged id for this record
|
2004-04-18 09:02:48 -04:00
|
|
|
*/
|
2003-07-18 22:48:17 -04:00
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
protected void validateSid(short id)
|
|
|
|
{
|
2003-07-18 22:48:17 -04:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
/**
|
|
|
|
* return the non static version of the id for this record.
|
|
|
|
*/
|
2003-07-18 22:48:17 -04:00
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public short getSid()
|
|
|
|
{
|
|
|
|
return sid;
|
2003-07-18 22:48:17 -04:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public int getRecordSize() {
|
|
|
|
|
|
|
|
int size = 0;
|
|
|
|
Iterator irecs = records.values().iterator();
|
|
|
|
|
|
|
|
while (irecs.hasNext()) {
|
|
|
|
size += (( Record ) irecs.next()).getRecordSize();
|
2003-07-18 22:48:17 -04:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
return size;
|
2003-07-18 22:48:17 -04:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
public Iterator getIterator()
|
|
|
|
{
|
|
|
|
return records.values().iterator();
|
2003-07-18 22:48:17 -04:00
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
/** Performs a deep clone of the record*/
|
|
|
|
public Object clone() {
|
|
|
|
ValueRecordsAggregate rec = new ValueRecordsAggregate();
|
|
|
|
for (Iterator valIter = getIterator(); valIter.hasNext();) {
|
|
|
|
CellValueRecordInterface val = (CellValueRecordInterface)((CellValueRecordInterface)valIter.next()).clone();
|
|
|
|
rec.insertCell(val);
|
|
|
|
}
|
|
|
|
return rec;
|
2003-07-18 22:48:17 -04:00
|
|
|
}
|
2004-10-06 23:37:16 -04:00
|
|
|
}
|