KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Supports the STRING record structure.
26  *
27  * @author Glen Stampoultzis (glens at apache.org)
28  */

29 public class StringRecord
30         extends Record
31 {
32     public final static short sid = 0x207;
33     private int field_1_string_length;
34     private byte field_2_unicode_flag;
35     private String JavaDoc field_3_string;
36
37
38     public StringRecord()
39     {
40     }
41
42     /**
43      * Constructs a String record and sets its fields appropriately.
44      *
45      * @param id id must be 0x204 or an exception will be throw upon validation
46      * @param size the size of the data area of the record
47      * @param data data of the record (should not contain sid/len)
48      */

49     public StringRecord( short id, short size, byte[] data )
50     {
51         super( id, size, data );
52     }
53
54     /**
55      * Constructs an String record and sets its fields appropriately.
56      *
57      * @param id id must be 0x204 or an exception will be throw upon validation
58      * @param size the size of the data area of the record
59      * @param data data of the record (should not contain sid/len)
60      * @param offset of the record
61      */

62     public StringRecord( short id, short size, byte[] data, int offset )
63     {
64         super( id, size, data, offset );
65     }
66
67
68     /**
69      * Throw a runtime exception in the event of a
70      * record passed with a differing ID.
71      *
72      * @param id alleged id for this record
73      */

74     protected void validateSid( short id )
75     {
76         if (id != this.sid)
77         {
78             throw new RecordFormatException("Not a valid StringRecord");
79         }
80     }
81
82     /**
83      * called by the constructor, should set class level fields. Should throw
84      * runtime exception for bad/icomplete data.
85      *
86      * @param data raw data
87      * @param size size of data
88      * @param offset of the record's data (provided a big array of the file)
89      */

90     protected void fillFields( byte[] data, short size, int offset )
91     {
92         field_1_string_length = LittleEndian.getUShort(data, 0 + offset);
93         field_2_unicode_flag = data[ 2 + offset ];
94         if (isUnCompressedUnicode())
95         {
96             field_3_string = StringUtil.getFromUnicodeBE(data, 3 + offset, field_1_string_length );
97         }
98         else
99         {
100             field_3_string = StringUtil.getFromCompressedUnicode(data, 3 + offset, field_1_string_length);
101         }
102     }
103
104     public boolean isInValueSection()
105     {
106         return true;
107     }
108
109     private int getStringLength()
110     {
111         return field_1_string_length;
112     }
113
114     private int getStringByteLength()
115     {
116         return isUnCompressedUnicode() ? field_1_string_length * 2 : field_1_string_length;
117     }
118
119     /**
120      * gives the current serialized size of the record. Should include the sid and reclength (4 bytes).
121      */

122     public int getRecordSize()
123     {
124         return 4 + 2 + 1 + getStringByteLength();
125     }
126
127     /**
128      * is this uncompressed unicode (16bit)? Or just 8-bit compressed?
129      * @return isUnicode - True for 16bit- false for 8bit
130      */

131     public boolean isUnCompressedUnicode()
132     {
133         return (field_2_unicode_flag == 1);
134     }
135
136     /**
137      * called by the class that is responsible for writing this sucker.
138      * Subclasses should implement this so that their data is passed back in a
139      * byte array.
140      *
141      * @param offset to begin writing at
142      * @param data byte array containing instance data
143      * @return number of bytes written
144      */

145     public int serialize( int offset, byte[] data )
146     {
147         LittleEndian.putShort(data, 0 + offset, sid);
148         LittleEndian.putShort(data, 2 + offset, ( short ) (3 + getStringByteLength()));
149         LittleEndian.putUShort(data, 4 + offset, field_1_string_length);
150         data[6 + offset] = field_2_unicode_flag;
151         if (isUnCompressedUnicode())
152         {
153             StringUtil.putUnicodeLE(field_3_string, data, 7 + offset);
154         }
155         else
156         {
157             StringUtil.putCompressedUnicode(field_3_string, data, 7 + offset);
158         }
159         return getRecordSize();
160     }
161
162     /**
163      * return the non static version of the id for this record.
164      */

165     public short getSid()
166     {
167         return sid;
168     }
169
170     /**
171      * @return The string represented by this record.
172      */

173     public String JavaDoc getString()
174     {
175         return field_3_string;
176     }
177
178     /**
179      * Sets whether the string is compressed or not
180      * @param unicode_flag 1 = uncompressed, 0 = compressed
181      */

182     public void setCompressedFlag( byte unicode_flag )
183     {
184         this.field_2_unicode_flag = unicode_flag;
185     }
186
187     /**
188      * Sets the string represented by this record.
189      */

190     public void setString( String JavaDoc string )
191     {
192         this.field_1_string_length = string.length();
193         this.field_3_string = string;
194     }
195
196
197
198     public String JavaDoc toString()
199     {
200         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
201
202         buffer.append("[STRING]\n");
203         buffer.append(" .string = ")
204             .append(field_3_string).append("\n");
205         buffer.append("[/STRING]\n");
206         return buffer.toString();
207     }
208     
209     public Object JavaDoc clone() {
210         StringRecord rec = new StringRecord();
211         rec.field_1_string_length = this.field_1_string_length;
212         rec.field_2_unicode_flag= this.field_2_unicode_flag;
213         rec.field_3_string = this.field_3_string;
214         return rec;
215
216     }
217
218 }
219
Popular Tags