KickJava   Java API By Example, From Geeks To Geeks.

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


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.Type;
30 import jxl.biff.IntegerHelper;
31 import jxl.biff.StringHelper;
32 import jxl.biff.FormattingRecords;
33 import jxl.biff.FormulaData;
34 import jxl.biff.WorkbookMethods;
35 import jxl.biff.formula.FormulaParser;
36 import jxl.biff.formula.ExternalSheet;
37 import jxl.biff.formula.FormulaException;
38
39 /**
40  * A string formula record, manufactured out of the Shared Formula
41  * "optimization"
42  */

43 public class SharedStringFormulaRecord extends BaseSharedFormulaRecord
44   implements LabelCell, FormulaData, StringFormulaCell
45 {
46   /**
47    * The logger
48    */

49   private static Logger logger = Logger.getLogger
50     (SharedStringFormulaRecord.class);
51
52   /**
53    * The value of this string formula
54    */

55   private String JavaDoc value;
56
57   /**
58    * Constructs this string formula
59    *
60    * @param t the record
61    * @param excelFile the excel file
62    * @param fr the formatting record
63    * @param es the external sheet
64    * @param nt the workbook
65    * @param si the sheet
66    * @param ws the workbook settings
67    */

68   public SharedStringFormulaRecord(Record t,
69                                    File excelFile,
70                                    FormattingRecords fr,
71                                    ExternalSheet es,
72                                    WorkbookMethods nt,
73                                    SheetImpl si,
74                                    WorkbookSettings ws)
75   {
76     super(t, fr, es, nt, si, excelFile.getPos());
77     int pos = excelFile.getPos();
78
79     // Save the position in the excel file
80
int filepos = excelFile.getPos();
81
82     // Look for the string record in one of the records after the
83
// formula. Put a cap on it to prevent ednas
84
Record nextRecord = excelFile.next();
85     int count = 0;
86     while (nextRecord.getType() != Type.STRING && count < 4)
87     {
88       nextRecord = excelFile.next();
89       count++;
90     }
91     Assert.verify(count < 4, " @ " + pos);
92
93     byte[] stringData = nextRecord.getData();
94     int chars = IntegerHelper.getInt(stringData[0], stringData[1]);
95
96     boolean unicode = false;
97     int startpos = 3;
98     if (stringData.length == chars + 2)
99     {
100       // String might only consist of a one byte length indicator, instead
101
// of the more normal 2
102
startpos = 2;
103       unicode = false;
104     }
105     else if (stringData[2] == 0x1)
106     {
107       // unicode string, two byte length indicator
108
startpos = 3;
109       unicode = true;
110     }
111     else
112     {
113       // ascii string, two byte length indicator
114
startpos = 3;
115       unicode = false;
116     }
117
118     if (!unicode)
119     {
120       value = StringHelper.getString(stringData, chars, startpos, ws);
121     }
122     else
123     {
124       value = StringHelper.getUnicodeString(stringData, chars, startpos);
125     }
126
127     // Restore the position in the excel file, to enable the SHRFMLA
128
// record to be picked up
129
excelFile.setPos(filepos);
130   }
131
132   /**
133    * Accessor for the value
134    *
135    * @return the value
136    */

137   public String JavaDoc getString()
138   {
139     return value;
140   }
141
142   /**
143    * Accessor for the contents as a string
144    *
145    * @return the value as a string
146    */

147   public String JavaDoc getContents()
148   {
149     return value;
150   }
151
152   /**
153    * Accessor for the cell type
154    *
155    * @return the cell type
156    */

157   public CellType getType()
158   {
159     return CellType.STRING_FORMULA;
160   }
161
162   /**
163    * Gets the raw bytes for the formula. This will include the
164    * parsed tokens array. Used when copying spreadsheets
165    *
166    * @return the raw record data
167    * @exception FormulaException
168    */

169   public byte[] getFormulaData() throws FormulaException
170   {
171     if (!getSheet().getWorkbookBof().isBiff8())
172     {
173       throw new FormulaException(FormulaException.biff8Supported);
174     }
175
176     // Get the tokens, taking into account the mapping from shared
177
// formula specific values into normal values
178
FormulaParser fp = new FormulaParser
179       (getTokens(), this,
180        getExternalSheet(), getNameTable(),
181        getSheet().getWorkbook().getSettings());
182     fp.parse();
183     byte[] rpnTokens = fp.getBytes();
184
185     byte[] data = new byte[rpnTokens.length + 22];
186
187     // Set the standard info for this cell
188
IntegerHelper.getTwoBytes(getRow(), data, 0);
189     IntegerHelper.getTwoBytes(getColumn(), data, 2);
190     IntegerHelper.getTwoBytes(getXFIndex(), data, 4);
191
192     // Set the two most significant bytes of the value to be 0xff in
193
// order to identify this as a string
194
data[6] = 0;
195     data[12] = -1;
196     data[13] = -1;
197
198     // Now copy in the parsed tokens
199
System.arraycopy(rpnTokens, 0, data, 22, rpnTokens.length);
200     IntegerHelper.getTwoBytes(rpnTokens.length, data, 20);
201
202     // Lop off the standard information
203
byte[] d = new byte[data.length - 6];
204     System.arraycopy(data, 6, d, 0, data.length - 6);
205
206     return d;
207   }
208 }
209
210
211
212
213
214
215
216
217
218
Popular Tags