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.IntList;
59   import org.apache.poi.util.LittleEndian;
60   
61   /**
62    * Title:        Index Record<P>
63    * Description:  Occurs right after BOF, tells you where the DBCELL records are for a sheet
64    *               Important for locating cells<P>
65    * NOT USED IN THIS RELEASE
66    * REFERENCE:  PG 323 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
67    * @author Andrew C. Oliver (acoliver at apache dot org)
68    * @version 2.0-pre
69    */
70   
71   public class IndexRecord
72       extends Record
73   {
74       public final static short sid             = 0x20B;
75       public final static int   DBCELL_CAPACITY = 30;
76       public int                field_1_zero;            // reserved must be 0
77       public int                field_2_first_row;       // first row on the sheet
78       public int                field_3_last_row_add1;   // last row
79       public int                field_4_zero;            // reserved must be 0
80       public IntList            field_5_dbcells;         // array of offsets to DBCELL records
81   
82       public IndexRecord()
83       {
84       }
85   
86       /**
87        * Constructs an Index record and sets its fields appropriately.
88        *
89        * @param id     id must be 0x208 or an exception will be throw upon validation
90        * @param size  the size of the data area of the record
91        * @param data  data of the record (should not contain sid/len)
92        */
93   
94       public IndexRecord(short id, short size, byte [] data)
95       {
96           super(id, size, data);
97       }
98   
99       /**
100       * Constructs an Index record and sets its fields appropriately.
101       *
102       * @param id     id must be 0x208 or an exception will be throw upon validation
103       * @param size  the size of the data area of the record
104       * @param data  data of the record (should not contain sid/len)
105       * @param offset of record data
106       */
107  
108      public IndexRecord(short id, short size, byte [] data, int offset)
109      {
110          super(id, size, data, offset);
111      }
112  
113      protected void validateSid(short id)
114      {
115          if (id != sid)
116          {
117              throw new RecordFormatException("NOT An Index RECORD");
118          }
119      }
120  
121      protected void fillFields(byte [] data, short size, int offset)
122      {
123          field_5_dbcells       =
124              new IntList(DBCELL_CAPACITY);   // initial capacity of 30
125          field_1_zero          = LittleEndian.getInt(data, 0 + offset);
126          field_2_first_row     = LittleEndian.getInt(data, 4 + offset);
127          field_3_last_row_add1 = LittleEndian.getInt(data, 8 + offset);
128          field_4_zero          = LittleEndian.getInt(data, 12 + offset);
129          for (int k = 16; k < size; k = k + 4)
130          {
131  
132              // System.out.println("getting " + k);
133              field_5_dbcells.add(LittleEndian.getInt(data, k + offset));
134          }
135      }
136  
137      public void setFirstRow(int row)
138      {
139          field_2_first_row = row;
140      }
141  
142      public void setLastRowAdd1(int row)
143      {
144          field_3_last_row_add1 = row;
145      }
146  
147      public void addDbcell(int cell)
148      {
149          if (field_5_dbcells == null)
150          {
151              field_5_dbcells = new IntList();
152          }
153          field_5_dbcells.add(cell);
154      }
155  
156      public void setDbcell(int cell, int value)
157      {
158          field_5_dbcells.set(cell, value);
159      }
160  
161      public int getFirstRow()
162      {
163          return field_2_first_row;
164      }
165  
166      public int getLastRowAdd1()
167      {
168          return field_3_last_row_add1;
169      }
170  
171      public int getNumDbcells()
172      {
173          if (field_5_dbcells == null)
174          {
175              return 0;
176          }
177          return field_5_dbcells.size();
178      }
179  
180      public int getDbcellAt(int cellnum)
181      {
182          return field_5_dbcells.get(cellnum);
183      }
184  
185      public String toString()
186      {
187          StringBuffer buffer = new StringBuffer();
188  
189          buffer.append("[INDEX]\n");
190          buffer.append("    .firstrow       = ")
191              .append(Integer.toHexString(getFirstRow())).append("\n");
192          buffer.append("    .lastrowadd1    = ")
193              .append(Integer.toHexString(getLastRowAdd1())).append("\n");
194          for (int k = 0; k < getNumDbcells(); k++)
195          {
196              buffer.append("    .dbcell_" + k + "       = ")
197                  .append(Integer.toHexString(getDbcellAt(k))).append("\n");
198          }
199          buffer.append("[/INDEX]\n");
200          return buffer.toString();
201      }
202  
203      public int serialize(int offset, byte [] data)
204      {
205          LittleEndian.putShort(data, 0 + offset, sid);
206          LittleEndian.putShort(data, 2 + offset,
207                                ( short ) (16 + (getNumDbcells() * 4)));
208          LittleEndian.putInt(data, 4 + offset, 0);
209          LittleEndian.putInt(data, 8 + offset, getFirstRow());
210          LittleEndian.putInt(data, 12 + offset, getLastRowAdd1());
211          LittleEndian.putInt(data, 16 + offset, 0);
212          for (int k = 0; k < getNumDbcells(); k++)
213          {
214              LittleEndian.putInt(data, (k * 4) + 20 + offset, getDbcellAt(k));
215          }
216          return getRecordSize();
217      }
218  
219      public int getRecordSize()
220      {
221          return 20 + (getNumDbcells() * 4);
222      }
223  
224      public short getSid()
225      {
226          return this.sid;
227      }
228  }
229