KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > BOFRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.record;
20
21 import org.apache.poi.util.LittleEndian;
22
23 /**
24  * Title: Beginning Of File<P>
25  * Description: Somewhat of a misnomer, its used for the beginning of a set of
26  * records that have a particular pupose or subject.
27  * Used in sheets and workbooks.<P>
28  * REFERENCE: PG 289 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
29  * @author Andrew C. Oliver
30  * @author Jason Height (jheight at chariot dot net dot au)
31  * @version 2.0-pre
32  */

33
34 public class BOFRecord
35     extends Record
36 {
37
38     /**
39      * for BIFF8 files the BOF is 0x809. For earlier versions it was 0x09 or 0x(biffversion)09
40      */

41
42     public final static short sid = 0x809;
43     private short field_1_version;
44     private short field_2_type;
45     private short field_3_build;
46     private short field_4_year;
47     private int field_5_history;
48     private int field_6_rversion;
49
50     /**
51      * suggested default (0x06 - BIFF8)
52      */

53
54     public final static short VERSION = 0x06;
55
56     /**
57      * suggested default 0x10d3
58      */

59
60     public final static short BUILD = 0x10d3;
61
62     /**
63      * suggested default 0x07CC (1996)
64      */

65
66     public final static short BUILD_YEAR = 0x07CC; // 1996
67

68     /**
69      * suggested default for a normal sheet (0x41)
70      */

71
72     public final static short HISTORY_MASK = 0x41;
73     public final static short TYPE_WORKBOOK = 0x05;
74     public final static short TYPE_VB_MODULE = 0x06;
75     public final static short TYPE_WORKSHEET = 0x10;
76     public final static short TYPE_CHART = 0x20;
77     public final static short TYPE_EXCEL_4_MACRO = 0x40;
78     public final static short TYPE_WORKSPACE_FILE = 0x100;
79
80     /**
81      * Constructs an empty BOFRecord with no fields set.
82      */

83
84     public BOFRecord()
85     {
86     }
87
88     /**
89      * Constructs a BOFRecord and sets its fields appropriately
90      *
91      * @param id id must be 0x809 or an exception will be throw upon validation
92      * @param size the size of the data area of the record
93      * @param data data of the record (should not contain sid/len)
94      */

95
96     public BOFRecord(short id, short size, byte [] data)
97     {
98         super(id, size, data);
99
100         // fillFields(data,size);
101
}
102
103     /**
104      * Constructs a BOFRecord and sets its fields appropriately
105      *
106      * @param id id must be 0x809 or an exception will be throw upon validation
107      * @param size the size of the data area of the record
108      * @param data data of the record (should not contain sid/len)
109      * @param offset the offset of the record's data
110      */

111
112     public BOFRecord(short id, short size, byte [] data, int offset)
113     {
114         super(id, size, data, offset);
115
116         // fillFields(data,size);
117
}
118
119     protected void validateSid(short id)
120     {
121         if (id != sid)
122         {
123             throw new RecordFormatException("NOT A BOF RECORD");
124         }
125     }
126
127     protected void fillFields(byte [] data, short size, int offset)
128     {
129         field_1_version = LittleEndian.getShort(data, 0 + offset);
130         field_2_type = LittleEndian.getShort(data, 2 + offset);
131         field_3_build = LittleEndian.getShort(data, 4 + offset);
132         field_4_year = LittleEndian.getShort(data, 6 + offset);
133         field_5_history = LittleEndian.getInt(data, 8 + offset);
134         field_6_rversion = LittleEndian.getInt(data, 12 + offset);
135     }
136
137     /**
138      * Version number - for BIFF8 should be 0x06
139      * @see #VERSION
140      * @param version version to be set
141      */

142
143     public void setVersion(short version)
144     {
145         field_1_version = version;
146     }
147
148     /**
149      * type of object this marks
150      * @see #TYPE_WORKBOOK
151      * @see #TYPE_VB_MODULE
152      * @see #TYPE_WORKSHEET
153      * @see #TYPE_CHART
154      * @see #TYPE_EXCEL_4_MACRO
155      * @see #TYPE_WORKSPACE_FILE
156      * @param type type to be set
157      */

158
159     public void setType(short type)
160     {
161         field_2_type = type;
162     }
163
164     /**
165      * build that wrote this file
166      * @see #BUILD
167      * @param build build number to set
168      */

169
170     public void setBuild(short build)
171     {
172         field_3_build = build;
173     }
174
175     /**
176      * Year of the build that wrote this file
177      * @see #BUILD_YEAR
178      * @param year build year to set
179      */

180
181     public void setBuildYear(short year)
182     {
183         field_4_year = year;
184     }
185
186     /**
187      * set the history bit mask (not very useful)
188      * @see #HISTORY_MASK
189      * @param bitmask bitmask to set for the history
190      */

191
192     public void setHistoryBitMask(int bitmask)
193     {
194         field_5_history = bitmask;
195     }
196
197     /**
198      * set the minimum version required to read this file
199      *
200      * @see #VERSION
201      * @param version version to set
202      */

203
204     public void setRequiredVersion(int version)
205     {
206         field_6_rversion = version;
207     }
208
209     /**
210      * Version number - for BIFF8 should be 0x06
211      * @see #VERSION
212      * @return short version number of the generator of this file
213      */

214
215     public short getVersion()
216     {
217         return field_1_version;
218     }
219
220     /**
221      * type of object this marks
222      * @see #TYPE_WORKBOOK
223      * @see #TYPE_VB_MODULE
224      * @see #TYPE_WORKSHEET
225      * @see #TYPE_CHART
226      * @see #TYPE_EXCEL_4_MACRO
227      * @see #TYPE_WORKSPACE_FILE
228      * @return short type of object
229      */

230
231     public short getType()
232     {
233         return field_2_type;
234     }
235
236     /**
237      * get the build that wrote this file
238      * @see #BUILD
239      * @return short build number of the generator of this file
240      */

241
242     public short getBuild()
243     {
244         return field_3_build;
245     }
246
247     /**
248      * Year of the build that wrote this file
249      * @see #BUILD_YEAR
250      * @return short build year of the generator of this file
251      */

252
253     public short getBuildYear()
254     {
255         return field_4_year;
256     }
257
258     /**
259      * get the history bit mask (not very useful)
260      * @see #HISTORY_MASK
261      * @return int bitmask showing the history of the file (who cares!)
262      */

263
264     public int getHistoryBitMask()
265     {
266         return field_5_history;
267     }
268
269     /**
270      * get the minimum version required to read this file
271      *
272      * @see #VERSION
273      * @return int least version that can read the file
274      */

275
276     public int getRequiredVersion()
277     {
278         return field_6_rversion;
279     }
280
281     public String JavaDoc toString()
282     {
283         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
284
285         buffer.append("[BOF RECORD]\n");
286         buffer.append(" .version = ")
287             .append(Integer.toHexString(getVersion())).append("\n");
288         buffer.append(" .type = ")
289             .append(Integer.toHexString(getType())).append("\n");
290         buffer.append(" .build = ")
291             .append(Integer.toHexString(getBuild())).append("\n");
292         buffer.append(" .buildyear = ").append(getBuildYear())
293             .append("\n");
294         buffer.append(" .history = ")
295             .append(Integer.toHexString(getHistoryBitMask())).append("\n");
296         buffer.append(" .requiredversion = ")
297             .append(Integer.toHexString(getRequiredVersion())).append("\n");
298         buffer.append("[/BOF RECORD]\n");
299         return buffer.toString();
300     }
301
302     public int serialize(int offset, byte [] data)
303     {
304         LittleEndian.putShort(data, 0 + offset, sid);
305         LittleEndian.putShort(data, 2 + offset,
306                               (( short ) 0x10)); // 16 byte length
307
LittleEndian.putShort(data, 4 + offset, getVersion());
308         LittleEndian.putShort(data, 6 + offset, getType());
309         LittleEndian.putShort(data, 8 + offset, getBuild());
310         LittleEndian.putShort(data, 10 + offset, getBuildYear());
311         LittleEndian.putInt(data, 12 + offset, getHistoryBitMask());
312         LittleEndian.putInt(data, 16 + offset, getRequiredVersion());
313         return getRecordSize();
314     }
315
316     public int getRecordSize()
317     {
318         return 20;
319     }
320
321     public short getSid()
322     {
323         return this.sid;
324     }
325
326     public Object JavaDoc clone() {
327       BOFRecord rec = new BOFRecord();
328       rec.field_1_version = field_1_version;
329       rec.field_2_type = field_2_type;
330       rec.field_3_build = field_3_build;
331       rec.field_4_year = field_4_year;
332       rec.field_5_history = field_5_history;
333       rec.field_6_rversion = field_6_rversion;
334       return rec;
335     }
336 }
337
Popular Tags