| Method from org.apache.poi.hssf.model.FormulaParser Detail: |
public Ptg[] getRPNPtg() {
return getRPNPtg(FORMULA_TYPE_CELL);
}
API call to retrive the array of Ptgs created as
a result of the parsing |
public Ptg[] getRPNPtg(int formulaType) {
OperandClassTransformer oct = new OperandClassTransformer(formulaType);
// RVA is for 'operand class': 'reference', 'value', 'array'
oct.transformFormula(_rootNode);
return ParseNode.toTokenArray(_rootNode);
}
|
public void parse() {
pointer=0;
GetChar();
_rootNode = comparisonExpression();
if(pointer < = formulaLength) {
String msg = "Unused input [" + formulaString.substring(pointer-1)
+ "] after attempting to parse the formula [" + formulaString + "]";
throw new FormulaParseException(msg);
}
} Deprecated! use - Ptg[] FormulaParser.parse(String, HSSFWorkbook) directly
API call to execute the parsing of the formula |
public static Ptg[] parse(String formula,
HSSFWorkbook book) {
FormulaParser fp = new FormulaParser(formula, book);
fp.parse();
return fp.getRPNPtg();
}
|
public String toFormulaString(List lptgs) {
return toFormulaString(book, lptgs);
}
Convenience method which takes in a list then passes it to the
other toFormulaString signature. Works on the current
workbook for 3D and named references |
public String toFormulaString(Ptg[] ptgs) {
return toFormulaString(book, ptgs);
}
Static method to convert an array of Ptgs in RPN order
to a human readable string format in infix mode. Works
on the current workbook for named and 3D references. |
public static String toFormulaString(HSSFWorkbook book,
List lptgs) {
String retval = null;
if (lptgs == null || lptgs.size() == 0) return "#NAME";
Ptg[] ptgs = new Ptg[lptgs.size()];
ptgs = (Ptg[])lptgs.toArray(ptgs);
retval = toFormulaString(book, ptgs);
return retval;
}
Convenience method which takes in a list then passes it to the
other toFormulaString signature. |
public static String toFormulaString(HSSFWorkbook book,
Ptg[] ptgs) {
if (ptgs == null || ptgs.length == 0) {
// TODO - what is the justification for returning "#NAME" (which is not "#NAME?", btw)
return "#NAME";
}
Stack stack = new Stack();
for (int i=0 ; i < ptgs.length; i++) {
Ptg ptg = ptgs[i];
// TODO - what about MemNoMemPtg?
if(ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) {
// marks the start of a list of area expressions which will be naturally combined
// by their trailing operators (e.g. UnionPtg)
// TODO - put comment and throw exception in toFormulaString() of these classes
continue;
}
if (ptg instanceof ParenthesisPtg) {
String contents = (String)stack.pop();
stack.push ("(" + contents + ")");
continue;
}
if (ptg instanceof AttrPtg) {
AttrPtg attrPtg = ((AttrPtg) ptg);
if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isGoto()) {
continue;
}
if (attrPtg.isSpace()) {
// POI currently doesn't render spaces in formulas
continue;
// but if it ever did, care must be taken:
// tAttrSpace comes *before* the operand it applies to, which may be consistent
// with how the formula text appears but is against the RPN ordering assumed here
}
if (attrPtg.isSemiVolatile()) {
// similar to tAttrSpace - RPN is violated
continue;
}
if (attrPtg.isSum()) {
String[] operands = getOperands(stack, attrPtg.getNumberOfOperands());
stack.push(attrPtg.toFormulaString(operands));
continue;
}
throw new RuntimeException("Unexpected tAttr: " + attrPtg.toString());
}
if (! (ptg instanceof OperationPtg)) {
stack.push(ptg.toFormulaString(book));
continue;
}
OperationPtg o = (OperationPtg) ptg;
String[] operands = getOperands(stack, o.getNumberOfOperands());
stack.push(o.toFormulaString(operands));
}
if(stack.isEmpty()) {
// inspection of the code above reveals that every stack.pop() is followed by a
// stack.push(). So this is either an internal error or impossible.
throw new IllegalStateException("Stack underflow");
}
String result = (String) stack.pop();
if(!stack.isEmpty()) {
// Might be caused by some tokens like AttrPtg and Mem*Ptg, which really shouldn't
// put anything on the stack
throw new IllegalStateException("too much stuff left on the stack");
}
return result;
}
Static method to convert an array of Ptgs in RPN order
to a human readable string format in infix mode. |