diff --git a/src/java/org/apache/poi/hssf/record/CustomField.java b/src/java/org/apache/poi/hssf/record/CustomField.java
deleted file mode 100644
index 474d3b2b4..000000000
--- a/src/java/org/apache/poi/hssf/record/CustomField.java
+++ /dev/null
@@ -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 fillField()
- */
- 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);
-
-
-}
diff --git a/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java b/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java
index d2135c085..4d7f312bd 100755
--- a/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java
+++ b/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java
@@ -17,8 +17,6 @@
package org.apache.poi.hssf.record;
-import java.util.Stack;
-
import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil;
@@ -124,11 +122,7 @@ public final class ExternalNameRecord extends Record {
}
private int getNameDefinitionSize() {
- int result = 0;
- for (int i = 0; i < field_5_name_definition.length; i++) {
- result += field_5_name_definition[i].getSize();
- }
- return result;
+ return Ptg.getEncodedSize(field_5_name_definition);
}
diff --git a/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java b/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java
index 9e82adec6..4a607c9e3 100644
--- a/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java
+++ b/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java
@@ -20,47 +20,38 @@ package org.apache.poi.hssf.record;
import org.apache.poi.hssf.record.formula.Ptg;
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
* where I'm heading.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
-public final class LinkedDataFormulaField implements CustomField {
- Stack formulaTokens = new Stack();
+public final class LinkedDataFormulaField {
+ private Ptg[] formulaTokens;
public int getSize()
{
- int size = 0;
- for ( Iterator iterator = formulaTokens.iterator(); iterator.hasNext(); )
- {
- Ptg token = (Ptg) iterator.next();
- size += token.getSize();
- }
- return size + 2;
+ return 2 + Ptg.getEncodedSize(formulaTokens);
}
public int fillField( RecordInputStream in )
{
- short tokenSize = in.readShort();
- formulaTokens = Ptg.createParsedExpressionTokens(tokenSize, in);
-
+ int tokenSize = in.readUShort();
+ formulaTokens = Ptg.readTokens(tokenSize, in);
return tokenSize + 2;
}
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 " )
.append( k )
.append( "=" )
- .append( formulaTokens.get( k ).toString() )
+ .append(ptg.toString() )
.append( "\n" )
- .append( ( (Ptg) formulaTokens.get( k ) ).toDebugString() )
+ .append(ptg.toDebugString() )
.append( "\n" );
}
}
@@ -75,34 +66,26 @@ public final class LinkedDataFormulaField implements CustomField {
public int serializeField( int offset, byte[] data )
{
int size = getSize();
- LittleEndian.putShort(data, offset, (short)(size - 2));
+ LittleEndian.putUShort(data, offset, size - 2);
int pos = offset + 2;
- pos += Ptg.serializePtgStack(formulaTokens, data, pos);
+ pos += Ptg.serializePtgs(formulaTokens, data, pos);
return size;
}
- public Object clone()
+ public void setFormulaTokens(Ptg[] ptgs)
{
- try
- {
- // todo: clone tokens? or are they immutable?
- return super.clone();
- }
- catch ( CloneNotSupportedException e )
- {
- // should not happen
- return null;
- }
+ this.formulaTokens = (Ptg[])ptgs.clone();
}
- public void setFormulaTokens( Stack formulaTokens )
+ public Ptg[] getFormulaTokens()
{
- this.formulaTokens = (Stack) formulaTokens.clone();
- }
-
- public Stack getFormulaTokens()
- {
- return (Stack)this.formulaTokens.clone();
+ return (Ptg[])this.formulaTokens.clone();
}
+ public LinkedDataFormulaField copy() {
+ LinkedDataFormulaField result = new LinkedDataFormulaField();
+
+ result.formulaTokens = getFormulaTokens();
+ return result;
+ }
}
diff --git a/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java b/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java
index 46610ec2b..543418c35 100644
--- a/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java
+++ b/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java
@@ -125,7 +125,7 @@ public final class LinkedDataRecord extends Record {
rec.field_2_referenceType = field_2_referenceType;
rec.field_3_options = field_3_options;
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;
}
diff --git a/src/java/org/apache/poi/hssf/record/formula/Ptg.java b/src/java/org/apache/poi/hssf/record/formula/Ptg.java
index 4b952b65f..55fac1b0b 100644
--- a/src/java/org/apache/poi/hssf/record/formula/Ptg.java
+++ b/src/java/org/apache/poi/hssf/record/formula/Ptg.java
@@ -19,10 +19,8 @@ package org.apache.poi.hssf.record.formula;
import java.util.ArrayList;
import java.util.List;
-import java.util.Stack;
import org.apache.poi.hssf.record.RecordInputStream;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Ptg 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 size) may be read if and ArrayPtgs are present.
*/
public static Ptg[] readTokens(int size, RecordInputStream in) {
- Stack temp = createParsedExpressionTokens((short)size, in);
- return toPtgArray(temp);
- }
-
- /**
- * @deprecated - use readTokens()
- */
- public static Stack createParsedExpressionTokens(short size, RecordInputStream in) {
- Stack stack = new Stack();
+ List temp = new ArrayList(4 + size / 2);
int pos = 0;
List arrayPtgs = null;
while (pos < size) {
@@ -71,7 +61,7 @@ public abstract class Ptg implements Cloneable {
} else {
pos += ptg.getSize();
}
- stack.push( ptg );
+ temp.add( ptg );
}
if(pos != size) {
throw new RuntimeException("Ptg array size mismatch");
@@ -82,7 +72,7 @@ public abstract class Ptg implements Cloneable {
p.readTokenValues(in);
}
}
- return stack;
+ return toPtgArray(temp);
}
public static Ptg createPtg(RecordInputStream in) {
@@ -200,19 +190,11 @@ public abstract class Ptg implements Cloneable {
l.toArray(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[])}
* if there are no array tokens present.
* @return the full size taken to encode the specified Ptgs
*/
- // TODO - several duplicates of this code should be refactored here
public static int getEncodedSize(Ptg[] ptgs) {
int result = 0;
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 not written by this method.
* @return number of bytes written
*/
- public static int serializePtgs(Ptg[] ptgs, byte[] data, int offset) {
- return serializePtgStack(createStack(ptgs), data, offset);
- }
-
- /**
- * @deprecated use serializePtgs()
- */
- public static int serializePtgStack(Stack expression, byte[] array, int offset) {
+ public static int serializePtgs(Ptg[] ptgs, byte[] array, int offset) {
int pos = 0;
- int size = 0;
- if (expression != null)
- size = expression.size();
+ int size = ptgs.length;
List arrayPtgs = null;
for (int k = 0; k < size; k++) {
- Ptg ptg = ( Ptg ) expression.get(k);
+ Ptg ptg = ptgs[k];
ptg.writeBytes(array, pos + offset);
if (ptg instanceof ArrayPtg) {
diff --git a/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java b/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java
index 641f1e8c5..2527a6c5b 100644
--- a/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java
+++ b/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java
@@ -19,9 +19,9 @@ package org.apache.poi.hssf.record;
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
@@ -167,7 +167,7 @@ recordid = 0x1051, size =8
Area3DPtg ptgExpected = new Area3DPtg(0, 7936, 0, 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( data.length + 4, record.getRecordSize() );
@@ -182,10 +182,8 @@ recordid = 0x1051, size =8
record.setIndexNumberFmtRecord( (short)0 );
Area3DPtg ptg = new Area3DPtg(0, 7936, 0, 0,
false, false, false, false, 0);
- Stack s = new Stack();
- s.push(ptg);
LinkedDataFormulaField formulaOfLink = new LinkedDataFormulaField();
- formulaOfLink.setFormulaTokens(s);
+ formulaOfLink.setFormulaTokens(new Ptg[] { ptg, });
record.setFormulaOfLink(formulaOfLink );
byte [] recordBytes = record.serialize();
diff --git a/src/testcases/org/apache/poi/hssf/record/formula/TestReferencePtg.java b/src/testcases/org/apache/poi/hssf/record/formula/TestReferencePtg.java
index b50bb76b4..a94552ef9 100644
--- a/src/testcases/org/apache/poi/hssf/record/formula/TestReferencePtg.java
+++ b/src/testcases/org/apache/poi/hssf/record/formula/TestReferencePtg.java
@@ -18,7 +18,6 @@
package org.apache.poi.hssf.record.formula;
import java.util.Arrays;
-import java.util.Stack;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
@@ -95,9 +94,9 @@ public final class TestReferencePtg extends TestCase {
};
public void testReadWrite_tRefN_bug45091() {
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];
- Ptg.serializePtgStack(ptgs, outData, 0);
+ Ptg.serializePtgs(ptgs, outData, 0);
if (outData[0] == 0x24) {
throw new AssertionFailedError("Identified bug 45091");
}