KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > read > biff > FormulaRecord


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.read.biff;
21
22 import common.Assert;
23 import common.Logger;
24
25 import jxl.CellType;
26 import jxl.WorkbookSettings;
27 import jxl.biff.FormattingRecords;
28 import jxl.biff.IntegerHelper;
29 import jxl.biff.DoubleHelper;
30 import jxl.biff.WorkbookMethods;
31 import jxl.biff.formula.ExternalSheet;
32
33 /**
34  * A formula's last calculated value
35  */

36 class FormulaRecord extends CellValue
37 {
38   /**
39    * The logger
40    */

41   private static Logger logger = Logger.getLogger(FormulaRecord.class);
42
43   /**
44    * The "real" formula record - will be either a string a or a number
45    */

46   private CellValue formula;
47
48   /**
49    * Flag to indicate whether this is a shared formula
50    */

51   private boolean shared;
52
53   /**
54    * Static class for a dummy override, indicating that the formula
55    * passed in is not a shared formula
56    */

57   private static class IgnoreSharedFormula {};
58   public static final IgnoreSharedFormula ignoreSharedFormula
59     = new IgnoreSharedFormula();
60
61   /**
62    * Constructs this object from the raw data. Creates either a
63    * NumberFormulaRecord or a StringFormulaRecord depending on whether
64    * this formula represents a numerical calculation or not
65    *
66    * @param t the raw data
67    * @param excelFile the excel file
68    * @param fr the formatting records
69    * @param es the workbook, which contains the external sheet references
70    * @param nt the name table
71    * @param si the sheet
72    * @param ws the workbook settings
73    */

74   public FormulaRecord(Record t,
75                        File excelFile,
76                        FormattingRecords fr,
77                        ExternalSheet es,
78                        WorkbookMethods nt,
79                        SheetImpl si,
80                        WorkbookSettings ws)
81   {
82     super(t, fr, si);
83
84     byte[] data = getRecord().getData();
85
86     shared = false;
87
88     // Check to see if this forms part of a shared formula
89
int grbit = IntegerHelper.getInt(data[14], data[15]);
90     if ((grbit & 0x08) != 0)
91     {
92       shared = true;
93
94       if (data[6] == 0 && data[12] == -1 && data[13] == -1)
95       {
96         // It is a shared string formula
97
formula = new SharedStringFormulaRecord
98           (t, excelFile, fr, es, nt, si, ws);
99       }
100       else
101       {
102         // It is a numerical formula
103
double value = DoubleHelper.getIEEEDouble(data, 6);
104         SharedNumberFormulaRecord snfr = new SharedNumberFormulaRecord
105           (t, excelFile, value, fr, es, nt, si);
106         snfr.setNumberFormat(fr.getNumberFormat(getXFIndex()));
107         formula = snfr;
108       }
109
110       return;
111     }
112
113     // microsoft and their goddam magic values determine whether this
114
// is a string or a number value
115
if (data[6] == 0 && data[12] == -1 && data[13] == -1)
116     {
117       // we have a string
118
formula = new StringFormulaRecord(t, excelFile, fr, es, nt, si, ws);
119     }
120     else if (data[6] == 1 &&
121              data[12] == -1 &&
122              data[13] == -1)
123     {
124       // We have a boolean formula
125
// multiple values. Thanks to Frank for spotting this
126
formula = new BooleanFormulaRecord(t, fr, es, nt, si);
127     }
128     else if (data[6] == 2 &&
129              data[12] == -1 &&
130              data[13] == -1)
131     {
132       // The cell is in error
133
formula = new ErrorFormulaRecord(t, fr, es, nt, si);
134     }
135     else if (data[6] == 3 && data[12] == -1 && data[13] == -1)
136     {
137       // we have a string which evaluates to null
138
formula = new StringFormulaRecord(t, fr, es, nt, si);
139     }
140     else
141     {
142       // it is most assuredly a number
143
formula = new NumberFormulaRecord(t, fr, es, nt, si);
144     }
145   }
146
147   /**
148    * Constructs this object from the raw data. Creates either a
149    * NumberFormulaRecord or a StringFormulaRecord depending on whether
150    * this formula represents a numerical calculation or not
151    *
152    * @param t the raw data
153    * @param excelFile the excel file
154    * @param fr the formatting records
155    * @param es the workbook, which contains the external sheet references
156    * @param nt the name table
157    * @param i a dummy override to indicate that we don't want to do
158    * any shared formula processing
159    * @param si the sheet impl
160    * @param ws the workbook settings
161    */

162   public FormulaRecord(Record t,
163                        File excelFile,
164                        FormattingRecords fr,
165                        ExternalSheet es,
166                        WorkbookMethods nt,
167                        IgnoreSharedFormula i,
168                        SheetImpl si,
169                        WorkbookSettings ws)
170   {
171     super(t, fr, si);
172     byte[] data = getRecord().getData();
173
174     shared = false;
175
176     // microsoft and their magic values determine whether this
177
// is a string or a number value
178
if (data[6] == 0 && data[12] == -1 && data[13] == -1)
179     {
180       // we have a string
181
formula = new StringFormulaRecord(t, excelFile, fr, es, nt, si, ws);
182     }
183     else if (data[6] == 1 &&
184              data[12] == -1 &&
185              data[13] == -1)
186     {
187       // We have a boolean formula
188
// multiple values. Thanks to Frank for spotting this
189
formula = new BooleanFormulaRecord(t, fr, es, nt, si);
190     }
191     else if (data[6] == 2 &&
192              data[12] == -1 &&
193              data[13] == -1)
194     {
195       // The cell is in error
196
formula = new ErrorFormulaRecord(t, fr, es, nt, si);
197     }
198     else
199     {
200       // it is most assuredly a number
201
formula = new NumberFormulaRecord(t, fr, es, nt, si);
202     }
203   }
204
205   /**
206    * Returns the numerical value as a string
207    *
208    * @return The numerical value of the formula as a string
209    */

210   public String JavaDoc getContents()
211   {
212     Assert.verify(false);
213     return "";
214   }
215
216   /**
217    * Returns the cell type
218    *
219    * @return The cell type
220    */

221   public CellType getType()
222   {
223     Assert.verify(false);
224     return CellType.EMPTY;
225   }
226
227   /**
228    * Gets the "real" formula
229    *
230    * @return the cell value
231    */

232   final CellValue getFormula()
233   {
234     return formula;
235   }
236
237   /**
238    * Interrogates this formula to determine if it forms part of a shared
239    * formula
240    *
241    * @return TRUE if this is shared formula, FALSE otherwise
242    */

243   final boolean isShared()
244   {
245     return shared;
246   }
247
248 }
249
250
251
252
253
254
255
Popular Tags