KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
24  * Title: DBCell Record
25  * Description: Used by Excel and other MS apps to quickly find rows in the sheets.<P>
26  * REFERENCE: PG 299/440 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
27  * @author Andrew C. Oliver (acoliver at apache dot org)
28  * @author Jason Height
29  * @version 2.0-pre
30  */

31
32 public class DBCellRecord
33     extends Record
34 {
35     public final static int BLOCK_SIZE = 32;
36     public final static short sid = 0xd7;
37     private int field_1_row_offset;
38     private short[] field_2_cell_offsets;
39
40     public DBCellRecord()
41     {
42     }
43
44     /**
45      * Constructs a DBCellRecord and sets its fields appropriately
46      *
47      * @param id id must be 0xd7 or an exception will be throw upon validation
48      * @param size the size of the data area of the record
49      * @param data data of the record (should not contain sid/len)
50      */

51
52     public DBCellRecord(short id, short size, byte [] data)
53     {
54         super(id, size, data);
55     }
56
57     /**
58      * Constructs a DBCellRecord and sets its fields appropriately
59      *
60      * @param id id must be 0xd7 or an exception will be throw upon validation
61      * @param size the size of the data area of the record
62      * @param data data of the record (should not contain sid/len)
63      * @param offset of the record's data
64      */

65
66     public DBCellRecord(short id, short size, byte [] data, int offset)
67     {
68         super(id, size, data, offset);
69     }
70
71     protected void validateSid(short id)
72     {
73         if (id != sid)
74         {
75             throw new RecordFormatException("NOT A valid DBCell RECORD");
76         }
77     }
78
79     protected void fillFields(byte [] data, short size, int offset)
80     {
81         field_1_row_offset = LittleEndian.getUShort(data, 0 + offset);
82         field_2_cell_offsets = new short[ (size - 4) / 2 ];
83         int element = 0;
84
85         for (int k = 4; k < data.length; k += 2)
86         {
87             field_2_cell_offsets[ element++ ] = LittleEndian.getShort(data,
88                     k + offset);
89         }
90     }
91
92     /**
93      * sets offset from the start of this DBCellRecord to the start of the first cell in
94      * the next DBCell block.
95      *
96      * @param offset offset to the start of the first cell in the next DBCell block
97      */

98
99     public void setRowOffset(int offset)
100     {
101         field_1_row_offset = offset;
102     }
103
104     // need short list impl.
105
public void addCellOffset(short offset)
106     {
107         if (field_2_cell_offsets == null)
108         {
109             field_2_cell_offsets = new short[ 1 ];
110         }
111         else
112         {
113             short[] temp = new short[ field_2_cell_offsets.length + 1 ];
114
115             System.arraycopy(field_2_cell_offsets, 0, temp, 0,
116                              field_2_cell_offsets.length);
117             field_2_cell_offsets = temp;
118         }
119         field_2_cell_offsets[ field_2_cell_offsets.length - 1 ] = offset;
120     }
121
122     /**
123      * gets offset from the start of this DBCellRecord to the start of the first cell in
124      * the next DBCell block.
125      *
126      * @return rowoffset to the start of the first cell in the next DBCell block
127      */

128
129     public int getRowOffset()
130     {
131         return field_1_row_offset;
132     }
133
134     /**
135      * return the cell offset in the array
136      *
137      * @param index of the cell offset to retrieve
138      * @return celloffset from the celloffset array
139      */

140
141     public short getCellOffsetAt(int index)
142     {
143         return field_2_cell_offsets[ index ];
144     }
145
146     /**
147      * get the number of cell offsets in the celloffset array
148      *
149      * @return number of cell offsets
150      */

151
152     public int getNumCellOffsets()
153     {
154         return field_2_cell_offsets.length;
155     }
156
157     public String JavaDoc toString()
158     {
159         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
160
161         buffer.append("[DBCELL]\n");
162         buffer.append(" .rowoffset = ")
163             .append(Integer.toHexString(getRowOffset())).append("\n");
164         for (int k = 0; k < getNumCellOffsets(); k++)
165         {
166             buffer.append(" .cell_" + k + " = ")
167                 .append(Integer.toHexString(getCellOffsetAt(k))).append("\n");
168         }
169         buffer.append("[/DBCELL]\n");
170         return buffer.toString();
171     }
172
173     public int serialize(int offset, byte [] data)
174     {
175         if (field_2_cell_offsets == null)
176         {
177             field_2_cell_offsets = new short[ 0 ];
178         }
179         LittleEndian.putShort(data, 0 + offset, sid);
180         LittleEndian.putShort(data, 2 + offset,
181                               (( short ) (4 + (getNumCellOffsets() * 2))));
182         LittleEndian.putInt(data, 4 + offset, getRowOffset());
183         for (int k = 0; k < getNumCellOffsets(); k++)
184         {
185             LittleEndian.putShort(data, 8 + 2*k + offset, getCellOffsetAt(k));
186         }
187         return getRecordSize();
188     }
189
190     public int getRecordSize()
191     {
192         return 8 + (getNumCellOffsets() * 2);
193     }
194     
195     /** Returns the size of a DBCellRecord when it needs to reference a certain number of rows*/
196     public static int getRecordSizeForRows(int rows) {
197       return 8 + (rows * 2);
198     }
199
200     public short getSid()
201     {
202         return this.sid;
203     }
204
205     public boolean isInValueSection()
206     {
207         return true;
208     }
209 }
210
Popular Tags