private String composeFormula(FormulaRecord record)
{
- return FormulaParser.toFormulaString((SheetReferences)null,record.getParsedExpression());
+ return org.apache.poi.hssf.model.FormulaParser.toFormulaString((SheetReferences)null,record.getParsedExpression());
}
/**
import java.util.Iterator;
import java.util.List;
+import java.util.ArrayList;
/**
* Title: Static String Table Record
{
SSTSerializer serializer = new SSTSerializer(
_record_lengths, field_3_strings, getNumStrings(), getNumUniqueStrings() );
- return serializer.serialize( offset, data );
+ return serializer.serialize( getRecordSize(), offset, data );
}
- // we can probably simplify this later...this calculates the size
- // w/o serializing but still is a bit slow
public int getRecordSize()
{
- SSTSerializer serializer = new SSTSerializer(
- _record_lengths, field_3_strings, getNumStrings(), getNumUniqueStrings() );
-
- return serializer.getRecordSize();
+ SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(field_3_strings);
+ int recordSize = calculator.getRecordSize();
+ _record_lengths = calculator.getRecordLengths();
+ return recordSize;
}
SSTDeserializer getDeserializer()
import org.apache.poi.util.LittleEndianConsts;
import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
/**
* Used to calculate the record sizes for a particular record.
*/
class SSTRecordSizeCalculator
{
- private SSTSerializer serializer;
-
private UnicodeString unistr = null;
private int stringReminant = 0;
private int unipos = 0;
private boolean firstRecord = true;
private int totalWritten = 0;
private int recordSize = 0;
+ private List recordLengths = new ArrayList();
private int pos = 0;
+ private Map strings;
- public SSTRecordSizeCalculator(SSTSerializer serializer)
+ public SSTRecordSizeCalculator(Map strings)
{
- this.serializer = serializer;
+ this.strings = strings;
}
/**
{
initVars();
- serializer.recordLengths = new ArrayList();
- int retval = 0;
- int totalStringSpaceRequired = serializer.calculateUnicodeSize();
+ int retval;
+ int totalStringSpaceRequired = SSTSerializer.calculateUnicodeSize(strings);
if ( totalStringSpaceRequired > SSTRecord.MAX_DATA_SPACE )
{
{
// short data: write one simple SST record
retval = SSTRecord.SST_RECORD_OVERHEAD + totalStringSpaceRequired;
- serializer.recordLengths.add( new Integer( totalStringSpaceRequired ) );
+ recordLengths.add( new Integer( totalStringSpaceRequired ) );
}
return retval;
}
+ public List getRecordLengths()
+ {
+ return recordLengths;
+ }
+
private int sizeOverContinuation( int totalStringSpaceRequired )
{
int retval;
finished = true;
}
recordSize = size + SSTRecord.STD_RECORD_OVERHEAD;
- serializer.recordLengths.add( new Integer( size ) );
+ recordLengths.add( new Integer( size ) );
pos = 4;
}
if ( isRemainingString )
recordSize = SSTRecord.MAX_RECORD_SIZE;
pos = 12;
firstRecord = false;
- serializer.recordLengths.add( new Integer( recordSize - SSTRecord.STD_RECORD_OVERHEAD ) );
+ recordLengths.add( new Integer( recordSize - SSTRecord.STD_RECORD_OVERHEAD ) );
}
private void calcRemainingStrings()
{
- for ( ; unipos < serializer.strings.size(); unipos++ )
+ for ( ; unipos < strings.size(); unipos++ )
{
int available = SSTRecord.MAX_RECORD_SIZE - pos;
Integer intunipos = new Integer( unipos );
- unistr = ( (UnicodeString) serializer.strings.get( intunipos ) );
+ unistr = ( (UnicodeString) strings.get( intunipos ) );
if ( unistr.getRecordSize() <= available )
{
totalBytesWritten += unistr.getRecordSize();
int shortrecord = recordSize
- ( available - toBeWritten );
- serializer.recordLengths.set(
- serializer.recordLengths.size() - 1,
+ recordLengths.set(
+ recordLengths.size() - 1,
new Integer(
shortrecord - SSTRecord.STD_RECORD_OVERHEAD ) );
recordSize = shortrecord;
{
int shortrecord = recordSize - available;
- serializer.recordLengths.set( serializer.recordLengths.size() - 1,
+ recordLengths.set( recordLengths.size() - 1,
new Integer( shortrecord - SSTRecord.STD_RECORD_OVERHEAD ) );
recordSize = shortrecord;
}
if ( available != toBeWritten )
{
int shortrecord = recordSize - ( available - toBeWritten );
- serializer.recordLengths.set( serializer.recordLengths.size() - 1,
+ recordLengths.set( recordLengths.size() - 1,
new Integer( shortrecord - SSTRecord.STD_RECORD_OVERHEAD ) );
recordSize = shortrecord;
}
import org.apache.poi.util.BinaryTree;
import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
/**
* This class handles serialization of SST records. It utilizes the record processor
{
// todo: make private again
- List recordLengths;
- BinaryTree strings;
+ private List recordLengths;
+ private BinaryTree strings;
private int numStrings;
private int numUniqueStrings;
*
* @return the byte array
*/
- public int serialize( int offset, byte[] data )
+ public int serialize( int record_size, int offset, byte[] data )
{
- int record_size = getRecordSize();
int record_length_index = 0;
if ( calculateUnicodeSize() > SSTRecord.MAX_DATA_SPACE )
- // todo: make private again
/**
* Calculates the total unicode size for all the strings.
*
* @return the total size.
*/
- int calculateUnicodeSize()
+ public static int calculateUnicodeSize(Map strings)
{
int retval = 0;
for ( int k = 0; k < strings.size(); k++ )
{
- retval += getUnicodeString( k ).getRecordSize();
+ retval += getUnicodeString( strings, k ).getRecordSize();
}
return retval;
}
+ public int calculateUnicodeSize()
+ {
+ return calculateUnicodeSize(strings);
+ }
+
/**
* This case is chosen when an SST record does not span over to a continue record.
*
*/
private void serializeSingleSSTRecord( byte[] data, int offset, int record_length_index )
{
- int len = ( (Integer) recordLengths.get( record_length_index++ ) ).intValue();
+ int len = ( (Integer) recordLengths.get( record_length_index ) ).intValue();
int recordSize = SSTRecord.SST_RECORD_OVERHEAD + len - SSTRecord.STD_RECORD_OVERHEAD;
sstRecordHeader.writeSSTHeader( data, 0 + offset, recordSize );
int pos = SSTRecord.SST_RECORD_OVERHEAD;
}
private UnicodeString getUnicodeString( int index )
+ {
+ return getUnicodeString(strings, index);
+ }
+
+ private static UnicodeString getUnicodeString( Map strings, int index )
{
Integer intunipos = new Integer( index );
return ( (UnicodeString) strings.get( intunipos ) );
public int getRecordSize()
{
- return new SSTRecordSizeCalculator(this).getRecordSize();
+ SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings);
+ int recordSize = calculator.getRecordSize();
+ recordLengths = calculator.getRecordLengths();
+ return recordSize;
}
+ public List getRecordLengths()
+ {
+ return recordLengths;
+ }
}
{
private static final String SMALL_STRING = "Small string";
private static final int COMPRESSED_PLAIN_STRING_OVERHEAD = 3;
- private List recordLengths;
+// private List recordLengths;
private BinaryTree strings;
private static final int OPTION_FIELD_SIZE = 1;
throws Exception
{
strings.put(new Integer(0), makeUnicodeString(SMALL_STRING));
- SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(new SSTSerializer(recordLengths, strings, 1, 1));
- assertEquals(SSTRecord.SST_RECORD_OVERHEAD + COMPRESSED_PLAIN_STRING_OVERHEAD + SMALL_STRING.length(), calculator.getRecordSize());
+ SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings);
+ assertEquals(SSTRecord.SST_RECORD_OVERHEAD + COMPRESSED_PLAIN_STRING_OVERHEAD + SMALL_STRING.length(),
+ calculator.getRecordSize());
}
public void testBigStringAcrossUnicode()
{
String bigString = new String(new char[SSTRecord.MAX_DATA_SPACE + 100]);
strings.put(new Integer(0), makeUnicodeString(bigString));
- SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(new SSTSerializer(recordLengths, strings, 1, 1));
+ SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings);
assertEquals(SSTRecord.SST_RECORD_OVERHEAD
+ COMPRESSED_PLAIN_STRING_OVERHEAD
+ SSTRecord.MAX_DATA_SPACE
{
String perfectFit = new String(new char[SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD]);
strings.put(new Integer(0), makeUnicodeString(perfectFit));
- SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(new SSTSerializer(recordLengths, strings, 1, 1));
+ SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings);
assertEquals(SSTRecord.SST_RECORD_OVERHEAD
+ COMPRESSED_PLAIN_STRING_OVERHEAD
+ perfectFit.length(),
calculator.getRecordSize());
}
- /**
- * Test the case where it's too big by one.
- */
public void testJustOversized()
throws Exception
{
String tooBig = new String(new char[SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD + 1]);
strings.put(new Integer(0), makeUnicodeString(tooBig));
- SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(new SSTSerializer(recordLengths, strings, 1, 1));
+ SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings);
assertEquals(SSTRecord.SST_RECORD_OVERHEAD
+ COMPRESSED_PLAIN_STRING_OVERHEAD
+ tooBig.length() - 1
- // continued
+ // continue record
+ SSTRecord.STD_RECORD_OVERHEAD
+ OPTION_FIELD_SIZE
- + 1 // 1 char continued
- ,
+ + 1,
calculator.getRecordSize());
}
String perfectFit = new String(new char[SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD]);
strings.put(new Integer(0), makeUnicodeString(perfectFit));
strings.put(new Integer(1), makeUnicodeString(SMALL_STRING));
- SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(new SSTSerializer(recordLengths, strings, 1, 1));
+ SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings);
assertEquals(SSTRecord.SST_RECORD_OVERHEAD
+ SSTRecord.MAX_DATA_SPACE
// second string
strings.put(new Integer(0), makeUnicodeString(almostPerfectFit));
String oneCharString = new String(new char[1]);
strings.put(new Integer(1), makeUnicodeString(oneCharString));
- SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(new SSTSerializer(recordLengths, strings, 1, 1));
+ SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings);
assertEquals(SSTRecord.SST_RECORD_OVERHEAD
+ COMPRESSED_PLAIN_STRING_OVERHEAD
+ almostPerfectFit.length()
public void setUp()
{
- recordLengths = new ArrayList();
strings = new BinaryTree();
}