⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringformulaparser.java

📁 实现JAVA界面的代码GWT
💻 JAVA
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   StringFormulaParser.java

package jxl.biff.formula;

import common.Logger;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import jxl.WorkbookSettings;
import jxl.biff.WorkbookMethods;

// Referenced classes of package jxl.biff.formula:
//            ParseItem, Operand, StringFunction, Operator, 
//            StringOperator, ArgumentSeparator, OpenParentheses, Parenthesis, 
//            CloseParentheses, Yylex, FormulaException, Attribute, 
//            VariableArgFunction, BuiltInFunction, IntegerValue, DoubleValue, 
//            Parser, Token, Function, ExternalSheet

class StringFormulaParser
    implements Parser
{

    private static Logger logger;
    private String formula;
    private String parsedFormula;
    private ParseItem root;
    private Stack arguments;
    private WorkbookSettings settings;
    private ExternalSheet externalSheet;
    private WorkbookMethods nameTable;
    static Class class$jxl$biff$formula$StringFormulaParser; /* synthetic field */

    public StringFormulaParser(String f, ExternalSheet es, WorkbookMethods nt, WorkbookSettings ws)
    {
        formula = f;
        settings = ws;
        externalSheet = es;
        nameTable = nt;
    }

    public void parse()
        throws FormulaException
    {
        ArrayList tokens = getTokens();
        Iterator i = tokens.iterator();
        root = parseCurrent(i);
    }

    private ParseItem parseCurrent(Iterator i)
        throws FormulaException
    {
        Stack stack = new Stack();
        Stack operators = new Stack();
        Stack args = null;
        boolean parenthesesClosed = false;
        ParseItem pi;
        for(ParseItem lastParseItem = null; i.hasNext() && !parenthesesClosed; lastParseItem = pi)
        {
            pi = (ParseItem)i.next();
            if(pi instanceof Operand)
            {
                handleOperand((Operand)pi, stack);
                continue;
            }
            if(pi instanceof StringFunction)
            {
                handleFunction((StringFunction)pi, i, stack);
                continue;
            }
            if(pi instanceof Operator)
            {
                Operator op = (Operator)pi;
                if(op instanceof StringOperator)
                {
                    StringOperator sop = (StringOperator)op;
                    if(stack.isEmpty() || (lastParseItem instanceof Operator))
                        op = sop.getUnaryOperator();
                    else
                        op = sop.getBinaryOperator();
                }
                if(operators.empty())
                {
                    operators.push(op);
                    continue;
                }
                Operator operator = (Operator)operators.peek();
                if(op.getPrecedence() < operator.getPrecedence())
                {
                    operators.push(op);
                } else
                {
                    operators.pop();
                    operator.getOperands(stack);
                    stack.push(operator);
                    operators.push(op);
                }
                continue;
            }
            if(pi instanceof ArgumentSeparator)
            {
                Operator o;
                for(; !operators.isEmpty(); stack.push(o))
                {
                    o = (Operator)operators.pop();
                    o.getOperands(stack);
                }

                if(args == null)
                    args = new Stack();
                args.push(stack.pop());
                stack.clear();
                continue;
            }
            if(pi instanceof OpenParentheses)
            {
                ParseItem pi2 = parseCurrent(i);
                Parenthesis p = new Parenthesis();
                pi2.setParent(p);
                p.add(pi2);
                stack.push(p);
                continue;
            }
            if(pi instanceof CloseParentheses)
                parenthesesClosed = true;
        }

        Operator o;
        for(; !operators.isEmpty(); stack.push(o))
        {
            o = (Operator)operators.pop();
            o.getOperands(stack);
        }

        ParseItem rt = stack.empty() ? null : (ParseItem)stack.pop();
        if(args != null && rt != null)
            args.push(rt);
        arguments = args;
        if(!stack.empty() || !operators.empty())
            logger.warn("Formula " + formula + " has a non-empty parse stack");
        return rt;
    }

    private ArrayList getTokens()
        throws FormulaException
    {
        ArrayList tokens = new ArrayList();
        StringReader sr = new StringReader(formula);
        Yylex lex = new Yylex(sr);
        lex.setExternalSheet(externalSheet);
        lex.setNameTable(nameTable);
        try
        {
            for(ParseItem pi = lex.yylex(); pi != null; pi = lex.yylex())
                tokens.add(pi);

        }
        catch(IOException e)
        {
            logger.warn(e.toString());
        }
        catch(Error e)
        {
            throw new FormulaException(FormulaException.LEXICAL_ERROR, formula + " at char  " + lex.getPos());
        }
        return tokens;
    }

    public String getFormula()
    {
        if(parsedFormula == null)
        {
            StringBuffer sb = new StringBuffer();
            root.getString(sb);
            parsedFormula = sb.toString();
        }
        return parsedFormula;
    }

    public byte[] getBytes()
    {
        byte bytes[] = root.getBytes();
        if(root.isVolatile())
        {
            byte newBytes[] = new byte[bytes.length + 4];
            System.arraycopy(bytes, 0, newBytes, 4, bytes.length);
            newBytes[0] = Token.ATTRIBUTE.getCode();
            newBytes[1] = 1;
            bytes = newBytes;
        }
        return bytes;
    }

    private void handleFunction(StringFunction sf, Iterator i, Stack stack)
        throws FormulaException
    {
        ParseItem pi2 = parseCurrent(i);
        if(sf.getFunction(settings) == Function.UNKNOWN)
            throw new FormulaException(FormulaException.UNRECOGNIZED_FUNCTION);
        if(sf.getFunction(settings) == Function.SUM && arguments == null)
        {
            Attribute a = new Attribute(sf, settings);
            a.add(pi2);
            stack.push(a);
            return;
        }
        if(sf.getFunction(settings) == Function.IF)
        {
            Attribute a = new Attribute(sf, settings);
            VariableArgFunction vaf = new VariableArgFunction(settings);
            int numargs = arguments.size();
            for(int j = 0; j < numargs; j++)
            {
                ParseItem pi3 = (ParseItem)arguments.get(j);
                vaf.add(pi3);
            }

            a.setIfConditions(vaf);
            stack.push(a);
            return;
        }
        if(sf.getFunction(settings).getNumArgs() == 255)
        {
            if(arguments == null)
            {
                int numArgs = pi2 == null ? 0 : 1;
                VariableArgFunction vaf = new VariableArgFunction(sf.getFunction(settings), numArgs, settings);
                if(pi2 != null)
                    vaf.add(pi2);
                stack.push(vaf);
            } else
            {
                int numargs = arguments.size();
                VariableArgFunction vaf = new VariableArgFunction(sf.getFunction(settings), numargs, settings);
                ParseItem args[] = new ParseItem[numargs];
                for(int j = 0; j < numargs; j++)
                {
                    ParseItem pi3 = (ParseItem)arguments.pop();
                    args[numargs - j - 1] = pi3;
                }

                for(int j = 0; j < args.length; j++)
                    vaf.add(args[j]);

                stack.push(vaf);
                arguments.clear();
                arguments = null;
            }
            return;
        }
        BuiltInFunction bif = new BuiltInFunction(sf.getFunction(settings), settings);
        int numargs = sf.getFunction(settings).getNumArgs();
        if(numargs == 1)
        {
            bif.add(pi2);
        } else
        {
            if(arguments == null && numargs != 0 || arguments != null && numargs != arguments.size())
                throw new FormulaException(FormulaException.INCORRECT_ARGUMENTS);
            for(int j = 0; j < numargs; j++)
            {
                ParseItem pi3 = (ParseItem)arguments.get(j);
                bif.add(pi3);
            }

        }
        stack.push(bif);
    }

    public void adjustRelativeCellReferences(int colAdjust, int rowAdjust)
    {
        root.adjustRelativeCellReferences(colAdjust, rowAdjust);
    }

    public void columnInserted(int sheetIndex, int col, boolean currentSheet)
    {
        root.columnInserted(sheetIndex, col, currentSheet);
    }

    public void columnRemoved(int sheetIndex, int col, boolean currentSheet)
    {
        root.columnRemoved(sheetIndex, col, currentSheet);
    }

    public void rowInserted(int sheetIndex, int row, boolean currentSheet)
    {
        root.rowInserted(sheetIndex, row, currentSheet);
    }

    public void rowRemoved(int sheetIndex, int row, boolean currentSheet)
    {
        root.rowRemoved(sheetIndex, row, currentSheet);
    }

    private void handleOperand(Operand o, Stack stack)
    {
        if(!(o instanceof IntegerValue))
        {
            stack.push(o);
            return;
        }
        if(o instanceof IntegerValue)
        {
            IntegerValue iv = (IntegerValue)o;
            if(!iv.isOutOfRange())
            {
                stack.push(iv);
            } else
            {
                DoubleValue dv = new DoubleValue(iv.getValue());
                stack.push(dv);
            }
        }
    }

    static Class class$(String x0)
    {
        return Class.forName(x0);
        ClassNotFoundException x1;
        x1;
        throw new NoClassDefFoundError(x1.getMessage());
    }

    static 
    {
        logger = Logger.getLogger(class$jxl$biff$formula$StringFormulaParser != null ? class$jxl$biff$formula$StringFormulaParser : (class$jxl$biff$formula$StringFormulaParser = class$("jxl.biff.formula.StringFormulaParser")));
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -