1    /* ====================================================================
2     * The Apache Software License, Version 1.1
3     *
4     * Copyright (c) 2002 The Apache Software Foundation.  All rights
5     * reserved.
6     *
7     * Redistribution and use in source and binary forms, with or without
8     * modification, are permitted provided that the following conditions
9     * are met:
10    *
11    * 1. Redistributions of source code must retain the above copyright
12    *    notice, this list of conditions and the following disclaimer.
13    *
14    * 2. Redistributions in binary form must reproduce the above copyright
15    *    notice, this list of conditions and the following disclaimer in
16    *    the documentation and/or other materials provided with the
17    *    distribution.
18    *
19    * 3. The end-user documentation included with the redistribution,
20    *    if any, must include the following acknowledgment:
21    *       "This product includes software developed by the
22    *        Apache Software Foundation (http://www.apache.org/)."
23    *    Alternately, this acknowledgment may appear in the software itself,
24    *    if and wherever such third-party acknowledgments normally appear.
25    *
26    * 4. The names "Apache" and "Apache Software Foundation" and
27    *    "Apache POI" must not be used to endorse or promote products
28    *    derived from this software without prior written permission. For
29    *    written permission, please contact apache@apache.org.
30    *
31    * 5. Products derived from this software may not be called "Apache",
32    *    "Apache POI", nor may "Apache" appear in their name, without
33    *    prior written permission of the Apache Software Foundation.
34    *
35    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46    * SUCH DAMAGE.
47    * ====================================================================
48    *
49    * This software consists of voluntary contributions made by many
50    * individuals on behalf of the Apache Software Foundation.  For more
51    * information on the Apache Software Foundation, please see
52    * <http://www.apache.org/>.
53    */
54   
55   package org.apache.poi.hssf.record;
56   
57   import org.apache.poi.util.LittleEndian;
58   
59   /**
60    * Title: Recalc Id Record<P>
61    * Description:  This record contains an ID that marks when a worksheet was last
62    *               recalculated. It's an optimization Excel uses to determine if it
63    *               needs to  recalculate the spreadsheet when it's opened. So far, only
64    *               the two values <code>0xC1 0x01 0x00 0x00 0x80 0x38 0x01 0x00</code>
65    *               (do not recalculate) and <code>0xC1 0x01 0x00 0x00 0x60 0x69 0x01
66    *               0x00</code> have been seen. If the field <code>isNeeded</code> is
67    *               set to false (default), then this record is swallowed during the
68    *               serialization process<P>
69    * REFERENCE:  http://chicago.sourceforge.net/devel/docs/excel/biff8.html<P>
70    * @author Luc Girardin (luc dot girardin at macrofocus dot com)
71    * @version 2.0-pre
72    * @see org.apache.poi.hssf.model.Workbook
73    */
74   
75   public class RecalcIdRecord
76       extends Record
77   {
78       public final static short sid = 0x1c1;
79       public short[]            field_1_recalcids;
80   
81       private boolean isNeeded = false;
82   
83       public RecalcIdRecord()
84       {
85       }
86   
87       /**
88        * Constructs a RECALCID record and sets its fields appropriately.
89        *
90        * @param id     id must be 0x13d or an exception will be throw upon validation
91        * @param size  the size of the data area of the record
92        * @param data  data of the record (should not contain sid/len)
93        */
94   
95       public RecalcIdRecord(short id, short size, byte [] data)
96       {
97           super(id, size, data);
98       }
99   
100      /**
101       * Constructs a RECALCID record and sets its fields appropriately.
102       *
103       * @param id     id must be 0x13d or an exception will be throw upon validation
104       * @param size  the size of the data area of the record
105       * @param data  data of the record (should not contain sid/len)
106       * @param offset of the record
107       */
108  
109      public RecalcIdRecord(short id, short size, byte [] data, int offset)
110      {
111          super(id, size, data, offset);
112      }
113  
114      protected void validateSid(short id)
115      {
116          if (id != sid)
117          {
118              throw new RecordFormatException("NOT A RECALCID RECORD");
119          }
120      }
121  
122      protected void fillFields(byte [] data, short size, int offset)
123      {
124          field_1_recalcids = new short[ size / 2 ];
125          for (int k = 0; k < field_1_recalcids.length; k++)
126          {
127              field_1_recalcids[ k ] = LittleEndian.getShort(data,
128                                                          (k * 2) + offset);
129          }
130      }
131  
132      /**
133       * set the recalc array.
134       * @param array of recalc id's
135       */
136  
137      public void setRecalcIdArray(short [] array)
138      {
139          field_1_recalcids = array;
140      }
141  
142      /**
143       * get the recalc array.
144       * @return array of recalc id's
145       */
146  
147      public short [] getRecalcIdArray()
148      {
149          return field_1_recalcids;
150      }
151  
152      public void setIsNeeded(boolean isNeeded) {
153          this.isNeeded = isNeeded;
154      }
155  
156      public boolean isNeeded() {
157          return isNeeded;
158      }
159  
160      public String toString()
161      {
162          StringBuffer buffer = new StringBuffer();
163  
164          buffer.append("[RECALCID]\n");
165          buffer.append("    .elements        = ").append(field_1_recalcids.length)
166              .append("\n");
167          for (int k = 0; k < field_1_recalcids.length; k++)
168          {
169              buffer.append("    .element_" + k + "       = ")
170                  .append(field_1_recalcids[ k ]).append("\n");
171          }
172          buffer.append("[/RECALCID]\n");
173          return buffer.toString();
174      }
175  
176      public int serialize(int offset, byte [] data)
177      {
178          short[] tabids     = getRecalcIdArray();
179          short   length     = ( short ) (tabids.length * 2);
180          int     byteoffset = 4;
181  
182          LittleEndian.putShort(data, 0 + offset, sid);
183          LittleEndian.putShort(data, 2 + offset,
184                                (( short ) length));
185  
186          // 2 (num bytes in a short)
187          for (int k = 0; k < (length / 2); k++)
188          {
189              LittleEndian.putShort(data, byteoffset + offset, tabids[ k ]);
190              byteoffset += 2;
191          }
192          return getRecordSize();
193      }
194  
195      public int getRecordSize()
196      {
197          return 4 + (getRecalcIdArray().length * 2);
198      }
199  
200      public short getSid()
201      {
202          return this.sid;
203      }
204  }
205