+++ /dev/null
-/* ====================================================================
- 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);
-
-
-}
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;
}
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);
}
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" );
}
}
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()
- {
- try
- {
- // todo: clone tokens? or are they immutable?
- return super.clone();
- }
- catch ( CloneNotSupportedException e )
- {
- // should not happen
- return null;
- }
- }
-
- public void setFormulaTokens( Stack formulaTokens )
+ public void setFormulaTokens(Ptg[] ptgs)
{
- this.formulaTokens = (Stack) formulaTokens.clone();
+ this.formulaTokens = (Ptg[])ptgs.clone();
}
- public Stack getFormulaTokens()
+ public Ptg[] getFormulaTokens()
{
- return (Stack)this.formulaTokens.clone();
+ return (Ptg[])this.formulaTokens.clone();
}
+ public LinkedDataFormulaField copy() {
+ LinkedDataFormulaField result = new LinkedDataFormulaField();
+
+ result.formulaTokens = getFormulaTokens();
+ return result;
+ }
}
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;
}
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;
/**
* <tt>Ptg</tt> represents a syntactic token in a formula. 'PTG' is an acronym for
* 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) {
- 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) {
} else {
pos += ptg.getSize();
}
- stack.push( ptg );
+ temp.add( ptg );
}
if(pos != size) {
throw new RuntimeException("Ptg array size mismatch");
p.readTokenValues(in);
}
}
- return stack;
+ return toPtgArray(temp);
}
public static Ptg createPtg(RecordInputStream in) {
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 <tt>Ptg</tt>s
*/
- // 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++) {
* The 2 byte encode length field is <b>not</b> 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) {
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
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() );
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();
package org.apache.poi.hssf.record.formula;
import java.util.Arrays;
-import java.util.Stack;
import junit.framework.AssertionFailedError;
import junit.framework.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");
}