1 19 20 package jxl.biff.formula; 21 22 import java.util.Stack ; 23 24 import common.Assert; 25 import common.Logger; 26 27 import jxl.WorkbookSettings; 28 import jxl.Sheet; 29 import jxl.biff.IntegerHelper; 30 34 35 class Attribute extends Operator implements ParsedThing 36 { 37 40 private static Logger logger = Logger.getLogger(Attribute.class); 41 42 45 private int options; 46 47 50 private int word; 51 52 55 private WorkbookSettings settings; 56 57 private final static int sumMask = 0x10; 58 private final static int ifMask = 0x02; 59 private final static int gotoMask = 0x08; 60 61 64 private VariableArgFunction ifConditions; 65 66 69 public Attribute(WorkbookSettings ws) 70 { 71 settings = ws; 72 } 73 74 80 public Attribute(StringFunction sf, WorkbookSettings ws) 81 { 82 settings = ws; 83 84 if (sf.getFunction(settings) == Function.SUM) 85 { 86 options |= sumMask; 87 } 88 else if (sf.getFunction(settings) == Function.IF) 89 { 90 options |= ifMask; 91 } 92 } 93 94 99 void setIfConditions(VariableArgFunction vaf) 100 { 101 ifConditions = vaf; 102 103 options |= ifMask; 106 } 107 108 115 public int read(byte[] data, int pos) 116 { 117 options = data[pos]; 118 word = IntegerHelper.getInt(data[pos+1], data[pos+2]); 119 return 3; 120 } 121 122 127 public boolean isFunction() 128 { 129 return (options & (sumMask | ifMask) ) != 0; 130 } 131 132 137 public boolean isSum() 138 { 139 return (options & sumMask) != 0; 140 } 141 142 147 public boolean isIf() 148 { 149 return (options & ifMask) != 0; 150 } 151 152 157 public boolean isGoto() 158 { 159 return (options & gotoMask) != 0; 160 } 161 162 165 public void getOperands(Stack s) 166 { 167 if ((options & sumMask) != 0) 168 { 169 ParseItem o1 = (ParseItem) s.pop(); 170 171 add(o1); 172 } 173 else if ((options & ifMask) != 0) 174 { 175 ParseItem o1 = (ParseItem) s.pop(); 176 add(o1); 177 } 178 } 179 180 public void getString(StringBuffer buf) 181 { 182 if ((options & sumMask) != 0) 183 { 184 ParseItem[] operands = getOperands(); 185 buf.append(Function.SUM.getName(settings)); 186 buf.append('('); 187 operands[0].getString(buf); 188 buf.append(')'); 189 } 190 else if ((options & ifMask) != 0) 191 { 192 buf.append(Function.IF.getName(settings)); 193 buf.append('('); 194 195 ParseItem[] operands = ifConditions.getOperands(); 196 197 for (int i = 0 ; i < operands.length-1 ; i++) 199 { 200 operands[i].getString(buf); 201 buf.append(','); 202 } 203 operands[operands.length-1].getString(buf); 204 buf.append(')'); 205 } 206 } 207 208 215 byte[] getBytes() 216 { 217 byte[] data = new byte[0]; 218 if (isSum()) 219 { 220 ParseItem[] operands = getOperands(); 222 223 for (int i = operands.length - 1 ; i >= 0 ; i--) 225 { 226 byte[] opdata = operands[i].getBytes(); 227 228 byte[] newdata = new byte[data.length + opdata.length]; 230 System.arraycopy(data, 0, newdata, 0, data.length); 231 System.arraycopy(opdata, 0, newdata, data.length, opdata.length); 232 data = newdata; 233 } 234 235 byte[] newdata = new byte[data.length + 4]; 237 System.arraycopy(data, 0, newdata, 0, data.length); 238 newdata[data.length] = Token.ATTRIBUTE.getCode(); 239 newdata[data.length+1] = sumMask; 240 data = newdata; 241 } 242 else if (isIf()) 243 { 244 return getIf(); 245 } 246 247 return data; 248 } 249 250 255 private byte[] getIf() 256 { 257 ParseItem[] operands = ifConditions.getOperands(); 258 259 int falseOffsetPos = 0; 261 int gotoEndPos = 0; 262 int numArgs = operands.length; 263 264 byte[] data = operands[0].getBytes(); 266 267 int pos = data.length; 269 byte[] newdata = new byte[data.length+4]; 270 System.arraycopy(data, 0, newdata, 0, data.length); 271 data = newdata; 272 data[pos] = Token.ATTRIBUTE.getCode(); 273 data[pos+1] = 0x2; 274 falseOffsetPos = pos+2; 275 276 byte[] truedata = operands[1].getBytes(); 278 newdata = new byte[data.length + truedata.length]; 279 System.arraycopy(data, 0, newdata, 0, data.length); 280 System.arraycopy(truedata, 0, newdata, data.length, truedata.length); 281 data = newdata; 282 283 pos = data.length; 285 newdata = new byte[data.length+4]; 286 System.arraycopy(data, 0, newdata, 0, data.length); 287 data = newdata; 288 data[pos] = Token.ATTRIBUTE.getCode(); 289 data[pos+1] = 0x8; 290 gotoEndPos = pos+2; 291 292 if (numArgs > 2) 294 { 295 IntegerHelper.getTwoBytes(data.length - falseOffsetPos - 2, 297 data, falseOffsetPos); 298 299 byte[] falsedata = operands[numArgs-1].getBytes(); 301 newdata = new byte[data.length + falsedata.length]; 302 System.arraycopy(data, 0, newdata, 0, data.length); 303 System.arraycopy(falsedata, 0, newdata, data.length, falsedata.length); 304 data = newdata; 305 306 pos = data.length; 308 newdata = new byte[data.length+4]; 309 System.arraycopy(data, 0, newdata, 0, data.length); 310 data = newdata; 311 data[pos] = Token.ATTRIBUTE.getCode(); 312 data[pos+1] = 0x8; 313 data[pos+2] = 0x3; 314 } 315 316 pos = data.length; 318 newdata = new byte[data.length+4]; 319 System.arraycopy(data, 0, newdata, 0, data.length); 320 data = newdata; 321 data[pos] = Token.FUNCTIONVARARG.getCode(); 322 data[pos+1] = (byte) numArgs; 323 data[pos+2] = 1; 324 data[pos+3] = 0; 326 int endPos = data.length - 1; 328 329 if (numArgs < 3) 330 { 331 IntegerHelper.getTwoBytes(endPos - falseOffsetPos - 5, 333 data, falseOffsetPos); 334 } 335 336 IntegerHelper.getTwoBytes(endPos - gotoEndPos - 2, 338 data, gotoEndPos); 339 340 return data; 341 } 342 343 349 int getPrecedence() 350 { 351 return 3; 352 } 353 354 360 public void adjustRelativeCellReferences(int colAdjust, int rowAdjust) 361 { 362 ParseItem[] operands = null; 363 364 if (isIf()) 365 { 366 operands = ifConditions.getOperands(); 367 } 368 else 369 { 370 operands = getOperands(); 371 } 372 373 for (int i = 0 ; i < operands.length ; i++) 374 { 375 operands[i].adjustRelativeCellReferences(colAdjust, rowAdjust); 376 } 377 } 378 379 389 void columnInserted(int sheetIndex, int col, boolean currentSheet) 390 { 391 ParseItem[] operands = null; 392 393 if (isIf()) 394 { 395 operands = ifConditions.getOperands(); 396 } 397 else 398 { 399 operands = getOperands(); 400 } 401 402 for (int i = 0 ; i < operands.length ; i++) 403 { 404 operands[i].columnInserted(sheetIndex, col, currentSheet); 405 } 406 } 407 408 418 void columnRemoved(int sheetIndex, int col, boolean currentSheet) 419 { 420 ParseItem[] operands = null; 421 422 if (isIf()) 423 { 424 operands = ifConditions.getOperands(); 425 } 426 else 427 { 428 operands = getOperands(); 429 } 430 431 for (int i = 0 ; i < operands.length ; i++) 432 { 433 operands[i].columnRemoved(sheetIndex, col, currentSheet); 434 } 435 } 436 437 447 void rowInserted(int sheetIndex, int row, boolean currentSheet) 448 { 449 ParseItem[] operands = null; 450 451 if (isIf()) 452 { 453 operands = ifConditions.getOperands(); 454 } 455 else 456 { 457 operands = getOperands(); 458 } 459 460 for (int i = 0 ; i < operands.length ; i++) 461 { 462 operands[i].rowInserted(sheetIndex, row, currentSheet); 463 } 464 } 465 466 476 void rowRemoved(int sheetIndex, int row, boolean currentSheet) 477 { 478 ParseItem[] operands = null; 479 480 if (isIf()) 481 { 482 operands = ifConditions.getOperands(); 483 } 484 else 485 { 486 operands = getOperands(); 487 } 488 489 for (int i = 0 ; i < operands.length ; i++) 490 { 491 operands[i].rowRemoved(sheetIndex, row, currentSheet); 492 } 493 } 494 } 495 496 497 498 499 | Popular Tags |