KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.poi.util.BitField;
24
25 /**
26  * Title: Style Record<P>
27  * Description: Describes a builtin to the gui or user defined style<P>
28  * REFERENCE: PG 390 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
29  * @author Andrew C. Oliver (acoliver at apache dot org)
30  * @author aviks : string fixes for UserDefined Style
31  * @version 2.0-pre
32  */

33
34 public class StyleRecord
35     extends Record
36 {
37     public final static short sid = 0x293;
38     public final static short STYLE_USER_DEFINED = 0;
39     public final static short STYLE_BUILT_IN = 1;
40
41     // shared by both user defined and builtin styles
42
private short field_1_xf_index; // TODO: bitfield candidate
43

44     // only for built in styles
45
private byte field_2_builtin_style;
46     private byte field_3_outline_style_level;
47
48     // only for user defined styles
49
private short field_2_name_length; //OO doc says 16 bit length, so we believe
50
private byte field_3_string_options;
51     private BitField fHighByte;
52     private String JavaDoc field_4_name;
53
54     public StyleRecord()
55     {
56     }
57
58     /**
59      * Constructs a Style record and sets its fields appropriately.
60      *
61      * @param id id must be 0x293 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      */

65
66     public StyleRecord(short id, short size, byte [] data)
67     {
68         super(id, size, data);
69     }
70
71     /**
72      * Constructs a Style record and sets its fields appropriately.
73      *
74      * @param id id must be 0x293 or an exception will be throw upon validation
75      * @param size the size of the data area of the record
76      * @param data data of the record (should not contain sid/len)
77      * @param offset
78      */

79
80     public StyleRecord(short id, short size, byte [] data, int offset)
81     {
82         super(id, size, data, offset);
83     }
84
85     protected void validateSid(short id)
86     {
87         if (id != sid)
88         {
89             throw new RecordFormatException("NOT A STYLE RECORD");
90         }
91     }
92
93     protected void fillFields(byte [] data, short size, int offset)
94     {
95         fHighByte = new BitField(0x01); //have to init here, since we are being called
96
//from super, and class level init hasnt been done.
97
field_1_xf_index = LittleEndian.getShort(data, 0 + offset);
98         if (getType() == STYLE_BUILT_IN)
99         {
100             field_2_builtin_style = data[ 2 + offset ];
101             field_3_outline_style_level = data[ 3 + offset ];
102         }
103         else if (getType() == STYLE_USER_DEFINED)
104         {
105             field_2_name_length = LittleEndian.getShort(data, 2 + offset );
106             field_3_string_options = data[4+offset];
107             
108             if (fHighByte.isSet(field_3_string_options)) {
109                 field_4_name= StringUtil.getFromUnicodeBE(data,offset+5,field_2_name_length);
110             }else {
111                 field_4_name=StringUtil.getFromCompressedUnicode(data,offset+5,field_2_name_length);
112             }
113         }
114
115         // todo sanity check exception to make sure we're one or the other
116
}
117
118     /**
119      * set the entire index field (including the type) (see bit setters that reference this method)
120      * @param index bitmask
121      */

122
123     public void setIndex(short index)
124     {
125         field_1_xf_index = index;
126     }
127
128     // bitfields for field 1
129

130     /**
131      * set the type of the style (builtin or user-defined)
132      * @see #STYLE_USER_DEFINED
133      * @see #STYLE_BUILT_IN
134      * @param type of style (userdefined/builtin)
135      * @see #setIndex(short)
136      */

137
138     public void setType(short type)
139     {
140         field_1_xf_index = setField(field_1_xf_index, type, 0x8000, 15);
141     }
142
143     /**
144      * set the actual index of the style extended format record
145      * @see #setIndex(short)
146      * @param index of the xf record
147      */

148
149     public void setXFIndex(short index)
150     {
151         field_1_xf_index = setField(field_1_xf_index, index, 0x1FFF, 0);
152     }
153
154     // end bitfields
155
// only for user defined records
156

157     /**
158      * if this is a user defined record set the length of the style name
159      * @param length of the style's name
160      * @see #setName(String)
161      */

162
163     public void setNameLength(byte length)
164     {
165         field_2_name_length = length;
166     }
167
168     /**
169      * set the style's name
170      * @param name of the style
171      * @see #setNameLength(byte)
172      */

173
174     public void setName(String JavaDoc name)
175     {
176         field_4_name = name;
177         //TODO set name length and string options
178
}
179
180     // end user defined
181
// only for buildin records
182

183     /**
184      * if this is a builtin style set teh number of the built in style
185      * @param builtin style number (0-7)
186      *
187      */

188
189     public void setBuiltin(byte builtin)
190     {
191         field_2_builtin_style = builtin;
192     }
193
194     /**
195      * set the row or column level of the style (if builtin 1||2)
196      */

197
198     public void setOutlineStyleLevel(byte level)
199     {
200         field_3_outline_style_level = level;
201     }
202
203     // end builtin records
204
// field 1
205

206     /**
207      * get the entire index field (including the type) (see bit getters that reference this method)
208      * @return bitmask
209      */

210
211     public short getIndex()
212     {
213         return field_1_xf_index;
214     }
215
216     // bitfields for field 1
217

218     /**
219      * get the type of the style (builtin or user-defined)
220      * @see #STYLE_USER_DEFINED
221      * @see #STYLE_BUILT_IN
222      * @return type of style (userdefined/builtin)
223      * @see #getIndex()
224      */

225
226     public short getType()
227     {
228         return ( short ) ((field_1_xf_index & 0x8000) >> 15);
229     }
230
231     /**
232      * get the actual index of the style extended format record
233      * @see #getIndex()
234      * @return index of the xf record
235      */

236
237     public short getXFIndex()
238     {
239         return ( short ) (field_1_xf_index & 0x1FFF);
240     }
241
242     // end bitfields
243
// only for user defined records
244

245     /**
246      * if this is a user defined record get the length of the style name
247      * @return length of the style's name
248      * @see #getName()
249      */

250
251     public short getNameLength()
252     {
253         return field_2_name_length;
254     }
255
256     /**
257      * get the style's name
258      * @return name of the style
259      * @see #getNameLength()
260      */

261
262     public String JavaDoc getName()
263     {
264         return field_4_name;
265     }
266
267     // end user defined
268
// only for buildin records
269

270     /**
271      * if this is a builtin style get the number of the built in style
272      * @return builtin style number (0-7)
273      *
274      */

275
276     public byte getBuiltin()
277     {
278         return field_2_builtin_style;
279     }
280
281     /**
282      * get the row or column level of the style (if builtin 1||2)
283      */

284
285     public byte getOutlineStyleLevel()
286     {
287         return field_3_outline_style_level;
288     }
289
290     // end builtin records
291
public String JavaDoc toString()
292     {
293         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
294
295         buffer.append("[STYLE]\n");
296         buffer.append(" .xf_index_raw = ")
297             .append(Integer.toHexString(getIndex())).append("\n");
298         buffer.append(" .type = ")
299             .append(Integer.toHexString(getType())).append("\n");
300         buffer.append(" .xf_index = ")
301             .append(Integer.toHexString(getXFIndex())).append("\n");
302         if (getType() == STYLE_BUILT_IN)
303         {
304             buffer.append(" .builtin_style = ")
305                 .append(Integer.toHexString(getBuiltin())).append("\n");
306             buffer.append(" .outline_level = ")
307                 .append(Integer.toHexString(getOutlineStyleLevel()))
308                 .append("\n");
309         }
310         else if (getType() == STYLE_USER_DEFINED)
311         {
312             buffer.append(" .name_length = ")
313                 .append(Integer.toHexString(getNameLength())).append("\n");
314             buffer.append(" .name = ").append(getName())
315                 .append("\n");
316         }
317         buffer.append("[/STYLE]\n");
318         return buffer.toString();
319     }
320
321     private short setField(int fieldValue, int new_value, int mask,
322                            int shiftLeft)
323     {
324         return ( short ) ((fieldValue & ~mask)
325                           | ((new_value << shiftLeft) & mask));
326     }
327
328     public int serialize(int offset, byte [] data)
329     {
330         LittleEndian.putShort(data, 0 + offset, sid);
331         if (getType() == STYLE_BUILT_IN)
332         {
333             LittleEndian.putShort(data, 2 + offset,
334                                   (( short ) 0x04)); // 4 bytes (8 total)
335
}
336         else
337         {
338             LittleEndian.putShort(data, 2 + offset,
339                                   (( short ) (getRecordSize()-4)));
340         }
341         LittleEndian.putShort(data, 4 + offset, getIndex());
342         if (getType() == STYLE_BUILT_IN)
343         {
344             data[ 6 + offset ] = getBuiltin();
345             data[ 7 + offset ] = getOutlineStyleLevel();
346         }
347         else
348         {
349             LittleEndian.putShort(data, 6 + offset , getNameLength());
350             data[8+offset]=this.field_3_string_options;
351             StringUtil.putCompressedUnicode(getName(), data, 9 + offset);
352         }
353         return getRecordSize();
354     }
355
356     public int getRecordSize()
357     {
358         int retval;
359
360         if (getType() == STYLE_BUILT_IN)
361         {
362             retval = 8;
363         }
364         else
365         {
366              if (fHighByte.isSet(field_3_string_options)) {
367                  retval= 9+2*getNameLength();
368              }else {
369                 retval = 9 + getNameLength();
370              }
371         }
372         return retval;
373     }
374
375     public short getSid()
376     {
377         return this.sid;
378     }
379 }
380
Popular Tags