KickJava   Java API By Example, From Geeks To Geeks.

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


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.DateFormat JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import jxl.DateCell;
26 import jxl.CellType;
27 import jxl.biff.IntegerHelper;
28 import jxl.biff.DoubleHelper;
29 import jxl.biff.FormulaData;
30 import jxl.biff.FormattingRecords;
31 import jxl.biff.formula.FormulaParser;
32 import jxl.biff.formula.FormulaException;
33
34 /**
35  * A number formula record, manufactured out of the Shared Formula
36  * "optimization"
37  */

38 public class SharedDateFormulaRecord extends BaseSharedFormulaRecord
39   implements DateCell, FormulaData
40 {
41   /**
42    * Re-use the date record to handle all the formatting information and
43    * date calculations
44    */

45   private DateRecord dateRecord;
46
47   /**
48    * The double value
49    */

50   private double value;
51
52   /**
53    * Constructs this number formula
54    *
55    * @param nfr the number formula records
56    * @param fr the formatting records
57    * @param nf flag indicating whether this uses the 1904 date system
58    * @param si the sheet
59    * @param pos the position
60    */

61   public SharedDateFormulaRecord(SharedNumberFormulaRecord nfr,
62                                  FormattingRecords fr,
63                                  boolean nf,
64                                  SheetImpl si,
65                                  int pos)
66   {
67     super(nfr.getRecord(),
68           fr,
69           nfr.getExternalSheet(),
70           nfr.getNameTable(),
71           si,
72           pos);
73    dateRecord = new DateRecord(nfr, nfr.getXFIndex(), fr, nf, si);
74     value = nfr.getValue();
75   }
76
77   /**
78    * Accessor for the value
79    *
80    * @return the value
81    */

82   public double getValue()
83   {
84     return value;
85   }
86
87   /**
88    * Accessor for the contents as a string
89    *
90    * @return the value as a string
91    */

92   public String JavaDoc getContents()
93   {
94     return dateRecord.getContents();
95   }
96
97   /**
98    * Accessor for the cell type
99    *
100    * @return the cell type
101    */

102   public CellType getType()
103   {
104     return CellType.DATE_FORMULA;
105   }
106
107   /**
108    * Gets the raw bytes for the formula. This will include the
109    * parsed tokens array. Used when copying spreadsheets
110    *
111    * @return the raw record data
112    * @exception FormulaException
113    */

114   public byte[] getFormulaData() throws FormulaException
115   {
116     if (!getSheet().getWorkbookBof().isBiff8())
117     {
118       throw new FormulaException(FormulaException.biff8Supported);
119     }
120
121     // Get the tokens, taking into account the mapping from shared
122
// formula specific values into normal values
123
FormulaParser fp = new FormulaParser
124       (getTokens(), this,
125        getExternalSheet(), getNameTable(),
126        getSheet().getWorkbook().getSettings());
127     fp.parse();
128     byte[] rpnTokens = fp.getBytes();
129
130     byte[] data = new byte[rpnTokens.length + 22];
131
132     // Set the standard info for this cell
133
IntegerHelper.getTwoBytes(getRow(), data, 0);
134     IntegerHelper.getTwoBytes(getColumn(), data, 2);
135     IntegerHelper.getTwoBytes(getXFIndex(), data, 4);
136     DoubleHelper.getIEEEBytes(value, data, 6);
137
138     // Now copy in the parsed tokens
139
System.arraycopy(rpnTokens, 0, data, 22, rpnTokens.length);
140     IntegerHelper.getTwoBytes(rpnTokens.length, data, 20);
141
142     // Lop off the standard information
143
byte[] d = new byte[data.length - 6];
144     System.arraycopy(data, 6, d, 0, data.length - 6);
145
146     return d;
147   }
148
149   /**
150    * Gets the date
151    *
152    * @return the date
153    */

154   public Date JavaDoc getDate()
155   {
156     return dateRecord.getDate();
157   }
158
159   /**
160    * Indicates whether the date value contained in this cell refers to a date,
161    * or merely a time
162    *
163    * @return TRUE if the value refers to a time
164    */

165   public boolean isTime()
166   {
167     return dateRecord.isTime();
168   }
169
170   /**
171    * Gets the DateFormat used to format the cell. This will normally be
172    * the format specified in the excel spreadsheet, but in the event of any
173    * difficulty parsing this, it will revert to the default date/time format.
174    *
175    * @return the DateFormat object used to format the date in the original
176    * excel cell
177    */

178   public DateFormat JavaDoc getDateFormat()
179   {
180     return dateRecord.getDateFormat();
181   }
182
183 }
184
185
186
187
188
189
190
191
192
193
Popular Tags