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