KickJava   Java API By Example, From Geeks To Geeks.

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


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: Recalc Id Record<P>
25  * Description: This record contains an ID that marks when a worksheet was last
26  * recalculated. It's an optimization Excel uses to determine if it
27  * needs to recalculate the spreadsheet when it's opened. So far, only
28  * the two values <code>0xC1 0x01 0x00 0x00 0x80 0x38 0x01 0x00</code>
29  * (do not recalculate) and <code>0xC1 0x01 0x00 0x00 0x60 0x69 0x01
30  * 0x00</code> have been seen. If the field <code>isNeeded</code> is
31  * set to false (default), then this record is swallowed during the
32  * serialization process<P>
33  * REFERENCE: http://chicago.sourceforge.net/devel/docs/excel/biff8.html<P>
34  * @author Luc Girardin (luc dot girardin at macrofocus dot com)
35  * @version 2.0-pre
36  * @see org.apache.poi.hssf.model.Workbook
37  */

38
39 public class RecalcIdRecord
40     extends Record
41 {
42     public final static short sid = 0x1c1;
43     public short[] field_1_recalcids;
44
45     private boolean isNeeded = false;
46
47     public RecalcIdRecord()
48     {
49     }
50
51     /**
52      * Constructs a RECALCID record and sets its fields appropriately.
53      *
54      * @param id id must be 0x13d or an exception will be throw upon validation
55      * @param size the size of the data area of the record
56      * @param data data of the record (should not contain sid/len)
57      */

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

72
73     public RecalcIdRecord(short id, short size, byte [] data, int offset)
74     {
75         super(id, size, data, offset);
76     }
77
78     protected void validateSid(short id)
79     {
80         if (id != sid)
81         {
82             throw new RecordFormatException("NOT A RECALCID RECORD");
83         }
84     }
85
86     protected void fillFields(byte [] data, short size, int offset)
87     {
88         field_1_recalcids = new short[ size / 2 ];
89         for (int k = 0; k < field_1_recalcids.length; k++)
90         {
91             field_1_recalcids[ k ] = LittleEndian.getShort(data,
92                                                         (k * 2) + offset);
93         }
94     }
95
96     /**
97      * set the recalc array.
98      * @param array of recalc id's
99      */

100
101     public void setRecalcIdArray(short [] array)
102     {
103         field_1_recalcids = array;
104     }
105
106     /**
107      * get the recalc array.
108      * @return array of recalc id's
109      */

110
111     public short [] getRecalcIdArray()
112     {
113         return field_1_recalcids;
114     }
115
116     public void setIsNeeded(boolean isNeeded) {
117         this.isNeeded = isNeeded;
118     }
119
120     public boolean isNeeded() {
121         return isNeeded;
122     }
123
124     public String JavaDoc toString()
125     {
126         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
127
128         buffer.append("[RECALCID]\n");
129         buffer.append(" .elements = ").append(field_1_recalcids.length)
130             .append("\n");
131         for (int k = 0; k < field_1_recalcids.length; k++)
132         {
133             buffer.append(" .element_" + k + " = ")
134                 .append(field_1_recalcids[ k ]).append("\n");
135         }
136         buffer.append("[/RECALCID]\n");
137         return buffer.toString();
138     }
139
140     public int serialize(int offset, byte [] data)
141     {
142         short[] tabids = getRecalcIdArray();
143         short length = ( short ) (tabids.length * 2);
144         int byteoffset = 4;
145
146         LittleEndian.putShort(data, 0 + offset, sid);
147         LittleEndian.putShort(data, 2 + offset,
148                               (( short ) length));
149
150         // 2 (num bytes in a short)
151
for (int k = 0; k < (length / 2); k++)
152         {
153             LittleEndian.putShort(data, byteoffset + offset, tabids[ k ]);
154             byteoffset += 2;
155         }
156         return getRecordSize();
157     }
158
159     public int getRecordSize()
160     {
161         return 4 + (getRecalcIdArray().length * 2);
162     }
163
164     public short getSid()
165     {
166         return this.sid;
167     }
168 }
169
Popular Tags