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    * BlankRecord.java
58    *
59    * Created on December 10, 2001, 12:07 PM
60    */
61   package org.apache.poi.hssf.record;
62   
63   import org.apache.poi.util.LittleEndian;
64   
65   /**
66    * Title:        Blank cell record <P>
67    * Description:  Represents a column in a row with no value but with styling.<P>
68    * REFERENCE:  PG 287 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
69    * @author Andrew C. Oliver (acoliver at apache dot org)
70    * @version 2.0-pre
71    */
72   
73   public class BlankRecord
74       extends Record
75       implements CellValueRecordInterface, Comparable
76   {
77       public final static short sid = 0x201;
78       private short             field_1_row;
79       private short             field_2_col;
80       private short             field_3_xf;
81   
82       /** Creates a new instance of BlankRecord */
83   
84       public BlankRecord()
85       {
86       }
87   
88       /**
89        * Constructs a BlankRecord and sets its fields appropriately
90        *
91        * @param id     id must be 0x201 or an exception will be throw upon validation
92        * @param size  the size of the data area of the record
93        * @param data  data of the record (should not contain sid/len)
94        */
95   
96       public BlankRecord(short id, short size, byte [] data)
97       {
98           super(id, size, data);
99       }
100  
101      /**
102       * Constructs a BlankRecord and sets its fields appropriately
103       *
104       * @param id     id must be 0x201 or an exception will be throw upon validation
105       * @param size  the size of the data area of the record
106       * @param data  data of the record (should not contain sid/len)
107       * @param offset of the record's data
108       */
109  
110      public BlankRecord(short id, short size, byte [] data, int offset)
111      {
112          super(id, size, data, offset);
113      }
114  
115      protected void fillFields(byte [] data, short size, int offset)
116      {
117          field_1_row = LittleEndian.getShort(data, 0 + offset);
118          field_2_col = LittleEndian.getShort(data, 2 + offset);
119          field_3_xf  = LittleEndian.getShort(data, 4 + offset);
120      }
121  
122      /**
123       * called by constructor, should throw runtime exception in the event of a
124       * record passed with a differing ID.
125       *
126       * @param id alleged id for this record
127       */
128  
129      protected void validateSid(short id)
130      {
131          if (id != sid)
132          {
133              throw new RecordFormatException("NOT A BLANKRECORD!");
134          }
135      }
136  
137      /**
138       * set the row this cell occurs on
139       * @param row the row this cell occurs within
140       */
141  
142      public void setRow(short row)
143      {
144          field_1_row = row;
145      }
146  
147      /**
148       * get the row this cell occurs on
149       *
150       * @return the row
151       */
152  
153      public short getRow()
154      {
155          return field_1_row;
156      }
157  
158      /**
159       * get the column this cell defines within the row
160       *
161       * @return the column
162       */
163  
164      public short getColumn()
165      {
166          return field_2_col;
167      }
168  
169      /**
170       * set the index of the extended format record to style this cell with
171       *
172       * @param xf - the 0-based index of the extended format
173       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
174       */
175  
176      public void setXFIndex(short xf)
177      {
178          field_3_xf = xf;
179      }
180  
181      /**
182       * get the index of the extended format record to style this cell with
183       *
184       * @return extended format index
185       */
186  
187      public short getXFIndex()
188      {
189          return field_3_xf;
190      }
191  
192      /**
193       * set the column this cell defines within the row
194       *
195       * @param col the column this cell defines
196       */
197  
198      public void setColumn(short col)
199      {
200          field_2_col = col;
201      }
202  
203      public boolean isBefore(CellValueRecordInterface i)
204      {
205          if (this.getRow() > i.getRow())
206          {
207              return false;
208          }
209          if ((this.getRow() == i.getRow())
210                  && (this.getColumn() > i.getColumn()))
211          {
212              return false;
213          }
214          if ((this.getRow() == i.getRow())
215                  && (this.getColumn() == i.getColumn()))
216          {
217              return false;
218          }
219          return true;
220      }
221  
222      public boolean isAfter(CellValueRecordInterface i)
223      {
224          if (this.getRow() < i.getRow())
225          {
226              return false;
227          }
228          if ((this.getRow() == i.getRow())
229                  && (this.getColumn() < i.getColumn()))
230          {
231              return false;
232          }
233          if ((this.getRow() == i.getRow())
234                  && (this.getColumn() == i.getColumn()))
235          {
236              return false;
237          }
238          return true;
239      }
240  
241      public boolean isEqual(CellValueRecordInterface i)
242      {
243          return ((this.getRow() == i.getRow())
244                  && (this.getColumn() == i.getColumn()));
245      }
246  
247      public boolean isInValueSection()
248      {
249          return true;
250      }
251  
252      public boolean isValue()
253      {
254          return true;
255      }
256  
257      /**
258       * return the non static version of the id for this record.
259       */
260  
261      public short getSid()
262      {
263          return BlankRecord.sid;
264      }
265  
266      public String toString()
267      {
268          StringBuffer buffer = new StringBuffer();
269  
270          buffer.append("[BLANK]\n");
271          buffer.append("row       = ").append(Integer.toHexString(getRow()))
272              .append("\n");
273          buffer.append("col       = ").append(Integer.toHexString(getColumn()))
274              .append("\n");
275          buffer.append("xf        = ")
276              .append(Integer.toHexString(getXFIndex())).append("\n");
277          buffer.append("[/BLANK]\n");
278          return buffer.toString();
279      }
280  
281      /**
282       * called by the class that is responsible for writing this sucker.
283       * Subclasses should implement this so that their data is passed back in a
284       * byte array.
285       *
286       * @return byte array containing instance data
287       */
288  
289      public int serialize(int offset, byte [] data)
290      {
291          LittleEndian.putShort(data, 0 + offset, sid);
292          LittleEndian.putShort(data, 2 + offset, ( short ) 6);
293          LittleEndian.putShort(data, 4 + offset, getRow());
294          LittleEndian.putShort(data, 6 + offset, getColumn());
295          LittleEndian.putShort(data, 8 + offset, getXFIndex());
296          return getRecordSize();
297      }
298  
299      public int getRecordSize()
300      {
301          return 10;
302      }
303  
304      public int compareTo(Object obj)
305      {
306          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
307  
308          if ((this.getRow() == loc.getRow())
309                  && (this.getColumn() == loc.getColumn()))
310          {
311              return 0;
312          }
313          if (this.getRow() < loc.getRow())
314          {
315              return -1;
316          }
317          if (this.getRow() > loc.getRow())
318          {
319              return 1;
320          }
321          if (this.getColumn() < loc.getColumn())
322          {
323              return -1;
324          }
325          if (this.getColumn() > loc.getColumn())
326          {
327              return 1;
328          }
329          return -1;
330      }
331  
332      public boolean equals(Object obj)
333      {
334          if (!(obj instanceof CellValueRecordInterface))
335          {
336              return false;
337          }
338          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
339  
340          if ((this.getRow() == loc.getRow())
341                  && (this.getColumn() == loc.getColumn()))
342          {
343              return true;
344          }
345          return false;
346      }
347  }
348