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