Fixes to chart handling and to a hidden bug to do with where the beginning of the sheet is located. Also fixed an issue with doubles and the NaN values. Some of this could use some more refactoring.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352827 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Glen Stampoultzis 2002-08-21 11:56:49 +00:00
parent ef21669380
commit f42b7bbdbb
10 changed files with 151 additions and 104 deletions

View File

@ -78,7 +78,7 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
* @see org.apache.poi.hssf.dev.EFHSSF * @see org.apache.poi.hssf.dev.EFHSSF
* *
* @author Andrew C. Oliver (acoliver at apache dot org) * @author Andrew C. Oliver (acoliver at apache dot org)
* @authro Carey Sublette (careysub@earthling.net) * @author Carey Sublette (careysub@earthling.net)
*/ */
public class HSSFEventFactory public class HSSFEventFactory

View File

@ -59,12 +59,10 @@ import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.hssf import org.apache.poi.hssf
.record.*; // normally I don't do this, buy we literally mean ALL .record.*; // normally I don't do this, buy we literally mean ALL
import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.util.IntList; import org.apache.poi.util.*;
import org.apache.poi.util.POILogger;
import org.apache.poi.hssf.record import org.apache.poi.hssf.record
.aggregates.*; // normally I don't do this, buy we literally mean ALL .aggregates.*; // normally I don't do this, buy we literally mean ALL
@ -100,7 +98,6 @@ public class Sheet
int preoffset = 0; // offset of the sheet in a new file int preoffset = 0; // offset of the sheet in a new file
int loc = 0; int loc = 0;
protected boolean containsLabels = false; protected boolean containsLabels = false;
;
protected int dimsloc = 0; protected int dimsloc = 0;
protected DimensionsRecord dims; protected DimensionsRecord dims;
protected DefaultColWidthRecord defaultcolwidth = null; protected DefaultColWidthRecord defaultcolwidth = null;
@ -112,14 +109,13 @@ public class Sheet
protected PrintGridlinesRecord printGridlines = null; protected PrintGridlinesRecord printGridlines = null;
protected MergeCellsRecord merged = null; protected MergeCellsRecord merged = null;
protected int mergedloc = 0; protected int mergedloc = 0;
private static POILogger log = private static POILogger log = POILogFactory.getLogger(Sheet.class);
POILogFactory.getLogger(Sheet.class); private ArrayList columnSizes = null; // holds column info
private ArrayList columnSizes =
null; // holds column info
protected ValueRecordsAggregate cells = null; protected ValueRecordsAggregate cells = null;
protected RowRecordsAggregate rows = null; protected RowRecordsAggregate rows = null;
private Iterator valueRecIterator = null; private Iterator valueRecIterator = null;
private Iterator rowRecIterator = null; private Iterator rowRecIterator = null;
protected int eofLoc = 0;
/** /**
* Creates new Sheet with no intialization --useless at this point * Creates new Sheet with no intialization --useless at this point
@ -164,20 +160,24 @@ public class Sheet
if (rec.getSid() == LabelRecord.sid) if (rec.getSid() == LabelRecord.sid)
{ {
log.log(log.DEBUG, "Hit label record"); log.log(log.DEBUG, "Hit label record.");
retval.containsLabels = true; retval.containsLabels = true;
} }
else if (rec.getSid() == BOFRecord.sid) else if (rec.getSid() == BOFRecord.sid)
{ {
bofEofNestingLevel++; bofEofNestingLevel++;
log.log(log.DEBUG, "Hit BOF record. Nesting increased to " + bofEofNestingLevel);
} }
else if ((rec.getSid() == EOFRecord.sid) else if (rec.getSid() == EOFRecord.sid)
&& (--bofEofNestingLevel == 0))
{ {
log.log(log.DEBUG, "Hit EOF record at "); --bofEofNestingLevel;
log.log(log.DEBUG, "Hit EOF record. Nesting decreased to " + bofEofNestingLevel);
if (bofEofNestingLevel == 0) {
records.add(rec); records.add(rec);
retval.eofLoc = k;
break; break;
} }
}
else if (rec.getSid() == DimensionsRecord.sid) else if (rec.getSid() == DimensionsRecord.sid)
{ {
retval.dims = ( DimensionsRecord ) rec; retval.dims = ( DimensionsRecord ) rec;
@ -204,7 +204,7 @@ public class Sheet
{ {
retval.defaultrowheight = ( DefaultRowHeightRecord ) rec; retval.defaultrowheight = ( DefaultRowHeightRecord ) rec;
} }
else if ( rec.isValue() ) else if ( rec.isValue() && bofEofNestingLevel == 1 )
{ {
if ( isfirstcell ) if ( isfirstcell )
{ {
@ -218,6 +218,10 @@ public class Sheet
rec = null; rec = null;
} }
} }
else if ( rec.getSid() == StringRecord.sid )
{
rec = null;
}
else if ( rec.getSid() == RowRecord.sid ) else if ( rec.getSid() == RowRecord.sid )
{ {
if ( isfirstrow ) if ( isfirstrow )
@ -255,6 +259,14 @@ public class Sheet
} }
} }
retval.records = records; retval.records = records;
if (retval.rows == null)
{
retval.rows = new RowRecordsAggregate();
}
if (retval.cells == null)
{
retval.cells = new ValueRecordsAggregate();
}
log.log(log.DEBUG, "sheet createSheet (existing file) exited"); log.log(log.DEBUG, "sheet createSheet (existing file) exited");
return retval; return retval;
} }
@ -608,11 +620,19 @@ public class Sheet
// } // }
for (int k = 0; k < records.size(); k++) for (int k = 0; k < records.size(); k++)
{ {
// byte[] rec = (( byte [] ) bytes.get(k)); // byte[] rec = (( byte [] ) bytes.get(k));
// System.arraycopy(rec, 0, data, offset + pos, rec.length); // System.arraycopy(rec, 0, data, offset + pos, rec.length);
pos += (( Record ) records.get(k)).serialize(pos + offset, Record record = (( Record ) records.get(k));
data); // rec.length;
//uncomment to test record sizes
// byte[] data2 = new byte[record.getRecordSize()];
// record.serialize(0, data2 ); // rec.length;
// if (LittleEndian.getUShort(data2, 2) != record.getRecordSize() - 4
// && record instanceof RowRecordsAggregate == false && record instanceof ValueRecordsAggregate == false)
// throw new RuntimeException("Blah!!!");
pos += record.serialize(pos + offset, data ); // rec.length;
} }
log.log(log.DEBUG, "Sheet.serialize returning "); log.log(log.DEBUG, "Sheet.serialize returning ");
return pos; return pos;
@ -2116,4 +2136,10 @@ public class Sheet
} }
m.setMargin(size); m.setMargin(size);
} }
public int getEofLoc()
{
return eofLoc;
}
} }

View File

@ -57,8 +57,6 @@ package org.apache.poi.hssf.record;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import java.util.ArrayList;
/** /**
* Title: DBCell Record (Currently read only. Not required.) * Title: DBCell Record (Currently read only. Not required.)
* Description: Used to find rows in blocks...TODO<P> * Description: Used to find rows in blocks...TODO<P>
@ -130,7 +128,7 @@ public class DBCellRecord
* sets offset from the start of this DBCellRecord to the start of the first cell in * sets offset from the start of this DBCellRecord to the start of the first cell in
* the next DBCell block. * the next DBCell block.
* *
* @param rowoffset to the start of the first cell in the next DBCell block * @param offset offset to the start of the first cell in the next DBCell block
*/ */
public void setRowOffset(int offset) public void setRowOffset(int offset)

View File

@ -370,7 +370,7 @@ public class FormulaRecord
LittleEndian.putShort(data, 4 + offset, ( short ) getRow()); LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
LittleEndian.putShort(data, 6 + offset, getColumn()); LittleEndian.putShort(data, 6 + offset, getColumn());
LittleEndian.putShort(data, 8 + offset, getXFIndex()); LittleEndian.putShort(data, 8 + offset, getXFIndex());
LittleEndian.putDouble(data, 10 + offset, getValue()); LittleEndian.putDouble(data, 10 + offset, field_4_value);
LittleEndian.putShort(data, 18 + offset, getOptions()); LittleEndian.putShort(data, 18 + offset, getOptions());
LittleEndian.putInt(data, 20 + offset, field_6_zero); LittleEndian.putInt(data, 20 + offset, field_6_zero);
LittleEndian.putShort(data, 24 + offset, getExpressionLength()); LittleEndian.putShort(data, 24 + offset, getExpressionLength());

View File

@ -55,9 +55,7 @@
package org.apache.poi.hssf.record.aggregates; package org.apache.poi.hssf.record.aggregates;
import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.*;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.UnknownRecord;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -100,7 +98,8 @@ public class ValueRecordsAggregate
}*/ }*/
// XYLocator xy = new XYLocator(cell.getRow(), cell.getColumn()); // XYLocator xy = new XYLocator(cell.getRow(), cell.getColumn());
records.put(cell, cell); Object o = records.put(cell, cell);
if ((cell.getColumn() < firstcell) || (firstcell == -1)) if ((cell.getColumn() < firstcell) || (firstcell == -1))
{ {
firstcell = cell.getColumn(); firstcell = cell.getColumn();
@ -138,15 +137,26 @@ public class ValueRecordsAggregate
{ {
int k = 0; int k = 0;
FormulaRecordAggregate lastFormulaAggregate = null;
for (k = offset; k < records.size(); k++) for (k = offset; k < records.size(); k++)
{ {
Record rec = ( Record ) records.get(k); Record rec = ( Record ) records.get(k);
if (!rec.isInValueSection() && !(rec instanceof UnknownRecord)) if (rec instanceof StringRecord == false && !rec.isInValueSection() && !(rec instanceof UnknownRecord))
{ {
break; break;
} }
if (rec.isValue()) if (rec instanceof FormulaRecord)
{
lastFormulaAggregate = new FormulaRecordAggregate((FormulaRecord)rec, null);
insertCell( lastFormulaAggregate );
}
else if (rec instanceof StringRecord)
{
lastFormulaAggregate.setStringRecord((StringRecord)rec);
}
else if (rec.isValue())
{ {
insertCell(( CellValueRecordInterface ) rec); insertCell(( CellValueRecordInterface ) rec);
} }
@ -175,7 +185,6 @@ public class ValueRecordsAggregate
} }
return pos - offset; return pos - offset;
} }
/** /**
* called by the constructor, should set class level fields. Should throw * called by the constructor, should set class level fields. Should throw
* runtime exception for bad/icomplete data. * runtime exception for bad/icomplete data.

View File

@ -299,7 +299,7 @@ public abstract class Ptg
*/ */
public abstract String toFormulaString(SheetReferences refs); public abstract String toFormulaString(SheetReferences refs);
/** /**
* dump a debug representation (hexdump) to a strnig * dump a debug representation (hexdump) to a string
*/ */
public String toDebugString() { public String toDebugString() {
byte[] ba = new byte[getSize()]; byte[] ba = new byte[getSize()];

View File

@ -71,6 +71,7 @@ import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.BlankRecord; import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoolErrRecord; import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.ExtendedFormatRecord; import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.hssf.util.SheetReferences; import org.apache.poi.hssf.util.SheetReferences;
@ -259,10 +260,11 @@ public class HSSFCell
break; break;
case CELL_TYPE_FORMULA : case CELL_TYPE_FORMULA :
record = new FormulaRecord(); FormulaRecord formulaRecord = new FormulaRecord();
(( FormulaRecord ) record).setColumn(col); record = new FormulaRecordAggregate(formulaRecord,null);
(( FormulaRecord ) record).setRow(row); formulaRecord.setColumn(col);
(( FormulaRecord ) record).setXFIndex(( short ) 0); formulaRecord.setRow(row);
formulaRecord.setXFIndex(( short ) 0);
case CELL_TYPE_BOOLEAN : case CELL_TYPE_BOOLEAN :
record = new BoolErrRecord(); record = new BoolErrRecord();
(( BoolErrRecord ) record).setColumn(col); (( BoolErrRecord ) record).setColumn(col);
@ -314,15 +316,14 @@ public class HSSFCell
case CELL_TYPE_STRING : case CELL_TYPE_STRING :
stringValue = stringValue =
book book.getSSTString( ( (LabelSSTRecord ) cval).getSSTIndex());
.getSSTString((( LabelSSTRecord ) cval).getSSTIndex());
break; break;
case CELL_TYPE_BLANK : case CELL_TYPE_BLANK :
break; break;
case CELL_TYPE_FORMULA : case CELL_TYPE_FORMULA :
cellValue = (( FormulaRecord ) cval).getValue(); cellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue();
break; break;
case CELL_TYPE_BOOLEAN : case CELL_TYPE_BOOLEAN :
@ -369,7 +370,7 @@ public class HSSFCell
retval = HSSFCell.CELL_TYPE_STRING; retval = HSSFCell.CELL_TYPE_STRING;
break; break;
case FormulaRecord.sid : case FormulaRecordAggregate.sid :
retval = HSSFCell.CELL_TYPE_FORMULA; retval = HSSFCell.CELL_TYPE_FORMULA;
break; break;
@ -446,20 +447,20 @@ public class HSSFCell
{ {
case CELL_TYPE_FORMULA : case CELL_TYPE_FORMULA :
FormulaRecord frec = null; FormulaRecordAggregate frec = null;
if (cellType != this.cellType) if (cellType != this.cellType)
{ {
frec = new FormulaRecord(); frec = new FormulaRecordAggregate(new FormulaRecord(),null);
} }
else else
{ {
frec = ( FormulaRecord ) record; frec = ( FormulaRecordAggregate ) record;
} }
frec.setColumn(getCellNum()); frec.setColumn(getCellNum());
if (setValue) if (setValue)
{ {
frec.setValue(getNumericCellValue()); frec.getFormulaRecord().setValue(getNumericCellValue());
} }
frec.setXFIndex(( short ) cellStyle.getIndex()); frec.setXFIndex(( short ) cellStyle.getIndex());
frec.setRow(row); frec.setRow(row);
@ -676,8 +677,7 @@ public class HSSFCell
} }
else else
{ {
if ((cellType != CELL_TYPE_STRING) if ((cellType != CELL_TYPE_STRING ) && ( cellType != CELL_TYPE_FORMULA))
&& (cellType != CELL_TYPE_FORMULA))
{ {
setCellType(CELL_TYPE_STRING, false); setCellType(CELL_TYPE_STRING, false);
} }
@ -702,9 +702,9 @@ public class HSSFCell
setCellType(CELL_TYPE_BLANK,false); setCellType(CELL_TYPE_BLANK,false);
} else { } else {
setCellType(CELL_TYPE_FORMULA,false); setCellType(CELL_TYPE_FORMULA,false);
FormulaRecord rec = (FormulaRecord) record; FormulaRecordAggregate rec = (FormulaRecordAggregate) record;
rec.setOptions(( short ) 2); rec.getFormulaRecord().setOptions(( short ) 2);
rec.setValue(0); rec.getFormulaRecord().setValue(0);
rec.setXFIndex(( short ) 0x0f); rec.setXFIndex(( short ) 0x0f);
FormulaParser fp = new FormulaParser(formula+";",book); FormulaParser fp = new FormulaParser(formula+";",book);
fp.parse(); fp.parse();
@ -713,9 +713,9 @@ public class HSSFCell
//System.out.println("got Ptgs " + ptg.length); //System.out.println("got Ptgs " + ptg.length);
for (int k = 0; k < ptg.length; k++) { for (int k = 0; k < ptg.length; k++) {
size += ptg[ k ].getSize(); size += ptg[ k ].getSize();
rec.pushExpressionToken(ptg[ k ]); rec.getFormulaRecord().pushExpressionToken(ptg[ k ]);
} }
rec.setExpressionLength(( short ) size); rec.getFormulaRecord().setExpressionLength(( short ) size);
//Workbook.currentBook = null; //Workbook.currentBook = null;
} }
} }
@ -723,7 +723,7 @@ public class HSSFCell
public String getCellFormula() { public String getCellFormula() {
//Workbook.currentBook=book; //Workbook.currentBook=book;
SheetReferences refs = book.getSheetReferences(); SheetReferences refs = book.getSheetReferences();
String retval = FormulaParser.toFormulaString(refs, ((FormulaRecord)record).getParsedExpression()); String retval = FormulaParser.toFormulaString(refs, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());
//Workbook.currentBook=null; //Workbook.currentBook=null;
return retval; return retval;
} }
@ -825,8 +825,7 @@ public class HSSFCell
public void setCellValue(boolean value) public void setCellValue(boolean value)
{ {
if ((cellType != CELL_TYPE_BOOLEAN) if ((cellType != CELL_TYPE_BOOLEAN ) && ( cellType != CELL_TYPE_FORMULA))
&& (cellType != CELL_TYPE_FORMULA))
{ {
setCellType(CELL_TYPE_BOOLEAN, false); setCellType(CELL_TYPE_BOOLEAN, false);
} }

View File

@ -152,14 +152,14 @@ public class HSSFWorkbook
workbook = Workbook.createWorkbook(records); workbook = Workbook.createWorkbook(records);
setPropertiesFromWorkbook(workbook); setPropertiesFromWorkbook(workbook);
int numRecords = workbook.getNumRecords(); int recOffset = workbook.getNumRecords();
int sheetNum = 0; int sheetNum = 0;
while (numRecords < records.size()) while (recOffset < records.size())
{ {
Sheet sheet = Sheet.createSheet(records, sheetNum++, numRecords); Sheet sheet = Sheet.createSheet(records, sheetNum++, recOffset );
numRecords += sheet.getNumRecords(); recOffset = sheet.getEofLoc()+1;
sheet.convertLabelRecords( sheet.convertLabelRecords(
workbook); // convert all LabelRecord records to LabelSSTRecord workbook); // convert all LabelRecord records to LabelSSTRecord
HSSFSheet hsheet = new HSSFSheet(workbook, sheet); HSSFSheet hsheet = new HSSFSheet(workbook, sheet);
@ -511,8 +511,8 @@ public class HSSFWorkbook
{ {
totalsize = 4096; totalsize = 4096;
} }
byte[] retval = new byte[totalsize]; byte[] data = new byte[totalsize];
int pos = workbook.serialize(0, retval); int pos = workbook.serialize(0, data);
// System.arraycopy(wb, 0, retval, 0, wb.length); // System.arraycopy(wb, 0, retval, 0, wb.length);
for (int k = 0; k < sheets.size(); k++) for (int k = 0; k < sheets.size(); k++)
@ -521,13 +521,13 @@ public class HSSFWorkbook
// byte[] sb = (byte[])sheetbytes.get(k); // byte[] sb = (byte[])sheetbytes.get(k);
// System.arraycopy(sb, 0, retval, pos, sb.length); // System.arraycopy(sb, 0, retval, pos, sb.length);
pos += ((HSSFSheet) sheets.get(k)).getSheet().serialize(pos, pos += ((HSSFSheet) sheets.get(k)).getSheet().serialize(pos,
retval); // sb.length; data); // sb.length;
} }
for (int k = pos; k < totalsize; k++) for (int k = pos; k < totalsize; k++)
{ {
retval[k] = 0; data[k] = 0;
} }
return retval; return data;
} }
public int addSSTString(String string) public int addSSTString(String string)

View File

@ -386,7 +386,7 @@ public class LittleEndian
public static void putDouble(final byte[] data, final int offset, public static void putDouble(final byte[] data, final int offset,
final double value) { final double value) {
putNumber(data, offset, Double.doubleToLongBits(value), DOUBLE_SIZE); putNumber(data, offset, Double.doubleToRawLongBits(value), DOUBLE_SIZE);
} }

View File

@ -147,9 +147,13 @@ public class TestLittleEndian
{ {
56, 50, -113, -4, -63, -64, -13, 63, 76, -32, -42, -35, 60, -43, 3, 64 56, 50, -113, -4, -63, -64, -13, 63, 76, -32, -42, -35, 60, -43, 3, 64
}; };
private static final byte[] _nan_double_array =
{
(byte)0x00, (byte)0x00, (byte)0x3C, (byte)0x00, (byte)0x20, (byte)0x04, (byte)0xFF, (byte)0xFF
};
private static final double[] _doubles = private static final double[] _doubles =
{ {
1.23456, 2.47912 1.23456, 2.47912, Double.NaN
}; };
/** /**
@ -158,10 +162,21 @@ public class TestLittleEndian
public void testGetDouble() public void testGetDouble()
{ {
assertEquals(_doubles[ 0 ], LittleEndian.getDouble(_double_array), assertEquals(_doubles[ 0 ], LittleEndian.getDouble(_double_array), 0.000001 );
0.000001); assertEquals(_doubles[ 1 ], LittleEndian.getDouble( _double_array, LittleEndian.DOUBLE_SIZE), 0.000001);
assertEquals(_doubles[ 1 ], LittleEndian assertTrue(Double.isNaN(LittleEndian.getDouble(_nan_double_array)));
.getDouble(_double_array, LittleEndian.DOUBLE_SIZE), 0.000001);
// does not work. apparently nan does not always equal nan!
//assertEquals(_doubles[ 2 ], LittleEndian.getDouble(_nan_double_array), 0.000001);
double nan = LittleEndian.getDouble(_nan_double_array);
byte[] data = new byte[8];
LittleEndian.putDouble(data, nan);
for ( int i = 0; i < data.length; i++ )
{
byte b = data[i];
assertEquals(data[i], _nan_double_array[i]);
}
} }
/** /**