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 29 public class StringRecord 30 extends Record 31 { 32 public final static short sid = 0x207; 33 private int field_1_string_length; 34 private byte field_2_unicode_flag; 35 private String field_3_string; 36 37 38 public StringRecord() 39 { 40 } 41 42 49 public StringRecord( short id, short size, byte[] data ) 50 { 51 super( id, size, data ); 52 } 53 54 62 public StringRecord( short id, short size, byte[] data, int offset ) 63 { 64 super( id, size, data, offset ); 65 } 66 67 68 74 protected void validateSid( short id ) 75 { 76 if (id != this.sid) 77 { 78 throw new RecordFormatException("Not a valid StringRecord"); 79 } 80 } 81 82 90 protected void fillFields( byte[] data, short size, int offset ) 91 { 92 field_1_string_length = LittleEndian.getUShort(data, 0 + offset); 93 field_2_unicode_flag = data[ 2 + offset ]; 94 if (isUnCompressedUnicode()) 95 { 96 field_3_string = StringUtil.getFromUnicodeBE(data, 3 + offset, field_1_string_length ); 97 } 98 else 99 { 100 field_3_string = StringUtil.getFromCompressedUnicode(data, 3 + offset, field_1_string_length); 101 } 102 } 103 104 public boolean isInValueSection() 105 { 106 return true; 107 } 108 109 private int getStringLength() 110 { 111 return field_1_string_length; 112 } 113 114 private int getStringByteLength() 115 { 116 return isUnCompressedUnicode() ? field_1_string_length * 2 : field_1_string_length; 117 } 118 119 122 public int getRecordSize() 123 { 124 return 4 + 2 + 1 + getStringByteLength(); 125 } 126 127 131 public boolean isUnCompressedUnicode() 132 { 133 return (field_2_unicode_flag == 1); 134 } 135 136 145 public int serialize( int offset, byte[] data ) 146 { 147 LittleEndian.putShort(data, 0 + offset, sid); 148 LittleEndian.putShort(data, 2 + offset, ( short ) (3 + getStringByteLength())); 149 LittleEndian.putUShort(data, 4 + offset, field_1_string_length); 150 data[6 + offset] = field_2_unicode_flag; 151 if (isUnCompressedUnicode()) 152 { 153 StringUtil.putUnicodeLE(field_3_string, data, 7 + offset); 154 } 155 else 156 { 157 StringUtil.putCompressedUnicode(field_3_string, data, 7 + offset); 158 } 159 return getRecordSize(); 160 } 161 162 165 public short getSid() 166 { 167 return sid; 168 } 169 170 173 public String getString() 174 { 175 return field_3_string; 176 } 177 178 182 public void setCompressedFlag( byte unicode_flag ) 183 { 184 this.field_2_unicode_flag = unicode_flag; 185 } 186 187 190 public void setString( String string ) 191 { 192 this.field_1_string_length = string.length(); 193 this.field_3_string = string; 194 } 195 196 197 198 public String toString() 199 { 200 StringBuffer buffer = new StringBuffer (); 201 202 buffer.append("[STRING]\n"); 203 buffer.append(" .string = ") 204 .append(field_3_string).append("\n"); 205 buffer.append("[/STRING]\n"); 206 return buffer.toString(); 207 } 208 209 public Object clone() { 210 StringRecord rec = new StringRecord(); 211 rec.field_1_string_length = this.field_1_string_length; 212 rec.field_2_unicode_flag= this.field_2_unicode_flag; 213 rec.field_3_string = this.field_3_string; 214 return rec; 215 216 } 217 218 } 219 | Popular Tags |