001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.record;
019:
020: import org.apache.poi.util.LittleEndian;
021:
022: /**
023: * Title: Beginning Of File<P>
024: * Description: Somewhat of a misnomer, its used for the beginning of a set of
025: * records that have a particular pupose or subject.
026: * Used in sheets and workbooks.<P>
027: * REFERENCE: PG 289 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
028: * @author Andrew C. Oliver
029: * @author Jason Height (jheight at chariot dot net dot au)
030: * @version 2.0-pre
031: */
032:
033: public class BOFRecord extends Record {
034:
035: /**
036: * for BIFF8 files the BOF is 0x809. For earlier versions it was 0x09 or 0x(biffversion)09
037: */
038:
039: public final static short sid = 0x809;
040: private short field_1_version;
041: private short field_2_type;
042: private short field_3_build;
043: private short field_4_year;
044: private int field_5_history;
045: private int field_6_rversion;
046:
047: /**
048: * suggested default (0x06 - BIFF8)
049: */
050:
051: public final static short VERSION = 0x06;
052:
053: /**
054: * suggested default 0x10d3
055: */
056:
057: public final static short BUILD = 0x10d3;
058:
059: /**
060: * suggested default 0x07CC (1996)
061: */
062:
063: public final static short BUILD_YEAR = 0x07CC; // 1996
064:
065: /**
066: * suggested default for a normal sheet (0x41)
067: */
068:
069: public final static short HISTORY_MASK = 0x41;
070: public final static short TYPE_WORKBOOK = 0x05;
071: public final static short TYPE_VB_MODULE = 0x06;
072: public final static short TYPE_WORKSHEET = 0x10;
073: public final static short TYPE_CHART = 0x20;
074: public final static short TYPE_EXCEL_4_MACRO = 0x40;
075: public final static short TYPE_WORKSPACE_FILE = 0x100;
076:
077: /**
078: * Constructs an empty BOFRecord with no fields set.
079: */
080:
081: public BOFRecord() {
082: }
083:
084: /**
085: * Constructs a BOFRecord and sets its fields appropriately
086: * @param in the RecordInputstream to read the record from
087: */
088:
089: public BOFRecord(RecordInputStream in) {
090: super (in);
091:
092: // fillFields(data,size);
093: }
094:
095: protected void validateSid(short id) {
096: if (id != sid) {
097: throw new RecordFormatException("NOT A BOF RECORD");
098: }
099: }
100:
101: protected void fillFields(RecordInputStream in) {
102: field_1_version = in.readShort();
103: field_2_type = in.readShort();
104:
105: // Some external tools don't generate all of
106: // the remaining fields
107: if (in.remaining() >= 2) {
108: field_3_build = in.readShort();
109: }
110: if (in.remaining() >= 2) {
111: field_4_year = in.readShort();
112: }
113: if (in.remaining() >= 4) {
114: field_5_history = in.readInt();
115: }
116: if (in.remaining() >= 4) {
117: field_6_rversion = in.readInt();
118: }
119: }
120:
121: /**
122: * Version number - for BIFF8 should be 0x06
123: * @see #VERSION
124: * @param version version to be set
125: */
126:
127: public void setVersion(short version) {
128: field_1_version = version;
129: }
130:
131: /**
132: * type of object this marks
133: * @see #TYPE_WORKBOOK
134: * @see #TYPE_VB_MODULE
135: * @see #TYPE_WORKSHEET
136: * @see #TYPE_CHART
137: * @see #TYPE_EXCEL_4_MACRO
138: * @see #TYPE_WORKSPACE_FILE
139: * @param type type to be set
140: */
141:
142: public void setType(short type) {
143: field_2_type = type;
144: }
145:
146: /**
147: * build that wrote this file
148: * @see #BUILD
149: * @param build build number to set
150: */
151:
152: public void setBuild(short build) {
153: field_3_build = build;
154: }
155:
156: /**
157: * Year of the build that wrote this file
158: * @see #BUILD_YEAR
159: * @param year build year to set
160: */
161:
162: public void setBuildYear(short year) {
163: field_4_year = year;
164: }
165:
166: /**
167: * set the history bit mask (not very useful)
168: * @see #HISTORY_MASK
169: * @param bitmask bitmask to set for the history
170: */
171:
172: public void setHistoryBitMask(int bitmask) {
173: field_5_history = bitmask;
174: }
175:
176: /**
177: * set the minimum version required to read this file
178: *
179: * @see #VERSION
180: * @param version version to set
181: */
182:
183: public void setRequiredVersion(int version) {
184: field_6_rversion = version;
185: }
186:
187: /**
188: * Version number - for BIFF8 should be 0x06
189: * @see #VERSION
190: * @return short version number of the generator of this file
191: */
192:
193: public short getVersion() {
194: return field_1_version;
195: }
196:
197: /**
198: * type of object this marks
199: * @see #TYPE_WORKBOOK
200: * @see #TYPE_VB_MODULE
201: * @see #TYPE_WORKSHEET
202: * @see #TYPE_CHART
203: * @see #TYPE_EXCEL_4_MACRO
204: * @see #TYPE_WORKSPACE_FILE
205: * @return short type of object
206: */
207:
208: public short getType() {
209: return field_2_type;
210: }
211:
212: /**
213: * get the build that wrote this file
214: * @see #BUILD
215: * @return short build number of the generator of this file
216: */
217:
218: public short getBuild() {
219: return field_3_build;
220: }
221:
222: /**
223: * Year of the build that wrote this file
224: * @see #BUILD_YEAR
225: * @return short build year of the generator of this file
226: */
227:
228: public short getBuildYear() {
229: return field_4_year;
230: }
231:
232: /**
233: * get the history bit mask (not very useful)
234: * @see #HISTORY_MASK
235: * @return int bitmask showing the history of the file (who cares!)
236: */
237:
238: public int getHistoryBitMask() {
239: return field_5_history;
240: }
241:
242: /**
243: * get the minimum version required to read this file
244: *
245: * @see #VERSION
246: * @return int least version that can read the file
247: */
248:
249: public int getRequiredVersion() {
250: return field_6_rversion;
251: }
252:
253: public String toString() {
254: StringBuffer buffer = new StringBuffer();
255:
256: buffer.append("[BOF RECORD]\n");
257: buffer.append(" .version = ").append(
258: Integer.toHexString(getVersion())).append("\n");
259: buffer.append(" .type = ").append(
260: Integer.toHexString(getType())).append("\n");
261: buffer.append(" .build = ").append(
262: Integer.toHexString(getBuild())).append("\n");
263: buffer.append(" .buildyear = ").append(getBuildYear())
264: .append("\n");
265: buffer.append(" .history = ").append(
266: Integer.toHexString(getHistoryBitMask())).append("\n");
267: buffer.append(" .requiredversion = ").append(
268: Integer.toHexString(getRequiredVersion())).append("\n");
269: buffer.append("[/BOF RECORD]\n");
270: return buffer.toString();
271: }
272:
273: public int serialize(int offset, byte[] data) {
274: LittleEndian.putShort(data, 0 + offset, sid);
275: LittleEndian.putShort(data, 2 + offset, ((short) 0x10)); // 16 byte length
276: LittleEndian.putShort(data, 4 + offset, getVersion());
277: LittleEndian.putShort(data, 6 + offset, getType());
278: LittleEndian.putShort(data, 8 + offset, getBuild());
279: LittleEndian.putShort(data, 10 + offset, getBuildYear());
280: LittleEndian.putInt(data, 12 + offset, getHistoryBitMask());
281: LittleEndian.putInt(data, 16 + offset, getRequiredVersion());
282: return getRecordSize();
283: }
284:
285: public int getRecordSize() {
286: return 20;
287: }
288:
289: public short getSid() {
290: return sid;
291: }
292:
293: public Object clone() {
294: BOFRecord rec = new BOFRecord();
295: rec.field_1_version = field_1_version;
296: rec.field_2_type = field_2_type;
297: rec.field_3_build = field_3_build;
298: rec.field_4_year = field_4_year;
299: rec.field_5_history = field_5_history;
300: rec.field_6_rversion = field_6_rversion;
301: return rec;
302: }
303: }
|