KickJava   Java API By Example, From Geeks To Geeks.

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


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: Format Record<P>
26  * Description: describes a number format -- those goofy strings like $(#,###)<P>
27  *
28  * REFERENCE: PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
29  * @author Andrew C. Oliver (acoliver at apache dot org)
30  * @author Shawn M. Laubach (slaubach at apache dot org)
31  * @version 2.0-pre
32  */

33
34 public class FormatRecord
35     extends Record
36 {
37     public final static short sid = 0x41e;
38     private short field_1_index_code;
39     private short field_2_formatstring_len;
40     private short field_3_unicode_len; // unicode string length
41
private boolean field_3_unicode_flag; // it is not undocumented - it is unicode flag
42
private String JavaDoc field_4_formatstring;
43
44     public FormatRecord()
45     {
46     }
47
48     /**
49      * Constructs a Format record and sets its fields appropriately.
50      *
51      * @param id id must be 0x41e or an exception will be throw upon validation
52      * @param size the size of the data area of the record
53      * @param data data of the record (should not contain sid/len)
54      */

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

69
70     public FormatRecord(short id, short size, byte [] data, int offset)
71     {
72         super(id, size, data, offset);
73     }
74
75     protected void validateSid(short id)
76     {
77         if (id != sid)
78         {
79             throw new RecordFormatException("NOT A FORMAT RECORD");
80         }
81     }
82
83     protected void fillFields(byte [] data, short size, int offset)
84     {
85         field_1_index_code = LittleEndian.getShort(data, 0 + offset);
86         // field_2_formatstring_len = data[ 2 + offset ];
87
field_3_unicode_len = LittleEndian.getShort( data, 2 + offset );
88     field_2_formatstring_len = field_3_unicode_len;
89         field_3_unicode_flag = ( data[ 4 + offset ] & (byte)0x01 ) != 0;
90                                               
91                                               
92       if ( field_3_unicode_flag ) {
93           // unicode
94
field_4_formatstring = StringUtil.getFromUnicodeLE( data, 5 + offset, field_3_unicode_len );
95       }
96       else {
97           // not unicode
98
field_4_formatstring = StringUtil.getFromCompressedUnicode(data, 5 + offset, field_3_unicode_len );
99       }
100     }
101
102     /**
103      * set the format index code (for built in formats)
104      *
105      * @param index the format index code
106      * @see org.apache.poi.hssf.model.Workbook
107      */

108
109     public void setIndexCode(short index)
110     {
111         field_1_index_code = index;
112     }
113
114     /**
115      * set the format string length
116      *
117      * @param len the length of the format string
118      * @see #setFormatString(String)
119      */

120     
121     public void setFormatStringLength(byte len)
122     {
123         field_2_formatstring_len = len;
124     field_3_unicode_len = len;
125     }
126
127     /**
128      * set whether the string is unicode
129      *
130      * @param unicode flag for whether string is unicode
131      */

132
133     public void setUnicodeFlag(boolean unicode) {
134     field_3_unicode_flag = unicode;
135     }
136
137     /**
138      * set the format string
139      *
140      * @param fs the format string
141      * @see #setFormatStringLength(byte)
142      */

143
144     public void setFormatString(String JavaDoc fs)
145     {
146         field_4_formatstring = fs;
147     }
148
149     /**
150      * get the format index code (for built in formats)
151      *
152      * @return the format index code
153      * @see org.apache.poi.hssf.model.Workbook
154      */

155
156     public short getIndexCode()
157     {
158         return field_1_index_code;
159     }
160
161     /**
162      * get the format string length
163      *
164      * @return the length of the format string
165      * @see #getFormatString()
166      */

167
168     public short getFormatStringLength()
169     {
170         return field_3_unicode_flag ? field_3_unicode_len : field_2_formatstring_len;
171     }
172
173     /**
174      * get whether the string is unicode
175      *
176      * @return flag for whether string is unicode
177      */

178
179     public boolean getUnicodeFlag() {
180     return field_3_unicode_flag;
181     }
182
183     /**
184      * get the format string
185      *
186      * @return the format string
187      * @see #getFormatStringLength()
188      */

189
190     public String JavaDoc getFormatString()
191     {
192         return field_4_formatstring;
193     }
194
195     public String JavaDoc toString()
196     {
197         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
198
199         buffer.append("[FORMAT]\n");
200         buffer.append(" .indexcode = ")
201             .append(Integer.toHexString(getIndexCode())).append("\n");
202         /*
203         buffer.append(" .formatstringlen = ")
204             .append(Integer.toHexString(getFormatStringLength()))
205             .append("\n");
206         */

207         buffer.append(" .unicode length = ")
208             .append(Integer.toHexString(field_3_unicode_len)).append("\n");
209         buffer.append(" .isUnicode = ")
210             .append( field_3_unicode_flag ).append("\n");
211         buffer.append(" .formatstring = ").append(getFormatString())
212             .append("\n");
213         buffer.append("[/FORMAT]\n");
214         return buffer.toString();
215     }
216
217     public int serialize(int offset, byte [] data)
218     {
219         LittleEndian.putShort(data, 0 + offset, sid);
220         LittleEndian.putShort(data, 2 + offset, (short)( 2 + 2 + 1 + ( (field_3_unicode_flag)
221                                                                   ? 2 * field_3_unicode_len
222                                                                   : field_3_unicode_len ) ) );
223                                                   // index + len + flag + format string length
224
LittleEndian.putShort(data, 4 + offset, getIndexCode());
225         LittleEndian.putShort(data, 6 + offset, field_3_unicode_len);
226         data[ 8 + offset ] = (byte)( (field_3_unicode_flag) ? 0x01 : 0x00 );
227
228       if ( field_3_unicode_flag ) {
229           // unicode
230
StringUtil.putUnicodeLE( getFormatString(), data, 9 + offset );
231       }
232       else {
233           // not unicode
234
StringUtil.putCompressedUnicode( getFormatString(), data, 9 + offset );
235       }
236       
237         return getRecordSize();
238     }
239
240     public int getRecordSize()
241     {
242         return 9 + ( ( field_3_unicode_flag ) ? 2 * field_3_unicode_len : field_3_unicode_len );
243     }
244
245     public short getSid()
246     {
247         return this.sid;
248     }
249 }
250
Popular Tags