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 package org.apache.poi.hssf.record; 57 58 /** 59 * Title: Record 60 * Description: All HSSF Records inherit from this class. It 61 * populates the fields common to all records (id, size and data). 62 * Subclasses should be sure to validate the id, 63 * Company: 64 * @author Andrew C. Oliver 65 * @author Marc Johnson (mjohnson at apache dot org) 66 * @author Jason Height (jheight at chariot dot net dot au) 67 * @version 2.0-pre 68 */ 69 70 public abstract class Record 71 { 72 73 /** 74 * The static ID, subclasses should override this value with the id for the 75 * record type they handle. 76 */ 77 78 public short sid = 0; 79 private short id = 0; 80 private short size = 0; 81 private byte[] data = null; 82 83 /** 84 * instantiates a blank record strictly for ID matching 85 */ 86 87 public Record() 88 { 89 } 90 91 /** 92 * Constructor Record 93 * 94 * @param id record id 95 * @param size record size 96 * @param data raw data 97 */ 98 99 public Record(short id, short size, byte [] data) 100 { 101 this.id = id; 102 this.size = size; 103 this.data = data; 104 validateSid(id); 105 fillFields(data, size); 106 } 107 108 /** 109 * Constructor Record 110 * 111 * @param id record id 112 * @param size record size 113 * @param data raw data 114 */ 115 116 public Record(short id, short size, byte [] data, int offset) 117 { 118 this.id = id; 119 this.size = size; 120 this.data = data; 121 validateSid(id); 122 fillFields(data, size, 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 abstract void validateSid(short id); 133 134 /** 135 * called by the constructor, should set class level fields. Should throw 136 * runtime exception for bad/icomplete data. 137 * 138 * @param data raw data 139 */ 140 141 protected void fillFields(byte [] data, short size) 142 { 143 fillFields(data, size, 0); 144 } 145 146 /** 147 * called by the constructor, should set class level fields. Should throw 148 * runtime exception for bad/icomplete data. 149 * 150 * @param data raw data 151 * @param size size of data 152 * @param offset of the record's data (provided a big array of the file) 153 */ 154 155 protected abstract void fillFields(byte [] data, short size, int offset); 156 157 /** 158 * called by the class that is responsible for writing this sucker. 159 * Subclasses should implement this so that their data is passed back in a 160 * byte array. 161 * 162 * @return byte array containing instance data 163 */ 164 165 public byte [] serialize() 166 { 167 byte[] retval = new byte[ getRecordSize() ]; 168 169 serialize(0, retval); 170 return retval; 171 } 172 173 /** 174 * called by the class that is responsible for writing this sucker. 175 * Subclasses should implement this so that their data is passed back in a 176 * byte array. 177 * 178 * @param offset to begin writing at 179 * @param data byte array containing instance data 180 * @return number of bytes written 181 */ 182 183 public abstract int serialize(int offset, byte [] data); 184 185 /** 186 * gives the current serialized size of the record. Should include the sid and reclength (4 bytes). 187 */ 188 189 public int getRecordSize() 190 { 191 192 // this is kind od a stupid way to do it but for now we just serialize 193 // the record and return the size of the byte array 194 return serialize().length; 195 } 196 197 /** 198 * tells whether this type of record contains a value 199 */ 200 201 public boolean isValue() 202 { 203 return false; 204 } 205 206 /** 207 * DBCELL, ROW, VALUES all say yes 208 */ 209 210 public boolean isInValueSection() 211 { 212 return false; 213 } 214 215 /** 216 * get a string representation of the record (for biffview/debugging) 217 */ 218 219 public String toString() 220 { 221 return super.toString(); 222 } 223 224 /** 225 * Process a continuation record; default handling is to ignore 226 * it -- TODO add logging 227 * 228 * @param record the continuation record's data 229 */ 230 231 // made public to satisfy biffviewer 232 233 /* protected */ 234 public void processContinueRecord(byte [] record) 235 { 236 237 // System.out.println("Got a continue record ... NOW what??"); 238 } 239 240 /** 241 * return the non static version of the id for this record. 242 */ 243 244 public abstract short getSid(); 245 246 public Object clone() { 247 throw new RuntimeException("The class "+getClass().getName()+" needs to define a clone method"); 248 } 249 } 250