1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package org.openoffice.xmerge.converter.xml.sxc.pexcel.records.formula; 25 26 import java.util.HashMap; 27 28 public class FunctionLookup extends SymbolLookup { 29 30 private HashMap stringToArgs = null; 31 32 /** 33 * The default constructor - invokes {@link #initialize() initialize()} 34 */ FunctionLookup()35 public FunctionLookup() { 36 initialize(); 37 } 38 39 /** 40 * Initialize the lookup table for functions 41 */ initialize()42 public void initialize() { 43 if ((stringToID != null) || (idToString != null) || (stringToArgs !=null)) { 44 return; 45 } 46 stringToID = new HashMap(); 47 idToString = new HashMap(); 48 stringToArgs = new HashMap(); 49 50 // Functions with Variable number of Arguments 51 // Math and Trig 52 addEntry("SUM", TokenConstants.TSUM, -1); 53 addEntry("MIN", TokenConstants.TMIN, -1); 54 addEntry("PRODUCT", TokenConstants.TPRODUCT, -1); 55 addEntry("LOG", TokenConstants.TLOG, -1); 56 addEntry("SUMIF", TokenConstants.TSUMIF, -1); 57 addEntry("TRUNC", TokenConstants.TRUNC, -1); 58 // Financial 59 addEntry("DDB", TokenConstants.TDDB, -1); 60 addEntry("FV", TokenConstants.TFV, -1); 61 addEntry("IRR", TokenConstants.TIRR, -1); 62 addEntry("NPER", TokenConstants.TNPER, -1); 63 addEntry("NPV", TokenConstants.TNPV, -1); 64 addEntry("PMT", TokenConstants.TPMT, -1); 65 addEntry("PV", TokenConstants.TPV, -1); 66 addEntry("RATE", TokenConstants.TRATE, -1); 67 // Statistical 68 addEntry("AVERAGE", TokenConstants.TAVERAGE, -1); 69 addEntry("COUNT", TokenConstants.TCOUNT, -1); 70 addEntry("COUNTA", TokenConstants.TCOUNTA, -1); 71 addEntry("MAX", TokenConstants.TMAX, -1 ); 72 addEntry("MIN", TokenConstants.TMIN, -1); 73 addEntry("STDEV", TokenConstants.TSTDEV, -1 ); 74 addEntry("STDEVP", TokenConstants.TSTDEVP, -1 ); 75 addEntry("VAR", TokenConstants.TVAR, -1); 76 addEntry("VARP", TokenConstants.TVARP, -1); 77 // Lookup 78 addEntry("CHOOSE", TokenConstants.TCHOOSE, -1); 79 addEntry("HLOOKUP", TokenConstants.THLOOKUP, -1); 80 addEntry("INDEX", TokenConstants.TINDEX, -1); 81 addEntry("MATCH", TokenConstants.TMATCH, -1) ; 82 addEntry("VLOOKUP", TokenConstants.TVLOOKUP, -1); 83 // Text 84 addEntry("RIGHT", TokenConstants.TRIGHT, -1); 85 addEntry("SUBSTITUTE", TokenConstants.TSUBSTITUTE, -1); 86 addEntry("FIND", TokenConstants.TFIND, -1); 87 addEntry("LEFT", TokenConstants.TLEFT, -1); 88 // Logical 89 addEntry("AND", TokenConstants.TAND, -1 ); 90 addEntry("IF", TokenConstants.TIF, -1) ; 91 addEntry("OR", TokenConstants.TOR, -1); 92 93 // Functions with Fixed number of Arguments 94 // Math and Trig 95 addEntry("ABS", TokenConstants.TABS, 1); 96 addEntry("ACOS", TokenConstants.TACOS, 1); 97 addEntry("ASIN", TokenConstants.TASIN, 1); 98 addEntry("ATAN", TokenConstants.TATAN, 1); 99 addEntry("ATAN2", TokenConstants.TATAN2, 1); 100 addEntry("COS", TokenConstants.TCOS, 1); 101 addEntry("COUNTIF", TokenConstants.TCOUNTIF, 1); 102 addEntry("DEGREES", TokenConstants.TDEGREES, 1); 103 addEntry("EXP", TokenConstants.TEXP, 1); 104 addEntry("FACT", TokenConstants.TFACT, 1); 105 addEntry("INT", TokenConstants.TINTE, 1); 106 addEntry("LN", TokenConstants.TLN, 1); 107 addEntry("LOG10", TokenConstants.TLOG10, 1); 108 addEntry("MOD", TokenConstants.TMOD, 1); 109 addEntry("PI", TokenConstants.TPI, 0); 110 addEntry("POWER", TokenConstants.TPOWERF, 2); 111 addEntry("RADIANS", TokenConstants.TRADIANS, 1); 112 addEntry("RAND", TokenConstants.TRAND, 1); 113 addEntry("ROUND", TokenConstants.TROUND, 1); 114 addEntry("SQRT", TokenConstants.TSQRT, 1); 115 addEntry("TAN", TokenConstants.TTAN, 1); 116 addEntry("SIN", TokenConstants.TSIN, 1); 117 // Financial 118 addEntry("SLN", TokenConstants.TSLN, 3); 119 addEntry("SYD", TokenConstants.TSYD, 4); 120 // Date and Time 121 addEntry("DATE", TokenConstants.TDATE, 3); 122 addEntry("DATEVALUE", TokenConstants.TDATEVALUE, 1); 123 addEntry("DAY", TokenConstants.TDAY, 1); 124 addEntry("HOUR", TokenConstants.THOUR, 1); 125 addEntry("MINUTE", TokenConstants.TMINUTE, 1 ); 126 addEntry("MONTH", TokenConstants.TMONTH, 1); 127 addEntry("NOW", TokenConstants.TNOW, 0); 128 addEntry("SECOND", TokenConstants.TSECOND, 1); 129 addEntry("TIME", TokenConstants.TTIME, 3); 130 addEntry("TIMEVALUE", TokenConstants.TTIMEVALUE, 1); 131 addEntry("YEAR", TokenConstants.TYEAR, 1); 132 // Statistical 133 addEntry("COUNTBLANK", TokenConstants.TCOUNTBLANK, 1); 134 // lookup 135 addEntry("COLUMNS", TokenConstants.TCOLUMNS, 1); 136 addEntry("ROWS", TokenConstants.TROWS, 1); 137 // Database 138 addEntry("DAVERAGE", TokenConstants.TDAVAERAGE, 3); 139 addEntry("DCOUNT", TokenConstants.TDCOUNT, 3); 140 addEntry("DCOUNTA", TokenConstants.TDCOUNTA, 2); 141 addEntry("DGET", TokenConstants.TDGET, 3); 142 addEntry("DMAX", TokenConstants.TDMAX, 3); 143 addEntry("DMIN", TokenConstants.TDMIN, 3); 144 addEntry("DPRODUCT", TokenConstants.TDPRODUCT, 3); 145 addEntry("DSTDEV", TokenConstants.TDSTDEV, 3); 146 addEntry("DSTDEVP", TokenConstants.TDSTDEVP, 3) ; 147 addEntry("DSUM", TokenConstants.TDSUM, 3); 148 addEntry("DVAR", TokenConstants.TDVAR, 3); 149 addEntry("DVARP", TokenConstants.TDVARP, 3); 150 // Text 151 addEntry("EXACT", TokenConstants.TEXACT, 2); 152 addEntry("LEN", TokenConstants.TLEN, 1); 153 addEntry("LOWER", TokenConstants.TLOWER, 1); 154 addEntry("MID", TokenConstants.TMID, 3); // ?????? 155 addEntry("PROPER", TokenConstants.TPROPER, 1); 156 addEntry("REPLACE", TokenConstants.TREPLACE, 4); 157 addEntry("REPT", TokenConstants.TREPT, 2); 158 addEntry("T", TokenConstants.TT, 1); 159 addEntry("TRIM", TokenConstants.TRIM, 1); 160 addEntry("UPPER", TokenConstants.TUPPER, 1); 161 addEntry("VALUE", TokenConstants.TVALUE, 1); 162 // Logical 163 addEntry("FALSE", TokenConstants.TFALSE, 0); 164 addEntry("NOT", TokenConstants.TNOT, 1); 165 addEntry("TRUE", TokenConstants.TTRUE, 0); 166 // Informational 167 addEntry("ERRORTYPE", TokenConstants.TERRORTYPE, 1); 168 addEntry("ISBLANK", TokenConstants.TISBLANK, 1); 169 addEntry("ISERR", TokenConstants.TISERR, 1); 170 addEntry("ISERROR", TokenConstants.TISERROR, 1); 171 addEntry("ISLOGICAL", TokenConstants.TISLOGICAL, 1); 172 addEntry("ISNA", TokenConstants.TISNA, 1); 173 addEntry("ISNONTEXT", TokenConstants.TISNONTEXT, 1); 174 addEntry("ISNUMBER", TokenConstants.TISNUMBER, 1); 175 addEntry("ISTEXT", TokenConstants.TISTEXT, 1); 176 addEntry("N", TokenConstants.TN, 1); 177 addEntry("NA", TokenConstants.TNA, 0); 178 179 } 180 181 /** 182 * Associate a function with an identifier and specifiy the number of arguments for that function 183 * @param symbol The function string that will act as the key in the lookup table 184 * @param id The identifier for the function 185 * @param args The number of arguments this function requires 186 */ addEntry(String symbol, int id, int args)187 public void addEntry(String symbol, int id, int args) { 188 addEntry(symbol, id); 189 stringToArgs.put(symbol, new Integer(args)); 190 } 191 192 /** 193 * Retrieve the number of arguments for this function 194 * @param symbol The function name 195 * @return The number of arguments required by this function 196 */ getArgCountFromString(String symbol)197 public int getArgCountFromString(String symbol) { 198 return ((Integer)stringToArgs.get(symbol)).intValue(); 199 } 200 } 201