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   /*
57    * MulBlankRecord.java
58    *
59    * Created on December 10, 2001, 12:49 PM
60    */
61   package org.apache.poi.hssf.record;
62   
63   import org.apache.poi.util.LittleEndian;
64   
65   /**
66    * Title:        Mulitple Blank cell record <P>
67    * Description:  Represents a  set of columns in a row with no value but with styling.
68    *               In this release we have read-only support for this record type.
69    *               The RecordFactory converts this to a set of BlankRecord objects.<P>
70    * REFERENCE:  PG 329 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
71    * @author Andrew C. Oliver (acoliver at apache dot org)
72    * @version 2.0-pre
73    * @see org.apache.poi.hssf.record.RecordFactory
74    * @see org.apache.poi.hssf.record.BlankRecord
75    */
76   
77   public class MulBlankRecord
78       extends Record
79   {
80       public final static short sid = 0xbe;
81       private short             field_1_row;
82       private short             field_2_first_col;
83       private short[]           field_3_xfs;
84       private short             field_4_last_col;
85   
86       /** Creates new MulBlankRecord */
87   
88       public MulBlankRecord()
89       {
90       }
91   
92       /**
93        * Constructs a MulBlank record and sets its fields appropriately.
94        *
95        * @param id     id must be 0xbe 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        */
99   
100      public MulBlankRecord(short id, short size, byte [] data)
101      {
102          super(id, size, data);
103      }
104  
105      /**
106       * Constructs a MulBlank record and sets its fields appropriately.
107       *
108       * @param id     id must be 0xbe or an exception will be throw upon validation
109       * @param size  the size of the data area of the record
110       * @param data  data of the record (should not contain sid/len)
111       * @param offset of the record's data
112       */
113  
114      public MulBlankRecord(short id, short size, byte [] data, int offset)
115      {
116          super(id, size, data, offset);
117      }
118  
119      /**
120       * get the row number of the cells this represents
121       *
122       * @return row number
123       */
124  
125      public short getRow()
126      {
127          return field_1_row;
128      }
129  
130      /**
131       * starting column (first cell this holds in the row)
132       * @return first column number
133       */
134  
135      public short getFirstColumn()
136      {
137          return field_2_first_col;
138      }
139  
140      /**
141       * ending column (last cell this holds in the row)
142       * @return first column number
143       */
144  
145      public short getLastColumn()
146      {
147          return field_4_last_col;
148      }
149  
150      /**
151       * get the number of columns this contains (last-first +1)
152       * @return number of columns (last - first +1)
153       */
154  
155      public int getNumColumns()
156      {
157          return field_4_last_col - field_2_first_col + 1;
158      }
159  
160      /**
161       * returns the xf index for column (coffset = column - field_2_first_col)
162       * @param the column (coffset = column - field_2_first_col)
163       * @return the XF index for the column
164       */
165  
166      public short getXFAt(int coffset)
167      {
168          return field_3_xfs[ coffset ];
169      }
170  
171      /**
172       * called by the constructor, should set class level fields.  Should throw
173       * runtime exception for bad/icomplete data.
174       *
175       * @param data raw data
176       * @param size size of data
177       */
178  
179      protected void fillFields(byte [] data, short size, int offset)
180      {
181          field_1_row       = LittleEndian.getShort(data, 0 + offset);
182          field_2_first_col = LittleEndian.getShort(data, 2 + offset);
183          field_3_xfs       = parseXFs(data, 4, offset, size);
184          field_4_last_col  = LittleEndian.getShort(data,
185                                                    (field_3_xfs.length * 2)
186                                                    + 4 + offset);
187      }
188  
189      private short [] parseXFs(byte [] data, int offset, int recoffset,
190                                short size)
191      {
192          short[] retval = new short[ ((size - offset) - 2) / 2 ];
193          int     idx    = 0;
194  
195          for (; offset < size - 2; )
196          {
197              short xf = 0;
198  
199              xf            = LittleEndian.getShort(data, offset + recoffset);
200              offset        += 2;
201              retval[ idx ] = xf;
202              idx++;
203          }
204          return retval;
205      }
206  
207      public String toString()
208      {
209          StringBuffer buffer = new StringBuffer();
210  
211          buffer.append("[MULBLANK]\n");
212          buffer.append("firstcol  = ")
213              .append(Integer.toHexString(getFirstColumn())).append("\n");
214          buffer.append(" lastcol  = ")
215              .append(Integer.toHexString(getLastColumn())).append("\n");
216          for (int k = 0; k < getNumColumns(); k++)
217          {
218              buffer.append("xf").append(k).append("        = ")
219                  .append(Integer.toHexString(getXFAt(k))).append("\n");
220          }
221          buffer.append("[/MULBLANK]\n");
222          return buffer.toString();
223      }
224  
225      /**
226       * called by constructor, should throw runtime exception in the event of a
227       * record passed with a differing ID.
228       *
229       * @param id alleged id for this record
230       */
231  
232      protected void validateSid(short id)
233      {
234          if (id != sid)
235          {
236              throw new RecordFormatException("Not a MulBlankRecord!");
237          }
238      }
239  
240      public short getSid()
241      {
242          return this.sid;
243      }
244  
245      public int serialize(int offset, byte [] data)
246      {
247          throw new RecordFormatException(
248              "Sorry, you can't serialize a MulBlank in this release");
249      }
250  }
251