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 import org.apache.poi.util.LittleEndian; 59 import org.apache.poi.util.StringUtil; 60 61 /** 62 * Title: Bound Sheet Record (aka BundleSheet) <P> 63 * Description: Defines a sheet within a workbook. Basically stores the sheetname 64 * and tells where the Beginning of file record is within the HSSF 65 * file. <P> 66 * REFERENCE: PG 291 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P> 67 * @author Andrew C. Oliver (acoliver at apache dot org) 68 * @version 2.0-pre 69 */ 70 71 public class BoundSheetRecord 72 extends Record 73 { 74 public final static short sid = 0x85; 75 private int field_1_position_of_BOF; 76 private short field_2_option_flags; 77 private byte field_3_sheetname_length; 78 private byte field_4_compressed_unicode_flag; // not documented 79 private String field_5_sheetname; 80 81 public BoundSheetRecord() 82 { 83 } 84 85 /** 86 * Constructs a BoundSheetRecord and sets its fields appropriately 87 * 88 * @param id id must be 0x85 or an exception will be throw upon validation 89 * @param size the size of the data area of the record 90 * @param data data of the record (should not contain sid/len) 91 */ 92 93 public BoundSheetRecord(short id, short size, byte [] data) 94 { 95 super(id, size, data); 96 } 97 98 /** 99 * Constructs a BoundSheetRecord and sets its fields appropriately 100 * 101 * @param id id must be 0x85 or an exception will be throw upon validation 102 * @param size the size of the data area of the record 103 * @param data data of the record (should not contain sid/len) 104 * @param offset of the record's data 105 */ 106 107 public BoundSheetRecord(short id, short size, byte [] data, int offset) 108 { 109 super(id, size, data, offset); 110 } 111 112 protected void validateSid(short id) 113 { 114 if (id != sid) 115 { 116 throw new RecordFormatException("NOT A Bound Sheet RECORD"); 117 } 118 } 119 120 protected void fillFields(byte [] data, short size, int offset) 121 { 122 field_1_position_of_BOF = LittleEndian.getInt(data, 123 0 + offset); 124 field_2_option_flags = LittleEndian.getShort(data, 125 4 + offset); 126 field_3_sheetname_length = data[ 6 + offset ]; 127 field_4_compressed_unicode_flag = data[ 7 + offset ]; 128 field_5_sheetname = new String(data, 8 + offset, 129 LittleEndian.ubyteToInt( field_3_sheetname_length)); 130 } 131 132 /** 133 * set the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file 134 * 135 * @param pos offset in bytes 136 */ 137 138 public void setPositionOfBof(int pos) 139 { 140 field_1_position_of_BOF = pos; 141 } 142 143 /** 144 * set the option flags (unimportant for HSSF supported sheets) 145 * 146 * @param flags to set 147 */ 148 149 public void setOptionFlags(short flags) 150 { 151 field_2_option_flags = flags; 152 } 153 154 /** 155 * Set the length of the sheetname in characters 156 * 157 * @param len number of characters in the sheet name 158 * @see #setSheetname(String) 159 */ 160 161 public void setSheetnameLength(byte len) 162 { 163 field_3_sheetname_length = len; 164 } 165 166 /** 167 * set whether or not to interperate the Sheetname as compressed unicode (8/16 bit) 168 * (This is undocumented but can be found as Q187919 on the Microsoft(tm) Support site) 169 * @param flag (0/1) 0- compressed, 1 - uncompressed (16-bit) 170 */ 171 172 public void setCompressedUnicodeFlag(byte flag) 173 { 174 field_4_compressed_unicode_flag = flag; 175 } 176 177 /** 178 * Set the sheetname for this sheet. (this appears in the tabs at the bottom) 179 * @param sheetname the name of the sheet 180 */ 181 182 public void setSheetname(String sheetname) 183 { 184 field_5_sheetname = sheetname; 185 } 186 187 /** 188 * get the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file 189 * 190 * @return offset in bytes 191 */ 192 193 public int getPositionOfBof() 194 { 195 return field_1_position_of_BOF; 196 } 197 198 /** 199 * get the option flags (unimportant for HSSF supported sheets) 200 * 201 * @return flags to set 202 */ 203 204 public short getOptionFlags() 205 { 206 return field_2_option_flags; 207 } 208 209 /** 210 * get the length of the sheetname in characters 211 * 212 * @return number of characters in the sheet name 213 * @see #getSheetname() 214 */ 215 216 public byte getSheetnameLength() 217 { 218 return field_3_sheetname_length; 219 } 220 221 /** 222 * get whether or not to interperate the Sheetname as compressed unicode (8/16 bit) 223 * (This is undocumented but can be found as Q187919 on the Microsoft(tm) Support site) 224 * @return flag (0/1) 0- compressed, 1 - uncompressed (16-bit) 225 */ 226 227 public byte getCompressedUnicodeFlag() 228 { 229 return field_4_compressed_unicode_flag; 230 } 231 232 /** 233 * get the sheetname for this sheet. (this appears in the tabs at the bottom) 234 * @return sheetname the name of the sheet 235 */ 236 237 public String getSheetname() 238 { 239 return field_5_sheetname; 240 } 241 242 public String toString() 243 { 244 StringBuffer buffer = new StringBuffer(); 245 246 buffer.append("[BOUNDSHEET]\n"); 247 buffer.append(" .bof = ") 248 .append(Integer.toHexString(getPositionOfBof())).append("\n"); 249 buffer.append(" .optionflags = ") 250 .append(Integer.toHexString(getOptionFlags())).append("\n"); 251 buffer.append(" .sheetname length= ") 252 .append(Integer.toHexString(getSheetnameLength())).append("\n"); 253 buffer.append(" .unicodeflag = ") 254 .append(Integer.toHexString(getCompressedUnicodeFlag())) 255 .append("\n"); 256 buffer.append(" .sheetname = ").append(getSheetname()) 257 .append("\n"); 258 buffer.append("[/BOUNDSHEET]\n"); 259 return buffer.toString(); 260 } 261 262 public int serialize(int offset, byte [] data) 263 { 264 LittleEndian.putShort(data, 0 + offset, sid); 265 LittleEndian.putShort(data, 2 + offset, 266 ( short ) (0x08 + getSheetnameLength())); 267 LittleEndian.putInt(data, 4 + offset, getPositionOfBof()); 268 LittleEndian.putShort(data, 8 + offset, getOptionFlags()); 269 data[ 10 + offset ] = getSheetnameLength(); 270 data[ 11 + offset ] = getCompressedUnicodeFlag(); 271 272 // we assume compressed unicode (bein the dern americans we are ;-p) 273 StringUtil.putCompressedUnicode(getSheetname(), data, 12 + offset); 274 return getRecordSize(); 275 } 276 277 public int getRecordSize() 278 { 279 return 12 + getSheetnameLength(); 280 } 281 282 public short getSid() 283 { 284 return this.sid; 285 } 286 } 287