1 19 20 package jxl.biff.formula; 21 22 import java.util.Stack ; 23 24 import common.Assert; 25 import common.Logger; 26 import jxl.WorkbookSettings; 27 import jxl.Sheet; 28 import jxl.biff.IntegerHelper; 29 30 33 class BuiltInFunction extends Operator implements ParsedThing 34 { 35 38 private static Logger logger = Logger.getLogger(BuiltInFunction.class); 39 40 43 private Function function; 44 45 48 private WorkbookSettings settings; 49 50 54 public BuiltInFunction(WorkbookSettings ws) 55 { 56 settings = ws; 57 } 58 59 65 public BuiltInFunction(Function f, WorkbookSettings ws) 66 { 67 function = f; 68 settings = ws; 69 } 70 71 78 public int read(byte[] data, int pos) 79 { 80 int index = IntegerHelper.getInt(data[pos], data[pos+1]); 81 function = Function.getFunction(index); 82 Assert.verify(function != function.UNKNOWN, "function code " + index); 83 return 2; 84 } 85 86 89 public void getOperands(Stack s) 90 { 91 ParseItem[] items = new ParseItem[function.getNumArgs()]; 93 for (int i = function.getNumArgs() - 1; i >= 0 ; i--) 95 { 96 ParseItem pi = (ParseItem) s.pop(); 97 98 items[i] = pi; 99 } 100 101 for (int i = 0 ; i < function.getNumArgs(); i++) 102 { 103 add(items[i]); 104 } 105 106 113 } 114 115 120 public void getString(StringBuffer buf) 121 { 122 buf.append(function.getName(settings)); 123 buf.append('('); 124 125 int numArgs = function.getNumArgs(); 126 127 if (numArgs > 0) 128 { 129 ParseItem[] operands = getOperands(); 130 131 operands[0].getString(buf); 133 134 for (int i = 1; i < numArgs; i++) 135 { 136 buf.append(','); 137 operands[i].getString(buf); 138 } 139 140 150 } 151 152 buf.append(')'); 153 } 154 155 162 public void adjustRelativeCellReferences(int colAdjust, int rowAdjust) 163 { 164 ParseItem[] operands = getOperands(); 165 166 for (int i = 0 ; i < operands.length ; i++) 167 { 168 operands[i].adjustRelativeCellReferences(colAdjust, rowAdjust); 169 } 170 } 171 172 182 void columnInserted(int sheetIndex, int col, boolean currentSheet) 183 { 184 ParseItem[] operands = getOperands(); 185 for (int i = 0 ; i < operands.length ; i++) 186 { 187 operands[i].columnInserted(sheetIndex, col, currentSheet); 188 } 189 } 190 191 201 void columnRemoved(int sheetIndex, int col, boolean currentSheet) 202 { 203 ParseItem[] operands = getOperands(); 204 for (int i = 0 ; i < operands.length ; i++) 205 { 206 operands[i].columnRemoved(sheetIndex, col, currentSheet); 207 } 208 } 209 210 211 221 void rowInserted(int sheetIndex, int row, boolean currentSheet) 222 { 223 ParseItem[] operands = getOperands(); 224 for (int i = 0 ; i < operands.length ; i++) 225 { 226 operands[i].rowInserted(sheetIndex, row, currentSheet); 227 } 228 } 229 230 240 void rowRemoved(int sheetIndex, int row, boolean currentSheet) 241 { 242 ParseItem[] operands = getOperands(); 243 for (int i = 0 ; i < operands.length ; i++) 244 { 245 operands[i].rowRemoved(sheetIndex, row, currentSheet); 246 } 247 } 248 249 254 byte[] getBytes() 255 { 256 ParseItem[] operands = getOperands(); 258 byte[] data = new byte[0]; 259 260 for (int i = 0 ; i < operands.length; i++) 261 { 262 byte[] opdata = operands[i].getBytes(); 263 264 byte[] newdata = new byte[data.length + opdata.length]; 266 System.arraycopy(data, 0, newdata, 0, data.length); 267 System.arraycopy(opdata, 0, newdata, data.length, opdata.length); 268 data = newdata; 269 } 270 271 byte[] newdata = new byte[data.length + 3]; 273 System.arraycopy(data, 0, newdata, 0, data.length); 274 newdata[data.length] = !useAlternateCode() ? Token.FUNCTION.getCode() : 275 Token.FUNCTION.getCode2(); 276 IntegerHelper.getTwoBytes(function.getCode(), newdata, data.length+1); 277 278 return newdata; 279 } 280 281 287 int getPrecedence() 288 { 289 return 3; 290 } 291 292 } 293 294 | Popular Tags |