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:        Sup Book  <P>
61    * Description:  A Extrenal Workbook Deciption (Sup Book)
62    *               Its only a dummy record for making new ExternSheet Record <P>
63    * REFERENCE:  <P>
64    * @author Libin Roman (Vista Portal LDT. Developer)
65    * @author Andrew C. Oliver (acoliver@apache.org)
66    *
67    */
68   public class SupBookRecord extends Record
69   {
70       public final static short sid = 0x1AE;
71       private short             field_1_number_of_sheets;
72       private short             field_2_flag;
73   
74   
75       public SupBookRecord()
76       {
77           setFlag((short)0x401);
78       }
79   
80       /**
81        * Constructs a Extern Sheet record and sets its fields appropriately.
82        *
83        * @param id     id must be 0x16 or an exception will be throw upon validation
84        * @param size  the size of the data area of the record
85        * @param data  data of the record (should not contain sid/len)
86        */
87       public SupBookRecord(short id, short size, byte[] data)
88       {
89           super(id, size, data);
90       }
91   
92       /**
93        * Constructs a Extern Sheet record and sets its fields appropriately.
94        *
95        * @param id     id must be 0x1ae or an exception will be throw upon validation
96        * @param size  the size of the data area of the record
97        * @param data  data of the record (should not contain sid/len)
98        * @param offset of the record's data
99        */
100      public SupBookRecord(short id, short size, byte[] data, int offset)
101      {
102          super(id, size, data, offset);
103      }
104  
105      protected void validateSid(short id)
106      {
107          if (id != sid)
108          {
109              throw new RecordFormatException("NOT An Supbook RECORD");
110          }
111      }
112  
113      /**
114       * called by the constructor, should set class level fields.  Should throw
115       * runtime exception for bad/icomplete data.
116       *
117       * @param data raw data
118       * @param size size of data
119       * @param offset of the record's data (provided a big array of the file)
120       */
121      protected void fillFields(byte [] data, short size, int offset)
122      {
123          //For now We use it only for one case
124          //When we need to add an named range when no named ranges was
125          //before it
126          field_1_number_of_sheets = LittleEndian.getShort(data,offset+0);
127          field_2_flag = LittleEndian.getShort(data,offset+2);
128      }
129  
130  
131      public String toString()
132      {
133          StringBuffer buffer = new StringBuffer();
134          buffer.append("[SUPBOOK]\n");
135          buffer.append("numberosheets = ").append(getNumberOfSheets()).append('\n');
136          buffer.append("flag          = ").append(getFlag()).append('\n');
137          buffer.append("[/SUPBOOK]\n");
138          return buffer.toString();
139      }
140  
141      /**
142       * called by the class that is responsible for writing this sucker.
143       * Subclasses should implement this so that their data is passed back in a
144       * byte array.
145       *
146       * @param offset to begin writing at
147       * @param data byte array containing instance data
148       * @return number of bytes written
149       */
150      public int serialize(int offset, byte [] data)
151      {
152          LittleEndian.putShort(data, 0 + offset, sid);
153          LittleEndian.putShort(data, 2 + offset, (short) 4);
154          LittleEndian.putShort(data, 4 + offset, field_1_number_of_sheets);
155          LittleEndian.putShort(data, 6 + offset, field_2_flag);
156  
157          return getRecordSize();
158      }
159  
160      public void setNumberOfSheets(short number){
161          field_1_number_of_sheets = number;
162      }
163  
164      public short getNumberOfSheets(){
165          return field_1_number_of_sheets;
166      }
167  
168      public void setFlag(short flag){
169          field_2_flag = flag;
170      }
171  
172      public short getFlag() {
173          return field_2_flag;
174      }
175  
176      public int getRecordSize()
177      {
178          return 4 + 4;
179      }
180  
181      public short getSid()
182      {
183          return this.sid;
184      }
185  }
186