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 33 34 public class FormatRecord 35 extends Record 36 { 37 public final static short sid = 0x41e; 38 private short field_1_index_code; 39 private short field_2_formatstring_len; 40 private short field_3_unicode_len; private boolean field_3_unicode_flag; private String field_4_formatstring; 43 44 public FormatRecord() 45 { 46 } 47 48 55 56 public FormatRecord(short id, short size, byte [] data) 57 { 58 super(id, size, data); 59 } 60 61 69 70 public FormatRecord(short id, short size, byte [] data, int offset) 71 { 72 super(id, size, data, offset); 73 } 74 75 protected void validateSid(short id) 76 { 77 if (id != sid) 78 { 79 throw new RecordFormatException("NOT A FORMAT RECORD"); 80 } 81 } 82 83 protected void fillFields(byte [] data, short size, int offset) 84 { 85 field_1_index_code = LittleEndian.getShort(data, 0 + offset); 86 field_3_unicode_len = LittleEndian.getShort( data, 2 + offset ); 88 field_2_formatstring_len = field_3_unicode_len; 89 field_3_unicode_flag = ( data[ 4 + offset ] & (byte)0x01 ) != 0; 90 91 92 if ( field_3_unicode_flag ) { 93 field_4_formatstring = StringUtil.getFromUnicodeLE( data, 5 + offset, field_3_unicode_len ); 95 } 96 else { 97 field_4_formatstring = StringUtil.getFromCompressedUnicode(data, 5 + offset, field_3_unicode_len ); 99 } 100 } 101 102 108 109 public void setIndexCode(short index) 110 { 111 field_1_index_code = index; 112 } 113 114 120 121 public void setFormatStringLength(byte len) 122 { 123 field_2_formatstring_len = len; 124 field_3_unicode_len = len; 125 } 126 127 132 133 public void setUnicodeFlag(boolean unicode) { 134 field_3_unicode_flag = unicode; 135 } 136 137 143 144 public void setFormatString(String fs) 145 { 146 field_4_formatstring = fs; 147 } 148 149 155 156 public short getIndexCode() 157 { 158 return field_1_index_code; 159 } 160 161 167 168 public short getFormatStringLength() 169 { 170 return field_3_unicode_flag ? field_3_unicode_len : field_2_formatstring_len; 171 } 172 173 178 179 public boolean getUnicodeFlag() { 180 return field_3_unicode_flag; 181 } 182 183 189 190 public String getFormatString() 191 { 192 return field_4_formatstring; 193 } 194 195 public String toString() 196 { 197 StringBuffer buffer = new StringBuffer (); 198 199 buffer.append("[FORMAT]\n"); 200 buffer.append(" .indexcode = ") 201 .append(Integer.toHexString(getIndexCode())).append("\n"); 202 207 buffer.append(" .unicode length = ") 208 .append(Integer.toHexString(field_3_unicode_len)).append("\n"); 209 buffer.append(" .isUnicode = ") 210 .append( field_3_unicode_flag ).append("\n"); 211 buffer.append(" .formatstring = ").append(getFormatString()) 212 .append("\n"); 213 buffer.append("[/FORMAT]\n"); 214 return buffer.toString(); 215 } 216 217 public int serialize(int offset, byte [] data) 218 { 219 LittleEndian.putShort(data, 0 + offset, sid); 220 LittleEndian.putShort(data, 2 + offset, (short)( 2 + 2 + 1 + ( (field_3_unicode_flag) 221 ? 2 * field_3_unicode_len 222 : field_3_unicode_len ) ) ); 223 LittleEndian.putShort(data, 4 + offset, getIndexCode()); 225 LittleEndian.putShort(data, 6 + offset, field_3_unicode_len); 226 data[ 8 + offset ] = (byte)( (field_3_unicode_flag) ? 0x01 : 0x00 ); 227 228 if ( field_3_unicode_flag ) { 229 StringUtil.putUnicodeLE( getFormatString(), data, 9 + offset ); 231 } 232 else { 233 StringUtil.putCompressedUnicode( getFormatString(), data, 9 + offset ); 235 } 236 237 return getRecordSize(); 238 } 239 240 public int getRecordSize() 241 { 242 return 9 + ( ( field_3_unicode_flag ) ? 2 * field_3_unicode_len : field_3_unicode_len ); 243 } 244 245 public short getSid() 246 { 247 return this.sid; 248 } 249 } 250 | Popular Tags |