went ahead and synced up. We'll get this up to spec shortly.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352442 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andrew C. Oliver 2002-04-18 12:00:53 +00:00
parent cdb7d382fe
commit 9460fa07a2
4 changed files with 176 additions and 5 deletions

View File

@ -157,7 +157,7 @@ public class FormulaRecord
int offset) int offset)
{ {
Stack stack = new Stack(); Stack stack = new Stack();
int pos = 22 + offset; /*int pos = 22 + offset;
while (pos < size) while (pos < size)
{ {
@ -165,7 +165,7 @@ public class FormulaRecord
pos += ptg.getSize(); pos += ptg.getSize();
stack.push(ptg); stack.push(ptg);
} }*/
return stack; return stack;
} }

View File

@ -71,6 +71,8 @@ public class AddPtg
{ {
public final static int SIZE = 1; public final static int SIZE = 1;
public final static byte sid = 0x03; public final static byte sid = 0x03;
private final static String ADD = "+";
/** Creates new AddPtg */ /** Creates new AddPtg */
@ -83,6 +85,10 @@ public class AddPtg
// doesn't need anything // doesn't need anything
} }
protected AddPtg(String formula, int offset) {
}
public void writeBytes(byte [] array, int offset) public void writeBytes(byte [] array, int offset)
{ {
@ -108,6 +114,21 @@ public class AddPtg
{ {
return "+"; return "+";
} }
public static boolean isNextStringToken(String formula, int pos) {
boolean retval = false;
while (pos < formula.length() && Character.isWhitespace(formula.charAt(pos))) {
pos++;
}
if (pos < formula.length()) {
if (formula.charAt(pos) == ADD.toCharArray()[0]) {
retval = true;
}
}
return retval;
}
public String toFormulaString(Ptg [] operands) public String toFormulaString(Ptg [] operands)
{ {
@ -118,4 +139,12 @@ public class AddPtg
buffer.append(operands[ 1 ].toFormulaString()); buffer.append(operands[ 1 ].toFormulaString());
return buffer.toString(); return buffer.toString();
} }
public int getPrecedence() {
return 5;
}
public int getStringLength() {
return 1;
}
} }

View File

@ -75,6 +75,8 @@ public class IntPtg
public final static byte sid = 0x1e; public final static byte sid = 0x1e;
private short field_1_value; private short field_1_value;
private String val;
private int strlen = 0;
/** Creates new IntPtg */ /** Creates new IntPtg */
public IntPtg() public IntPtg()
@ -85,6 +87,14 @@ public class IntPtg
{ {
setValue(LittleEndian.getShort(data, offset + 1)); setValue(LittleEndian.getShort(data, offset + 1));
} }
protected IntPtg(String formula, int offset) {
val = parseString(formula, offset);
if (val == null) throw new RuntimeException("WHOOAA there...thats got no int!");
strlen=val.length();
field_1_value = Short.parseShort(val);
}
public void setValue(short value) public void setValue(short value)
{ {
@ -111,4 +121,45 @@ public class IntPtg
{ {
return "" + getValue(); return "" + getValue();
} }
private static String parseString(String formula, int pos) {
String retval = null;
while (pos < formula.length() && Character.isWhitespace(formula.charAt(pos))) {
pos++;
}
if (pos < formula.length()) {
if (Character.isDigit(formula.charAt(pos)) ) {
int numpos = pos;
while (numpos < formula.length() && Character.isDigit(formula.charAt(numpos))){
numpos++;
}
if (numpos == formula.length() || formula.charAt(numpos) != '.') {
String numberstr = formula.substring(pos,numpos);
try {
int number = Short.parseShort(numberstr);
retval = numberstr;
} catch (NumberFormatException e) {
retval = null;
}
}
}
}
return retval;
}
public static boolean isNextStringToken(String formula, int pos) {
return (parseString(formula,pos) != null);
}
public int getPrecedence() {
return 5;
}
public int getStringLength() {
return strlen;
}
} }

View File

@ -60,6 +60,9 @@
*/ */
package org.apache.poi.hssf.record.formula; package org.apache.poi.hssf.record.formula;
import java.util.List;
import java.util.ArrayList;
/** /**
* *
* @author andy * @author andy
@ -69,12 +72,78 @@ public abstract class Ptg
{ {
/** Creates new Ptg */ /** Creates new Ptg */
public Ptg() public Ptg()
{ {
} }
public static Ptg[] parse(String formula, int offset) {
List ptgs = new ArrayList();
while (offset < formula.length()) {
Ptg ptg = getNextToken(formula,offset);
offset += ptg.getStringLength();
ptgs.add(ptg);
}
Ptg[] retval = new Ptg[ptgs.size()];
retval = (Ptg[])ptgs.toArray(retval);
return retval;
}
public static String ptgsToString(Ptg[] ptgs) {
StringBuffer res = new StringBuffer();
for (int k =0; k < ptgs.length; k++) {
Ptg ptg = ptgs[k];
res.append(ptg.toFormulaString());
}
return res.toString();
}
public static Ptg getNextToken(String formula, int offset) {
//later use array of reflected methods like RecordFactory
Ptg retval = null;
if (AddPtg.isNextStringToken(formula,offset)) {
retval = new AddPtg(formula,offset);
} else if (IntPtg.isNextStringToken(formula,offset)) {
retval = new IntPtg(formula,offset);
} else {
throw new RuntimeException("didn't parse "+formula+" at " +offset);
}
return retval;
}
/* private static List ptgsToList(Class [] ptgs)
{
List result = new ArrayList();
Constructor constructor;
public static Ptg createPtg(byte [] data, int offset) for (int i = 0; i < ptgs.length; i++)
{
Class ptg = null;
ptg = ptgs[ i ];
try
{
constructor = ptg.getConstructor(new Class[]
{
byte [].class, int.class
});
}
catch (Exception illegalArgumentException)
{
throw new RuntimeException(
"Now that didn't work nicely at all (couldn't do that there list of ptgs)");
}
result.add(constructor);
}
return result;
}*/
/*public static Ptg createPtg(byte [] data, int offset)
{ {
byte id = data[ offset + 0 ]; byte id = data[ offset + 0 ];
Ptg retval = null; Ptg retval = null;
@ -148,7 +217,7 @@ public abstract class Ptg
+ " (" + ( int ) id + ")"); + " (" + ( int ) id + ")");
} }
return retval; return retval;
} }*/
public abstract int getSize(); public abstract int getSize();
@ -164,4 +233,26 @@ public abstract class Ptg
public abstract void writeBytes(byte [] array, int offset); public abstract void writeBytes(byte [] array, int offset);
public abstract String toFormulaString(); public abstract String toFormulaString();
/**
* Ptg's should override this
*/
// public boolean isNextStringToken(String formula, int pos) {
// return false;
// }
public int getPrecedence() {
return 100;
}
public int getStringLength() {
return 0;
}
public static void main(String[] args) {
if (args != null && args[0] != null) {
System.out.println("Parsed Formula="+ ptgsToString(parse(args[0],0)));
}
}
} }