KickJava   Java API By Example, From Geeks To Geeks.

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


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: Extended Static String Table<P>
27  * Description: This record is used for a quick lookup into the SST record. This
28  * record breaks the SST table into a set of buckets. The offsets
29  * to these buckets within the SST record are kept as well as the
30  * position relative to the start of the SST record.
31  * REFERENCE: PG 313 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
32  * @author Andrew C. Oliver (acoliver at apache dot org)
33  * @author Jason Height (jheight at apache dot org)
34  * @version 2.0-pre
35  * @see org.apache.poi.hssf.record.ExtSSTInfoSubRecord
36  */

37
38 public class ExtSSTRecord
39     extends Record
40 {
41     public static final int DEFAULT_BUCKET_SIZE = 8;
42     //Cant seem to find this documented but from the biffviewer it is clear that
43
//Excel only records the indexes for the first 128 buckets.
44
public static final int MAX_BUCKETS = 128;
45     public final static short sid = 0xff;
46     private short field_1_strings_per_bucket = DEFAULT_BUCKET_SIZE;
47     private ArrayList JavaDoc field_2_sst_info;
48
49
50     public ExtSSTRecord()
51     {
52         field_2_sst_info = new ArrayList JavaDoc();
53     }
54
55     /**
56      * Constructs a EOFRecord record and sets its fields appropriately.
57      *
58      * @param id id must be 0xff or an exception will be throw upon validation
59      * @param size the size of the data area of the record
60      * @param data data of the record (should not contain sid/len)
61      */

62
63     public ExtSSTRecord(short id, short size, byte [] data)
64     {
65         super(id, size, data);
66     }
67
68     /**
69      * Constructs a EOFRecord record and sets its fields appropriately.
70      *
71      * @param id id must be 0xff or an exception will be throw upon validation
72      * @param size the size of the data area of the record
73      * @param data data of the record (should not contain sid/len)
74      * @param offset of the record's data
75      */

76
77     public ExtSSTRecord(short id, short size, byte [] data, int offset)
78     {
79         super(id, size, data, offset);
80     }
81
82     protected void validateSid(short id)
83     {
84         if (id != sid)
85         {
86             throw new RecordFormatException("NOT An EXTSST RECORD");
87         }
88     }
89
90     protected void fillFields(byte [] data, short size, int offset)
91     {
92         field_2_sst_info = new ArrayList JavaDoc();
93         field_1_strings_per_bucket = LittleEndian.getShort(data, 0 + offset);
94         for (int k = 2; k < (size-offset); k += 8)
95         {
96             byte[] tempdata = new byte[ 8 + offset ];
97
98             System.arraycopy(data, k, tempdata, 0, 8);
99             ExtSSTInfoSubRecord rec = new ExtSSTInfoSubRecord(( short ) 0,
100                                           ( short ) 8, tempdata);
101
102             field_2_sst_info.add(rec);
103         }
104     }
105
106     public void setNumStringsPerBucket(short numStrings)
107     {
108         field_1_strings_per_bucket = numStrings;
109     }
110
111     public void addInfoRecord(ExtSSTInfoSubRecord rec)
112     {
113         field_2_sst_info.add(rec);
114     }
115
116     public short getNumStringsPerBucket()
117     {
118         return field_1_strings_per_bucket;
119     }
120
121     public int getNumInfoRecords()
122     {
123         return field_2_sst_info.size();
124     }
125
126     public ExtSSTInfoSubRecord getInfoRecordAt(int elem)
127     {
128         return ( ExtSSTInfoSubRecord ) field_2_sst_info.get(elem);
129     }
130
131     public String JavaDoc toString()
132     {
133         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
134
135         buffer.append("[EXTSST]\n");
136         buffer.append(" .dsst = ")
137             .append(Integer.toHexString(getNumStringsPerBucket()))
138             .append("\n");
139         buffer.append(" .numInfoRecords = ").append(getNumInfoRecords())
140             .append("\n");
141         for (int k = 0; k < getNumInfoRecords(); k++)
142         {
143             buffer.append(" .inforecord = ").append(k).append("\n");
144             buffer.append(" .streampos = ")
145                 .append(Integer
146                 .toHexString(getInfoRecordAt(k).getStreamPos())).append("\n");
147             buffer.append(" .sstoffset = ")
148                 .append(Integer
149                 .toHexString(getInfoRecordAt(k).getBucketSSTOffset()))
150                     .append("\n");
151         }
152         buffer.append("[/EXTSST]\n");
153         return buffer.toString();
154     }
155
156     public int serialize(int offset, byte [] data)
157     {
158         LittleEndian.putShort(data, 0 + offset, sid);
159         LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));
160         LittleEndian.putShort(data, 4 + offset, field_1_strings_per_bucket);
161         int pos = 6;
162
163         for (int k = 0; k < getNumInfoRecords(); k++)
164         {
165             ExtSSTInfoSubRecord rec = getInfoRecordAt(k);
166             pos += rec.serialize(pos + offset, data);
167         }
168         return pos;
169     }
170
171     /** Returns the size of this record */
172     public int getRecordSize()
173     {
174         return 6 + 8*getNumInfoRecords();
175     }
176
177     public static final int getNumberOfInfoRecsForStrings(int numStrings) {
178       int infoRecs = (numStrings / DEFAULT_BUCKET_SIZE);
179       if ((numStrings % DEFAULT_BUCKET_SIZE) != 0)
180         infoRecs ++;
181       //Excel seems to max out after 128 info records.
182
//This isnt really documented anywhere...
183
if (infoRecs > MAX_BUCKETS)
184         infoRecs = MAX_BUCKETS;
185       return infoRecs;
186     }
187
188     /** Given a number of strings (in the sst), returns the size of the extsst record*/
189     public static final int getRecordSizeForStrings(int numStrings) {
190       return 4 + 2 + (getNumberOfInfoRecsForStrings(numStrings) * 8);
191     }
192
193     public short getSid()
194     {
195         return sid;
196     }
197
198     public void setBucketOffsets( int[] bucketAbsoluteOffsets, int[] bucketRelativeOffsets )
199     {
200         this.field_2_sst_info = new ArrayList JavaDoc(bucketAbsoluteOffsets.length);
201         for ( int i = 0; i < bucketAbsoluteOffsets.length; i++ )
202         {
203             ExtSSTInfoSubRecord r = new ExtSSTInfoSubRecord();
204             r.setBucketRecordOffset((short)bucketRelativeOffsets[i]);
205             r.setStreamPos(bucketAbsoluteOffsets[i]);
206             field_2_sst_info.add(r);
207         }
208     }
209
210 }
211
Popular Tags