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-08-23 04:52:54 -04:00
|
|
|
import java.util.TreeMap;
|
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;
|
|
|
|
// int size = 0;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* if (records.get(cell) == null)
|
|
|
|
{
|
|
|
|
size += (( Record ) cell).getRecordSize();
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|
2004-08-23 04:52:54 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size += (( Record ) cell).getRecordSize()
|
|
|
|
- (( Record ) records.get(cell)).getRecordSize();
|
|
|
|
}*/
|
2003-07-18 22:48:17 -04:00
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
// XYLocator xy = new XYLocator(cell.getRow(), cell.getColumn());
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
// size -= (( Record ) cell).getRecordSize();
|
2002-01-30 21:22:28 -05:00
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
// XYLocator xy = new XYLocator(cell.getRow(), cell.getColumn());
|
|
|
|
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-08-23 04:52:54 -04:00
|
|
|
Iterator itr = records.values().iterator();
|
|
|
|
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-08-23 04:52:54 -04:00
|
|
|
pos += (( Record ) itr.next()).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-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;
|
|
|
|
// 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-04-09 09:05:39 -04:00
|
|
|
}
|
2004-08-23 04:52:54 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* class XYLocator implements Comparable {
|
|
|
|
* private int row = 0;
|
|
|
|
* private int col = 0;
|
|
|
|
* public XYLocator(int row, int col) {
|
|
|
|
* this.row = row;
|
|
|
|
* this.col = col;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* public int getRow() {
|
|
|
|
* return row;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* public int getCol() {
|
|
|
|
* return col;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* public int compareTo(Object obj) {
|
|
|
|
* XYLocator loc = (XYLocator)obj;
|
|
|
|
*
|
|
|
|
* if (this.getRow() == loc.getRow() &&
|
|
|
|
* this.getCol() == loc.getCol() )
|
|
|
|
* return 0;
|
|
|
|
*
|
|
|
|
* if (this.getRow() < loc.getRow())
|
|
|
|
* return -1;
|
|
|
|
*
|
|
|
|
* if (this.getRow() > loc.getRow())
|
|
|
|
* return 1;
|
|
|
|
*
|
|
|
|
* if (this.getCol() < loc.getCol())
|
|
|
|
* return -1;
|
|
|
|
*
|
|
|
|
* if (this.getCol() > loc.getCol())
|
|
|
|
* return 1;
|
|
|
|
*
|
|
|
|
* return -1;
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* public boolean equals(Object obj) {
|
|
|
|
* if (!(obj instanceof XYLocator)) return false;
|
|
|
|
*
|
|
|
|
* XYLocator loc = (XYLocator)obj;
|
|
|
|
* if (this.getRow() == loc.getRow()
|
|
|
|
* &&
|
|
|
|
* this.getCol() == loc.getCol()
|
|
|
|
* ) return true;
|
|
|
|
* return false;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
*/
|