1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   /*
57    * LabelRecord.java
58    *
59    * Created on November 11, 2001, 12:51 PM
60    */
61   package org.apache.poi.hssf.record;
62   
63   import org.apache.poi.util.LittleEndian;
64   import org.apache.poi.util.StringUtil;
65   
66   /**
67    * Label Record - read only support for strings stored directly in the cell..  Don't
68    * use this (except to read), use LabelSST instead <P>
69    * REFERENCE:  PG 325 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
70    * @author Andrew C. Oliver (acoliver at apache dot org)
71    * @author Jason Height (jheight at chariot dot net dot au)
72    * @version 2.0-pre
73    * @see org.apache.poi.hssf.record.LabelSSTRecord
74    */
75   
76   public class LabelRecord
77       extends Record
78       implements CellValueRecordInterface
79   {
80       public final static short sid = 0x204;
81       //private short             field_1_row;
82       private int             field_1_row;
83       private short             field_2_column;
84       private short             field_3_xf_index;
85       private short             field_4_string_len;
86       private byte              field_5_unicode_flag;
87       private String            field_6_value;
88   
89       /** Creates new LabelRecord */
90   
91       public LabelRecord()
92       {
93       }
94   
95       /**
96        * Constructs an Label record and sets its fields appropriately.
97        *
98        * @param id     id must be 0x204 or an exception will be throw upon validation
99        * @param size  the size of the data area of the record
100       * @param data  data of the record (should not contain sid/len)
101       */
102  
103      public LabelRecord(short id, short size, byte [] data)
104      {
105          super(id, size, data);
106      }
107  
108      /**
109       * Constructs an Label record and sets its fields appropriately.
110       *
111       * @param id     id must be 0x204 or an exception will be throw upon validation
112       * @param size  the size of the data area of the record
113       * @param data  data of the record (should not contain sid/len)
114       * @param offset of the record
115       */
116  
117      public LabelRecord(short id, short size, byte [] data, int offset)
118      {
119          super(id, size, data, offset);
120      }
121  
122      /**
123       * called by constructor, should throw runtime exception in the event of a
124       * record passed with a differing ID.
125       *
126       * @param id alleged id for this record
127       */
128  
129      protected void validateSid(short id)
130      {
131          if (id != this.sid)
132          {
133              throw new RecordFormatException("Not a valid LabelRecord");
134          }
135      }
136  
137      /**
138       * called by the constructor, should set class level fields.  Should throw
139       * runtime exception for bad/icomplete data.
140       *
141       * @param data raw data
142       * @param size size of data
143       */
144  
145      protected void fillFields(byte [] data, short size, int offset)
146      {
147          //field_1_row          = LittleEndian.getShort(data, 0 + offset);
148          field_1_row          = LittleEndian.getUShort(data, 0 + offset);
149          field_2_column       = LittleEndian.getShort(data, 2 + offset);
150          field_3_xf_index     = LittleEndian.getShort(data, 4 + offset);
151          field_4_string_len   = LittleEndian.getShort(data, 6 + offset);
152          field_5_unicode_flag = data[ 8 + offset ];
153          if (isUnCompressedUnicode())
154          {
155              field_6_value = StringUtil.getFromUnicode(data, 8 + offset,
156                                                        field_4_string_len);
157          }
158          else
159          {
160              field_6_value = new String(data, 9 + offset, getStringLength());
161          }
162      }
163  
164  /* READ ONLY ACCESS... THIS IS FOR COMPATIBILITY ONLY...USE LABELSST!
165        public void setRow(short row) {
166          field_1_row = row;
167        }
168  
169        public void setColumn(short col) {
170          field_2_column = col;
171        }
172  
173        public void setXFIndex(short index) {
174          field_3_xf_index = index;
175        }
176    */
177      //public short getRow()
178      public int getRow()
179      {
180          return field_1_row;
181      }
182  
183      public short getColumn()
184      {
185          return field_2_column;
186      }
187  
188      public short getXFIndex()
189      {
190          return field_3_xf_index;
191      }
192  
193      /**
194       * get the number of characters this string contains
195       * @return number of characters
196       */
197  
198      public short getStringLength()
199      {
200          return field_4_string_len;
201      }
202  
203      /**
204       * is this uncompressed unicode (16bit)?  Or just 8-bit compressed?
205       * @return isUnicode - True for 16bit- false for 8bit
206       */
207  
208      public boolean isUnCompressedUnicode()
209      {
210          return (field_5_unicode_flag == 1);
211      }
212  
213      /**
214       * get the value
215       *
216       * @return the text string
217       * @see #getStringLength()
218       */
219  
220      public String getValue()
221      {
222          return field_6_value;
223      }
224  
225      /**
226       * THROWS A RUNTIME EXCEPTION..  USE LABELSSTRecords.  YOU HAVE NO REASON to use LABELRecord!!
227       */
228  
229      public int serialize(int offset, byte [] data)
230      {
231          throw new RecordFormatException(
232              "Label Records are supported READ ONLY...convert to LabelSST");
233      }
234  
235      public short getSid()
236      {
237          return this.sid;
238      }
239  
240      public boolean isBefore(CellValueRecordInterface i)
241      {
242          if (this.getRow() > i.getRow())
243          {
244              return false;
245          }
246          if ((this.getRow() == i.getRow())
247                  && (this.getColumn() > i.getColumn()))
248          {
249              return false;
250          }
251          if ((this.getRow() == i.getRow())
252                  && (this.getColumn() == i.getColumn()))
253          {
254              return false;
255          }
256          return true;
257      }
258  
259      public boolean isAfter(CellValueRecordInterface i)
260      {
261          if (this.getRow() < i.getRow())
262          {
263              return false;
264          }
265          if ((this.getRow() == i.getRow())
266                  && (this.getColumn() < i.getColumn()))
267          {
268              return false;
269          }
270          if ((this.getRow() == i.getRow())
271                  && (this.getColumn() == i.getColumn()))
272          {
273              return false;
274          }
275          return true;
276      }
277  
278      public boolean isEqual(CellValueRecordInterface i)
279      {
280          return ((this.getRow() == i.getRow())
281                  && (this.getColumn() == i.getColumn()));
282      }
283  
284      public boolean isInValueSection()
285      {
286          return true;
287      }
288  
289      public boolean isValue()
290      {
291          return true;
292      }
293  
294      /**
295       * NO-OP!
296       */
297  
298      public void setColumn(short col)
299      {
300      }
301  
302      /**
303       * NO-OP!
304       */
305  
306      //public void setRow(short row)
307      public void setRow(int row)
308      {
309      }
310  
311      /**
312       * no op!
313       */
314  
315      public void setXFIndex(short xf)
316      {
317      }
318  
319      public Object clone() {
320        LabelRecord rec = new LabelRecord();
321        rec.field_1_row = field_1_row;
322        rec.field_2_column = field_2_column;
323        rec.field_3_xf_index = field_3_xf_index;
324        rec.field_4_string_len = field_4_string_len;
325        rec.field_5_unicode_flag = field_5_unicode_flag;
326        rec.field_6_value = field_6_value;
327        return rec;
328      }
329  }
330