KickJava   Java API By Example, From Geeks To Geeks.

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


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: Sheet Tab Index Array Record<P>
25  * Description: Contains an array of sheet id's. Sheets always keep their ID
26  * regardless of what their name is.<P>
27  * REFERENCE: PG 412 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
28  * @author Andrew C. Oliver (acoliver at apache dot org)
29  * @version 2.0-pre
30  */

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

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

63
64     public TabIdRecord(short id, short size, byte [] data, int offset)
65     {
66         super(id, size, data, offset);
67     }
68
69     protected void validateSid(short id)
70     {
71         if (id != sid)
72         {
73             throw new RecordFormatException("NOT A TABID RECORD");
74         }
75     }
76
77     protected void fillFields(byte [] data, short size, int offset)
78     {
79         field_1_tabids = new short[ size / 2 ];
80         for (int k = 0; k < field_1_tabids.length; k++)
81         {
82             field_1_tabids[ k ] = LittleEndian.getShort(data,
83                                                         (k * 2) + offset);
84         }
85     }
86
87     /**
88      * set the tab array. (0,1,2).
89      * @param array of tab id's {0,1,2}
90      */

91
92     public void setTabIdArray(short [] array)
93     {
94         field_1_tabids = array;
95     }
96
97     /**
98      * get the tab array. (0,1,2).
99      * @return array of tab id's {0,1,2}
100      */

101
102     public short [] getTabIdArray()
103     {
104         return field_1_tabids;
105     }
106
107     public String JavaDoc toString()
108     {
109         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
110
111         buffer.append("[TABID]\n");
112         buffer.append(" .elements = ").append(field_1_tabids.length)
113             .append("\n");
114         for (int k = 0; k < field_1_tabids.length; k++)
115         {
116             buffer.append(" .element_" + k + " = ")
117                 .append(field_1_tabids[ k ]).append("\n");
118         }
119         buffer.append("[/TABID]\n");
120         return buffer.toString();
121     }
122
123     public int serialize(int offset, byte [] data)
124     {
125         short[] tabids = getTabIdArray();
126         short length = ( short ) (tabids.length * 2);
127         int byteoffset = 4;
128
129         LittleEndian.putShort(data, 0 + offset, sid);
130         LittleEndian.putShort(data, 2 + offset,
131                               (( short ) length)); // nubmer tabids *
132

133         // 2 (num bytes in a short)
134
for (int k = 0; k < (length / 2); k++)
135         {
136             LittleEndian.putShort(data, byteoffset + offset, tabids[ k ]);
137             byteoffset += 2;
138         }
139         return getRecordSize();
140     }
141
142     public int getRecordSize()
143     {
144         return 4 + (getTabIdArray().length * 2);
145     }
146
147     public short getSid()
148     {
149         return this.sid;
150     }
151 }
152
Popular Tags