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