1 16 17 package org.apache.poi.hssf.record.formula; 18 19 import org.apache.poi.hssf.model.Workbook; 20 import org.apache.poi.util.BitField; 21 import org.apache.poi.util.StringUtil; 22 23 30 31 public class StringPtg 32 extends Ptg 33 { 34 public final static int SIZE = 9; 35 public final static byte sid = 0x17; 36 int field_1_length; 39 byte field_2_options; 40 BitField fHighByte = new BitField(0x01); 41 private String field_3_string; 42 43 private StringPtg() { 44 } 46 47 48 public StringPtg(byte [] data, int offset) 49 { 50 offset++; 51 field_1_length = data[offset] & 0xFF; 52 field_2_options = data[offset+1]; 53 if (fHighByte.isSet(field_2_options)) { 54 field_3_string= StringUtil.getFromUnicodeLE(data,offset+2,field_1_length); 55 }else { 56 field_3_string=StringUtil.getFromCompressedUnicode(data,offset+2,field_1_length); 57 } 58 59 } 61 62 67 public StringPtg(String value) { 68 if (value.length() >255) { 69 throw new IllegalArgumentException ("String literals in formulas cant be bigger than 255 characters ASCII"); 70 } 71 this.field_2_options=0; 72 this.fHighByte.setBoolean(field_2_options, false); 73 this.field_3_string=value; 74 this.field_1_length=value.length(); } 76 77 82 83 84 public String getValue() 85 { 86 return field_3_string; 87 } 88 89 public void writeBytes(byte [] array, int offset) 90 { 91 array[ offset + 0 ] = sid; 92 array[ offset + 1 ] = (byte)field_1_length; 93 array[ offset + 2 ] = field_2_options; 94 if (fHighByte.isSet(field_2_options)) { 95 StringUtil.putUnicodeLE(getValue(),array,offset+3); 96 }else { 97 StringUtil.putCompressedUnicode(getValue(),array,offset+3); 98 } 99 } 100 101 public int getSize() 102 { 103 if (fHighByte.isSet(field_2_options)) { 104 return 2*field_1_length+3; 105 } else { 106 return field_1_length+3; 107 } 108 } 109 110 public String toFormulaString(Workbook book) 111 { 112 return "\""+getValue()+"\""; 113 } 114 public byte getDefaultOperandClass() { 115 return Ptg.CLASS_VALUE; 116 } 117 118 public Object clone() { 119 StringPtg ptg = new StringPtg(); 120 ptg.field_1_length = field_1_length; 121 ptg.field_2_options=field_2_options; 122 ptg.field_3_string=field_3_string; 123 return ptg; 124 } 125 126 } 127 128 | Popular Tags |