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