KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
20  * LabelRecord.java
21  *
22  * Created on November 11, 2001, 12:51 PM
23  */

24 package org.apache.poi.hssf.record;
25
26 import org.apache.poi.util.LittleEndian;
27 import org.apache.poi.util.StringUtil;
28
29 /**
30  * Label Record - read only support for strings stored directly in the cell.. Don't
31  * use this (except to read), use LabelSST instead <P>
32  * REFERENCE: PG 325 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
33  * @author Andrew C. Oliver (acoliver at apache dot org)
34  * @author Jason Height (jheight at chariot dot net dot au)
35  * @version 2.0-pre
36  * @see org.apache.poi.hssf.record.LabelSSTRecord
37  */

38
39 public class LabelRecord
40     extends Record
41     implements CellValueRecordInterface
42 {
43     public final static short sid = 0x204;
44     //private short field_1_row;
45
private int field_1_row;
46     private short field_2_column;
47     private short field_3_xf_index;
48     private short field_4_string_len;
49     private byte field_5_unicode_flag;
50     private String JavaDoc field_6_value;
51
52     /** Creates new LabelRecord */
53
54     public LabelRecord()
55     {
56     }
57
58     /**
59      * Constructs an Label record and sets its fields appropriately.
60      *
61      * @param id id must be 0x204 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 LabelRecord(short id, short size, byte [] data)
67     {
68         super(id, size, data);
69     }
70
71     /**
72      * Constructs an Label record and sets its fields appropriately.
73      *
74      * @param id id must be 0x204 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 of the record
78      */

79
80     public LabelRecord(short id, short size, byte [] data, int offset)
81     {
82         super(id, size, data, offset);
83     }
84
85     /**
86      * called by constructor, should throw runtime exception in the event of a
87      * record passed with a differing ID.
88      *
89      * @param id alleged id for this record
90      */

91
92     protected void validateSid(short id)
93     {
94         if (id != this.sid)
95         {
96             throw new RecordFormatException("Not a valid LabelRecord");
97         }
98     }
99
100     /**
101      * called by the constructor, should set class level fields. Should throw
102      * runtime exception for bad/icomplete data.
103      *
104      * @param data raw data
105      * @param size size of data
106      */

107
108     protected void fillFields(byte [] data, short size, int offset)
109     {
110         //field_1_row = LittleEndian.getShort(data, 0 + offset);
111
field_1_row = LittleEndian.getUShort(data, 0 + offset);
112         field_2_column = LittleEndian.getShort(data, 2 + offset);
113         field_3_xf_index = LittleEndian.getShort(data, 4 + offset);
114         field_4_string_len = LittleEndian.getShort(data, 6 + offset);
115         field_5_unicode_flag = data[ 8 + offset ];
116         if (field_4_string_len > 0) {
117           if (isUnCompressedUnicode()) {
118             field_6_value = StringUtil.getFromUnicodeLE(data, 9 + offset,
119                                                       field_4_string_len);
120         }
121           else {
122             field_6_value = StringUtil.getFromCompressedUnicode(data, 9 + offset,
123                 getStringLength());
124         }
125         } else field_6_value = null;
126     }
127
128 /* READ ONLY ACCESS... THIS IS FOR COMPATIBILITY ONLY...USE LABELSST!
129       public void setRow(short row) {
130         field_1_row = row;
131       }
132
133       public void setColumn(short col) {
134         field_2_column = col;
135       }
136
137       public void setXFIndex(short index) {
138         field_3_xf_index = index;
139       }
140   */

141     //public short getRow()
142
public int getRow()
143     {
144         return field_1_row;
145     }
146
147     public short getColumn()
148     {
149         return field_2_column;
150     }
151
152     public short getXFIndex()
153     {
154         return field_3_xf_index;
155     }
156
157     /**
158      * get the number of characters this string contains
159      * @return number of characters
160      */

161
162     public short getStringLength()
163     {
164         return field_4_string_len;
165     }
166
167     /**
168      * is this uncompressed unicode (16bit)? Or just 8-bit compressed?
169      * @return isUnicode - True for 16bit- false for 8bit
170      */

171
172     public boolean isUnCompressedUnicode()
173     {
174         return (field_5_unicode_flag == 1);
175     }
176
177     /**
178      * get the value
179      *
180      * @return the text string
181      * @see #getStringLength()
182      */

183
184     public String JavaDoc getValue()
185     {
186         return field_6_value;
187     }
188
189     /**
190      * THROWS A RUNTIME EXCEPTION.. USE LABELSSTRecords. YOU HAVE NO REASON to use LABELRecord!!
191      */

192
193     public int serialize(int offset, byte [] data)
194     {
195         throw new RecordFormatException(
196             "Label Records are supported READ ONLY...convert to LabelSST");
197     }
198
199     public short getSid()
200     {
201         return this.sid;
202     }
203
204     public String JavaDoc toString()
205     {
206         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
207         buffer.append("[LABEL]\n");
208         buffer.append(" .row = ")
209             .append(Integer.toHexString(getRow())).append("\n");
210         buffer.append(" .column = ")
211             .append(Integer.toHexString(getColumn())).append("\n");
212         buffer.append(" .xfindex = ")
213             .append(Integer.toHexString(getXFIndex())).append("\n");
214         buffer.append(" .string_len = ")
215             .append(Integer.toHexString(field_4_string_len)).append("\n");
216         buffer.append(" .unicode_flag = ")
217             .append(Integer.toHexString(field_5_unicode_flag)).append("\n");
218         buffer.append(" .value = ")
219             .append(getValue()).append("\n");
220         buffer.append("[/LABEL]\n");
221         return buffer.toString();
222     }
223
224
225     public boolean isBefore(CellValueRecordInterface i)
226     {
227         if (this.getRow() > i.getRow())
228         {
229             return false;
230         }
231         if ((this.getRow() == i.getRow())
232                 && (this.getColumn() > i.getColumn()))
233         {
234             return false;
235         }
236         if ((this.getRow() == i.getRow())
237                 && (this.getColumn() == i.getColumn()))
238         {
239             return false;
240         }
241         return true;
242     }
243
244     public boolean isAfter(CellValueRecordInterface i)
245     {
246         if (this.getRow() < i.getRow())
247         {
248             return false;
249         }
250         if ((this.getRow() == i.getRow())
251                 && (this.getColumn() < i.getColumn()))
252         {
253             return false;
254         }
255         if ((this.getRow() == i.getRow())
256                 && (this.getColumn() == i.getColumn()))
257         {
258             return false;
259         }
260         return true;
261     }
262
263     public boolean isEqual(CellValueRecordInterface i)
264     {
265         return ((this.getRow() == i.getRow())
266                 && (this.getColumn() == i.getColumn()));
267     }
268
269     public boolean isInValueSection()
270     {
271         return true;
272     }
273
274     public boolean isValue()
275     {
276         return true;
277     }
278
279     /**
280      * NO-OP!
281      */

282
283     public void setColumn(short col)
284     {
285     }
286
287     /**
288      * NO-OP!
289      */

290
291     //public void setRow(short row)
292
public void setRow(int row)
293     {
294     }
295
296     /**
297      * no op!
298      */

299
300     public void setXFIndex(short xf)
301     {
302     }
303
304     public Object JavaDoc clone() {
305       LabelRecord rec = new LabelRecord();
306       rec.field_1_row = field_1_row;
307       rec.field_2_column = field_2_column;
308       rec.field_3_xf_index = field_3_xf_index;
309       rec.field_4_string_len = field_4_string_len;
310       rec.field_5_unicode_flag = field_5_unicode_flag;
311       rec.field_6_value = field_6_value;
312       return rec;
313     }
314 }
315
Popular Tags