KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > formula > FormulaParser


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 the 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.Cell;
23 import jxl.WorkbookSettings;
24 import jxl.Workbook;
25 import jxl.biff.WorkbookMethods;
26
27 /**
28  * Parses the formula passed in (either as parsed strings or as a string)
29  * into a tree of operators and operands
30  */

31 public class FormulaParser
32 {
33   /**
34    * The formula parser. The object implementing this interface will either
35    * parse tokens or strings
36    */

37   private Parser parser;
38
39   /**
40    * Constructor which creates the parse tree out of tokens
41    *
42    * @param tokens the list of parsed tokens
43    * @param rt the cell containing the formula
44    * @param es a handle to the external sheet
45    * @param nt a handle to the name table
46    * @param ws the workbook settings
47    * @exception FormulaException
48    */

49   public FormulaParser(byte[] tokens, Cell rt, ExternalSheet es,
50                        WorkbookMethods nt,
51                        WorkbookSettings ws)
52    throws FormulaException
53   {
54     // A null workbook bof means that it is a writable workbook and therefore
55
// must be biff8
56
if (es.getWorkbookBof() != null &&
57         !es.getWorkbookBof().isBiff8())
58     {
59       throw new FormulaException(FormulaException.biff8Supported);
60     }
61     parser = new TokenFormulaParser(tokens, rt, es, nt, ws);
62   }
63
64   /**
65    * Constructor which creates the parse tree out of the string
66    *
67    * @param form the formula string
68    * @param es the external sheet handle
69    * @param nt the name table
70    * @param ws the workbook settings
71    */

72   public FormulaParser(String JavaDoc form, ExternalSheet es, WorkbookMethods nt,
73                        WorkbookSettings ws)
74   {
75     parser = new StringFormulaParser(form, es, nt, ws);
76   }
77
78
79   /**
80    * Adjusts all the relative cell references in this formula by the
81    * amount specified. Used when copying formulas
82    *
83    * @param colAdjust the amount to add on to each relative cell reference
84    * @param rowAdjust the amount to add on to each relative row reference
85    */

86   public void adjustRelativeCellReferences(int colAdjust, int rowAdjust)
87   {
88     parser.adjustRelativeCellReferences(colAdjust, rowAdjust);
89   }
90
91   /**
92    * Parses the formula into a parse tree
93    *
94    * @exception FormulaException
95    */

96   public void parse() throws FormulaException
97   {
98     parser.parse();
99   }
100
101   /**
102    * Gets the formula as a string
103    *
104    * @exception FormulaException
105    */

106   public String JavaDoc getFormula() throws FormulaException
107   {
108     return parser.getFormula();
109   }
110
111   /**
112    * Gets the bytes for the formula. This takes into account any
113    * token mapping necessary because of shared formulas
114    *
115    * @return the bytes in RPN
116    */

117   public byte[] getBytes()
118   {
119     return parser.getBytes();
120   }
121
122   /**
123    * Called when a column is inserted on the specified sheet. Tells
124    * the formula parser to update all of its cell references beyond this
125    * column
126    *
127    * @param sheetIndex the sheet on which the column was inserted
128    * @param col the column number which was inserted
129    * @param currentSheet TRUE if this formula is on the sheet in which the
130    * column was inserted, FALSE otherwise
131    */

132   public void columnInserted(int sheetIndex, int col, boolean currentSheet)
133   {
134     parser.columnInserted(sheetIndex, col, currentSheet);
135   }
136
137   /**
138    * Called when a column is inserted on the specified sheet. Tells
139    * the formula parser to update all of its cell references beyond this
140    * column
141    *
142    * @param sheetIndex the sheet on which the column was inserted
143    * @param col the column number which was inserted
144    * @param currentSheet TRUE if this formula is on the sheet in which the
145    * column was inserted, FALSE otherwise
146    */

147   public void columnRemoved(int sheetIndex, int col, boolean currentSheet)
148   {
149     parser.columnRemoved(sheetIndex, col, currentSheet);
150   }
151
152   /**
153    * Called when a column is inserted on the specified sheet. Tells
154    * the formula parser to update all of its cell references beyond this
155    * column
156    *
157    * @param sheetIndex the sheet on which the column was inserted
158    * @param row the column number which was inserted
159    * @param currentSheet TRUE if this formula is on the sheet in which the
160    * column was inserted, FALSE otherwise
161    */

162   public void rowInserted(int sheetIndex, int row, boolean currentSheet)
163   {
164     parser.rowInserted(sheetIndex, row, currentSheet);
165   }
166
167   /**
168    * Called when a column is inserted on the specified sheet. Tells
169    * the formula parser to update all of its cell references beyond this
170    * column
171    *
172    * @param sheetIndex the sheet on which the column was inserted
173    * @param col the column number which was inserted
174    * @param currentSheet TRUE if this formula is on the sheet in which the
175    * column was inserted, FALSE otherwise
176    */

177   public void rowRemoved(int sheetIndex, int row, boolean currentSheet)
178   {
179     parser.rowRemoved(sheetIndex, row, currentSheet);
180   }
181
182 }
183
Popular Tags