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 import org.apache.poi.util.BitField; 24 25 33 34 public class StyleRecord 35 extends Record 36 { 37 public final static short sid = 0x293; 38 public final static short STYLE_USER_DEFINED = 0; 39 public final static short STYLE_BUILT_IN = 1; 40 41 private short field_1_xf_index; 44 private byte field_2_builtin_style; 46 private byte field_3_outline_style_level; 47 48 private short field_2_name_length; private byte field_3_string_options; 51 private BitField fHighByte; 52 private String field_4_name; 53 54 public StyleRecord() 55 { 56 } 57 58 65 66 public StyleRecord(short id, short size, byte [] data) 67 { 68 super(id, size, data); 69 } 70 71 79 80 public StyleRecord(short id, short size, byte [] data, int offset) 81 { 82 super(id, size, data, offset); 83 } 84 85 protected void validateSid(short id) 86 { 87 if (id != sid) 88 { 89 throw new RecordFormatException("NOT A STYLE RECORD"); 90 } 91 } 92 93 protected void fillFields(byte [] data, short size, int offset) 94 { 95 fHighByte = new BitField(0x01); field_1_xf_index = LittleEndian.getShort(data, 0 + offset); 98 if (getType() == STYLE_BUILT_IN) 99 { 100 field_2_builtin_style = data[ 2 + offset ]; 101 field_3_outline_style_level = data[ 3 + offset ]; 102 } 103 else if (getType() == STYLE_USER_DEFINED) 104 { 105 field_2_name_length = LittleEndian.getShort(data, 2 + offset ); 106 field_3_string_options = data[4+offset]; 107 108 if (fHighByte.isSet(field_3_string_options)) { 109 field_4_name= StringUtil.getFromUnicodeBE(data,offset+5,field_2_name_length); 110 }else { 111 field_4_name=StringUtil.getFromCompressedUnicode(data,offset+5,field_2_name_length); 112 } 113 } 114 115 } 117 118 122 123 public void setIndex(short index) 124 { 125 field_1_xf_index = index; 126 } 127 128 130 137 138 public void setType(short type) 139 { 140 field_1_xf_index = setField(field_1_xf_index, type, 0x8000, 15); 141 } 142 143 148 149 public void setXFIndex(short index) 150 { 151 field_1_xf_index = setField(field_1_xf_index, index, 0x1FFF, 0); 152 } 153 154 157 162 163 public void setNameLength(byte length) 164 { 165 field_2_name_length = length; 166 } 167 168 173 174 public void setName(String name) 175 { 176 field_4_name = name; 177 } 179 180 183 188 189 public void setBuiltin(byte builtin) 190 { 191 field_2_builtin_style = builtin; 192 } 193 194 197 198 public void setOutlineStyleLevel(byte level) 199 { 200 field_3_outline_style_level = level; 201 } 202 203 206 210 211 public short getIndex() 212 { 213 return field_1_xf_index; 214 } 215 216 218 225 226 public short getType() 227 { 228 return ( short ) ((field_1_xf_index & 0x8000) >> 15); 229 } 230 231 236 237 public short getXFIndex() 238 { 239 return ( short ) (field_1_xf_index & 0x1FFF); 240 } 241 242 245 250 251 public short getNameLength() 252 { 253 return field_2_name_length; 254 } 255 256 261 262 public String getName() 263 { 264 return field_4_name; 265 } 266 267 270 275 276 public byte getBuiltin() 277 { 278 return field_2_builtin_style; 279 } 280 281 284 285 public byte getOutlineStyleLevel() 286 { 287 return field_3_outline_style_level; 288 } 289 290 public String toString() 292 { 293 StringBuffer buffer = new StringBuffer (); 294 295 buffer.append("[STYLE]\n"); 296 buffer.append(" .xf_index_raw = ") 297 .append(Integer.toHexString(getIndex())).append("\n"); 298 buffer.append(" .type = ") 299 .append(Integer.toHexString(getType())).append("\n"); 300 buffer.append(" .xf_index = ") 301 .append(Integer.toHexString(getXFIndex())).append("\n"); 302 if (getType() == STYLE_BUILT_IN) 303 { 304 buffer.append(" .builtin_style = ") 305 .append(Integer.toHexString(getBuiltin())).append("\n"); 306 buffer.append(" .outline_level = ") 307 .append(Integer.toHexString(getOutlineStyleLevel())) 308 .append("\n"); 309 } 310 else if (getType() == STYLE_USER_DEFINED) 311 { 312 buffer.append(" .name_length = ") 313 .append(Integer.toHexString(getNameLength())).append("\n"); 314 buffer.append(" .name = ").append(getName()) 315 .append("\n"); 316 } 317 buffer.append("[/STYLE]\n"); 318 return buffer.toString(); 319 } 320 321 private short setField(int fieldValue, int new_value, int mask, 322 int shiftLeft) 323 { 324 return ( short ) ((fieldValue & ~mask) 325 | ((new_value << shiftLeft) & mask)); 326 } 327 328 public int serialize(int offset, byte [] data) 329 { 330 LittleEndian.putShort(data, 0 + offset, sid); 331 if (getType() == STYLE_BUILT_IN) 332 { 333 LittleEndian.putShort(data, 2 + offset, 334 (( short ) 0x04)); } 336 else 337 { 338 LittleEndian.putShort(data, 2 + offset, 339 (( short ) (getRecordSize()-4))); 340 } 341 LittleEndian.putShort(data, 4 + offset, getIndex()); 342 if (getType() == STYLE_BUILT_IN) 343 { 344 data[ 6 + offset ] = getBuiltin(); 345 data[ 7 + offset ] = getOutlineStyleLevel(); 346 } 347 else 348 { 349 LittleEndian.putShort(data, 6 + offset , getNameLength()); 350 data[8+offset]=this.field_3_string_options; 351 StringUtil.putCompressedUnicode(getName(), data, 9 + offset); 352 } 353 return getRecordSize(); 354 } 355 356 public int getRecordSize() 357 { 358 int retval; 359 360 if (getType() == STYLE_BUILT_IN) 361 { 362 retval = 8; 363 } 364 else 365 { 366 if (fHighByte.isSet(field_3_string_options)) { 367 retval= 9+2*getNameLength(); 368 }else { 369 retval = 9 + getNameLength(); 370 } 371 } 372 return retval; 373 } 374 375 public short getSid() 376 { 377 return this.sid; 378 } 379 } 380 | Popular Tags |