Home » JCraft » jxl » biff » formula » [javadoc | source]

    1   /*********************************************************************
    2   *
    3   *      Copyright (C) 2002 Andrew Khan
    4   *
    5   * This library is free software; you can redistribute it and/or
    6   * modify it under the terms of the GNU Lesser General Public
    7   * License as published by the7 Free Software Foundation; either
    8   * version 2.1 of the License, or (at your option) any later version.
    9   *
   10   * This library is distributed in the hope that it will be useful,
   11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13   * Lesser General Public License for more details.
   14   *
   15   * You should have received a copy of the GNU Lesser General Public
   16   * License along with this library; if not, write to the Free Software
   17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   18   ***************************************************************************/
   19   
   20   package jxl.biff.formula;
   21   
   22   import jxl.common.Assert;
   23   import jxl.common.Logger;
   24   
   25   import jxl.Cell;
   26   import jxl.WorkbookSettings;
   27   import jxl.biff.WorkbookMethods;
   28   
   29   /**
   30    * Parses the formula passed in (either as parsed strings or as a string)
   31    * into a tree of operators and operands
   32    */
   33   public class FormulaParser
   34   {
   35     /**
   36      * The logger
   37      */
   38     private static final Logger logger = Logger.getLogger(FormulaParser.class);
   39   
   40     /**
   41      * The formula parser.  The object implementing this interface will either
   42      * parse tokens or strings
   43      */
   44     private Parser parser;
   45   
   46     /**
   47      * Constructor which creates the parse tree out of tokens
   48      *
   49      * @param tokens the list of parsed tokens
   50      * @param rt the cell containing the formula
   51      * @param es a handle to the external sheet
   52      * @param nt a handle to the name table
   53      * @param ws the workbook settings
   54      * @param pc the parse context
   55      * @exception FormulaException
   56      */
   57     public FormulaParser(byte[] tokens,
   58                          Cell rt,
   59                          ExternalSheet es,
   60                          WorkbookMethods nt,
   61                          WorkbookSettings ws)
   62      throws FormulaException
   63     {
   64       // A null workbook bof means that it is a writable workbook and therefore
   65       // must be biff8
   66       if (es.getWorkbookBof() != null &&
   67           !es.getWorkbookBof().isBiff8())
   68       {
   69         throw new FormulaException(FormulaException.BIFF8_SUPPORTED);
   70       }
   71       Assert.verify(nt != null);
   72       parser = new TokenFormulaParser(tokens, rt, es, nt, ws, 
   73                                       ParseContext.DEFAULT);
   74     }
   75   
   76     /**
   77      * Constructor which creates the parse tree out of tokens
   78      *
   79      * @param tokens the list of parsed tokens
   80      * @param rt the cell containing the formula
   81      * @param es a handle to the external sheet
   82      * @param nt a handle to the name table
   83      * @param ws the workbook settings
   84      * @param pc the parse context
   85      * @exception FormulaException
   86      */
   87     public FormulaParser(byte[] tokens,
   88                          Cell rt,
   89                          ExternalSheet es,
   90                          WorkbookMethods nt,
   91                          WorkbookSettings ws,
   92                          ParseContext pc)
   93      throws FormulaException
   94     {
   95       // A null workbook bof means that it is a writable workbook and therefore
   96       // must be biff8
   97       if (es.getWorkbookBof() != null &&
   98           !es.getWorkbookBof().isBiff8())
   99       {
  100         throw new FormulaException(FormulaException.BIFF8_SUPPORTED);
  101       }
  102       Assert.verify(nt != null);
  103       parser = new TokenFormulaParser(tokens, rt, es, nt, ws, pc);
  104     }
  105   
  106     /**
  107      * Constructor which creates the parse tree out of the string
  108      *
  109      * @param form the formula string
  110      * @param es the external sheet handle
  111      * @param nt the name table
  112      * @param ws the workbook settings
  113      */
  114     public FormulaParser(String form,
  115                          ExternalSheet es,
  116                          WorkbookMethods nt,
  117                          WorkbookSettings ws)
  118     {
  119       parser = new StringFormulaParser(form, es, nt, ws,
  120                                        ParseContext.DEFAULT);
  121     }
  122   
  123     /**
  124      * Constructor which creates the parse tree out of the string
  125      *
  126      * @param form the formula string
  127      * @param es the external sheet handle
  128      * @param nt the name table
  129      * @param ws the workbook settings
  130      * @param pc the context of the parse
  131      */
  132     public FormulaParser(String form,
  133                          ExternalSheet es,
  134                          WorkbookMethods nt,
  135                          WorkbookSettings ws,
  136                          ParseContext pc)
  137     {
  138       parser = new StringFormulaParser(form, es, nt, ws, pc);
  139     }
  140   
  141   
  142     /**
  143      * Adjusts all the relative cell references in this formula by the
  144      * amount specified.  Used when copying formulas
  145      *
  146      * @param colAdjust the amount to add on to each relative cell reference
  147      * @param rowAdjust the amount to add on to each relative row reference
  148      */
  149     public void adjustRelativeCellReferences(int colAdjust, int rowAdjust)
  150     {
  151       parser.adjustRelativeCellReferences(colAdjust, rowAdjust);
  152     }
  153   
  154     /**
  155      * Parses the formula into a parse tree
  156      *
  157      * @exception FormulaException
  158      */
  159     public void parse() throws FormulaException
  160     {
  161       parser.parse();
  162     }
  163   
  164     /**
  165      * Gets the formula as a string
  166      *
  167      * @return the formula as a string
  168      * @exception FormulaException
  169      */
  170     public String getFormula() throws FormulaException
  171     {
  172       return parser.getFormula();
  173     }
  174   
  175     /**
  176      * Gets the bytes for the formula. This takes into account any
  177      * token mapping necessary because of shared formulas
  178      *
  179      * @return the bytes in RPN
  180      */
  181     public byte[] getBytes()
  182     {
  183       return parser.getBytes();
  184     }
  185   
  186     /**
  187      * Called when a column is inserted on the specified sheet.  Tells
  188      * the formula  parser to update all of its cell references beyond this
  189      * column
  190      *
  191      * @param sheetIndex the sheet on which the column was inserted
  192      * @param col the column number which was inserted
  193      * @param currentSheet TRUE if this formula is on the sheet in which the
  194      * column was inserted, FALSE otherwise
  195      */
  196     public void columnInserted(int sheetIndex, int col, boolean currentSheet)
  197     {
  198       parser.columnInserted(sheetIndex, col, currentSheet);
  199     }
  200   
  201     /**
  202      * Called when a column is inserted on the specified sheet.  Tells
  203      * the formula  parser to update all of its cell references beyond this
  204      * column
  205      *
  206      * @param sheetIndex the sheet on which the column was inserted
  207      * @param col the column number which was removed
  208      * @param currentSheet TRUE if this formula is on the sheet in which the
  209      * column was inserted, FALSE otherwise
  210      */
  211     public void columnRemoved(int sheetIndex, int col, boolean currentSheet)
  212     {
  213       parser.columnRemoved(sheetIndex, col, currentSheet);
  214     }
  215   
  216     /**
  217      * Called when a column is inserted on the specified sheet.  Tells
  218      * the formula  parser to update all of its cell references beyond this
  219      * column
  220      *
  221      * @param sheetIndex the sheet on which the column was inserted
  222      * @param row the row number which was inserted
  223      * @param currentSheet TRUE if this formula is on the sheet in which the
  224      * column was inserted, FALSE otherwise
  225      */
  226     public void rowInserted(int sheetIndex, int row, boolean currentSheet)
  227     {
  228       parser.rowInserted(sheetIndex, row, currentSheet);
  229     }
  230   
  231     /**
  232      * Called when a column is inserted on the specified sheet.  Tells
  233      * the formula  parser to update all of its cell references beyond this
  234      * column
  235      *
  236      * @param sheetIndex the sheet on which the column was inserted
  237      * @param row the row number which was removed
  238      * @param currentSheet TRUE if this formula is on the sheet in which the
  239      * column was inserted, FALSE otherwise
  240      */
  241     public void rowRemoved(int sheetIndex, int row, boolean currentSheet)
  242     {
  243       parser.rowRemoved(sheetIndex, row, currentSheet);
  244     }
  245   
  246     /**
  247      * If this formula was on an imported sheet, check that
  248      * cell references to another sheet are warned appropriately
  249      *
  250      * @return TRUE if the formula is valid import, FALSE otherwise
  251      */
  252     public boolean handleImportedCellReferences()
  253     {
  254       return parser.handleImportedCellReferences();
  255     }
  256   }

Home » JCraft » jxl » biff » formula » [javadoc | source]