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   package org.apache.poi.hssf.record;
57   
58   import org.apache.poi.util.LittleEndian;
59   
60   /**
61    * Title:        Label SST Record<P>
62    * Description:  Refers to a string in the shared string table and is a column
63    *               value.  <P>
64    * REFERENCE:  PG 325 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
65    * @author Andrew C. Oliver (acoliver at apache dot org)
66    * @version 2.0-pre
67    */
68   
69   public class LabelSSTRecord
70       extends Record
71       implements CellValueRecordInterface, Comparable
72   {
73       public final static short sid = 0xfd;
74       private short             field_1_row;
75       private short             field_2_column;
76       private short             field_3_xf_index;
77       private int               field_4_sst_index;
78   
79       public LabelSSTRecord()
80       {
81       }
82   
83       /**
84        * Constructs an LabelSST record and sets its fields appropriately.
85        *
86        * @param id     id must be 0xfd or an exception will be throw upon validation
87        * @param size  the size of the data area of the record
88        * @param data  data of the record (should not contain sid/len)
89        */
90   
91       public LabelSSTRecord(short id, short size, byte [] data)
92       {
93           super(id, size, data);
94       }
95   
96       /**
97        * Constructs an LabelSST record and sets its fields appropriately.
98        *
99        * @param id     id must be 0xfd or an exception will be throw upon validation
100       * @param size  the size of the data area of the record
101       * @param data  data of the record (should not contain sid/len)
102       * @param offset of the record's data
103       */
104  
105      public LabelSSTRecord(short id, short size, byte [] data, int offset)
106      {
107          super(id, size, data, offset);
108      }
109  
110      protected void validateSid(short id)
111      {
112          if (id != sid)
113          {
114              throw new RecordFormatException("NOT A valid LabelSST RECORD");
115          }
116      }
117  
118      protected void fillFields(byte [] data, short size, int offset)
119      {
120          field_1_row       = LittleEndian.getShort(data, 0 + offset);
121          field_2_column    = LittleEndian.getShort(data, 2 + offset);
122          field_3_xf_index  = LittleEndian.getShort(data, 4 + offset);
123          field_4_sst_index = LittleEndian.getInt(data, 6 + offset);
124      }
125  
126      public void setRow(short row)
127      {
128          field_1_row = row;
129      }
130  
131      public void setColumn(short col)
132      {
133          field_2_column = col;
134      }
135  
136      /**
137       * set the index to the extended format record
138       *
139       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
140       * @param index - the index to the XF record
141       */
142  
143      public void setXFIndex(short index)
144      {
145          field_3_xf_index = index;
146      }
147  
148      /**
149       * set the index to the string in the SSTRecord
150       *
151       * @param index - of string in the SST Table
152       * @see org.apache.poi.hssf.record.SSTRecord
153       */
154  
155      public void setSSTIndex(int index)
156      {
157          field_4_sst_index = index;
158      }
159  
160      public short getRow()
161      {
162          return field_1_row;
163      }
164  
165      public short getColumn()
166      {
167          return field_2_column;
168      }
169  
170      /**
171       * get the index to the extended format record
172       *
173       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
174       * @return the index to the XF record
175       */
176  
177      public short getXFIndex()
178      {
179          return field_3_xf_index;
180      }
181  
182      /**
183       * get the index to the string in the SSTRecord
184       *
185       * @return index of string in the SST Table
186       * @see org.apache.poi.hssf.record.SSTRecord
187       */
188  
189      public int getSSTIndex()
190      {
191          return field_4_sst_index;
192      }
193  
194      public String toString()
195      {
196          StringBuffer buffer = new StringBuffer();
197  
198          buffer.append("[LABELSST]\n");
199          buffer.append("    .row            = ")
200              .append(Integer.toHexString(getRow())).append("\n");
201          buffer.append("    .column         = ")
202              .append(Integer.toHexString(getColumn())).append("\n");
203          buffer.append("    .xfindex        = ")
204              .append(Integer.toHexString(getXFIndex())).append("\n");
205          buffer.append("    .sstindex       = ")
206              .append(Integer.toHexString(getSSTIndex())).append("\n");
207          buffer.append("[/LABELSST]\n");
208          return buffer.toString();
209      }
210  
211      public int serialize(int offset, byte [] data)
212      {
213          LittleEndian.putShort(data, 0 + offset, sid);
214          LittleEndian.putShort(data, 2 + offset, ( short ) 10);
215          LittleEndian.putShort(data, 4 + offset, getRow());
216          LittleEndian.putShort(data, 6 + offset, getColumn());
217          LittleEndian.putShort(data, 8 + offset, getXFIndex());
218          LittleEndian.putInt(data, 10 + offset, getSSTIndex());
219          return getRecordSize();
220      }
221  
222      public int getRecordSize()
223      {
224          return 14;
225      }
226  
227      public short getSid()
228      {
229          return this.sid;
230      }
231  
232      public boolean isBefore(CellValueRecordInterface i)
233      {
234          if (this.getRow() > i.getRow())
235          {
236              return false;
237          }
238          if ((this.getRow() == i.getRow())
239                  && (this.getColumn() > i.getColumn()))
240          {
241              return false;
242          }
243          if ((this.getRow() == i.getRow())
244                  && (this.getColumn() == i.getColumn()))
245          {
246              return false;
247          }
248          return true;
249      }
250  
251      public boolean isAfter(CellValueRecordInterface i)
252      {
253          if (this.getRow() < i.getRow())
254          {
255              return false;
256          }
257          if ((this.getRow() == i.getRow())
258                  && (this.getColumn() < i.getColumn()))
259          {
260              return false;
261          }
262          if ((this.getRow() == i.getRow())
263                  && (this.getColumn() == i.getColumn()))
264          {
265              return false;
266          }
267          return true;
268      }
269  
270      public boolean isEqual(CellValueRecordInterface i)
271      {
272          return ((this.getRow() == i.getRow())
273                  && (this.getColumn() == i.getColumn()));
274      }
275  
276      public boolean isInValueSection()
277      {
278          return true;
279      }
280  
281      public boolean isValue()
282      {
283          return true;
284      }
285  
286      public int compareTo(Object obj)
287      {
288          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
289  
290          if ((this.getRow() == loc.getRow())
291                  && (this.getColumn() == loc.getColumn()))
292          {
293              return 0;
294          }
295          if (this.getRow() < loc.getRow())
296          {
297              return -1;
298          }
299          if (this.getRow() > loc.getRow())
300          {
301              return 1;
302          }
303          if (this.getColumn() < loc.getColumn())
304          {
305              return -1;
306          }
307          if (this.getColumn() > loc.getColumn())
308          {
309              return 1;
310          }
311          return -1;
312      }
313  
314      public boolean equals(Object obj)
315      {
316          if (!(obj instanceof CellValueRecordInterface))
317          {
318              return false;
319          }
320          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
321  
322          if ((this.getRow() == loc.getRow())
323                  && (this.getColumn() == loc.getColumn()))
324          {
325              return true;
326          }
327          return false;
328      }
329  }
330