KickJava   Java API By Example, From Geeks To Geeks.

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


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.LabelCell;
26 import jxl.CellType;
27 import jxl.StringFormulaCell;
28 import jxl.WorkbookSettings;
29 import jxl.biff.StringHelper;
30 import jxl.biff.IntegerHelper;
31 import jxl.biff.Type;
32 import jxl.biff.FormulaData;
33 import jxl.biff.WorkbookMethods;
34 import jxl.biff.FormattingRecords;
35 import jxl.biff.formula.FormulaParser;
36 import jxl.biff.formula.ExternalSheet;
37 import jxl.biff.formula.FormulaException;
38
39 /**
40  * A string formula's last calculated value
41  */

42 class StringFormulaRecord extends CellValue
43   implements LabelCell, FormulaData, StringFormulaCell
44 {
45   /**
46    * The logger
47    */

48   private static Logger logger = Logger.getLogger(StringFormulaRecord.class);
49
50   /**
51    * The last calculated value of the formula
52    */

53   private String JavaDoc value;
54
55   /**
56    * A handle to the class needed to access external sheets
57    */

58   private ExternalSheet externalSheet;
59
60   /**
61    * A handle to the name table
62    */

63   private WorkbookMethods nameTable;
64
65   /**
66    * The formula as an excel string
67    */

68   private String JavaDoc formulaString;
69
70   /**
71    * The raw data
72    */

73   private byte[] data;
74
75   /**
76    * Constructs this object from the raw data. We need to use the excelFile
77    * to retrieve the String record which follows this formula record
78    *
79    * @param t the raw data
80    * @param excelFile the excel file
81    * @param fr the formatting records
82    * @param es the external sheet records
83    * @param nt the workbook
84    * @param si the sheet impl
85    * @param ws the workbook settings
86    */

87   public StringFormulaRecord(Record t, File excelFile,
88                              FormattingRecords fr,
89                              ExternalSheet es,
90                              WorkbookMethods nt,
91                              SheetImpl si,
92                              WorkbookSettings ws)
93   {
94     super(t, fr, si);
95
96     externalSheet = es;
97     nameTable = nt;
98
99     data = getRecord().getData();
100
101     int pos = excelFile.getPos();
102
103     // Look for the string record in one of the records after the
104
// formula. Put a cap on it to prevent looping
105

106     Record nextRecord = excelFile.next();
107     int count = 0;
108     while (nextRecord.getType() != Type.STRING && count < 4)
109     {
110       nextRecord = excelFile.next();
111       count++;
112     }
113     Assert.verify(count < 4, " @ " + pos);
114     readString(nextRecord.getData(), ws);
115   }
116
117   /**
118    * Constructs this object from the raw data. Used when reading in formula
119    * strings which evaluate to null (in the case of some IF statements)
120    *
121    * @param t the raw data
122    * @param fr the formatting records
123    * @param es the external sheet records
124    * @param nt the workbook
125    * @param si the sheet impl
126    * @param ws the workbook settings
127    */

128   public StringFormulaRecord(Record t,
129                              FormattingRecords fr,
130                              ExternalSheet es,
131                              WorkbookMethods nt,
132                              SheetImpl si)
133   {
134     super(t, fr, si);
135
136     externalSheet = es;
137     nameTable = nt;
138
139     data = getRecord().getData();
140     value = "";
141   }
142
143
144   /**
145    * Reads in the string
146    *
147    * @param d the data
148    * @param ws the workbook settings
149    */

150   private void readString(byte[] d, WorkbookSettings ws)
151   {
152     int pos = 0;
153     int chars = IntegerHelper.getInt(d[0], d[1]);
154
155     if (chars == 0)
156     {
157       value="";
158       return;
159     }
160     pos += 2;
161     int optionFlags = d[pos];
162     pos++;
163
164     if ((optionFlags & 0xf) != optionFlags)
165     {
166       // Uh oh - looks like a plain old string, not unicode
167
// Recalculate all the positions
168
pos = 0;
169       chars = IntegerHelper.getInt(d[0], (byte) 0);
170       optionFlags = d[1];
171       pos = 2;
172     }
173
174     // See if it is an extended string
175
boolean extendedString = ((optionFlags & 0x04) != 0);
176
177     // See if string contains formatting information
178
boolean richString = ((optionFlags & 0x08) != 0);
179
180     if (richString)
181     {
182       pos += 2;
183     }
184
185     if (extendedString)
186     {
187       pos += 4;
188     }
189
190     // See if string is ASCII (compressed) or unicode
191
boolean asciiEncoding = ((optionFlags & 0x01) == 0);
192
193     if (asciiEncoding)
194     {
195       value = StringHelper.getString(d, chars, pos, ws);
196     }
197     else
198     {
199       value = StringHelper.getUnicodeString(d, chars, pos);
200     }
201   }
202
203   /**
204    * Interface method which returns the value
205    *
206    * @return the last calculated value of the formula
207    */

208   public String JavaDoc getContents()
209   {
210     return value;
211   }
212
213   /**
214    * Interface method which returns the value
215    *
216    * @return the last calculated value of the formula
217    */

218   public String JavaDoc getString()
219   {
220     return value;
221   }
222
223   /**
224    * Returns the cell type
225    *
226    * @return The cell type
227    */

228   public CellType getType()
229   {
230     return CellType.STRING_FORMULA;
231   }
232
233   /**
234    * Gets the raw bytes for the formula. This will include the
235    * parsed tokens array
236    *
237    * @return the raw record data
238    */

239   public byte[] getFormulaData() throws FormulaException
240   {
241     if (!getSheet().getWorkbook().getWorkbookBof().isBiff8())
242     {
243       throw new FormulaException(FormulaException.biff8Supported);
244     }
245
246     // Lop off the standard information
247
byte[] d = new byte[data.length - 6];
248     System.arraycopy(data, 6, d, 0, data.length - 6);
249
250     return d;
251   }
252
253   /**
254    * Gets the formula as an excel string
255    *
256    * @return the formula as an excel string
257    * @exception FormulaException
258    */

259   public String JavaDoc getFormula() throws FormulaException
260   {
261     if (formulaString == null)
262     {
263       byte[] tokens = new byte[data.length - 22];
264       System.arraycopy(data, 22, tokens, 0, tokens.length);
265       FormulaParser fp = new FormulaParser
266         (tokens, this, externalSheet, nameTable,
267          getSheet().getWorkbook().getSettings());
268       fp.parse();
269       formulaString = fp.getFormula();
270     }
271
272     return formulaString;
273   }
274
275 }
276
Popular Tags