KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
24
25 /**
26  * Title: Extern Sheet <P>
27  * Description: A List of Inndexes to SupBook <P>
28  * REFERENCE: <P>
29  * @author Libin Roman (Vista Portal LDT. Developer)
30  * @version 1.0-pre
31  */

32
33 public class ExternSheetRecord extends Record {
34     public final static short sid = 0x17;
35     private short field_1_number_of_REF_sturcutres;
36     private ArrayList JavaDoc field_2_REF_structures;
37     
38     public ExternSheetRecord() {
39         field_2_REF_structures = new ArrayList JavaDoc();
40     }
41     
42     /**
43      * Constructs a Extern Sheet record and sets its fields appropriately.
44      *
45      * @param id id must be 0x16 or an exception will be throw upon validation
46      * @param size the size of the data area of the record
47      * @param data data of the record (should not contain sid/len)
48      */

49     
50     public ExternSheetRecord(short id, short size, byte[] data) {
51         super(id, size, data);
52     }
53     
54     /**
55      * Constructs a Extern Sheet record and sets its fields appropriately.
56      *
57      * @param id id must be 0x16 or an exception will be throw upon validation
58      * @param size the size of the data area of the record
59      * @param data data of the record (should not contain sid/len)
60      * @param offset of the record's data
61      */

62     public ExternSheetRecord(short id, short size, byte[] data, int offset) {
63         super(id, size, data, offset);
64     }
65     
66     /**
67      * called by constructor, should throw runtime exception in the event of a
68      * record passed with a differing ID.
69      *
70      * @param id alleged id for this record
71      */

72     protected void validateSid(short id) {
73         if (id != sid) {
74             throw new RecordFormatException("NOT An ExternSheet RECORD");
75         }
76     }
77     
78     /**
79      * called by the constructor, should set class level fields. Should throw
80      * runtime exception for bad/icomplete data.
81      *
82      * @param data raw data
83      * @param size size of data
84      * @param offset of the record's data (provided a big array of the file)
85      */

86     protected void fillFields(byte [] data, short size, int offset) {
87         field_2_REF_structures = new ArrayList JavaDoc();
88         
89         field_1_number_of_REF_sturcutres = LittleEndian.getShort(data, 0 + offset);
90         
91         int pos = 2 + offset;
92         for (int i = 0 ; i < field_1_number_of_REF_sturcutres ; ++i) {
93             ExternSheetSubRecord rec = new ExternSheetSubRecord((short)0, (short)6 , data , pos);
94             
95             pos += 6;
96             
97             field_2_REF_structures.add( rec);
98         }
99     }
100     
101     /**
102      * sets the number of the REF structors , that is in Excel file
103      * @param numStruct number of REF structs
104      */

105     public void setNumOfREFStructures(short numStruct) {
106         field_1_number_of_REF_sturcutres = numStruct;
107     }
108     
109     /**
110      * return the number of the REF structors , that is in Excel file
111      * @return number of REF structs
112      */

113     public short getNumOfREFStructures() {
114         return field_1_number_of_REF_sturcutres;
115     }
116     
117     /**
118      * adds REF struct (ExternSheetSubRecord)
119      * @param rec REF struct
120      */

121     public void addREFRecord(ExternSheetSubRecord rec) {
122         field_2_REF_structures.add(rec);
123     }
124     
125     /** returns the number of REF Records, which is in model
126      * @return number of REF records
127      */

128     public int getNumOfREFRecords() {
129         return field_2_REF_structures.size();
130     }
131     
132     /** returns the REF record (ExternSheetSubRecord)
133      * @param elem index to place
134      * @return REF record
135      */

136     public ExternSheetSubRecord getREFRecordAt(int elem) {
137         ExternSheetSubRecord result = ( ExternSheetSubRecord ) field_2_REF_structures.get(elem);
138         
139         return result;
140     }
141     
142     public String JavaDoc toString() {
143         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
144         
145         buffer.append("[EXTERNSHEET]\n");
146         buffer.append(" numOfRefs = ").append(getNumOfREFStructures()).append("\n");
147         for (int k=0; k < this.getNumOfREFRecords(); k++) {
148             buffer.append("refrec #").append(k).append('\n');
149             buffer.append(getREFRecordAt(k).toString());
150             buffer.append("----refrec #").append(k).append('\n');
151         }
152         buffer.append("[/EXTERNSHEET]\n");
153         
154         
155         return buffer.toString();
156     }
157     
158     /**
159      * called by the class that is responsible for writing this sucker.
160      * Subclasses should implement this so that their data is passed back in a
161      * byte array.
162      *
163      * @param offset to begin writing at
164      * @param data byte array containing instance data
165      * @return number of bytes written
166      */

167     public int serialize(int offset, byte [] data) {
168         LittleEndian.putShort(data, 0 + offset, sid);
169         LittleEndian.putShort(data, 2 + offset,(short)(2 + (getNumOfREFRecords() *6)));
170         
171         LittleEndian.putShort(data, 4 + offset, getNumOfREFStructures());
172         
173         int pos = 6 ;
174         
175         for (int k = 0; k < getNumOfREFRecords(); k++) {
176             ExternSheetSubRecord record = getREFRecordAt(k);
177             System.arraycopy(record.serialize(), 0, data, pos + offset, 6);
178             
179             pos +=6;
180         }
181         return getRecordSize();
182     }
183     
184     public int getRecordSize() {
185         return 4 + 2 + getNumOfREFRecords() * 6;
186     }
187     
188     /**
189      * return the non static version of the id for this record.
190      */

191     public short getSid() {
192         return this.sid;
193     }
194 }
195
Popular Tags