KickJava   Java API By Example, From Geeks To Geeks.

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


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.IntList;
22 import org.apache.poi.util.LittleEndian;
23
24 /**
25  * Title: Index Record<P>
26  * Description: Occurs right after BOF, tells you where the DBCELL records are for a sheet
27  * Important for locating cells<P>
28  * NOT USED IN THIS RELEASE
29  * REFERENCE: PG 323 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
30  * @author Andrew C. Oliver (acoliver at apache dot org)
31  * @author Jason Height (jheight at chariot dot net dot au)
32  * @version 2.0-pre
33  */

34
35 public class IndexRecord
36     extends Record
37 {
38     public final static short sid = 0x20B;
39     public final static int DBCELL_CAPACITY = 30;
40     public int field_1_zero; // reserved must be 0
41
public int field_2_first_row; // first row on the sheet
42
public int field_3_last_row_add1; // last row
43
public int field_4_zero; // reserved must be 0
44
public IntList field_5_dbcells; // array of offsets to DBCELL records
45

46     public IndexRecord()
47     {
48     }
49
50     /**
51      * Constructs an Index record and sets its fields appropriately.
52      *
53      * @param id id must be 0x208 or an exception will be throw upon validation
54      * @param size the size of the data area of the record
55      * @param data data of the record (should not contain sid/len)
56      */

57
58     public IndexRecord(short id, short size, byte [] data)
59     {
60         super(id, size, data);
61     }
62
63     /**
64      * Constructs an Index record and sets its fields appropriately.
65      *
66      * @param id id must be 0x208 or an exception will be throw upon validation
67      * @param size the size of the data area of the record
68      * @param data data of the record (should not contain sid/len)
69      * @param offset of record data
70      */

71
72     public IndexRecord(short id, short size, byte [] data, int offset)
73     {
74         super(id, size, data, offset);
75     }
76
77     protected void validateSid(short id)
78     {
79         if (id != sid)
80         {
81             throw new RecordFormatException("NOT An Index RECORD");
82         }
83     }
84
85     protected void fillFields(byte [] data, short size, int offset)
86     {
87         field_5_dbcells =
88             new IntList(DBCELL_CAPACITY); // initial capacity of 30
89
field_1_zero = LittleEndian.getInt(data, 0 + offset);
90         field_2_first_row = LittleEndian.getInt(data, 4 + offset);
91         field_3_last_row_add1 = LittleEndian.getInt(data, 8 + offset);
92         field_4_zero = LittleEndian.getInt(data, 12 + offset);
93         for (int k = 16; k < size; k = k + 4)
94         {
95
96             // System.out.println("getting " + k);
97
field_5_dbcells.add(LittleEndian.getInt(data, k + offset));
98         }
99     }
100
101     public void setFirstRow(int row)
102     {
103         field_2_first_row = row;
104     }
105
106     public void setLastRowAdd1(int row)
107     {
108         field_3_last_row_add1 = row;
109     }
110
111     public void addDbcell(int cell)
112     {
113         if (field_5_dbcells == null)
114         {
115             field_5_dbcells = new IntList();
116         }
117         field_5_dbcells.add(cell);
118     }
119
120     public void setDbcell(int cell, int value)
121     {
122         field_5_dbcells.set(cell, value);
123     }
124
125     public int getFirstRow()
126     {
127         return field_2_first_row;
128     }
129
130     public int getLastRowAdd1()
131     {
132         return field_3_last_row_add1;
133     }
134
135     public int getNumDbcells()
136     {
137         if (field_5_dbcells == null)
138         {
139             return 0;
140         }
141         return field_5_dbcells.size();
142     }
143
144     public int getDbcellAt(int cellnum)
145     {
146         return field_5_dbcells.get(cellnum);
147     }
148
149     public String JavaDoc toString()
150     {
151         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
152
153         buffer.append("[INDEX]\n");
154         buffer.append(" .firstrow = ")
155             .append(Integer.toHexString(getFirstRow())).append("\n");
156         buffer.append(" .lastrowadd1 = ")
157             .append(Integer.toHexString(getLastRowAdd1())).append("\n");
158         for (int k = 0; k < getNumDbcells(); k++)
159         {
160             buffer.append(" .dbcell_" + k + " = ")
161                 .append(Integer.toHexString(getDbcellAt(k))).append("\n");
162         }
163         buffer.append("[/INDEX]\n");
164         return buffer.toString();
165     }
166
167     public int serialize(int offset, byte [] data)
168     {
169         LittleEndian.putShort(data, 0 + offset, sid);
170         LittleEndian.putShort(data, 2 + offset,
171                               ( short ) (16 + (getNumDbcells() * 4)));
172         LittleEndian.putInt(data, 4 + offset, 0);
173         LittleEndian.putInt(data, 8 + offset, getFirstRow());
174         LittleEndian.putInt(data, 12 + offset, getLastRowAdd1());
175         LittleEndian.putInt(data, 16 + offset, 0);
176         for (int k = 0; k < getNumDbcells(); k++)
177         {
178             LittleEndian.putInt(data, (k * 4) + 20 + offset, getDbcellAt(k));
179         }
180         return getRecordSize();
181     }
182
183     public int getRecordSize()
184     {
185         return 20 + (getNumDbcells() * 4);
186     }
187     
188     /** Returns the size of an INdexRecord when it needs to index the specified number of blocks
189       *
190       */

191      public static int getRecordSizeForBlockCount(int blockCount) {
192        return 20 + (4 * blockCount);
193      }
194
195     public short getSid()
196     {
197         return this.sid;
198     }
199
200     public Object JavaDoc clone() {
201       IndexRecord rec = new IndexRecord();
202       rec.field_1_zero = field_1_zero;
203       rec.field_2_first_row = field_2_first_row;
204       rec.field_3_last_row_add1 = field_3_last_row_add1;
205       rec.field_4_zero = field_4_zero;
206       rec.field_5_dbcells = new IntList();
207       rec.field_5_dbcells.addAll(field_5_dbcells);
208       return rec;
209     }
210 }
211
Popular Tags