1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.commons.scxml.env.jexl; |
19 |
|
|
20 |
|
import java.util.ArrayList; |
21 |
|
import java.util.HashMap; |
22 |
|
import java.util.List; |
23 |
|
import java.util.Map; |
24 |
|
import java.util.regex.Pattern; |
25 |
|
|
26 |
|
import org.apache.commons.jexl.Expression; |
27 |
|
import org.apache.commons.jexl.ExpressionFactory; |
28 |
|
import org.apache.commons.scxml.Context; |
29 |
|
import org.apache.commons.scxml.Evaluator; |
30 |
|
import org.apache.commons.scxml.SCXMLExpressionException; |
31 |
|
import org.w3c.dom.Node; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class JexlEvaluator implements Evaluator { |
39 |
|
|
40 |
|
|
41 |
|
private static final String ERR_CTX_TYPE = "Error evaluating JEXL " |
42 |
|
+ "expression, Context must be a org.apache.commons.jexl.JexlContext"; |
43 |
|
|
44 |
|
|
45 |
4 |
private static Pattern inFct = Pattern.compile("In\\("); |
46 |
|
|
47 |
4 |
private static Pattern dataFct = Pattern.compile("Data\\("); |
48 |
|
|
49 |
|
|
50 |
|
public JexlEvaluator() { |
51 |
28 |
super(); |
52 |
28 |
} |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
public Object eval(final Context ctx, final String expr) |
64 |
|
throws SCXMLExpressionException { |
65 |
35 |
if (expr == null) { |
66 |
0 |
return null; |
67 |
|
} |
68 |
35 |
JexlContext jexlCtx = null; |
69 |
35 |
if (ctx instanceof JexlContext) { |
70 |
35 |
jexlCtx = (JexlContext) ctx; |
71 |
|
} else { |
72 |
0 |
throw new SCXMLExpressionException(ERR_CTX_TYPE); |
73 |
|
} |
74 |
35 |
Expression exp = null; |
75 |
|
try { |
76 |
35 |
String evalExpr = inFct.matcher(expr). |
77 |
|
replaceAll("_builtin.isMember(_ALL_STATES, "); |
78 |
35 |
evalExpr = dataFct.matcher(evalExpr). |
79 |
|
replaceAll("_builtin.data("); |
80 |
35 |
exp = ExpressionFactory.createExpression(evalExpr); |
81 |
35 |
return exp.evaluate(getEffectiveContext(jexlCtx)); |
82 |
0 |
} catch (Exception e) { |
83 |
0 |
throw new SCXMLExpressionException(e); |
84 |
|
} |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
public Boolean evalCond(final Context ctx, final String expr) |
91 |
|
throws SCXMLExpressionException { |
92 |
40 |
if (expr == null) { |
93 |
0 |
return null; |
94 |
|
} |
95 |
40 |
JexlContext jexlCtx = null; |
96 |
40 |
if (ctx instanceof JexlContext) { |
97 |
40 |
jexlCtx = (JexlContext) ctx; |
98 |
|
} else { |
99 |
0 |
throw new SCXMLExpressionException(ERR_CTX_TYPE); |
100 |
|
} |
101 |
40 |
Expression exp = null; |
102 |
|
try { |
103 |
40 |
String evalExpr = inFct.matcher(expr). |
104 |
|
replaceAll("_builtin.isMember(_ALL_STATES, "); |
105 |
40 |
evalExpr = dataFct.matcher(evalExpr). |
106 |
|
replaceAll("_builtin.data("); |
107 |
40 |
exp = ExpressionFactory.createExpression(evalExpr); |
108 |
40 |
return (Boolean) exp.evaluate(getEffectiveContext(jexlCtx)); |
109 |
0 |
} catch (Exception e) { |
110 |
0 |
throw new SCXMLExpressionException(e); |
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
public Node evalLocation(final Context ctx, final String expr) |
118 |
|
throws SCXMLExpressionException { |
119 |
9 |
if (expr == null) { |
120 |
0 |
return null; |
121 |
|
} |
122 |
9 |
JexlContext jexlCtx = null; |
123 |
9 |
if (ctx instanceof JexlContext) { |
124 |
9 |
jexlCtx = (JexlContext) ctx; |
125 |
|
} else { |
126 |
0 |
throw new SCXMLExpressionException(ERR_CTX_TYPE); |
127 |
|
} |
128 |
9 |
Expression exp = null; |
129 |
|
try { |
130 |
9 |
String evalExpr = inFct.matcher(expr). |
131 |
|
replaceAll("_builtin.isMember(_ALL_STATES, "); |
132 |
9 |
evalExpr = dataFct.matcher(evalExpr). |
133 |
|
replaceFirst("_builtin.dataNode("); |
134 |
9 |
evalExpr = dataFct.matcher(evalExpr). |
135 |
|
replaceAll("_builtin.data("); |
136 |
9 |
exp = ExpressionFactory.createExpression(evalExpr); |
137 |
9 |
return (Node) exp.evaluate(getEffectiveContext(jexlCtx)); |
138 |
2 |
} catch (Exception e) { |
139 |
2 |
throw new SCXMLExpressionException(e); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
public Context newContext(final Context parent) { |
151 |
106 |
return new JexlContext(parent); |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
private JexlContext getEffectiveContext(final JexlContext nodeCtx) { |
164 |
84 |
List contexts = new ArrayList(); |
165 |
|
|
166 |
84 |
JexlContext currentCtx = nodeCtx; |
167 |
317 |
while (currentCtx != null) { |
168 |
233 |
contexts.add(currentCtx); |
169 |
233 |
currentCtx = (JexlContext) currentCtx.getParent(); |
170 |
|
} |
171 |
84 |
Map vars = new HashMap(); |
172 |
|
|
173 |
317 |
for (int i = contexts.size() - 1; i > -1; i--) { |
174 |
233 |
vars.putAll(((JexlContext) contexts.get(i)).getVars()); |
175 |
|
} |
176 |
84 |
return new JexlContext(vars); |
177 |
|
} |
178 |
|
|
179 |
|
} |
180 |
|
|