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   import org.apache.poi.hssf.util.RKUtil;
60   
61   /**
62    * Title:        RK Record
63    * Description:  An internal 32 bit number with the two most significant bits
64    *               storing the type.  This is part of a bizarre scheme to save disk
65    *               space and memory (gee look at all the other whole records that
66    *               are in the file just "cause"..,far better to waste processor
67    *               cycles on this then leave on of those "valuable" records out).<P>
68    * We support this in READ-ONLY mode.  HSSF converts these to NUMBER records<P>
69    *
70    *
71    *
72    * REFERENCE:  PG 376 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
73    * @author Andrew C. Oliver (acoliver at apache dot org)
74    * @version 2.0-pre
75    * @see org.apache.poi.hssf.record.NumberRecord
76    */
77   
78   public class RKRecord
79       extends Record
80       implements CellValueRecordInterface
81   {
82       public final static short sid                      = 0x27e;
83       public final static short RK_IEEE_NUMBER           = 0;
84       public final static short RK_IEEE_NUMBER_TIMES_100 = 1;
85       public final static short RK_INTEGER               = 2;
86       public final static short RK_INTEGER_TIMES_100     = 3;
87       private short             field_1_row;
88       private short             field_2_col;
89       private short             field_3_xf_index;
90       private int               field_4_rk_number;
91   
92       public RKRecord()
93       {
94       }
95   
96       /**
97        * Constructs a RK record and sets its fields appropriately.
98        *
99        * @param id     id must be 0x27e 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       */
103  
104      public RKRecord(short id, short size, byte [] data)
105      {
106          super(id, size, data);
107      }
108  
109      /**
110       * Constructs a RK record and sets its fields appropriately.
111       *
112       * @param id     id must be 0x27e or an exception will be throw upon validation
113       * @param size  the size of the data area of the record
114       * @param data  data of the record (should not contain sid/len)
115       * @param offset of the data
116       */
117  
118      public RKRecord(short id, short size, byte [] data, int offset)
119      {
120          super(id, size, data, offset);
121      }
122  
123      protected void validateSid(short id)
124      {
125          if (id != sid)
126          {
127              throw new RecordFormatException("NOT A valid RK RECORD");
128          }
129      }
130  
131      protected void fillFields(byte [] data, short size, int offset)
132      {
133          field_1_row       = LittleEndian.getShort(data, 0 + offset);
134          field_2_col       = LittleEndian.getShort(data, 2 + offset);
135          field_3_xf_index  = LittleEndian.getShort(data, 4 + offset);
136          field_4_rk_number = LittleEndian.getInt(data, 6 + offset);
137      }
138  
139      public short getRow()
140      {
141          return field_1_row;
142      }
143  
144      public short getColumn()
145      {
146          return field_2_col;
147      }
148  
149      public short getXFIndex()
150      {
151          return field_3_xf_index;
152      }
153  
154      public int getRKField()
155      {
156          return field_4_rk_number;
157      }
158  
159      /**
160       * Get the type of the number
161       *
162       * @return one of these values:
163       *         <OL START="0">
164       *             <LI>RK_IEEE_NUMBER</LI>
165       *             <LI>RK_IEEE_NUMBER_TIMES_100</LI>
166       *             <LI>RK_INTEGER</LI>
167       *             <LI>RK_INTEGER_TIMES_100</LI>
168       *         </OL>
169       */
170  
171      public short getRKType()
172      {
173          return ( short ) (field_4_rk_number & 3);
174      }
175  
176      /**
177       * Extract the value of the number
178       * <P>
179       * The mechanism for determining the value is dependent on the two
180       * low order bits of the raw number. If bit 1 is set, the number
181       * is an integer and can be cast directly as a double, otherwise,
182       * it's apparently the exponent and mantissa of a double (and the
183       * remaining low-order bits of the double's mantissa are 0's).
184       * <P>
185       * If bit 0 is set, the result of the conversion to a double is
186       * divided by 100; otherwise, the value is left alone.
187       * <P>
188       * [insert picture of Screwy Squirrel in full Napoleonic regalia]
189       *
190       * @return the value as a proper double (hey, it <B>could</B>
191       *         happen)
192       */
193  
194      public double getRKNumber()
195      {
196          return RKUtil.decodeNumber(field_4_rk_number);
197      }
198  
199  
200      public String toString()
201      {
202          StringBuffer buffer = new StringBuffer();
203  
204          buffer.append("[RK]\n");
205          buffer.append("    .row            = ")
206              .append(Integer.toHexString(getRow())).append("\n");
207          buffer.append("    .col            = ")
208              .append(Integer.toHexString(getColumn())).append("\n");
209          buffer.append("    .xfindex        = ")
210              .append(Integer.toHexString(getXFIndex())).append("\n");
211          buffer.append("    .rknumber       = ")
212              .append(Integer.toHexString(getRKField())).append("\n");
213          buffer.append("        .rktype     = ")
214              .append(Integer.toHexString(getRKType())).append("\n");
215          buffer.append("        .rknumber   = ").append(getRKNumber())
216              .append("\n");
217          buffer.append("[/RK]\n");
218          return buffer.toString();
219      }
220  
221  //temporarily just constructs a new number record and returns its value
222      public int serialize(int offset, byte [] data)
223      {
224          NumberRecord rec = new NumberRecord();
225  
226          rec.setColumn(getColumn());
227          rec.setRow(getRow());
228          rec.setValue(getRKNumber());
229          rec.setXFIndex(getXFIndex());
230          return rec.serialize(offset, data);
231      }
232  
233      /**
234       * Debugging main()
235       * <P>
236       * Normally I'd do this in a junit test, but let's face it -- once
237       * this algorithm has been tested and it works, we are never ever
238       * going to change it. This is driven by the Faceless Enemy's
239       * minions, who dare not change the algorithm out from under us.
240       *
241       * @param ignored_args command line arguments, which we blithely
242       *                     ignore
243       */
244  
245      public static void main(String ignored_args[])
246      {
247          int[]    values  =
248          {
249              0x3FF00000, 0x405EC001, 0x02F1853A, 0x02F1853B, 0xFCDD699A
250          };
251          double[] rvalues =
252          {
253              1, 1.23, 12345678, 123456.78, -13149594
254          };
255  
256          for (int j = 0; j < values.length; j++)
257          {
258              System.out.println("input = " + Integer.toHexString(values[ j ])
259                                 + " -> " + rvalues[ j ] + ": "
260                                 + RKUtil.decodeNumber(values[ j ]));
261          }
262      }
263  
264      public short getSid()
265      {
266          return this.sid;
267      }
268  
269      public boolean isBefore(CellValueRecordInterface i)
270      {
271          if (this.getRow() > i.getRow())
272          {
273              return false;
274          }
275          if ((this.getRow() == i.getRow())
276                  && (this.getColumn() > i.getColumn()))
277          {
278              return false;
279          }
280          if ((this.getRow() == i.getRow())
281                  && (this.getColumn() == i.getColumn()))
282          {
283              return false;
284          }
285          return true;
286      }
287  
288      public boolean isAfter(CellValueRecordInterface i)
289      {
290          if (this.getRow() < i.getRow())
291          {
292              return false;
293          }
294          if ((this.getRow() == i.getRow())
295                  && (this.getColumn() < i.getColumn()))
296          {
297              return false;
298          }
299          if ((this.getRow() == i.getRow())
300                  && (this.getColumn() == i.getColumn()))
301          {
302              return false;
303          }
304          return true;
305      }
306  
307      public boolean isEqual(CellValueRecordInterface i)
308      {
309          return ((this.getRow() == i.getRow())
310                  && (this.getColumn() == i.getColumn()));
311      }
312  
313      public boolean isInValueSection()
314      {
315          return true;
316      }
317  
318      public boolean isValue()
319      {
320          return true;
321      }
322  
323      public void setColumn(short col)
324      {
325      }
326  
327      public void setRow(short row)
328      {
329      }
330  
331      /**
332       * NO OP!
333       */
334  
335      public void setXFIndex(short xf)
336      {
337      }
338  }
339