1 2 17 18 19 package org.apache.poi.hssf.record; 20 21 import org.apache.poi.util.LittleEndian; 22 import org.apache.poi.util.StringUtil; 23 24 34 35 public class BoundSheetRecord 36 extends Record 37 { 38 public final static short sid = 0x85; 39 private int field_1_position_of_BOF; 40 private short field_2_option_flags; 41 private byte field_3_sheetname_length; 42 private byte field_4_compressed_unicode_flag; private String field_5_sheetname; 44 45 public BoundSheetRecord() 46 { 47 } 48 49 56 57 public BoundSheetRecord( short id, short size, byte[] data ) 58 { 59 super( id, size, data ); 60 } 61 62 70 71 public BoundSheetRecord( short id, short size, byte[] data, int offset ) 72 { 73 super( id, size, data, offset ); 74 } 75 76 protected void validateSid( short id ) 77 { 78 if ( id != sid ) 79 { 80 throw new RecordFormatException( "NOT A Bound Sheet RECORD" ); 81 } 82 } 83 84 94 95 protected void fillFields( byte[] data, short size, int offset ) 96 { 97 field_1_position_of_BOF = LittleEndian.getInt( data, 0 + offset ); field_2_option_flags = LittleEndian.getShort( data, 4 + offset ); field_3_sheetname_length = data[6 + offset]; field_4_compressed_unicode_flag = data[7 + offset]; 102 int nameLength = LittleEndian.ubyteToInt( field_3_sheetname_length ); 103 if ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 ) 104 { 105 field_5_sheetname = StringUtil.getFromUnicodeLE( data, 8 + offset, nameLength ); 106 } 107 else 108 { 109 field_5_sheetname = StringUtil.getFromCompressedUnicode( data, 8 + offset, nameLength ); 110 } 111 } 112 113 118 119 public void setPositionOfBof( int pos ) 120 { 121 field_1_position_of_BOF = pos; 122 } 123 124 129 130 public void setOptionFlags( short flags ) 131 { 132 field_2_option_flags = flags; 133 } 134 135 141 142 public void setSheetnameLength( byte len ) 143 { 144 field_3_sheetname_length = len; 145 } 146 147 152 153 public void setCompressedUnicodeFlag( byte flag ) 154 { 155 field_4_compressed_unicode_flag = flag; 156 } 157 158 163 164 public void setSheetname( String sheetname ) 165 { 166 167 if ((sheetname == null) || (sheetname.length()==0) 168 || (sheetname.length()>31) 169 || (sheetname.indexOf("/") > -1) 170 || (sheetname.indexOf("\\") > -1) 171 || (sheetname.indexOf("?") > -1) 172 || (sheetname.indexOf("*") > -1) 173 || (sheetname.indexOf("]") > -1) 174 || (sheetname.indexOf("[") > -1) ){ 175 throw new IllegalArgumentException ("Sheet name cannot be blank, greater than 31 chars, or contain any of /\\*?[]"); 176 } 177 field_5_sheetname = sheetname; 178 } 179 180 185 186 public int getPositionOfBof() 187 { 188 return field_1_position_of_BOF; 189 } 190 191 196 197 public short getOptionFlags() 198 { 199 return field_2_option_flags; 200 } 201 202 208 209 public byte getSheetnameLength() 210 { 211 return field_3_sheetname_length; 212 } 213 214 220 221 public byte getRawSheetnameLength() 222 { 223 return (byte) ( ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 ) 224 ? 2 * field_3_sheetname_length 225 : field_3_sheetname_length ); 226 } 227 228 233 234 public byte getCompressedUnicodeFlag() 235 { 236 return field_4_compressed_unicode_flag; 237 } 238 239 243 244 public String getSheetname() 245 { 246 return field_5_sheetname; 247 } 248 249 public String toString() 250 { 251 StringBuffer buffer = new StringBuffer (); 252 253 buffer.append( "[BOUNDSHEET]\n" ); 254 buffer.append( " .bof = " ) 255 .append( Integer.toHexString( getPositionOfBof() ) ).append( "\n" ); 256 buffer.append( " .optionflags = " ) 257 .append( Integer.toHexString( getOptionFlags() ) ).append( "\n" ); 258 buffer.append( " .sheetname length= " ) 259 .append( Integer.toHexString( getSheetnameLength() ) ).append( "\n" ); 260 buffer.append( " .unicodeflag = " ) 261 .append( Integer.toHexString( getCompressedUnicodeFlag() ) ) 262 .append( "\n" ); 263 buffer.append( " .sheetname = " ).append( getSheetname() ) 264 .append( "\n" ); 265 buffer.append( "[/BOUNDSHEET]\n" ); 266 return buffer.toString(); 267 } 268 269 public int serialize( int offset, byte[] data ) 270 { 271 LittleEndian.putShort( data, 0 + offset, sid ); 272 LittleEndian.putShort( data, 2 + offset, (short) ( 8 + getRawSheetnameLength() ) ); 273 LittleEndian.putInt( data, 4 + offset, getPositionOfBof() ); 274 LittleEndian.putShort( data, 8 + offset, getOptionFlags() ); 275 data[10 + offset] = (byte) ( getSheetnameLength() ); 276 data[11 + offset] = getCompressedUnicodeFlag(); 277 278 if ( ( field_4_compressed_unicode_flag & 0x01 ) == 1 ) 279 StringUtil.putUnicodeLE( getSheetname(), data, 12 + offset ); 280 else 281 StringUtil.putCompressedUnicode( getSheetname(), data, 12 + offset ); 282 283 284 return getRecordSize(); 285 286 306 } 307 308 public int getRecordSize() 309 { 310 return 12 + getRawSheetnameLength(); 312 } 313 314 public short getSid() 315 { 316 return this.sid; 317 } 318 } 319 | Popular Tags |