1 package com.genimen.djeneric.tools.generator.core.util; 2 3 import com.genimen.djeneric.repository.DjObject; 4 import com.genimen.djeneric.util.DjStringReplacer; 5 6 public class DefaultConvention implements Convention 7 { 8 9 public DefaultConvention() 10 { 11 } 12 13 public Object staticCall(String function, Object [] params, ParseContext context) throws Exception 14 { 15 if (function.equals("switch")) return doSwitch(params); 16 17 throw new Exception ("Static function " + function + " not defined"); 18 } 19 20 public Object call(Object self, String function, Object [] params, ParseContext context) throws Exception 21 { 22 if (function.equals("indexOf")) return indexOf(self, params); 23 if (function.equals("lastIndexOf")) return lastIndexOf(self, params); 24 if (function.equals("length")) return length(self, params); 25 if (function.equals("startsWith")) return startsWith(self, params); 26 if (function.equals("substring")) return substring(self, params); 27 if (function.equals("toLowerCase")) return toLowerCase(self, params); 28 if (function.equals("toUpperCase")) return toUpperCase(self, params); 29 if (function.equals("trim")) return trim(self, params); 30 if (function.equals("initcap")) return initcap(self, params); 31 if (function.equals("getClassName")) return getClassName(self, params); 32 if (function.equals("isNegative")) return isNegative(self, params); 33 if (function.equals("isPositive")) return isPositive(self, params); 34 if (function.equals("replace")) return replace(self, params); 35 if (function.equals("getIndented")) return getIndented(self, params); 36 37 throw new Exception ("Function " + function + " not defined"); 38 } 39 40 Object doSwitch(Object [] params) throws Exception 42 { 43 if (params.length < 3) throw new Exception ("switch expects at least 3 parameters (value, compare, result)"); 44 Object value = params[0]; 45 for (int i = 1; i < params.length - 1; i += 2) 46 { 47 if (value.equals(params[i])) return params[i + 1]; 48 } 49 if ((params.length % 2) != 0) throw new Exception ("switch failed; value \"" + value 50 + "\" not handled; you might want to use a default"); 51 return params[params.length - 1]; 52 } 53 54 56 Object indexOf(Object self, Object [] params) throws Exception 57 { 58 if (!(self instanceof String )) throw new Exception ("indexOf only operates on strings"); 59 60 if (params.length == 1) 61 { 62 if (!(params[0] instanceof String )) throw new Exception ("indexOf takes a String as parameter"); 63 return new Integer (self.toString().indexOf(params[0].toString())); 64 } 65 if (params.length == 2) 66 { 67 if (!(params[0] instanceof String )) throw new Exception ("indexOf takes a String as first parameter"); 68 if (!(params[1] instanceof Integer )) throw new Exception ("indexOf takes an integer as second parameter"); 69 return new Integer (self.toString().indexOf(params[0].toString(), ((Integer ) params[1]).intValue())); 70 } 71 throw new Exception ("indexOf takes 1 or 2 parameters"); 72 } 73 74 Object lastIndexOf(Object self, Object [] params) throws Exception 75 { 76 if (!(self instanceof String )) throw new Exception ("lastIndexOf only operates on strings"); 77 78 if (params.length == 1) 79 { 80 if (!(params[0] instanceof String )) throw new Exception ("lastIndexOf takes a String as parameter"); 81 return new Integer (self.toString().lastIndexOf(params[0].toString())); 82 } 83 if (params.length == 2) 84 { 85 if (!(params[0] instanceof String )) throw new Exception ("lastIndexOf takes a String as first parameter"); 86 if (!(params[1] instanceof Integer )) throw new Exception ("lastIndexOf takes an integer as second parameter"); 87 return new Integer (self.toString().lastIndexOf(params[0].toString(), ((Integer ) params[1]).intValue())); 88 } 89 throw new Exception ("lastIndexOf takes 1 or 2 parameters"); 90 } 91 92 Object length(Object self, Object [] params) throws Exception 93 { 94 if (params.length != 0) throw new Exception ("length takes no parameters"); 95 if (!(self instanceof String )) throw new Exception ("length only operates on strings"); 96 return new Integer (self.toString().length()); 97 } 98 99 Object startsWith(Object self, Object [] params) throws Exception 100 { 101 if (!(self instanceof String )) throw new Exception ("startsWith only operates on strings"); 102 103 if (params.length == 1) 104 { 105 if (!(params[0] instanceof String )) throw new Exception ("startsWith takes a String as parameter"); 106 return new Boolean (self.toString().startsWith(params[0].toString())); 107 } 108 if (params.length == 2) 109 { 110 if (!(params[0] instanceof String )) throw new Exception ("startsWith takes a String as first parameter"); 111 if (!(params[1] instanceof Integer )) throw new Exception ("startsWith takes an integer as second parameter"); 112 return new Boolean (self.toString().startsWith(params[0].toString(), ((Integer ) params[1]).intValue())); 113 } 114 throw new Exception ("startsWith takes 1 or 2 parameters"); 115 } 116 117 Object substring(Object self, Object [] params) throws Exception 118 { 119 if (!(self instanceof String )) throw new Exception ("substring only operates on strings"); 120 121 if (params.length == 1) 122 { 123 if (!(params[0] instanceof String )) throw new Exception ("substring takes a String as parameter"); 124 return self.toString().substring(((Integer ) params[0]).intValue()); 125 } 126 if (params.length == 2) 127 { 128 if (!(params[0] instanceof String )) throw new Exception ("substring takes a String as first parameter"); 129 if (!(params[1] instanceof Integer )) throw new Exception ("substring takes an integer as second parameter"); 130 return self.toString().substring(((Integer ) params[0]).intValue(), ((Integer ) params[1]).intValue()); 131 } 132 throw new Exception ("substring takes 1 or 2 parameters"); 133 } 134 135 Object toLowerCase(Object self, Object [] params) throws Exception 136 { 137 if (params.length != 0) throw new Exception ("toLowerCase takes no parameters"); 138 if (!(self instanceof String )) throw new Exception ("toLowerCase only operates on strings"); 139 return self.toString().toLowerCase(); 140 } 141 142 Object toUpperCase(Object self, Object [] params) throws Exception 143 { 144 if (params.length != 0) throw new Exception ("toUpperCase takes no parameters"); 145 if (!(self instanceof String )) throw new Exception ("toUpperCase only operates on strings"); 146 return self.toString().toUpperCase(); 147 } 148 149 Object trim(Object self, Object [] params) throws Exception 150 { 151 if (params.length != 0) throw new Exception ("trim takes no parameters"); 152 if (!(self instanceof String )) throw new Exception ("trim only operates on strings"); 153 return self.toString().trim(); 154 } 155 156 public final static String initCap(String src) 157 { 158 if ((src == null) || (src.length() == 0)) return src; 159 160 StringBuffer sb = new StringBuffer (src); 161 162 sb.setCharAt(0, sb.substring(0, 1).toUpperCase().charAt(0)); 163 164 for (int i = 0; i < sb.length(); i++) 165 { 166 if (i > 0) 167 { 168 if ((sb.charAt(i - 1) == ' ') || (sb.charAt(i - 1) == '_')) 169 { 170 sb.setCharAt(i, sb.substring(i, i + 1).toUpperCase().charAt(0)); 171 } 172 } 173 } 174 return new String (sb); 175 } 176 177 Object initcap(Object self, Object [] params) throws Exception 178 { 179 if (params.length != 0) throw new Exception ("initcap takes no parameters"); 180 if (!(self instanceof String )) throw new Exception ("initcap only operates on strings"); 181 return initCap(self.toString()); 182 } 183 184 Object getClassName(Object self, Object [] params) throws Exception 185 { 186 if (params.length != 0) throw new Exception ("getClassName takes no parameters"); 187 if (self instanceof DjObject) 188 { 189 DjObject obj = (DjObject) self; 190 return obj.getExtent().getObjectType(); 191 } 192 String result = self.getClass().getName(); 193 if (result.indexOf(".") == -1) 194 { 195 int idx = result.lastIndexOf("."); 196 if (idx != -1) 197 { 198 result = result.substring(idx + 1); 199 } 200 } 201 202 return result; 203 } 204 205 Object isNegative(Object self, Object [] params) throws Exception 206 { 207 if (params.length != 0) throw new Exception ("isNegative takes no parameters"); 208 if (self instanceof Integer ) return new Boolean (((Integer ) self).intValue() < 0); 209 if (self instanceof Long ) return new Boolean (((Long ) self).longValue() < 0); 210 if (self instanceof Float ) return new Boolean (((Float ) self).floatValue() < 0); 211 212 throw new Exception ("isNegative only operates on numbers"); 213 } 214 215 Object isPositive(Object self, Object [] params) throws Exception 216 { 217 if (params.length != 0) throw new Exception ("isPositive takes no parameters"); 218 if (self instanceof Integer ) return new Boolean (((Integer ) self).intValue() >= 0); 219 if (self instanceof Long ) return new Boolean (((Long ) self).longValue() >= 0); 220 if (self instanceof Float ) return new Boolean (((Float ) self).floatValue() >= 0); 221 222 throw new Exception ("isPositive only operates on numbers"); 223 } 224 225 Object replace(Object self, Object [] params) throws Exception 226 { 227 if (params.length != 2) throw new Exception ("replace takes 2 parameters"); 228 DjStringReplacer sr = new DjStringReplacer(self.toString()); 229 return sr.replace(params[0].toString(), params[1].toString()); 230 231 } 232 233 Object getIndented(Object self, Object [] params) throws Exception 234 { 235 if (params.length != 1) throw new Exception ("getIndented takes 1 parameter"); 236 if (!(params[0] instanceof Integer )) throw new Exception ("getIndented takes an integer as parameter"); 237 238 int ind = ((Integer ) params[0]).intValue(); 239 if (ind == 0 || self == null) return self; 240 241 String val = self.toString(); 242 243 StringBuffer result = new StringBuffer (val); 244 245 if (ind > 0) 246 { 247 for (int x = 0; x < ind; x++) 248 result.insert(0, ' '); 249 int i = 0; 250 while (i < result.length()) 251 { 252 if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n') 253 { 254 for (int x = 0; x < ind; x++) 255 result.insert(i + 1, ' '); 256 i += ind; 257 } 258 i++; 259 } 260 } 261 if (ind < 0) 262 { 263 ind = -ind; 264 for (int d = 0; d < ind; d++) 265 if (result.charAt(0) == ' ') result.deleteCharAt(0); 266 int i = 0; 267 while (i < result.length()) 268 { 269 if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n') 270 { 271 int end = i + ind; 272 if (end > result.length()) end = result.length(); 273 for (int d = i; d < end; d++) 274 if (result.charAt(i + 1) == ' ') result.deleteCharAt(i + 1); 275 } 276 i++; 277 } 278 } 279 return result.toString(); 280 } 281 } | Popular Tags |