diff --git a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java index 15f363bd1..e01ede0fa 100644 --- a/src/java/org/apache/poi/hssf/dev/FormulaViewer.java +++ b/src/java/org/apache/poi/hssf/dev/FormulaViewer.java @@ -82,6 +82,7 @@ import org.apache.poi.hssf.usermodel.*; * FormulaViewer - finds formulas in a BIFF8 file and attempts to read them/display * data from them. Only works if Formulas are enabled in "RecordFactory" * @author andy + * @author Avik */ public class FormulaViewer @@ -132,40 +133,34 @@ public class FormulaViewer public void parseFormulaRecord(FormulaRecord record) { - System.out.println("In ParseFormula Record"); - System.out.println("row = " + record.getRow()); - System.out.println("col = " + record.getColumn()); + System.out.println("=============================="); + System.out.print("row = " + record.getRow()); + System.out.println(", col = " + record.getColumn()); System.out.println("value = " + record.getValue()); - System.out.println("xf = " + record.getXFIndex()); - System.out.println("number of ptgs = " + System.out.print("xf = " + record.getXFIndex()); + System.out.print(", number of ptgs = " + record.getNumberOfExpressionTokens()); - System.out.println("options = " + record.getOptions()); - System.out.println(composeForumla(record)); + System.out.println(", options = " + record.getOptions()); + System.out.println("RPN List = "+formulaString(record)); + System.out.println("Formula text = "+ composeForumla(record)); } - public String composeForumla(FormulaRecord record) - { + private String formulaString(FormulaRecord record) { StringBuffer formula = new StringBuffer("="); int numptgs = record.getNumberOfExpressionTokens(); - List ptgs = record.getParsedExpression(); - - for (int ptgnum = numptgs - 1; ptgnum > (-1); ptgnum--) - { - Ptg ptg = ( Ptg ) ptgs.get(ptgnum); - OperationPtg optg = ( OperationPtg ) ptg; - int numops = optg.getNumberOfOperands(); - Ptg[] ops = new Ptg[ numops ]; - int opoffset = 1; - - for (int opnum = ops.length - 1; opnum > -1; opnum--) - { - ops[ opnum ] = ( Ptg ) ptgs.get(ptgnum - opoffset); - opoffset++; - } - formula.append(optg.toFormulaString(ops)); - ptgnum -= ops.length; - } - return formula.toString(); + List tokens = record.getParsedExpression(); + StringBuffer buf = new StringBuffer(); + for (int i=0;i0) { diff --git a/src/java/org/apache/poi/hssf/record/formula/IntPtg.java b/src/java/org/apache/poi/hssf/record/formula/IntPtg.java index f673ec293..e23ef4f7e 100644 --- a/src/java/org/apache/poi/hssf/record/formula/IntPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/IntPtg.java @@ -77,23 +77,12 @@ public class IntPtg private String val; private int strlen = 0; - /** Creates new IntPtg */ - - public IntPtg() - { - } - + public IntPtg(byte [] data, int offset) { 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); - } // IntPtg should be able to create itself, shouldnt have to call setValue protected IntPtg(String formulaToken) { @@ -126,37 +115,4 @@ public class IntPtg 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 int getStringLength() { - return strlen; - } } diff --git a/src/java/org/apache/poi/hssf/record/formula/MultiplyPtg.java b/src/java/org/apache/poi/hssf/record/formula/MultiplyPtg.java index 32be31d7f..9120b1244 100644 --- a/src/java/org/apache/poi/hssf/record/formula/MultiplyPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/MultiplyPtg.java @@ -77,7 +77,7 @@ public class MultiplyPtg /** Creates new AddPtg */ - public MultiplyPtg() + protected MultiplyPtg() { } @@ -87,11 +87,6 @@ public class MultiplyPtg // doesn't need anything } - protected MultiplyPtg(String formula, int offset) { - - } - - public void writeBytes(byte [] array, int offset) { array[ offset + 0 ] = sid; @@ -136,7 +131,7 @@ public class MultiplyPtg StringBuffer buffer = new StringBuffer(); buffer.append(operands[ 0 ]); - buffer.append("*"); + buffer.append(toFormulaString()); buffer.append(operands[ 1 ]); return buffer.toString(); } diff --git a/src/java/org/apache/poi/hssf/record/formula/NamePtg.java b/src/java/org/apache/poi/hssf/record/formula/NamePtg.java index fd6a1c922..727c9cc27 100644 --- a/src/java/org/apache/poi/hssf/record/formula/NamePtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/NamePtg.java @@ -78,8 +78,9 @@ public class NamePtg /** Creates new NamePtg */ - public NamePtg() + protected NamePtg(String name) { + //TODO } /** Creates new NamePtg */ diff --git a/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java b/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java index 6d20d90d4..d790f22d3 100644 --- a/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java @@ -57,8 +57,9 @@ package org.apache.poi.hssf.record.formula; import org.apache.poi.util.LittleEndian; /** - * Integer (short intger) - * Stores a (java) short value in a formula + * Number + * Stores a floating point value in a formula + * value stored in a 8 byte field using IEEE notation * @author Avik Sengupta */ @@ -69,25 +70,29 @@ public class NumberPtg public final static byte sid = 0x1f; private double field_1_value; - /** Creates new NumberPtg */ - - public NumberPtg() - { - } - + + /** Create a NumberPtg from a byte array read from disk */ public NumberPtg(byte [] data, int offset) { setValue(LittleEndian.getDouble(data, offset + 1)); } + /** Create a NumberPtg from a string representation of the number + * Number format is not checked, it is expected to be validated in the parser + * that calls this method. + * @param value : String representation of a floating point number + */ protected NumberPtg(String value) { setValue(Double.parseDouble(value)); } + + public void setValue(double value) { field_1_value = value; } - + + public double getValue() { return field_1_value; @@ -108,11 +113,6 @@ public class NumberPtg { return "" + getValue(); } - - - //TODO: do we really need this method?? - public int getStringLength() { - return 1; - } + } diff --git a/src/java/org/apache/poi/hssf/record/formula/OperationPtg.java b/src/java/org/apache/poi/hssf/record/formula/OperationPtg.java index 294d5d10f..ac9a61556 100644 --- a/src/java/org/apache/poi/hssf/record/formula/OperationPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/OperationPtg.java @@ -75,11 +75,17 @@ public abstract class OperationPtg extends Ptg public abstract int getType(); + /** + * returns a string representation of the operations + * the length of the input array should equal the number returned by + * @see getNumberOfOperands + * + */ public abstract String toFormulaString(String[] operands); + /** + * The number of operands expected by the operations + */ public abstract int getNumberOfOperands(); - - public abstract String toFormulaString(Ptg [] operands); - } diff --git a/src/java/org/apache/poi/hssf/record/formula/ParenthesisPtg.java b/src/java/org/apache/poi/hssf/record/formula/ParenthesisPtg.java index cc32670b8..0e9f901f4 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ParenthesisPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ParenthesisPtg.java @@ -72,7 +72,7 @@ public class ParenthesisPtg private final static int SIZE = 1; public final static byte sid = 0x15; - public ParenthesisPtg() + protected ParenthesisPtg() { } @@ -82,10 +82,7 @@ public class ParenthesisPtg // doesn't need anything } - protected ParenthesisPtg(String formula, int offset) { - - } - + public void writeBytes(byte [] array, int offset) { @@ -112,11 +109,7 @@ public class ParenthesisPtg return "()"; } - public String toFormulaString(Ptg [] operands) - { - return ""; - } - + public String toFormulaString(String[] operands) { return "("+operands[0]+")"; } diff --git a/src/java/org/apache/poi/hssf/record/formula/PowerPtg.java b/src/java/org/apache/poi/hssf/record/formula/PowerPtg.java index 99660175f..e853b1c49 100644 --- a/src/java/org/apache/poi/hssf/record/formula/PowerPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/PowerPtg.java @@ -75,7 +75,7 @@ public class PowerPtg /** Creates new AddPtg */ - public PowerPtg() + protected PowerPtg() { } @@ -110,24 +110,14 @@ public class PowerPtg return "^"; } - public String toFormulaString(Ptg [] operands) - { - StringBuffer buffer = new StringBuffer(); - - buffer.append(operands[ 0 ].toFormulaString()); - buffer.append("^"); - buffer.append(operands[ 1 ].toFormulaString()); - - return buffer.toString(); - } - + public String toFormulaString(String[] operands) { StringBuffer buffer = new StringBuffer(); buffer.append(operands[ 0 ]); - buffer.append("^"); + buffer.append(toFormulaString()); buffer.append(operands[ 1 ]); return buffer.toString(); } 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 330b228a2..66a58e1fb 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Ptg.java +++ b/src/java/org/apache/poi/hssf/record/formula/Ptg.java @@ -77,7 +77,7 @@ public abstract class Ptg * @param infixPtgs List of ptgs in infix order */ - /* DO NOI REMOVE + /* DO NOT REMOVE *we keep this method in case we wish to change the way we parse *It needs a getPrecedence in OperationsPtg @@ -287,15 +287,13 @@ public abstract class Ptg writeBytes(bytes, 0); return bytes; } - + /** write this Ptg to a byte array*/ public abstract void writeBytes(byte [] array, int offset); - - public abstract String toFormulaString(); - - public int getStringLength() { - return 0; - } + /** + * return a string representation of this token alone + */ + public abstract String toFormulaString(); /** * dump a debug representation (hexdump) to a strnig */ diff --git a/src/java/org/apache/poi/hssf/record/formula/ReferencePtg.java b/src/java/org/apache/poi/hssf/record/formula/ReferencePtg.java index 47c353c73..4a154bae7 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ReferencePtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ReferencePtg.java @@ -82,17 +82,13 @@ public class ReferencePtg extends Ptg private BitField rowRelative = new BitField(0x8000); private BitField colRelative = new BitField(0x4000); - /** Creates new ValueReferencePtg */ - - public ReferencePtg() - { - } + /** * Takes in a String represnetation of a cell reference and fills out the * numeric fields. */ - public ReferencePtg(String cellref) { + protected ReferencePtg(String cellref) { int[] xy = ReferenceUtil.getXYFromReference(cellref); setRow((short)xy[0]); setColumn((short)xy[1]); diff --git a/src/java/org/apache/poi/hssf/record/formula/SubtractPtg.java b/src/java/org/apache/poi/hssf/record/formula/SubtractPtg.java index 0053da25e..0d1f81921 100644 --- a/src/java/org/apache/poi/hssf/record/formula/SubtractPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/SubtractPtg.java @@ -73,9 +73,7 @@ public class SubtractPtg public final static int SIZE = 1; public final static byte sid = 0x04; - /** Creates new AddPtg */ - - public SubtractPtg() + protected SubtractPtg() { } @@ -110,21 +108,7 @@ public class SubtractPtg return "-"; } - public String toFormulaString(Ptg [] operands) - { - StringBuffer buffer = new StringBuffer(); - - buffer.append(operands[ 0 ].toFormulaString()); - buffer.append("-"); - buffer.append(operands[ 1 ].toFormulaString()); - return buffer.toString(); - } - - - public int getStringLength() { - return 1; - } - + public String toFormulaString(String[] operands) { StringBuffer buffer = new StringBuffer(); diff --git a/src/java/org/apache/poi/hssf/record/formula/ValueVariableFunctionPtg.java b/src/java/org/apache/poi/hssf/record/formula/ValueVariableFunctionPtg.java index fae7990d1..f652485ef 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ValueVariableFunctionPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ValueVariableFunctionPtg.java @@ -124,24 +124,13 @@ public class ValueVariableFunctionPtg return "NO IDEA YET VALUE VARIABLE"; } - public String toFormulaString(Ptg [] operands) - { - return toFormulaString(); - } - - + public String toFormulaString(String[] operands) { return toFormulaString(); } - public void manipulate(List source, List results, int pos) { - } - - public int getPrecedence() { - return 1; - } - + }