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