1 64 65 package com.jcorporate.expresso.ext.velocity; 66 67 68 import com.jcorporate.expresso.core.controller.Block; 69 import com.jcorporate.expresso.core.controller.ControllerElement; 70 import com.jcorporate.expresso.core.controller.ControllerException; 71 import com.jcorporate.expresso.core.controller.ControllerResponse; 72 import com.jcorporate.expresso.core.db.DBException; 73 import com.jcorporate.expresso.core.i18n.Messages; 74 import com.jcorporate.expresso.services.dbobj.SchemaList; 75 76 import java.util.List ; 77 import java.util.StringTokenizer ; 78 79 80 103 104 public class ExpressoTool { 105 106 107 117 118 public String getString(ControllerResponse response, String code, List args) throws ControllerException { 119 120 121 Object [] argsArray; 122 123 if (args == null) { 124 argsArray = new Object [0]; 125 } else { 126 argsArray = args.toArray(); 127 } 128 129 130 return Messages.getString(response.getSchemaStack(), 131 132 response.getLocale(), 133 134 code, 135 136 argsArray); 137 138 } 139 140 141 186 187 public Object getElement(ControllerResponse response, String path) throws ControllerException { 188 189 if (path == null || path.length() == 0) { 190 return null; 191 } 192 193 194 if (path.charAt(0) == '/') { 195 if (path.length() == 1) { 196 return null; 197 } else { 198 path = path.substring(1); 199 } 200 } 201 202 203 Object element = null; 204 205 206 StringTokenizer tok = new StringTokenizer (path, "/"); 207 208 if (tok.hasMoreTokens()) { 209 210 String token = tok.nextToken().trim(); 211 212 213 boolean isAttribute = false; 214 215 if (token.length() > 1) { 216 217 if (token.charAt(0) == '@') { 218 219 isAttribute = true; 220 221 token = token.substring(1); 222 223 } 224 225 } 226 227 228 if (token.length() != 0) { 229 230 if (isAttribute) { 231 232 return response.getAttribute(token); 233 234 } else { 235 236 element = response.getNestedMap().get(token); 237 238 239 if (element == null) { 240 241 if (token.startsWith("BLOCK:")) { 242 243 element = response.getBlock(token.substring(6)); 244 245 } else if (token.startsWith("OUTPUT:")) { 246 247 element = response.getBlock(token.substring(7)); 248 249 } else if (token.startsWith("INPUT:")) { 250 251 element = response.getBlock(token.substring(6)); 252 253 } else if (token.startsWith("TRANSITION:")) { 254 255 element = response.getBlock(token.substring(11)); 256 257 } 258 259 } 260 261 } 262 263 } 264 265 if (tok.hasMoreTokens()) { 266 267 element = getElementFrom(response, (ControllerElement) element, tok); 268 269 } 270 271 } 272 273 274 return element; 275 276 } 277 278 279 329 330 public Object getElementFrom(ControllerResponse response, ControllerElement from, String path) throws ControllerException { 331 332 if (from == null) { 333 return getElement(response, path); 334 } 335 336 337 if (path == null || path.length() == 0) { 338 return from; 339 } 340 341 342 if (path.charAt(0) == '/') { 343 return getElement(response, path); 344 } 345 346 347 StringTokenizer tok = new StringTokenizer (path, "/"); 348 349 350 Object element = getElementFrom(response, from, tok); 351 352 353 return element; 354 355 } 356 357 358 private Object getElementFrom(ControllerResponse response, ControllerElement from, StringTokenizer tok) throws ControllerException { 359 360 361 ControllerElement element = null; 362 363 364 while (tok.hasMoreTokens()) { 365 366 String token = tok.nextToken().trim(); 367 368 369 boolean isAttribute = false; 370 371 if (token.length() > 1) { 372 373 if (token.charAt(0) == '@') { 374 375 isAttribute = true; 376 377 token = token.substring(1); 378 379 } 380 381 } 382 383 384 if (token.length() != 0) { 385 386 if (isAttribute) { 387 388 return from.getAttribute(token); 389 390 } else { 391 392 element = (ControllerElement) from.getNestedMap().get(token); 393 394 395 if (element == null && (from instanceof Block)) { 396 397 if (token.startsWith("BLOCK:")) { 398 399 element = ((Block) from).getBlock(token.substring(6)); 400 401 } else if (token.startsWith("OUTPUT:")) { 402 403 element = ((Block) from).getBlock(token.substring(7)); 404 405 } else if (token.startsWith("INPUT:")) { 406 407 element = ((Block) from).getBlock(token.substring(6)); 408 409 } else if (token.startsWith("TRANSITION:")) { 410 411 element = ((Block) from).getBlock(token.substring(11)); 412 413 } 414 415 } 416 417 if (element == null) { 418 419 return null; 420 421 } else { 422 423 from = element; 424 425 } 426 427 } 428 429 } 430 431 } 432 433 return from; 434 435 } 436 437 438 446 447 public String getSetupValue(String dataContext, String key) throws DBException { 448 449 return com.jcorporate.expresso.services.dbobj.Setup.getValue(dataContext, key); 450 451 } 452 453 454 459 460 public com.jcorporate.expresso.core.misc.ConfigManager getConfigManager() { 461 462 return com.jcorporate.expresso.core.misc.ConfigManager.getInstance(); 463 464 } 465 466 467 475 476 public List getSchemaList(String dataContext) throws DBException { 477 478 SchemaList sl = new SchemaList(SchemaList.SYSTEM_ACCOUNT); 479 480 sl.setDBName(dataContext); 481 482 483 return sl.searchAndRetrieveList(); 484 485 } 486 487 } | Popular Tags |