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