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