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