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    * @author Jason Height (jheight at chariot dot net dot au)
69    * @version 2.0-pre
70    */
71   
72   public class IndexRecord
73       extends Record
74   {
75       public final static short sid             = 0x20B;
76       public final static int   DBCELL_CAPACITY = 30;
77       public int                field_1_zero;            // reserved must be 0
78       public int                field_2_first_row;       // first row on the sheet
79       public int                field_3_last_row_add1;   // last row
80       public int                field_4_zero;            // reserved must be 0
81       public IntList            field_5_dbcells;         // array of offsets to DBCELL records
82   
83       public IndexRecord()
84       {
85       }
86   
87       /**
88        * Constructs an Index record and sets its fields appropriately.
89        *
90        * @param id     id must be 0x208 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 IndexRecord(short id, short size, byte [] data)
96       {
97           super(id, size, data);
98       }
99   
100      /**
101       * Constructs an Index record and sets its fields appropriately.
102       *
103       * @param id     id must be 0x208 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 record data
107       */
108  
109      public IndexRecord(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 Index RECORD");
119          }
120      }
121  
122      protected void fillFields(byte [] data, short size, int offset)
123      {
124          field_5_dbcells       =
125              new IntList(DBCELL_CAPACITY);   // initial capacity of 30
126          field_1_zero          = LittleEndian.getInt(data, 0 + offset);
127          field_2_first_row     = LittleEndian.getInt(data, 4 + offset);
128          field_3_last_row_add1 = LittleEndian.getInt(data, 8 + offset);
129          field_4_zero          = LittleEndian.getInt(data, 12 + offset);
130          for (int k = 16; k < size; k = k + 4)
131          {
132  
133              // System.out.println("getting " + k);
134              field_5_dbcells.add(LittleEndian.getInt(data, k + offset));
135          }
136      }
137  
138      public void setFirstRow(int row)
139      {
140          field_2_first_row = row;
141      }
142  
143      public void setLastRowAdd1(int row)
144      {
145          field_3_last_row_add1 = row;
146      }
147  
148      public void addDbcell(int cell)
149      {
150          if (field_5_dbcells == null)
151          {
152              field_5_dbcells = new IntList();
153          }
154          field_5_dbcells.add(cell);
155      }
156  
157      public void setDbcell(int cell, int value)
158      {
159          field_5_dbcells.set(cell, value);
160      }
161  
162      public int getFirstRow()
163      {
164          return field_2_first_row;
165      }
166  
167      public int getLastRowAdd1()
168      {
169          return field_3_last_row_add1;
170      }
171  
172      public int getNumDbcells()
173      {
174          if (field_5_dbcells == null)
175          {
176              return 0;
177          }
178          return field_5_dbcells.size();
179      }
180  
181      public int getDbcellAt(int cellnum)
182      {
183          return field_5_dbcells.get(cellnum);
184      }
185  
186      public String toString()
187      {
188          StringBuffer buffer = new StringBuffer();
189  
190          buffer.append("[INDEX]\n");
191          buffer.append("    .firstrow       = ")
192              .append(Integer.toHexString(getFirstRow())).append("\n");
193          buffer.append("    .lastrowadd1    = ")
194              .append(Integer.toHexString(getLastRowAdd1())).append("\n");
195          for (int k = 0; k < getNumDbcells(); k++)
196          {
197              buffer.append("    .dbcell_" + k + "       = ")
198                  .append(Integer.toHexString(getDbcellAt(k))).append("\n");
199          }
200          buffer.append("[/INDEX]\n");
201          return buffer.toString();
202      }
203  
204      public int serialize(int offset, byte [] data)
205      {
206          LittleEndian.putShort(data, 0 + offset, sid);
207          LittleEndian.putShort(data, 2 + offset,
208                                ( short ) (16 + (getNumDbcells() * 4)));
209          LittleEndian.putInt(data, 4 + offset, 0);
210          LittleEndian.putInt(data, 8 + offset, getFirstRow());
211          LittleEndian.putInt(data, 12 + offset, getLastRowAdd1());
212          LittleEndian.putInt(data, 16 + offset, 0);
213          for (int k = 0; k < getNumDbcells(); k++)
214          {
215              LittleEndian.putInt(data, (k * 4) + 20 + offset, getDbcellAt(k));
216          }
217          return getRecordSize();
218      }
219  
220      public int getRecordSize()
221      {
222          return 20 + (getNumDbcells() * 4);
223      }
224  
225      public short getSid()
226      {
227          return this.sid;
228      }
229  
230      public Object clone() {
231        IndexRecord rec = new IndexRecord();
232        rec.field_1_zero = field_1_zero;
233        rec.field_2_first_row = field_2_first_row;
234        rec.field_3_last_row_add1 = field_3_last_row_add1;
235        rec.field_4_zero = field_4_zero;
236        rec.field_5_dbcells = new IntList();
237        rec.field_5_dbcells.addAll(field_5_dbcells);
238        return rec;
239      }
240  }
241