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    * BoolErrRecord.java
58    *
59    * Created on January 19, 2002, 9:30 AM
60    */
61   package org.apache.poi.hssf.record;
62   
63   import org.apache.poi.util.LittleEndian;
64   
65   /**
66    * Creates new BoolErrRecord. <P>
67    * REFERENCE:  PG ??? Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
68    * @author Michael P. Harhen
69    * @version 2.0-pre
70    */
71   
72   public class BoolErrRecord
73       extends Record
74       implements CellValueRecordInterface, Comparable
75   {
76       public final static short sid = 0x205;
77       //private short             field_1_row;
78       private int             field_1_row;
79       private short             field_2_column;
80       private short             field_3_xf_index;
81       private byte              field_4_bBoolErr;
82       private byte              field_5_fError;
83   
84       /** Creates new BoolErrRecord */
85   
86       public BoolErrRecord()
87       {
88       }
89   
90       /**
91        * Constructs a BoolErr record and sets its fields appropriately.
92        *
93        * @param id     id must be 0x205 or an exception will be throw upon validation
94        * @param size  the size of the data area of the record
95        * @param data  data of the record (should not contain sid/len)
96        */
97   
98       public BoolErrRecord(short id, short size, byte [] data)
99       {
100          super(id, size, data);
101      }
102  
103      /**
104       * Constructs a BoolErr record and sets its fields appropriately.
105       *
106       * @param id     id must be 0x205 or an exception will be throw upon validation
107       * @param size  the size of the data area of the record
108       * @param data  data of the record (should not contain sid/len)
109       * @param offset of the record
110       */
111  
112      public BoolErrRecord(short id, short size, byte [] data, int offset)
113      {
114          super(id, size, data, offset);
115      }
116  
117      /**
118       * called by the constructor, should set class level fields.  Should throw
119       * runtime exception for bad/icomplete data.
120       *
121       * @param data raw data
122       * @param size size of data
123       */
124  
125      protected void fillFields(byte [] data, short size, int offset)
126      {
127          //field_1_row      = LittleEndian.getShort(data, 0 + offset);
128          field_1_row      = LittleEndian.getUShort(data, 0 + offset);
129          field_2_column   = LittleEndian.getShort(data, 2 + offset);
130          field_3_xf_index = LittleEndian.getShort(data, 4 + offset);
131          field_4_bBoolErr = data[ 6 + offset ];
132          field_5_fError   = data[ 7 + offset ];
133      }
134  
135      //public void setRow(short row)
136      public void setRow(int row)
137      {
138          field_1_row = row;
139      }
140  
141      public void setColumn(short col)
142      {
143          field_2_column = col;
144      }
145  
146      /**
147       * set the index to the ExtendedFormat
148       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
149       * @param xf    index to the XF record
150       */
151  
152      public void setXFIndex(short xf)
153      {
154          field_3_xf_index = xf;
155      }
156  
157      /**
158       * set the boolean value for the cell
159       *
160       * @param value   representing the boolean value
161       */
162  
163      public void setValue(boolean value)
164      {
165          field_4_bBoolErr = value ? ( byte ) 1
166                                   : ( byte ) 0;
167          field_5_fError   = ( byte ) 0;
168      }
169  
170      /**
171       * set the error value for the cell
172       *
173       * @param value     error representing the error value
174       */
175  
176      public void setValue(byte value)
177      {
178          field_4_bBoolErr = value;
179          field_5_fError   = ( byte ) 1;
180      }
181  
182      //public short getRow()
183      public int getRow()
184      {
185          return field_1_row;
186      }
187  
188      public short getColumn()
189      {
190          return field_2_column;
191      }
192  
193      /**
194       * get the index to the ExtendedFormat
195       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
196       * @return index to the XF record
197       */
198  
199      public short getXFIndex()
200      {
201          return field_3_xf_index;
202      }
203  
204      /**
205       * get the value for the cell
206       *
207       * @return boolean representing the boolean value
208       */
209  
210      public boolean getBooleanValue()
211      {
212          return (field_4_bBoolErr != 0);
213      }
214  
215      /**
216       * get the error value for the cell
217       *
218       * @return byte representing the error value
219       */
220  
221      public byte getErrorValue()
222      {
223          return field_4_bBoolErr;
224      }
225  
226      /**
227       * Indicates whether the call holds a boolean value
228       *
229       * @return boolean true if the cell holds a boolean value
230       */
231  
232      public boolean isBoolean()
233      {
234          return (field_5_fError == ( byte ) 0);
235      }
236  
237      /**
238       * Indicates whether the call holds an error value
239       *
240       * @return boolean true if the cell holds an error value
241       */
242  
243      public boolean isError()
244      {
245          return (field_5_fError != ( byte ) 0);
246      }
247  
248      public String toString()
249      {
250          StringBuffer buffer = new StringBuffer();
251  
252          buffer.append("[BOOLERR]\n");
253          buffer.append("    .row            = ")
254              .append(Integer.toHexString(getRow())).append("\n");
255          buffer.append("    .col            = ")
256              .append(Integer.toHexString(getColumn())).append("\n");
257          buffer.append("    .xfindex        = ")
258              .append(Integer.toHexString(getXFIndex())).append("\n");
259          if (isBoolean())
260          {
261              buffer.append("    .booleanValue   = ").append(getBooleanValue())
262                  .append("\n");
263          }
264          else
265          {
266              buffer.append("    .errorValue     = ").append(getErrorValue())
267                  .append("\n");
268          }
269          buffer.append("[/BOOLERR]\n");
270          return buffer.toString();
271      }
272  
273      /**
274       * called by the class that is responsible for writing this sucker.
275       * Subclasses should implement this so that their data is passed back in a
276       * byte array.
277       *
278       * @return byte array containing instance data
279       */
280  
281      public int serialize(int offset, byte [] data)
282      {
283          LittleEndian.putShort(data, 0 + offset, sid);
284          LittleEndian.putShort(data, 2 + offset, ( short ) 8);
285          //LittleEndian.putShort(data, 4 + offset, getRow());
286          LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
287          LittleEndian.putShort(data, 6 + offset, getColumn());
288          LittleEndian.putShort(data, 8 + offset, getXFIndex());
289          data[ 10 + offset ] = field_4_bBoolErr;
290          data[ 11 + offset ] = field_5_fError;
291          return getRecordSize();
292      }
293  
294      public int getRecordSize()
295      {
296          return 12;
297      }
298  
299      /**
300       * called by constructor, should throw runtime exception in the event of a
301       * record passed with a differing ID.
302       *
303       * @param id alleged id for this record
304       */
305  
306      protected void validateSid(short id)
307      {
308          if (id != this.sid)
309          {
310              throw new RecordFormatException("Not a valid BoolErrRecord");
311          }
312      }
313  
314      public short getSid()
315      {
316          return this.sid;
317      }
318  
319      public boolean isBefore(CellValueRecordInterface i)
320      {
321          if (this.getRow() > i.getRow())
322          {
323              return false;
324          }
325          if ((this.getRow() == i.getRow())
326                  && (this.getColumn() > i.getColumn()))
327          {
328              return false;
329          }
330          if ((this.getRow() == i.getRow())
331                  && (this.getColumn() == i.getColumn()))
332          {
333              return false;
334          }
335          return true;
336      }
337  
338      public boolean isAfter(CellValueRecordInterface i)
339      {
340          if (this.getRow() < i.getRow())
341          {
342              return false;
343          }
344          if ((this.getRow() == i.getRow())
345                  && (this.getColumn() < i.getColumn()))
346          {
347              return false;
348          }
349          if ((this.getRow() == i.getRow())
350                  && (this.getColumn() == i.getColumn()))
351          {
352              return false;
353          }
354          return true;
355      }
356  
357      public boolean isEqual(CellValueRecordInterface i)
358      {
359          return ((this.getRow() == i.getRow())
360                  && (this.getColumn() == i.getColumn()));
361      }
362  
363      public boolean isInValueSection()
364      {
365          return true;
366      }
367  
368      public boolean isValue()
369      {
370          return true;
371      }
372  
373      public int compareTo(Object obj)
374      {
375          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
376  
377          if ((this.getRow() == loc.getRow())
378                  && (this.getColumn() == loc.getColumn()))
379          {
380              return 0;
381          }
382          if (this.getRow() < loc.getRow())
383          {
384              return -1;
385          }
386          if (this.getRow() > loc.getRow())
387          {
388              return 1;
389          }
390          if (this.getColumn() < loc.getColumn())
391          {
392              return -1;
393          }
394          if (this.getColumn() > loc.getColumn())
395          {
396              return 1;
397          }
398          return -1;
399      }
400  
401      public boolean equals(Object obj)
402      {
403          if (!(obj instanceof CellValueRecordInterface))
404          {
405              return false;
406          }
407          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
408  
409          if ((this.getRow() == loc.getRow())
410                  && (this.getColumn() == loc.getColumn()))
411          {
412              return true;
413          }
414          return false;
415      }
416  }
417