Removed last occurrences of storing Ptg arrays in Stacks. Some related clean-up.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@703100 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-10-09 08:33:54 +00:00
parent dbc713e083
commit d9075bc8dc
7 changed files with 35 additions and 140 deletions

View File

@ -1,52 +0,0 @@
/* ====================================================================
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;
public interface CustomField
extends Cloneable
{
/**
* @return The size of this field in bytes. This operation is not valid
* until after the call to <code>fillField()</code>
*/
int getSize();
/**
* Populates this fields data from the byte array passed in.
* @param in the RecordInputstream to read the record from
*/
int fillField(RecordInputStream in);
/**
* Appends the string representation of this field to the supplied
* StringBuffer.
*
* @param str The string buffer to append to.
*/
void toString(StringBuffer str);
/**
* Converts this field to it's byte array form.
* @param offset The offset into the byte array to start writing to.
* @param data The data array to write to.
* @return The number of bytes written.
*/
int serializeField(int offset, byte[] data);
}

View File

@ -17,8 +17,6 @@
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import java.util.Stack;
import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
@ -124,11 +122,7 @@ public final class ExternalNameRecord extends Record {
} }
private int getNameDefinitionSize() { private int getNameDefinitionSize() {
int result = 0; return Ptg.getEncodedSize(field_5_name_definition);
for (int i = 0; i < field_5_name_definition.length; i++) {
result += field_5_name_definition[i].getSize();
}
return result;
} }

View File

@ -20,47 +20,38 @@ package org.apache.poi.hssf.record;
import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import java.util.Stack;
import java.util.Iterator;
/** /**
* Not implemented yet. May commit it anyway just so people can see * Not implemented yet. May commit it anyway just so people can see
* where I'm heading. * where I'm heading.
* *
* @author Glen Stampoultzis (glens at apache.org) * @author Glen Stampoultzis (glens at apache.org)
*/ */
public final class LinkedDataFormulaField implements CustomField { public final class LinkedDataFormulaField {
Stack formulaTokens = new Stack(); private Ptg[] formulaTokens;
public int getSize() public int getSize()
{ {
int size = 0; return 2 + Ptg.getEncodedSize(formulaTokens);
for ( Iterator iterator = formulaTokens.iterator(); iterator.hasNext(); )
{
Ptg token = (Ptg) iterator.next();
size += token.getSize();
}
return size + 2;
} }
public int fillField( RecordInputStream in ) public int fillField( RecordInputStream in )
{ {
short tokenSize = in.readShort(); int tokenSize = in.readUShort();
formulaTokens = Ptg.createParsedExpressionTokens(tokenSize, in); formulaTokens = Ptg.readTokens(tokenSize, in);
return tokenSize + 2; return tokenSize + 2;
} }
public void toString( StringBuffer buffer ) public void toString( StringBuffer buffer )
{ {
for ( int k = 0; k < formulaTokens.size(); k++ ) for ( int k = 0; k < formulaTokens.length; k++ )
{ {
Ptg ptg = formulaTokens[k];
buffer.append( "Formula " ) buffer.append( "Formula " )
.append( k ) .append( k )
.append( "=" ) .append( "=" )
.append( formulaTokens.get( k ).toString() ) .append(ptg.toString() )
.append( "\n" ) .append( "\n" )
.append( ( (Ptg) formulaTokens.get( k ) ).toDebugString() ) .append(ptg.toDebugString() )
.append( "\n" ); .append( "\n" );
} }
} }
@ -75,34 +66,26 @@ public final class LinkedDataFormulaField implements CustomField {
public int serializeField( int offset, byte[] data ) public int serializeField( int offset, byte[] data )
{ {
int size = getSize(); int size = getSize();
LittleEndian.putShort(data, offset, (short)(size - 2)); LittleEndian.putUShort(data, offset, size - 2);
int pos = offset + 2; int pos = offset + 2;
pos += Ptg.serializePtgStack(formulaTokens, data, pos); pos += Ptg.serializePtgs(formulaTokens, data, pos);
return size; return size;
} }
public Object clone() public void setFormulaTokens(Ptg[] ptgs)
{ {
try this.formulaTokens = (Ptg[])ptgs.clone();
{
// todo: clone tokens? or are they immutable?
return super.clone();
}
catch ( CloneNotSupportedException e )
{
// should not happen
return null;
}
} }
public void setFormulaTokens( Stack formulaTokens ) public Ptg[] getFormulaTokens()
{ {
this.formulaTokens = (Stack) formulaTokens.clone(); return (Ptg[])this.formulaTokens.clone();
} }
public Stack getFormulaTokens() public LinkedDataFormulaField copy() {
{ LinkedDataFormulaField result = new LinkedDataFormulaField();
return (Stack)this.formulaTokens.clone();
}
result.formulaTokens = getFormulaTokens();
return result;
}
} }

View File

@ -125,7 +125,7 @@ public final class LinkedDataRecord extends Record {
rec.field_2_referenceType = field_2_referenceType; rec.field_2_referenceType = field_2_referenceType;
rec.field_3_options = field_3_options; rec.field_3_options = field_3_options;
rec.field_4_indexNumberFmtRecord = field_4_indexNumberFmtRecord; rec.field_4_indexNumberFmtRecord = field_4_indexNumberFmtRecord;
rec.field_5_formulaOfLink = ((LinkedDataFormulaField)field_5_formulaOfLink.clone()); rec.field_5_formulaOfLink = field_5_formulaOfLink.copy();
return rec; return rec;
} }

View File

@ -19,10 +19,8 @@ package org.apache.poi.hssf.record.formula;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Stack;
import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.record.RecordInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/** /**
* <tt>Ptg</tt> represents a syntactic token in a formula. 'PTG' is an acronym for * <tt>Ptg</tt> represents a syntactic token in a formula. 'PTG' is an acronym for
@ -49,15 +47,7 @@ public abstract class Ptg implements Cloneable {
* Extra data (beyond <tt>size</tt>) may be read if and <tt>ArrayPtg</tt>s are present. * Extra data (beyond <tt>size</tt>) may be read if and <tt>ArrayPtg</tt>s are present.
*/ */
public static Ptg[] readTokens(int size, RecordInputStream in) { public static Ptg[] readTokens(int size, RecordInputStream in) {
Stack temp = createParsedExpressionTokens((short)size, in); List temp = new ArrayList(4 + size / 2);
return toPtgArray(temp);
}
/**
* @deprecated - use readTokens()
*/
public static Stack createParsedExpressionTokens(short size, RecordInputStream in) {
Stack stack = new Stack();
int pos = 0; int pos = 0;
List arrayPtgs = null; List arrayPtgs = null;
while (pos < size) { while (pos < size) {
@ -71,7 +61,7 @@ public abstract class Ptg implements Cloneable {
} else { } else {
pos += ptg.getSize(); pos += ptg.getSize();
} }
stack.push( ptg ); temp.add( ptg );
} }
if(pos != size) { if(pos != size) {
throw new RuntimeException("Ptg array size mismatch"); throw new RuntimeException("Ptg array size mismatch");
@ -82,7 +72,7 @@ public abstract class Ptg implements Cloneable {
p.readTokenValues(in); p.readTokenValues(in);
} }
} }
return stack; return toPtgArray(temp);
} }
public static Ptg createPtg(RecordInputStream in) { public static Ptg createPtg(RecordInputStream in) {
@ -200,19 +190,11 @@ public abstract class Ptg implements Cloneable {
l.toArray(result); l.toArray(result);
return result; return result;
} }
private static Stack createStack(Ptg[] formulaTokens) {
Stack result = new Stack();
for (int i = 0; i < formulaTokens.length; i++) {
result.add(formulaTokens[i]);
}
return result;
}
/** /**
* This method will return the same result as {@link #getEncodedSizeWithoutArrayData(Ptg[])} * This method will return the same result as {@link #getEncodedSizeWithoutArrayData(Ptg[])}
* if there are no array tokens present. * if there are no array tokens present.
* @return the full size taken to encode the specified <tt>Ptg</tt>s * @return the full size taken to encode the specified <tt>Ptg</tt>s
*/ */
// TODO - several duplicates of this code should be refactored here
public static int getEncodedSize(Ptg[] ptgs) { public static int getEncodedSize(Ptg[] ptgs) {
int result = 0; int result = 0;
for (int i = 0; i < ptgs.length; i++) { for (int i = 0; i < ptgs.length; i++) {
@ -243,23 +225,14 @@ public abstract class Ptg implements Cloneable {
* The 2 byte encode length field is <b>not</b> written by this method. * The 2 byte encode length field is <b>not</b> written by this method.
* @return number of bytes written * @return number of bytes written
*/ */
public static int serializePtgs(Ptg[] ptgs, byte[] data, int offset) { public static int serializePtgs(Ptg[] ptgs, byte[] array, int offset) {
return serializePtgStack(createStack(ptgs), data, offset);
}
/**
* @deprecated use serializePtgs()
*/
public static int serializePtgStack(Stack expression, byte[] array, int offset) {
int pos = 0; int pos = 0;
int size = 0; int size = ptgs.length;
if (expression != null)
size = expression.size();
List arrayPtgs = null; List arrayPtgs = null;
for (int k = 0; k < size; k++) { for (int k = 0; k < size; k++) {
Ptg ptg = ( Ptg ) expression.get(k); Ptg ptg = ptgs[k];
ptg.writeBytes(array, pos + offset); ptg.writeBytes(array, pos + offset);
if (ptg instanceof ArrayPtg) { if (ptg instanceof ArrayPtg) {

View File

@ -19,9 +19,9 @@ package org.apache.poi.hssf.record;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.record.formula.Area3DPtg;
import java.util.Stack; import org.apache.poi.hssf.record.formula.Area3DPtg;
import org.apache.poi.hssf.record.formula.Ptg;
/** /**
* Tests the serialization and deserialization of the LinkedDataRecord * Tests the serialization and deserialization of the LinkedDataRecord
@ -167,7 +167,7 @@ recordid = 0x1051, size =8
Area3DPtg ptgExpected = new Area3DPtg(0, 7936, 0, 0, Area3DPtg ptgExpected = new Area3DPtg(0, 7936, 0, 0,
false, false, false, false, 0); false, false, false, false, 0);
Object ptgActual = record.getFormulaOfLink().getFormulaTokens().get(0); Object ptgActual = record.getFormulaOfLink().getFormulaTokens()[0];
assertEquals(ptgExpected.toString(), ptgActual.toString()); assertEquals(ptgExpected.toString(), ptgActual.toString());
assertEquals( data.length + 4, record.getRecordSize() ); assertEquals( data.length + 4, record.getRecordSize() );
@ -182,10 +182,8 @@ recordid = 0x1051, size =8
record.setIndexNumberFmtRecord( (short)0 ); record.setIndexNumberFmtRecord( (short)0 );
Area3DPtg ptg = new Area3DPtg(0, 7936, 0, 0, Area3DPtg ptg = new Area3DPtg(0, 7936, 0, 0,
false, false, false, false, 0); false, false, false, false, 0);
Stack s = new Stack();
s.push(ptg);
LinkedDataFormulaField formulaOfLink = new LinkedDataFormulaField(); LinkedDataFormulaField formulaOfLink = new LinkedDataFormulaField();
formulaOfLink.setFormulaTokens(s); formulaOfLink.setFormulaTokens(new Ptg[] { ptg, });
record.setFormulaOfLink(formulaOfLink ); record.setFormulaOfLink(formulaOfLink );
byte [] recordBytes = record.serialize(); byte [] recordBytes = record.serialize();

View File

@ -18,7 +18,6 @@
package org.apache.poi.hssf.record.formula; package org.apache.poi.hssf.record.formula;
import java.util.Arrays; import java.util.Arrays;
import java.util.Stack;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -95,9 +94,9 @@ public final class TestReferencePtg extends TestCase {
}; };
public void testReadWrite_tRefN_bug45091() { public void testReadWrite_tRefN_bug45091() {
TestcaseRecordInputStream in = new TestcaseRecordInputStream(-1, tRefN_data); TestcaseRecordInputStream in = new TestcaseRecordInputStream(-1, tRefN_data);
Stack ptgs = Ptg.createParsedExpressionTokens((short)tRefN_data.length, in); Ptg[] ptgs = Ptg.readTokens(tRefN_data.length, in);
byte[] outData = new byte[5]; byte[] outData = new byte[5];
Ptg.serializePtgStack(ptgs, outData, 0); Ptg.serializePtgs(ptgs, outData, 0);
if (outData[0] == 0x24) { if (outData[0] == 0x24) {
throw new AssertionFailedError("Identified bug 45091"); throw new AssertionFailedError("Identified bug 45091");
} }