Search in sources :

Example 1 with MemErrPtg

use of org.apache.poi.ss.formula.ptg.MemErrPtg in project poi by apache.

the class FormulaRenderer method toFormulaString.

/**
     * Static method to convert an array of {@link Ptg}s in RPN order
     * to a human readable string format in infix mode.
     * @param book  used for defined names and 3D references
     * @param ptgs  must not be <code>null</code>
     * @return a human readable String
     */
public static String toFormulaString(FormulaRenderingWorkbook book, Ptg[] ptgs) {
    if (ptgs == null || ptgs.length == 0) {
        throw new IllegalArgumentException("ptgs must not be null");
    }
    Stack<String> stack = new Stack<String>();
    for (Ptg ptg : ptgs) {
        // TODO - what about MemNoMemPtg?
        if (ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) {
            // TODO - put comment and throw exception in toFormulaString() of these classes
            continue;
        }
        if (ptg instanceof ParenthesisPtg) {
            String contents = stack.pop();
            stack.push("(" + contents + ")");
            continue;
        }
        if (ptg instanceof AttrPtg) {
            AttrPtg attrPtg = ((AttrPtg) ptg);
            if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isSkip()) {
                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);
        }
        if (ptg instanceof WorkbookDependentFormula) {
            WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg;
            stack.push(optg.toFormulaString(book));
            continue;
        }
        if (!(ptg instanceof OperationPtg)) {
            stack.push(ptg.toFormulaString());
            continue;
        }
        OperationPtg o = (OperationPtg) ptg;
        String[] operands = getOperands(stack, o.getNumberOfOperands());
        stack.push(o.toFormulaString(operands));
    }
    if (stack.isEmpty()) {
        // stack.push(). So this is either an internal error or impossible.
        throw new IllegalStateException("Stack underflow");
    }
    String result = stack.pop();
    if (!stack.isEmpty()) {
        // put anything on the stack
        throw new IllegalStateException("too much stuff left on the stack");
    }
    return result;
}
Also used : AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) MemErrPtg(org.apache.poi.ss.formula.ptg.MemErrPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) OperationPtg(org.apache.poi.ss.formula.ptg.OperationPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) OperationPtg(org.apache.poi.ss.formula.ptg.OperationPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) Stack(java.util.Stack) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) MemErrPtg(org.apache.poi.ss.formula.ptg.MemErrPtg)

Aggregations

Stack (java.util.Stack)1 AttrPtg (org.apache.poi.ss.formula.ptg.AttrPtg)1 MemAreaPtg (org.apache.poi.ss.formula.ptg.MemAreaPtg)1 MemErrPtg (org.apache.poi.ss.formula.ptg.MemErrPtg)1 MemFuncPtg (org.apache.poi.ss.formula.ptg.MemFuncPtg)1 OperationPtg (org.apache.poi.ss.formula.ptg.OperationPtg)1 ParenthesisPtg (org.apache.poi.ss.formula.ptg.ParenthesisPtg)1 Ptg (org.apache.poi.ss.formula.ptg.Ptg)1