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