1 19 20 package jxl.read.biff; 21 22 import common.Assert; 23 import common.Logger; 24 25 import jxl.LabelCell; 26 import jxl.CellType; 27 import jxl.StringFormulaCell; 28 import jxl.WorkbookSettings; 29 import jxl.biff.StringHelper; 30 import jxl.biff.IntegerHelper; 31 import jxl.biff.Type; 32 import jxl.biff.FormulaData; 33 import jxl.biff.WorkbookMethods; 34 import jxl.biff.FormattingRecords; 35 import jxl.biff.formula.FormulaParser; 36 import jxl.biff.formula.ExternalSheet; 37 import jxl.biff.formula.FormulaException; 38 39 42 class StringFormulaRecord extends CellValue 43 implements LabelCell, FormulaData, StringFormulaCell 44 { 45 48 private static Logger logger = Logger.getLogger(StringFormulaRecord.class); 49 50 53 private String value; 54 55 58 private ExternalSheet externalSheet; 59 60 63 private WorkbookMethods nameTable; 64 65 68 private String formulaString; 69 70 73 private byte[] data; 74 75 87 public StringFormulaRecord(Record t, File excelFile, 88 FormattingRecords fr, 89 ExternalSheet es, 90 WorkbookMethods nt, 91 SheetImpl si, 92 WorkbookSettings ws) 93 { 94 super(t, fr, si); 95 96 externalSheet = es; 97 nameTable = nt; 98 99 data = getRecord().getData(); 100 101 int pos = excelFile.getPos(); 102 103 106 Record nextRecord = excelFile.next(); 107 int count = 0; 108 while (nextRecord.getType() != Type.STRING && count < 4) 109 { 110 nextRecord = excelFile.next(); 111 count++; 112 } 113 Assert.verify(count < 4, " @ " + pos); 114 readString(nextRecord.getData(), ws); 115 } 116 117 128 public StringFormulaRecord(Record t, 129 FormattingRecords fr, 130 ExternalSheet es, 131 WorkbookMethods nt, 132 SheetImpl si) 133 { 134 super(t, fr, si); 135 136 externalSheet = es; 137 nameTable = nt; 138 139 data = getRecord().getData(); 140 value = ""; 141 } 142 143 144 150 private void readString(byte[] d, WorkbookSettings ws) 151 { 152 int pos = 0; 153 int chars = IntegerHelper.getInt(d[0], d[1]); 154 155 if (chars == 0) 156 { 157 value=""; 158 return; 159 } 160 pos += 2; 161 int optionFlags = d[pos]; 162 pos++; 163 164 if ((optionFlags & 0xf) != optionFlags) 165 { 166 pos = 0; 169 chars = IntegerHelper.getInt(d[0], (byte) 0); 170 optionFlags = d[1]; 171 pos = 2; 172 } 173 174 boolean extendedString = ((optionFlags & 0x04) != 0); 176 177 boolean richString = ((optionFlags & 0x08) != 0); 179 180 if (richString) 181 { 182 pos += 2; 183 } 184 185 if (extendedString) 186 { 187 pos += 4; 188 } 189 190 boolean asciiEncoding = ((optionFlags & 0x01) == 0); 192 193 if (asciiEncoding) 194 { 195 value = StringHelper.getString(d, chars, pos, ws); 196 } 197 else 198 { 199 value = StringHelper.getUnicodeString(d, chars, pos); 200 } 201 } 202 203 208 public String getContents() 209 { 210 return value; 211 } 212 213 218 public String getString() 219 { 220 return value; 221 } 222 223 228 public CellType getType() 229 { 230 return CellType.STRING_FORMULA; 231 } 232 233 239 public byte[] getFormulaData() throws FormulaException 240 { 241 if (!getSheet().getWorkbook().getWorkbookBof().isBiff8()) 242 { 243 throw new FormulaException(FormulaException.biff8Supported); 244 } 245 246 byte[] d = new byte[data.length - 6]; 248 System.arraycopy(data, 6, d, 0, data.length - 6); 249 250 return d; 251 } 252 253 259 public String getFormula() throws FormulaException 260 { 261 if (formulaString == null) 262 { 263 byte[] tokens = new byte[data.length - 22]; 264 System.arraycopy(data, 22, tokens, 0, tokens.length); 265 FormulaParser fp = new FormulaParser 266 (tokens, this, externalSheet, nameTable, 267 getSheet().getWorkbook().getSettings()); 268 fp.parse(); 269 formulaString = fp.getFormula(); 270 } 271 272 return formulaString; 273 } 274 275 } 276 | Popular Tags |