01: package org.jbpm.jpdl.el.impl;
02:
03: import org.jbpm.JbpmConfiguration;
04: import org.jbpm.JbpmException;
05: import org.jbpm.graph.exe.ExecutionContext;
06: import org.jbpm.jpdl.el.ELException;
07: import org.jbpm.jpdl.el.ExpressionEvaluator;
08: import org.jbpm.jpdl.el.VariableResolver;
09: import org.jbpm.jpdl.el.FunctionMapper;
10:
11: public class JbpmExpressionEvaluator {
12:
13: static ExpressionEvaluator evaluator = new ExpressionEvaluatorImpl();
14:
15: public static void setExpressionEvaluator(
16: ExpressionEvaluator expressionEvaluator) {
17: JbpmExpressionEvaluator.evaluator = expressionEvaluator;
18: }
19:
20: static VariableResolver variableResolver = null;
21:
22: public static void setVariableResolver(
23: VariableResolver variableResolver) {
24: JbpmExpressionEvaluator.variableResolver = variableResolver;
25: }
26:
27: static FunctionMapper functionMapper = null;
28:
29: public static void setFunctionMapper(FunctionMapper functionMapper) {
30: JbpmExpressionEvaluator.functionMapper = functionMapper;
31: }
32:
33: public static Object evaluate(String expression,
34: ExecutionContext executionContext) {
35: return evaluate(expression, executionContext,
36: getUsedVariableResolver(), getUsedFunctionMapper());
37: }
38:
39: public static Object evaluate(String expression,
40: ExecutionContext executionContext,
41: VariableResolver usedVariableResolver,
42: FunctionMapper functionMapper) {
43: Object result = null;
44:
45: ExecutionContext.pushCurrentContext(executionContext);
46: try {
47: String dollarExpression = translateExpressionToDollars(expression);
48: result = evaluator.evaluate(dollarExpression, Object.class,
49: usedVariableResolver, functionMapper);
50:
51: } catch (ELException e) {
52:
53: throw new JbpmException("couldn't evaluate expression '"
54: + expression + "'", (e.getRootCause() != null ? e
55: .getRootCause() : e));
56: } finally {
57: ExecutionContext.popCurrentContext(executionContext);
58: }
59:
60: return result;
61: }
62:
63: static String translateExpressionToDollars(String expression) {
64: char[] chars = expression.toCharArray();
65: int index = 0;
66: while (index != -1) {
67: index = expression.indexOf("#{", index);
68: if (index != -1) {
69: chars[index] = '$';
70: index++;
71: }
72: }
73: return new String(chars);
74: }
75:
76: public static VariableResolver getUsedVariableResolver() {
77: if (variableResolver != null) {
78: return variableResolver;
79: }
80: return (VariableResolver) JbpmConfiguration.Configs
81: .getObject("jbpm.variable.resolver");
82: }
83:
84: public static FunctionMapper getUsedFunctionMapper() {
85: if (functionMapper != null) {
86: return functionMapper;
87: }
88: if (JbpmConfiguration.Configs.hasObject("jbpm.function.mapper")) {
89: return (FunctionMapper) JbpmConfiguration.Configs
90: .getObject("jbpm.function.mapper");
91: }
92: return null;
93: }
94: }
|