1 */
108
109
110
111 package org.apache.poi.hssf.record;
112
113
114
115 import org.apache.poi.util.LittleEndian;
116
117
118
119 import java.util.ArrayList;
120
121
122
123 /**
124
125 * Title: Extern Sheet <P>
126
127 * Description: A List of Inndexes to SupBook <P>
128
129 * REFERENCE: <P>
130
131 * @author Libin Roman (Vista Portal LDT. Developer)
132
133 * @version 1.0-pre
134
135 */
136
137
138
139 public class ExternSheetRecord extends Record {
140
141 public final static short sid = 0x17;
142
143 private short field_1_number_of_REF_sturcutres;
144
145 private ArrayList field_2_REF_structures;
146
147
148
149 public ExternSheetRecord() {
150
151 field_2_REF_structures = new ArrayList();
152
153 }
154
155
156
157 /**
158
159 * Constructs a Extern Sheet record and sets its fields appropriately.
160
161 *
162
163 * @param id id must be 0x16 or an exception will be throw upon validation
164
165 * @param size the size of the data area of the record
166
167 * @param data data of the record (should not contain sid/len)
168
169 */
170
171
172
173 public ExternSheetRecord(short id, short size, byte[] data) {
174
175 super(id, size, data);
176
177 }
178
179
180
181 /**
182
183 * Constructs a Extern Sheet record and sets its fields appropriately.
184
185 *
186
187 * @param id id must be 0x16 or an exception will be throw upon validation
188
189 * @param size the size of the data area of the record
190
191 * @param data data of the record (should not contain sid/len)
192
193 * @param offset of the record's data
194
195 */
196
197 public ExternSheetRecord(short id, short size, byte[] data, int offset) {
198
199 super(id, size, data, offset);
200
201 }
202
203
204
205 /**
206
207 * called by constructor, should throw runtime exception in the event of a
208
209 * record passed with a differing ID.
210
211 *
212
213 * @param id alleged id for this record
214
215 */
216
217 protected void validateSid(short id) {
218
219 if (id != sid) {
220
221 throw new RecordFormatException("NOT An ExternSheet RECORD");
222
223 }
224
225 }
226
227
228
229 /**
230
231 * called by the constructor, should set class level fields. Should throw
232
233 * runtime exception for bad/icomplete data.
234
235 *
236
237 * @param data raw data
238
239 * @param size size of data
240
241 * @param offset of the record's data (provided a big array of the file)
242
243 */
244
245 protected void fillFields(byte [] data, short size, int offset) {
246
247 field_2_REF_structures = new ArrayList();
248
249
250
251 field_1_number_of_REF_sturcutres = LittleEndian.getShort(data, 0 + offset);
252
253
254
255 int pos = 2 + offset;
256
257 for (int i = 0 ; i < field_1_number_of_REF_sturcutres ; ++i) {
258
259 ExternSheetSubRecord rec = new ExternSheetSubRecord((short)0, (short)6 , data , pos);
260
261
262
263 pos += 6;
264
265
266
267 field_2_REF_structures.add( rec);
268
269 }
270
271 }
272
273
274
275 /** sets the number of the REF structors , that is in Excel file
276
277 * @param numStruct number of REF structs
278
279 */
280
281 public void setNumOfREFStructures(short numStruct) {
282
283 field_1_number_of_REF_sturcutres = numStruct;
284
285 }
286
287
288
289 /** return the number of the REF structors , that is in Excel file
290
291 * @return number of REF structs
292
293 */
294
295 public short getNumOfREFStructures() {
296
297 return field_1_number_of_REF_sturcutres;
298
299 }
300
301
302
303 /** adds REF struct (ExternSheetSubRecord)
304
305 * @param rec REF struct
306
307 */
308
309 public void addREFRecord(ExternSheetSubRecord rec) {
310
311 field_2_REF_structures.add(rec);
312
313 }
314
315
316
317 /** returns the number of REF Record , which is in model
318
319 * @return number of REF records
320
321 */
322
323 public int getNumOfREFRecord() {
324
325 return field_2_REF_structures.size();
326
327 }
328
329
330
331 /** return the REF record (ExternSheetSubRecord)
332
333 * @param elem index to place
334
335 * @return REF record
336
337 */
338
339 public ExternSheetSubRecord getREFRecordAt(int elem) {
340
341 ExternSheetSubRecord result = ( ExternSheetSubRecord ) field_2_REF_structures.get(elem);
342
343
344
345 return result;
346
347 }
348
349
350
351 public String toString() {
352
353 StringBuffer buffer = new StringBuffer();
354
355
356
357 return buffer.toString();
358
359 }
360
361
362
363 /**
364
365 * called by the class that is responsible for writing this sucker.
366
367 * Subclasses should implement this so that their data is passed back in a
368
369 * byte array.
370
371 *
372
373 * @param offset to begin writing at
374
375 * @param data byte array containing instance data
376
377 * @return number of bytes written
378
379 */
380
381 public int serialize(int offset, byte [] data) {
382
383 LittleEndian.putShort(data, 0 + offset, sid);
384
385 LittleEndian.putShort(data, 2 + offset,(short)(2 + (getNumOfREFRecord() *6)));
386
387
388
389 LittleEndian.putShort(data, 4 + offset, getNumOfREFStructures());
390
391
392
393 int pos = 6 ;
394
395
396
397 for (int k = 0; k < getNumOfREFRecord(); k++) {
398
399 ExternSheetSubRecord record = getREFRecordAt(k);
400
401 System.arraycopy(record.serialize(), 0, data, pos + offset, 6);
402
403
404
405 pos +=6;
406
407 }
408
409 return getRecordSize();
410
411 }
412
413
414
415 public int getRecordSize() {
416
417 return 4 + 2 + getNumOfREFRecord() * 6;
418
419 }
420
421
422
423 /**
424
425 * return the non static version of the id for this record.
426
427 */
428
429 public short getSid() {
430
431 return this.sid;
432
433 }
434
435 }
436
437 ???????????????ExternSheetRecord????????????????????????????????????????Record???????????????????????????????sid???????????????????????????????field_1_number_of_REF_sturcutres???????????????????????????????field_2_REF_structures????????????ExternSheetRecord?????????????????ExternSheetRecord???????????????id???????????????????size?????????????????????????data?????????????????ExternSheetRecord???????????????id???????????????????size?????????????????????????data???????????????????????????????offset?????????????????????????validateSid?????????????id???????????????????sid?????????????????????????fillFields?????????field_2_REF_structures?????????field_1_number_of_REF_sturcutres????????????????????????????????????????????LittleEndian?????????????????????????????????????????????????????????getShort??????????????????????????????????????????????????????????????????data????????????????????????????????????????????????????????????????????????????offset???????????????????????offset??????????????????????????i??????????????????????????????field_1_number_of_REF_sturcutres???????????????????????????????????????????????????????????????????i?????????????ExternSheetSubRecord??????????????????????????????????????????????????????????????????????????????????????data?????????????????????????????????????????????????????????????????????????????????????????????pos?????????????pos?????????????field_2_REF_structures?????????????????????????????????????????rec??????????????????????setNumOfREFStructures????????????????????????????????????????????numStruct???????????????????????getNumOfREFStructures??????????????????????addREFRecord??????????????????????????????ExternSheetSubRecord????????????????????????????????????rec?????????????????????getNumOfREFRecord????????????????field_2_REF_structures?????????????????ExternSheetSubRecord?????????????????????????????????getREFRecordAt?????????ExternSheetSubRecord????????????????????????????????????????????????????????????????field_2_REF_structures???????????????????????????????????????????????????????????????????????????????????????????elem????????????????result???????????????????toString????????????????buffer?????????????????????serialize?????????LittleEndian??????????????????????putShort???????????????????????????????data?????????????????????????????????????????offset?????????????????????????????????????????????????sid?????????LittleEndian??????????????????????putShort???????????????????????????????data?????????????????????????????????????????offset?????????????????????????????????????????????????????????????getNumOfREFRecord?????????LittleEndian??????????????????????putShort???????????????????????????????data?????????????????????????????????????????offset?????????????????????????????????????????????????getNumOfREFStructures?????????????????????????k?????????????????????????????getNumOfREFRecord??????????????????????????????????????????????????k?????????????ExternSheetSubRecord???????????????????????????????????????????getREFRecordAt??????????????????????????????????????????????????????????k??????????????????????????????record?????????????????????????????????????serialize?????????????????????????????????????????????????????data???????????????????????????????????????????????????????????pos?????????????????????????????????????????????????????????????????offset?????????????pos????????????????getRecordSize????????????????getRecordSize????????????????????????getNumOfREFRecord???????????????????????getSid