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    * @version 2.0-pre
72    * @see org.apache.poi.hssf.record.LabelSSTRecord
73    */
74   
75   public class LabelRecord
76       extends Record
77       implements CellValueRecordInterface
78   {
79       public final static short sid = 0x204;
80       //private short             field_1_row;
81       private int             field_1_row;
82       private short             field_2_column;
83       private short             field_3_xf_index;
84       private short             field_4_string_len;
85       private byte              field_5_unicode_flag;
86       private String            field_6_value;
87   
88       /** Creates new LabelRecord */
89   
90       public LabelRecord()
91       {
92       }
93   
94       /**
95        * Constructs an Label record and sets its fields appropriately.
96        *
97        * @param id     id must be 0x204 or an exception will be throw upon validation
98        * @param size  the size of the data area of the record
99        * @param data  data of the record (should not contain sid/len)
100       */
101  
102      public LabelRecord(short id, short size, byte [] data)
103      {
104          super(id, size, data);
105      }
106  
107      /**
108       * Constructs an Label record and sets its fields appropriately.
109       *
110       * @param id     id must be 0x204 or an exception will be throw upon validation
111       * @param size  the size of the data area of the record
112       * @param data  data of the record (should not contain sid/len)
113       * @param offset of the record
114       */
115  
116      public LabelRecord(short id, short size, byte [] data, int offset)
117      {
118          super(id, size, data, offset);
119      }
120  
121      /**
122       * called by constructor, should throw runtime exception in the event of a
123       * record passed with a differing ID.
124       *
125       * @param id alleged id for this record
126       */
127  
128      protected void validateSid(short id)
129      {
130          if (id != this.sid)
131          {
132              throw new RecordFormatException("Not a valid LabelRecord");
133          }
134      }
135  
136      /**
137       * called by the constructor, should set class level fields.  Should throw
138       * runtime exception for bad/icomplete data.
139       *
140       * @param data raw data
141       * @param size size of data
142       */
143  
144      protected void fillFields(byte [] data, short size, int offset)
145      {
146          //field_1_row          = LittleEndian.getShort(data, 0 + offset);
147          field_1_row          = LittleEndian.getUShort(data, 0 + offset);
148          field_2_column       = LittleEndian.getShort(data, 2 + offset);
149          field_3_xf_index     = LittleEndian.getShort(data, 4 + offset);
150          field_4_string_len   = LittleEndian.getShort(data, 6 + offset);
151          field_5_unicode_flag = data[ 8 + offset ];
152          if (isUnCompressedUnicode())
153          {
154              field_6_value = StringUtil.getFromUnicode(data, 8 + offset,
155                                                        field_4_string_len);
156          }
157          else
158          {
159              field_6_value = new String(data, 9 + offset, getStringLength());
160          }
161      }
162  
163  /* READ ONLY ACCESS... THIS IS FOR COMPATIBILITY ONLY...USE LABELSST!
164        public void setRow(short row) {
165          field_1_row = row;
166        }
167  
168        public void setColumn(short col) {
169          field_2_column = col;
170        }
171  
172        public void setXFIndex(short index) {
173          field_3_xf_index = index;
174        }
175    */
176      //public short getRow()
177      public int getRow()
178      {
179          return field_1_row;
180      }
181  
182      public short getColumn()
183      {
184          return field_2_column;
185      }
186  
187      public short getXFIndex()
188      {
189          return field_3_xf_index;
190      }
191  
192      /**
193       * get the number of characters this string contains
194       * @return number of characters
195       */
196  
197      public short getStringLength()
198      {
199          return field_4_string_len;
200      }
201  
202      /**
203       * is this uncompressed unicode (16bit)?  Or just 8-bit compressed?
204       * @return isUnicode - True for 16bit- false for 8bit
205       */
206  
207      public boolean isUnCompressedUnicode()
208      {
209          return (field_5_unicode_flag == 1);
210      }
211  
212      /**
213       * get the value
214       *
215       * @return the text string
216       * @see #getStringLength()
217       */
218  
219      public String getValue()
220      {
221          return field_6_value;
222      }
223  
224      /**
225       * THROWS A RUNTIME EXCEPTION..  USE LABELSSTRecords.  YOU HAVE NO REASON to use LABELRecord!!
226       */
227  
228      public int serialize(int offset, byte [] data)
229      {
230          throw new RecordFormatException(
231              "Label Records are supported READ ONLY...convert to LabelSST");
232      }
233  
234      public short getSid()
235      {
236          return this.sid;
237      }
238  
239      public boolean isBefore(CellValueRecordInterface i)
240      {
241          if (this.getRow() > i.getRow())
242          {
243              return false;
244          }
245          if ((this.getRow() == i.getRow())
246                  && (this.getColumn() > i.getColumn()))
247          {
248              return false;
249          }
250          if ((this.getRow() == i.getRow())
251                  && (this.getColumn() == i.getColumn()))
252          {
253              return false;
254          }
255          return true;
256      }
257  
258      public boolean isAfter(CellValueRecordInterface i)
259      {
260          if (this.getRow() < i.getRow())
261          {
262              return false;
263          }
264          if ((this.getRow() == i.getRow())
265                  && (this.getColumn() < i.getColumn()))
266          {
267              return false;
268          }
269          if ((this.getRow() == i.getRow())
270                  && (this.getColumn() == i.getColumn()))
271          {
272              return false;
273          }
274          return true;
275      }
276  
277      public boolean isEqual(CellValueRecordInterface i)
278      {
279          return ((this.getRow() == i.getRow())
280                  && (this.getColumn() == i.getColumn()));
281      }
282  
283      public boolean isInValueSection()
284      {
285          return true;
286      }
287  
288      public boolean isValue()
289      {
290          return true;
291      }
292  
293      /**
294       * NO-OP!
295       */
296  
297      public void setColumn(short col)
298      {
299      }
300  
301      /**
302       * NO-OP!
303       */
304  
305      //public void setRow(short row)
306      public void setRow(int row)
307      {
308      }
309  
310      /**
311       * no op!
312       */
313  
314      public void setXFIndex(short xf)
315      {
316      }
317  }
318