KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.text.DecimalFormat JavaDoc;
23 import java.text.NumberFormat JavaDoc;
24
25 import common.Logger;
26 import common.Assert;
27
28 import jxl.NumberCell;
29 import jxl.CellType;
30 import jxl.NumberFormulaCell;
31 import jxl.biff.IntegerHelper;
32 import jxl.biff.DoubleHelper;
33 import jxl.biff.FormulaData;
34 import jxl.biff.WorkbookMethods;
35 import jxl.biff.FormattingRecords;
36 import jxl.biff.formula.FormulaParser;
37 import jxl.biff.formula.ExternalSheet;
38 import jxl.biff.formula.FormulaException;
39
40 /**
41  * A number formula record, manufactured out of the Shared Formula
42  * "optimization"
43  */

44 public class SharedNumberFormulaRecord extends BaseSharedFormulaRecord
45   implements NumberCell, FormulaData, NumberFormulaCell
46 {
47   /**
48    * The logger
49    */

50   private static Logger logger =
51     Logger.getLogger(SharedNumberFormulaRecord.class);
52   /**
53    * The value of this number
54    */

55   private double value;
56   /**
57    * The cell format
58    */

59   private NumberFormat JavaDoc format;
60   /**
61    * A handle to the formatting records
62    */

63   private FormattingRecords formattingRecords;
64
65   /**
66    * The string format for the double value
67    */

68   private static DecimalFormat JavaDoc defaultFormat = new DecimalFormat JavaDoc("#.###");
69
70   /**
71    * Constructs this number
72    *
73    * @param t the data
74    * @param excelFile the excel biff data
75    * @param v the value
76    * @param fr the formatting records
77    * @param es the external sheet
78    * @param nt the name table
79    * @param si the sheet
80    */

81   public SharedNumberFormulaRecord(Record t,
82                                    File excelFile,
83                                    double v,
84                                    FormattingRecords fr,
85                                    ExternalSheet es,
86                                    WorkbookMethods nt,
87                                    SheetImpl si)
88   {
89     super(t, fr, es, nt, si, excelFile.getPos());
90     value = v;
91     format = defaultFormat; // format is set up later from the
92
// SharedFormulaRecord
93
}
94
95   /**
96    * Sets the format for the number based on the Excel spreadsheets' format.
97    * This is called from SheetImpl when it has been definitely established
98    * that this cell is a number and not a date
99    *
100    * @param f the format
101    */

102   final void setNumberFormat(NumberFormat JavaDoc f)
103   {
104     if (f != null)
105     {
106       format = f;
107     }
108   }
109
110   /**
111    * Accessor for the value
112    *
113    * @return the value
114    */

115   public double getValue()
116   {
117     return value;
118   }
119
120   /**
121    * Accessor for the contents as a string
122    *
123    * @return the value as a string
124    */

125   public String JavaDoc getContents()
126   {
127     return format.format(value);
128   }
129
130   /**
131    * Accessor for the cell type
132    *
133    * @return the cell type
134    */

135   public CellType getType()
136   {
137     return CellType.NUMBER_FORMULA;
138   }
139
140   /**
141    * Gets the raw bytes for the formula. This will include the
142    * parsed tokens array. Used when copying spreadsheets
143    *
144    * @return the raw record data
145    * @exception FormulaException
146    */

147   public byte[] getFormulaData() throws FormulaException
148   {
149     if (!getSheet().getWorkbookBof().isBiff8())
150     {
151       throw new FormulaException(FormulaException.biff8Supported);
152     }
153
154     // Get the tokens, taking into account the mapping from shared
155
// formula specific values into normal values
156
FormulaParser fp = new FormulaParser
157       (getTokens(), this,
158        getExternalSheet(), getNameTable(),
159        getSheet().getWorkbook().getSettings());
160     fp.parse();
161     byte[] rpnTokens = fp.getBytes();
162
163     byte[] data = new byte[rpnTokens.length + 22];
164
165     // Set the standard info for this cell
166
IntegerHelper.getTwoBytes(getRow(), data, 0);
167     IntegerHelper.getTwoBytes(getColumn(), data, 2);
168     IntegerHelper.getTwoBytes(getXFIndex(), data, 4);
169     DoubleHelper.getIEEEBytes(value, data, 6);
170
171     // Now copy in the parsed tokens
172
System.arraycopy(rpnTokens, 0, data, 22, rpnTokens.length);
173     IntegerHelper.getTwoBytes(rpnTokens.length, data, 20);
174
175     // Lop off the standard information
176
byte[] d = new byte[data.length - 6];
177     System.arraycopy(data, 6, d, 0, data.length - 6);
178
179     return d;
180   }
181
182   /**
183    * Gets the NumberFormat used to format this cell. This is the java
184    * equivalent of the Excel format
185    *
186    * @return the NumberFormat used to format the cell
187    */

188   public NumberFormat JavaDoc getNumberFormat()
189   {
190     return format;
191   }
192 }
193
194
195
196
197
198
199
200
201
202
Popular Tags