KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.poi.util.StringUtil;
23
24 /**
25  * Title: Header Record<P>
26  * Description: Specifies a header for a sheet<P>
27  * REFERENCE: PG 321 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
28  * @author Andrew C. Oliver (acoliver at apache dot org)
29  * @author Shawn Laubach (slaubach at apache dot org) Modified 3/14/02
30  * @author Jason Height (jheight at chariot dot net dot au)
31  * @version 2.0-pre
32  */

33
34 public class HeaderRecord
35     extends Record
36 {
37     public final static short sid = 0x14;
38     private byte field_1_header_len;
39     private String JavaDoc field_2_header;
40
41     public HeaderRecord()
42     {
43     }
44
45     /**
46      * Constructs an Header record and sets its fields appropriately.
47      *
48      * @param id id must be 0x14 or an exception will be throw upon validation
49      * @param size the size of the data area of the record
50      * @param data data of the record (should not contain sid/len)
51      */

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

66
67     public HeaderRecord(short id, short size, byte [] data, int offset)
68     {
69         super(id, size, data, offset);
70     }
71
72     protected void validateSid(short id)
73     {
74         if (id != sid)
75         {
76             throw new RecordFormatException("NOT A HEADERRECORD");
77         }
78     }
79
80     protected void fillFields(byte [] data, short size, int offset)
81     {
82         if (size > 0)
83         {
84             field_1_header_len = data[ 0 + offset ];
85             field_2_header = StringUtil.getFromCompressedUnicode(data, 3 + offset, // [Shawn] Changed 1 to 3 for offset of string
86
LittleEndian.ubyteToInt(field_1_header_len));
87         }
88     }
89
90     /**
91      * set the length of the header string
92      *
93      * @param len length of the header string
94      * @see #setHeader(String)
95      */

96
97     public void setHeaderLength(byte len)
98     {
99         field_1_header_len = len;
100     }
101
102     /**
103      * set the header string
104      *
105      * @param header string to display
106      * @see #setHeaderLength(byte)
107      */

108
109     public void setHeader(String JavaDoc header)
110     {
111         field_2_header = header;
112     }
113
114     /**
115      * get the length of the header string
116      *
117      * @return length of the header string
118      * @see #getHeader()
119      */

120
121     public short getHeaderLength()
122     {
123         return (short)(0xFF & field_1_header_len); // [Shawn] Fixed needing unsigned byte
124
}
125
126     /**
127      * get the header string
128      *
129      * @return header string to display
130      * @see #getHeaderLength()
131      */

132
133     public String JavaDoc getHeader()
134     {
135         return field_2_header;
136     }
137
138     public String JavaDoc toString()
139     {
140         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
141
142         buffer.append("[HEADER]\n");
143         buffer.append(" .length = ").append(getHeaderLength())
144             .append("\n");
145         buffer.append(" .header = ").append(getHeader())
146             .append("\n");
147         buffer.append("[/HEADER]\n");
148         return buffer.toString();
149     }
150
151     public int serialize(int offset, byte [] data)
152     {
153         int len = 4;
154
155         if (getHeaderLength() != 0)
156         {
157             len+=3; // [Shawn] Fixed for two null bytes in the length
158
}
159         LittleEndian.putShort(data, 0 + offset, sid);
160         LittleEndian.putShort(data, 2 + offset,
161                               ( short ) ((len - 4) + getHeaderLength()));
162
163         if (getHeaderLength() > 0)
164         {
165             data[ 4 + offset ] = (byte)getHeaderLength();
166             StringUtil.putCompressedUnicode(getHeader(), data, 7 + offset); // [Shawn] Place the string in the correct offset
167
}
168         return getRecordSize();
169     }
170
171     public int getRecordSize()
172     {
173         int retval = 4;
174
175         if (getHeaderLength() != 0)
176         {
177             retval+=3; // [Shawn] Fixed for two null bytes in the length
178
}
179         retval += getHeaderLength();
180         return retval;
181     }
182
183     public short getSid()
184     {
185         return this.sid;
186     }
187
188     public Object JavaDoc clone() {
189       HeaderRecord rec = new HeaderRecord();
190       rec.field_1_header_len = field_1_header_len;
191       rec.field_2_header = field_2_header;
192       return rec;
193     }
194 }
195
Popular Tags