1 30 package com.genimen.djeneric.tools.scriptengine.core.util; 31 32 import com.genimen.djeneric.language.Messages; 33 import com.genimen.djeneric.repository.DjObject; 34 import com.genimen.djeneric.util.DjStringReplacer; 35 36 public class DefaultFunctions implements FunctionSupport 37 { 38 39 public DefaultFunctions() 40 { 41 } 42 43 public boolean isStaticCall(String function) 44 { 45 return function.equals("switch") || function.equals("getParameter"); 46 } 47 48 public Object staticCall(String function, Object [] params, DjScriptExecutionTimeScope context) throws Exception 49 { 50 if (function.equals("switch")) return doSwitch(params); 51 if (function.equals("getParameter")) return getParameter(params, context); 52 53 throw new Exception (Messages.getString("DefaultConvention.StaticFunction", function)); 54 } 55 56 public boolean isCall(String function) 57 { 58 return function.equals("indexOf") || function.equals("lastIndexOf") || function.equals("length") 59 || function.equals("startsWith") || function.equals("substring") || function.equals("toLowerCase") 60 || function.equals("toUpperCase") || function.equals("trim") || function.equals("initcap") 61 || function.equals("getClassName") || function.equals("isNegative") || function.equals("isPositive") 62 || function.equals("replace"); 63 64 } 65 66 public Object call(Object self, String function, Object [] params, DjScriptExecutionTimeScope context) 67 throws Exception 68 { 69 if (function.equals("indexOf")) return indexOf(self, params); 70 if (function.equals("lastIndexOf")) return lastIndexOf(self, params); 71 if (function.equals("length")) return length(self, params); 72 if (function.equals("startsWith")) return startsWith(self, params); 73 if (function.equals("substring")) return substring(self, params); 74 if (function.equals("toLowerCase")) return toLowerCase(self, params); 75 if (function.equals("toUpperCase")) return toUpperCase(self, params); 76 if (function.equals("trim")) return trim(self, params); 77 if (function.equals("initcap")) return initcap(self, params); 78 if (function.equals("getClassName")) return getClassName(self, params); 79 if (function.equals("isNegative")) return isNegative(self, params); 80 if (function.equals("isPositive")) return isPositive(self, params); 81 if (function.equals("replace")) return replace(self, params); 82 83 throw new Exception (Messages.getString("DefaultConvention.Function", function)); 84 } 85 86 Object doSwitch(Object [] params) throws Exception 88 { 89 if (params.length < 3) throw new Exception (Messages.getString("DefaultConvention.switchExpects3")); 90 Object value = params[0]; 91 for (int i = 1; i < params.length - 1; i += 2) 92 { 93 if (value.equals(params[i])) return params[i + 1]; 94 } 95 if ((params.length % 2) != 0) throw new Exception (Messages.getString("DefaultConvention.switchFailed", value)); 96 return params[params.length - 1]; 97 } 98 99 Object getParameter(Object [] params, DjScriptExecutionTimeScope context) throws Exception 100 { 101 if (params.length != 1) throw new Exception (Messages.getString("DefaultConvention.getParameterExpects1")); 102 String name = params[0].toString(); 103 return context.getParameter(name); 104 } 105 106 108 Object indexOf(Object self, Object [] params) throws Exception 109 { 110 if (!(self instanceof String )) throw new Exception (Messages.getString("DefaultConvention.indexOfOnlyOnStrings")); 111 112 if (params.length == 1) 113 { 114 if (!(params[0] instanceof String )) throw new Exception (Messages 115 .getString("DefaultConvention.indexOfTakesString")); 116 return new Integer (self.toString().indexOf(params[0].toString())); 117 } 118 if (params.length == 2) 119 { 120 if (!(params[0] instanceof String )) throw new Exception (Messages 121 .getString("DefaultConvention.indexOfTakesStringAsFirst")); 122 if (!(params[1] instanceof Integer )) throw new Exception (Messages 123 .getString("DefaultConvention.indexOfTakesIntAsSecond")); 124 return new Integer (self.toString().indexOf(params[0].toString(), ((Integer ) params[1]).intValue())); 125 } 126 throw new Exception (Messages.getString("DefaultConvention.indexOfTakes1or2Params")); 127 } 128 129 Object lastIndexOf(Object self, Object [] params) throws Exception 130 { 131 if (!(self instanceof String )) throw new Exception (Messages.getString("DefaultConvention.lastIndexOfOnlyStrings")); 132 133 if (params.length == 1) 134 { 135 if (!(params[0] instanceof String )) throw new Exception (Messages 136 .getString("DefaultConvention.lastIndexOfTakesAString")); 137 return new Integer (self.toString().lastIndexOf(params[0].toString())); 138 } 139 if (params.length == 2) 140 { 141 if (!(params[0] instanceof String )) throw new Exception (Messages 142 .getString("DefaultConvention.lastIndexOfTakesStringAtFirst")); 143 if (!(params[1] instanceof Integer )) throw new Exception (Messages 144 .getString("DefaultConvention.lastIndexOfIntAsSecond")); 145 return new Integer (self.toString().lastIndexOf(params[0].toString(), ((Integer ) params[1]).intValue())); 146 } 147 throw new Exception (Messages.getString("DefaultConvention.lastIndexOfTakes1or2Params")); 148 } 149 150 Object length(Object self, Object [] params) throws Exception 151 { 152 if (params.length != 0) throw new Exception (Messages.getString("DefaultConvention.lengthTakesNoParams")); 153 if (!(self instanceof String )) throw new Exception (Messages.getString("DefaultConvention.lengthOnlyOnString")); 154 return new Integer (self.toString().length()); 155 } 156 157 Object startsWith(Object self, Object [] params) throws Exception 158 { 159 if (!(self instanceof String )) throw new Exception (Messages.getString("DefaultConvention.startsWithOnlyStrings")); 160 161 if (params.length == 1) 162 { 163 if (!(params[0] instanceof String )) throw new Exception (Messages 164 .getString("DefaultConvention.startsWithTakesString")); 165 return new Boolean (self.toString().startsWith(params[0].toString())); 166 } 167 if (params.length == 2) 168 { 169 if (!(params[0] instanceof String )) throw new Exception (Messages 170 .getString("DefaultConvention.startsWithTakesStringAsFirst")); 171 if (!(params[1] instanceof Integer )) throw new Exception (Messages 172 .getString("DefaultConvention.startsWithTakesIntAtSecond")); 173 return new Boolean (self.toString().startsWith(params[0].toString(), ((Integer ) params[1]).intValue())); 174 } 175 throw new Exception (Messages.getString("DefaultConvention.startsWithTakes1or2Params")); 176 } 177 178 Object substring(Object self, Object [] params) throws Exception 179 { 180 if (!(self instanceof String )) throw new Exception (Messages.getString("DefaultConvention.substringOnlyOnStrings")); 181 182 if (params.length == 1) 183 { 184 if (!(params[0] instanceof String )) throw new Exception (Messages 185 .getString("DefaultConvention.substringTakesAString")); 186 return self.toString().substring(((Integer ) params[0]).intValue()); 187 } 188 if (params.length == 2) 189 { 190 if (!(params[0] instanceof String )) throw new Exception (Messages 191 .getString("DefaultConvention.substringStringAsFirst")); 192 if (!(params[1] instanceof Integer )) throw new Exception (Messages 193 .getString("DefaultConvention.substringTakesAnIntAsSecond")); 194 return self.toString().substring(((Integer ) params[0]).intValue(), ((Integer ) params[1]).intValue()); 195 } 196 throw new Exception (Messages.getString("DefaultConvention.substringTakes1or2Params")); 197 } 198 199 Object toLowerCase(Object self, Object [] params) throws Exception 200 { 201 if (params.length != 0) throw new Exception (Messages.getString("DefaultConvention.toLowerCaseTakesNoParams")); 202 if (!(self instanceof String )) throw new Exception (Messages.getString("DefaultConvention.toLowerCaseOnStrings")); 203 return self.toString().toLowerCase(); 204 } 205 206 Object toUpperCase(Object self, Object [] params) throws Exception 207 { 208 if (params.length != 0) throw new Exception (Messages.getString("DefaultConvention.toUpperCaseTakesNoParams")); 209 if (!(self instanceof String )) throw new Exception (Messages.getString("DefaultConvention.toUpperCaseOnStrings")); 210 return self.toString().toUpperCase(); 211 } 212 213 Object trim(Object self, Object [] params) throws Exception 214 { 215 if (params.length != 0) throw new Exception (Messages.getString("DefaultConvention.trimTakesNoParams")); 216 if (!(self instanceof String )) throw new Exception (Messages.getString("DefaultConvention.trimOnlyStrings")); 217 return self.toString().trim(); 218 } 219 220 public final static String initCap(String src) 221 { 222 if ((src == null) || (src.length() == 0)) return src; 223 224 StringBuffer sb = new StringBuffer (src); 225 226 sb.setCharAt(0, sb.substring(0, 1).toUpperCase().charAt(0)); 227 228 for (int i = 0; i < sb.length(); i++) 229 { 230 if (i > 0) 231 { 232 if ((sb.charAt(i - 1) == ' ') || (sb.charAt(i - 1) == '_')) 233 { 234 sb.setCharAt(i, sb.substring(i, i + 1).toUpperCase().charAt(0)); 235 } 236 } 237 } 238 return new String (sb); 239 } 240 241 Object initcap(Object self, Object [] params) throws Exception 242 { 243 if (params.length != 0) throw new Exception (Messages.getString("DefaultConvention.initcapTakesNoParams")); 244 if (!(self instanceof String )) throw new Exception (Messages.getString("DefaultConvention.initcapOnlyOnStrings")); 245 return initCap(self.toString()); 246 } 247 248 Object getClassName(Object self, Object [] params) throws Exception 249 { 250 if (params.length != 0) throw new Exception (Messages.getString("DefaultConvention.GetClassNameNoParams")); 251 if (self instanceof DjObject) 252 { 253 DjObject obj = (DjObject) self; 254 return obj.getExtent().getObjectType(); 255 } 256 String result = self.getClass().getName(); 257 if (result.indexOf(".") == -1) 258 { 259 int idx = result.lastIndexOf("."); 260 if (idx != -1) 261 { 262 result = result.substring(idx + 1); 263 } 264 } 265 266 return result; 267 } 268 269 Object isNegative(Object self, Object [] params) throws Exception 270 { 271 if (params.length != 0) throw new Exception (Messages.getString("DefaultConvention.isNegativeTakesNoParams")); 272 if (self instanceof Integer ) return new Boolean (((Integer ) self).intValue() < 0); 273 if (self instanceof Long ) return new Boolean (((Long ) self).longValue() < 0); 274 if (self instanceof Float ) return new Boolean (((Float ) self).floatValue() < 0); 275 276 throw new Exception (Messages.getString("DefaultConvention.isNegativeOnlyOnNumbers")); 277 } 278 279 Object isPositive(Object self, Object [] params) throws Exception 280 { 281 if (params.length != 0) throw new Exception (Messages.getString("DefaultConvention.isPositiveTakesNoParams")); 282 if (self instanceof Integer ) return new Boolean (((Integer ) self).intValue() >= 0); 283 if (self instanceof Long ) return new Boolean (((Long ) self).longValue() >= 0); 284 if (self instanceof Float ) return new Boolean (((Float ) self).floatValue() >= 0); 285 286 throw new Exception (Messages.getString("DefaultConvention.isPositiveOnlyOnNumbers")); 287 } 288 289 Object replace(Object self, Object [] params) throws Exception 290 { 291 if (params.length != 2) throw new Exception (Messages.getString("DefaultConvention.replaceTakesNoParams")); 292 DjStringReplacer sr = new DjStringReplacer(self.toString()); 293 return sr.replace(params[0].toString(), params[1].toString()); 294 295 } 296 } | Popular Tags |