KickJava   Java API By Example, From Geeks To Geeks.

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


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: Bound Sheet Record (aka BundleSheet) <P>
26  * Description: Defines a sheet within a workbook. Basically stores the sheetname
27  * and tells where the Beginning of file record is within the HSSF
28  * file. <P>
29  * REFERENCE: PG 291 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
30  * @author Andrew C. Oliver (acoliver at apache dot org)
31  * @author Sergei Kozello (sergeikozello at mail.ru)
32  * @version 2.0-pre
33  */

34
35 public class BoundSheetRecord
36         extends Record
37 {
38     public final static short sid = 0x85;
39     private int field_1_position_of_BOF;
40     private short field_2_option_flags;
41     private byte field_3_sheetname_length;
42     private byte field_4_compressed_unicode_flag; // not documented
43
private String JavaDoc field_5_sheetname;
44
45     public BoundSheetRecord()
46     {
47     }
48
49     /**
50      * Constructs a BoundSheetRecord and sets its fields appropriately
51      *
52      * @param id id must be 0x85 or an exception will be throw upon validation
53      * @param size the size of the data area of the record
54      * @param data data of the record (should not contain sid/len)
55      */

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

70
71     public BoundSheetRecord( short id, short size, byte[] data, int offset )
72     {
73         super( id, size, data, offset );
74     }
75
76     protected void validateSid( short id )
77     {
78         if ( id != sid )
79         {
80             throw new RecordFormatException( "NOT A Bound Sheet RECORD" );
81         }
82     }
83
84     /**
85      * UTF8:
86      * sid + len + bof + flags + len(str) + unicode + str
87      * 2 + 2 + 4 + 2 + 1 + 1 + len(str)
88      *
89      * UNICODE:
90      * sid + len + bof + flags + len(str) + unicode + str
91      * 2 + 2 + 4 + 2 + 1 + 1 + 2 * len(str)
92      *
93      */

94
95     protected void fillFields( byte[] data, short size, int offset )
96     {
97         field_1_position_of_BOF = LittleEndian.getInt( data, 0 + offset ); // bof
98
field_2_option_flags = LittleEndian.getShort( data, 4 + offset ); // flags
99
field_3_sheetname_length = data[6 + offset]; // len(str)
100
field_4_compressed_unicode_flag = data[7 + offset]; // unicode
101

102         int nameLength = LittleEndian.ubyteToInt( field_3_sheetname_length );
103         if ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 )
104         {
105             field_5_sheetname = StringUtil.getFromUnicodeLE( data, 8 + offset, nameLength );
106         }
107         else
108         {
109             field_5_sheetname = StringUtil.getFromCompressedUnicode( data, 8 + offset, nameLength );
110         }
111     }
112
113     /**
114      * set the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file
115      *
116      * @param pos offset in bytes
117      */

118
119     public void setPositionOfBof( int pos )
120     {
121         field_1_position_of_BOF = pos;
122     }
123
124     /**
125      * set the option flags (unimportant for HSSF supported sheets)
126      *
127      * @param flags to set
128      */

129
130     public void setOptionFlags( short flags )
131     {
132         field_2_option_flags = flags;
133     }
134
135     /**
136      * Set the length of the sheetname in characters
137      *
138      * @param len number of characters in the sheet name
139      * @see #setSheetname(String)
140      */

141
142     public void setSheetnameLength( byte len )
143     {
144         field_3_sheetname_length = len;
145     }
146
147     /**
148      * set whether or not to interperate the Sheetname as compressed unicode (8/16 bit)
149      * (This is undocumented but can be found as Q187919 on the Microsoft(tm) Support site)
150      * @param flag (0/1) 0- compressed, 1 - uncompressed (16-bit)
151      */

152
153     public void setCompressedUnicodeFlag( byte flag )
154     {
155         field_4_compressed_unicode_flag = flag;
156     }
157
158     /**
159      * Set the sheetname for this sheet. (this appears in the tabs at the bottom)
160      * @param sheetname the name of the sheet
161      * @throws IllegalArgumentException if sheet name will cause excel to crash.
162      */

163
164     public void setSheetname( String JavaDoc sheetname )
165     {
166         
167         if ((sheetname == null) || (sheetname.length()==0)
168                 || (sheetname.length()>31)
169                 || (sheetname.indexOf("/") > -1)
170                 || (sheetname.indexOf("\\") > -1)
171                 || (sheetname.indexOf("?") > -1)
172                 || (sheetname.indexOf("*") > -1)
173                 || (sheetname.indexOf("]") > -1)
174                 || (sheetname.indexOf("[") > -1) ){
175                     throw new IllegalArgumentException JavaDoc("Sheet name cannot be blank, greater than 31 chars, or contain any of /\\*?[]");
176         }
177         field_5_sheetname = sheetname;
178     }
179
180     /**
181      * get the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file
182      *
183      * @return offset in bytes
184      */

185
186     public int getPositionOfBof()
187     {
188         return field_1_position_of_BOF;
189     }
190
191     /**
192      * get the option flags (unimportant for HSSF supported sheets)
193      *
194      * @return flags to set
195      */

196
197     public short getOptionFlags()
198     {
199         return field_2_option_flags;
200     }
201
202     /**
203      * get the length of the sheetname in characters
204      *
205      * @return number of characters in the sheet name
206      * @see #getSheetname()
207      */

208
209     public byte getSheetnameLength()
210     {
211         return field_3_sheetname_length;
212     }
213
214     /**
215      * get the length of the raw sheetname in characters
216      * the length depends on the unicode flag
217      *
218      * @return number of characters in the raw sheet name
219      */

220
221     public byte getRawSheetnameLength()
222     {
223         return (byte) ( ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 )
224                 ? 2 * field_3_sheetname_length
225                 : field_3_sheetname_length );
226     }
227
228     /**
229      * get whether or not to interperate the Sheetname as compressed unicode (8/16 bit)
230      * (This is undocumented but can be found as Q187919 on the Microsoft(tm) Support site)
231      * @return flag (0/1) 0- compressed, 1 - uncompressed (16-bit)
232      */

233
234     public byte getCompressedUnicodeFlag()
235     {
236         return field_4_compressed_unicode_flag;
237     }
238
239     /**
240      * get the sheetname for this sheet. (this appears in the tabs at the bottom)
241      * @return sheetname the name of the sheet
242      */

243
244     public String JavaDoc getSheetname()
245     {
246         return field_5_sheetname;
247     }
248
249     public String JavaDoc toString()
250     {
251         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
252
253         buffer.append( "[BOUNDSHEET]\n" );
254         buffer.append( " .bof = " )
255                 .append( Integer.toHexString( getPositionOfBof() ) ).append( "\n" );
256         buffer.append( " .optionflags = " )
257                 .append( Integer.toHexString( getOptionFlags() ) ).append( "\n" );
258         buffer.append( " .sheetname length= " )
259                 .append( Integer.toHexString( getSheetnameLength() ) ).append( "\n" );
260         buffer.append( " .unicodeflag = " )
261                 .append( Integer.toHexString( getCompressedUnicodeFlag() ) )
262                 .append( "\n" );
263         buffer.append( " .sheetname = " ).append( getSheetname() )
264                 .append( "\n" );
265         buffer.append( "[/BOUNDSHEET]\n" );
266         return buffer.toString();
267     }
268
269     public int serialize( int offset, byte[] data )
270     {
271         LittleEndian.putShort( data, 0 + offset, sid );
272         LittleEndian.putShort( data, 2 + offset, (short) ( 8 + getRawSheetnameLength() ) );
273         LittleEndian.putInt( data, 4 + offset, getPositionOfBof() );
274         LittleEndian.putShort( data, 8 + offset, getOptionFlags() );
275         data[10 + offset] = (byte) ( getSheetnameLength() );
276         data[11 + offset] = getCompressedUnicodeFlag();
277
278         if ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 )
279             StringUtil.putUnicodeLE( getSheetname(), data, 12 + offset );
280         else
281             StringUtil.putCompressedUnicode( getSheetname(), data, 12 + offset );
282
283
284         return getRecordSize();
285
286         /*
287         byte[] fake = new byte[] { (byte)0x85, 0x00, // sid
288                                         0x1a, 0x00, // length
289                                         0x3C, 0x09, 0x00, 0x00, // bof
290                                         0x00, 0x00, // flags
291                                         0x09, // len( str )
292                                         0x01, // unicode
293                                         // <str>
294                                         0x21, 0x04, 0x42, 0x04, 0x40, 0x04, 0x30, 0x04, 0x3D,
295                                         0x04, 0x38, 0x04, 0x47, 0x04, 0x3A, 0x04, 0x30, 0x04
296                                         // </str>
297                                     };
298
299                                     sid + len + bof + flags + len(str) + unicode + str
300                                      2 + 2 + 4 + 2 + 1 + 1 + len(str)
301
302         System.arraycopy( fake, 0, data, offset, fake.length );
303
304         return fake.length;
305         */

306     }
307
308     public int getRecordSize()
309     {
310         // Includes sid length + size length
311
return 12 + getRawSheetnameLength();
312     }
313
314     public short getSid()
315     {
316         return this.sid;
317     }
318 }
319
Popular Tags