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:
parent
dbc713e083
commit
d9075bc8dc
@ -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);
|
||||
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* <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.
|
||||
*/
|
||||
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 <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++) {
|
||||
@ -243,23 +225,14 @@ public abstract class Ptg implements Cloneable {
|
||||
* 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) {
|
||||
|
@ -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();
|
||||
|
@ -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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user