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 import java.io.UnsupportedEncodingException ; 25 26 37 38 public class UnicodeString 39 extends Record 40 implements Comparable 41 { 42 public final static short sid = 0xFFF; 43 private short field_1_charCount; private byte field_2_optionflags; private String field_3_string; private final int RICH_TEXT_BIT = 8; 47 private final int EXT_BIT = 4; 48 49 public UnicodeString() 50 { 51 } 52 53 54 public int hashCode() 55 { 56 int stringHash = 0; 57 if (field_3_string != null) 58 stringHash = field_3_string.hashCode(); 59 return field_1_charCount + stringHash; 60 } 61 62 69 public boolean equals(Object o) 70 { 71 if ((o == null) || (o.getClass() != this.getClass())) 72 { 73 return false; 74 } 75 UnicodeString other = ( UnicodeString ) o; 76 77 return ((field_1_charCount == other.field_1_charCount) 78 && (field_2_optionflags == other.field_2_optionflags) 79 && field_3_string.equals(other.field_3_string)); 80 } 81 82 88 89 public UnicodeString(short id, short size, byte [] data) 90 { 91 super(id, size, data); 92 } 93 94 97 98 public UnicodeString(short id, short size, byte [] data, String prefix) 99 { 100 this(id, size, data); 101 field_3_string = prefix + field_3_string; 102 setCharCount(); 103 } 104 105 108 109 protected void validateSid(short id) 110 { 111 112 } 114 115 protected void fillFields(byte [] data, short size) 116 { 117 field_1_charCount = LittleEndian.getShort(data, 0); 118 field_2_optionflags = data[ 2 ]; 119 if ((field_2_optionflags & 1) == 0) 120 { 121 try { 122 field_3_string = new String (data, 3, getCharCount(), 123 StringUtil.getPreferredEncoding()); 124 } catch (UnsupportedEncodingException e) { 125 String errorMessage = e.getMessage(); 128 129 if (errorMessage == null) { 131 errorMessage = e.toString(); 132 } 133 throw new RuntimeException (errorMessage); 134 } 135 } 136 else 137 { 138 char[] array = new char[ getCharCount() ]; 139 140 for (int j = 0; j < array.length; j++) 141 { 142 array[ j ] = ( char ) LittleEndian.getShort(data, 143 3 + (j * 2)); 144 } 145 field_3_string = new String (array); 146 } 147 } 148 149 156 157 public short getCharCount() 158 { 159 return field_1_charCount; 160 } 161 162 166 167 public void setCharCount(short cc) 168 { 169 field_1_charCount = cc; 170 } 171 172 178 179 public void setCharCount() 180 { 181 field_1_charCount = ( short ) field_3_string.length(); 182 } 183 184 191 192 public byte getOptionFlags() 193 { 194 return field_2_optionflags; 195 } 196 197 204 205 public void setOptionFlags(byte of) 206 { 207 field_2_optionflags = of; 208 } 209 210 217 218 public String getString() 219 { 220 return field_3_string; 221 } 222 223 227 228 public void setString(String string) 229 { 230 field_3_string = string; 231 if (getCharCount() < field_3_string.length()) 232 { 233 setCharCount(); 234 } 235 } 236 237 242 243 public String toString() 244 { 245 return getString(); 246 } 247 248 255 256 public String getDebugInfo() 257 { 258 StringBuffer buffer = new StringBuffer (); 259 260 buffer.append("[UNICODESTRING]\n"); 261 buffer.append(" .charcount = ") 262 .append(Integer.toHexString(getCharCount())).append("\n"); 263 buffer.append(" .optionflags = ") 264 .append(Integer.toHexString(getOptionFlags())).append("\n"); 265 buffer.append(" .string = ").append(getString()) 266 .append("\n"); 267 buffer.append("[/UNICODESTRING]\n"); 268 return buffer.toString(); 269 } 270 271 public int serialize(int offset, byte [] data) 272 { 273 int charsize = 1; 274 275 if (getOptionFlags() == 1) 276 { 277 charsize = 2; 278 } 279 280 LittleEndian.putShort(data, 0 + offset, getCharCount()); 282 data[ 2 + offset ] = getOptionFlags(); 283 284 try { 286 String unicodeString = new String (getString().getBytes("Unicode"),"Unicode"); 287 if (getOptionFlags() == 0) 288 { 289 StringUtil.putCompressedUnicode(unicodeString, data, 0x3 +offset); 290 } 291 else 292 { 293 StringUtil.putUnicodeLE(unicodeString, data, 294 0x3 + offset); 295 } 296 } 297 catch (Exception e) { 298 if (getOptionFlags() == 0) 299 { 300 StringUtil.putCompressedUnicode(getString(), data, 0x3 + 301 offset); 302 } 303 else 304 { 305 StringUtil.putUnicodeLE(getString(), data, 306 0x3 + offset); 307 } 308 } 309 return getRecordSize(); 310 } 311 312 private boolean isUncompressedUnicode() 313 { 314 return (getOptionFlags() & 0x01) == 1; 315 } 316 317 public int getRecordSize() 318 { 319 int charsize = isUncompressedUnicode() ? 2 : 1; 320 return 3 + (getString().length() * charsize); 321 } 322 323 public short getSid() 324 { 325 return this.sid; 326 } 327 328 336 337 protected void fillFields(byte [] data, short size, int offset) 338 { 339 } 340 341 public int compareTo(Object obj) 342 { 343 UnicodeString str = ( UnicodeString ) obj; 344 345 return this.getString().compareTo(str.getString()); 346 } 347 348 public boolean isRichText() 349 { 350 return (getOptionFlags() & RICH_TEXT_BIT) != 0; 351 } 352 353 int maxBrokenLength(final int proposedBrokenLength) 354 { 355 int rval = proposedBrokenLength; 356 357 if (isUncompressedUnicode()) 358 { 359 int proposedStringLength = proposedBrokenLength - 3; 360 361 if ((proposedStringLength % 2) == 1) 362 { 363 proposedStringLength--; 364 } 365 rval = proposedStringLength + 3; 366 } 367 return rval; 368 } 369 370 public boolean isExtendedText() 371 { 372 return (getOptionFlags() & EXT_BIT) != 0; 373 } 374 375 } 376 | Popular Tags |