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       * Indicates whether the call holds an error value
240       *
241       * @return boolean true if the cell holds an error value
242       */
243  
244      public boolean isError()
245      {
246          return (field_5_fError != ( byte ) 0);
247      }
248  
249      public String toString()
250      {
251          StringBuffer buffer = new StringBuffer();
252  
253          buffer.append("[BOOLERR]\n");
254          buffer.append("    .row            = ")
255              .append(Integer.toHexString(getRow())).append("\n");
256          buffer.append("    .col            = ")
257              .append(Integer.toHexString(getColumn())).append("\n");
258          buffer.append("    .xfindex        = ")
259              .append(Integer.toHexString(getXFIndex())).append("\n");
260          if (isBoolean())
261          {
262              buffer.append("    .booleanValue   = ").append(getBooleanValue())
263                  .append("\n");
264          }
265          else
266          {
267              buffer.append("    .errorValue     = ").append(getErrorValue())
268                  .append("\n");
269          }
270          buffer.append("[/BOOLERR]\n");
271          return buffer.toString();
272      }
273  
274      /**
275       * called by the class that is responsible for writing this sucker.
276       * Subclasses should implement this so that their data is passed back in a
277       * byte array.
278       *
279       * @return byte array containing instance data
280       */
281  
282      public int serialize(int offset, byte [] data)
283      {
284          LittleEndian.putShort(data, 0 + offset, sid);
285          LittleEndian.putShort(data, 2 + offset, ( short ) 8);
286          //LittleEndian.putShort(data, 4 + offset, getRow());
287          LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
288          LittleEndian.putShort(data, 6 + offset, getColumn());
289          LittleEndian.putShort(data, 8 + offset, getXFIndex());
290          data[ 10 + offset ] = field_4_bBoolErr;
291          data[ 11 + offset ] = field_5_fError;
292          return getRecordSize();
293      }
294  
295      public int getRecordSize()
296      {
297          return 12;
298      }
299  
300      /**
301       * called by constructor, should throw runtime exception in the event of a
302       * record passed with a differing ID.
303       *
304       * @param id alleged id for this record
305       */
306  
307      protected void validateSid(short id)
308      {
309          if (id != this.sid)
310          {
311              throw new RecordFormatException("Not a valid BoolErrRecord");
312          }
313      }
314  
315      public short getSid()
316      {
317          return this.sid;
318      }
319  
320      public boolean isBefore(CellValueRecordInterface i)
321      {
322          if (this.getRow() > i.getRow())
323          {
324              return false;
325          }
326          if ((this.getRow() == i.getRow())
327                  && (this.getColumn() > i.getColumn()))
328          {
329              return false;
330          }
331          if ((this.getRow() == i.getRow())
332                  && (this.getColumn() == i.getColumn()))
333          {
334              return false;
335          }
336          return true;
337      }
338  
339      public boolean isAfter(CellValueRecordInterface i)
340      {
341          if (this.getRow() < i.getRow())
342          {
343              return false;
344          }
345          if ((this.getRow() == i.getRow())
346                  && (this.getColumn() < i.getColumn()))
347          {
348              return false;
349          }
350          if ((this.getRow() == i.getRow())
351                  && (this.getColumn() == i.getColumn()))
352          {
353              return false;
354          }
355          return true;
356      }
357  
358      public boolean isEqual(CellValueRecordInterface i)
359      {
360          return ((this.getRow() == i.getRow())
361                  && (this.getColumn() == i.getColumn()));
362      }
363  
364      public boolean isInValueSection()
365      {
366          return true;
367      }
368  
369      public boolean isValue()
370      {
371          return true;
372      }
373  
374      public int compareTo(Object obj)
375      {
376          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
377  
378          if ((this.getRow() == loc.getRow())
379                  && (this.getColumn() == loc.getColumn()))
380          {
381              return 0;
382          }
383          if (this.getRow() < loc.getRow())
384          {
385              return -1;
386          }
387          if (this.getRow() > loc.getRow())
388          {
389              return 1;
390          }
391          if (this.getColumn() < loc.getColumn())
392          {
393              return -1;
394          }
395          if (this.getColumn() > loc.getColumn())
396          {
397              return 1;
398          }
399          return -1;
400      }
401  
402      public boolean equals(Object obj)
403      {
404          if (!(obj instanceof CellValueRecordInterface))
405          {
406              return false;
407          }
408          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
409  
410          if ((this.getRow() == loc.getRow())
411                  && (this.getColumn() == loc.getColumn()))
412          {
413              return true;
414          }
415          return false;
416      }
417  
418      public Object clone() {
419        BoolErrRecord rec = new BoolErrRecord();
420        rec.field_1_row = field_1_row;
421        rec.field_2_column = field_2_column;
422        rec.field_3_xf_index = field_3_xf_index;
423        rec.field_4_bBoolErr = field_4_bBoolErr;
424        rec.field_5_fError = field_5_fError;
425        return rec;
426      }
427  }
428