1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   package org.apache.poi.hssf.record;
57   
58   import org.apache.poi.util.LittleEndian;
59   
60   import java.util.ArrayList;
61   
62   /**
63    * Title:        Extended Static String Table<P>
64    * Description:  I really don't understand this thing... its supposed to be "a hash
65    *               table for optimizing external copy operations"  --
66    *<P>
67    *               This sounds like a job for Marc "BitMaster" Johnson aka the
68    *               "Hawaiian Master Chef".<P>
69    * REFERENCE:  PG 313 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
70    * @author Andrew C. Oliver (acoliver at apache dot org)
71    * @version 2.0-pre
72    * @see org.apache.poi.hssf.record.ExtSSTInfoSubRecord
73    */
74   
75   public class ExtSSTRecord
76       extends Record
77   {
78       public final static short sid = 0xff;
79       private short             field_1_strings_per_bucket;
80       private ArrayList         field_2_sst_info;
81   
82       public ExtSSTRecord()
83       {
84           field_2_sst_info = new ArrayList();
85       }
86   
87       /**
88        * Constructs a EOFRecord record and sets its fields appropriately.
89        *
90        * @param id     id must be 0xff or an exception will be throw upon validation
91        * @param size  the size of the data area of the record
92        * @param data  data of the record (should not contain sid/len)
93        */
94   
95       public ExtSSTRecord(short id, short size, byte [] data)
96       {
97           super(id, size, data);
98       }
99   
100      /**
101       * Constructs a EOFRecord record and sets its fields appropriately.
102       *
103       * @param id     id must be 0xff or an exception will be throw upon validation
104       * @param size  the size of the data area of the record
105       * @param data  data of the record (should not contain sid/len)
106       * @param offset of the record's data
107       */
108  
109      public ExtSSTRecord(short id, short size, byte [] data, int offset)
110      {
111          super(id, size, data, offset);
112      }
113  
114      protected void validateSid(short id)
115      {
116          if (id != sid)
117          {
118              throw new RecordFormatException("NOT An EXTSST RECORD");
119          }
120      }
121  
122      // this probably doesn't work but we don't really care at this point
123      protected void fillFields(byte [] data, short size, int offset)
124      {
125          field_2_sst_info           = new ArrayList();
126          field_1_strings_per_bucket = LittleEndian.getShort(data, 0 + offset);
127          for (int k = 2; k < ((data.length - offset) - size); k += 8)
128          {
129              byte[] tempdata = new byte[ 8 + offset ];
130  
131              System.arraycopy(data, k, tempdata, 0, 8);
132              ExtSSTInfoSubRecord rec = new ExtSSTInfoSubRecord(( short ) 0,
133                                            ( short ) 8, tempdata);
134  
135              field_2_sst_info.add(rec);
136          }
137      }
138  
139      public void setNumStringsPerBucket(short numStrings)
140      {
141          field_1_strings_per_bucket = numStrings;
142      }
143  
144      public void addInfoRecord(ExtSSTInfoSubRecord rec)
145      {
146          field_2_sst_info.add(rec);
147      }
148  
149      public short getNumStringsPerBucket()
150      {
151          return field_1_strings_per_bucket;
152      }
153  
154      public int getNumInfoRecords()
155      {
156          return field_2_sst_info.size();
157      }
158  
159      public ExtSSTInfoSubRecord getInfoRecordAt(int elem)
160      {
161          return ( ExtSSTInfoSubRecord ) field_2_sst_info.get(elem);
162      }
163  
164      public String toString()
165      {
166          StringBuffer buffer = new StringBuffer();
167  
168          buffer.append("[EXTSST]\n");
169          buffer.append("    .dsst           = ")
170              .append(Integer.toHexString(getNumStringsPerBucket()))
171              .append("\n");
172          buffer.append("    .numInfoRecords = ").append(getNumInfoRecords())
173              .append("\n");
174          for (int k = 0; k < getNumInfoRecords(); k++)
175          {
176              buffer.append("    .inforecord     = ").append(k).append("\n");
177              buffer.append("    .streampos      = ")
178                  .append(Integer
179                  .toHexString(getInfoRecordAt(k).getStreamPos())).append("\n");
180              buffer.append("    .sstoffset      = ")
181                  .append(Integer
182                  .toHexString(getInfoRecordAt(k).getBucketSSTOffset()))
183                      .append("\n");
184          }
185          buffer.append("[/EXTSST]\n");
186          return buffer.toString();
187      }
188  
189      public int serialize(int offset, byte [] data)
190      {
191          LittleEndian.putShort(data, 0 + offset, sid);
192  
193  //    LittleEndian.putShort(data,2,(short)(2 + (getNumInfoRecords() *8)));
194          LittleEndian.putShort(data, 2 + offset, ( short ) (2 + (0x3fa - 2)));
195          int pos = 4;
196  
197          for (int k = 0; k < getNumInfoRecords(); k++)
198          {
199              System.arraycopy(getInfoRecordAt(k).serialize(), 0, data,
200                               pos + offset, 8);
201          }
202          return getRecordSize();
203      }
204  
205      public int getRecordSize()
206      {
207          return 6 + 0x3fa - 2;
208      }
209  
210      public short getSid()
211      {
212          return this.sid;
213      }
214  }
215