1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
package org.uscxml.dm.jexl;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlException;
import org.apache.commons.jexl3.JexlExpression;
import org.apache.commons.jexl3.MapContext;
import org.uscxml.Data;
import org.uscxml.DataList;
import org.uscxml.DataMap;
import org.uscxml.DataModel;
import org.uscxml.DataModelExtension;
import org.uscxml.ErrorEvent;
import org.uscxml.Event;
import org.uscxml.StringList;
import org.uscxml.StringVector;
public class JexlDataModel extends DataModel {
protected static final JexlEngine jexl = new JexlBuilder().cache(512).strict(true).silent(false).create();
public MapContext ctx = new MapContext();
@Override
public void addExtension(DataModelExtension ext) {
throw new UnsupportedOperationException("Cannot add extensions to the jexl datamodel");
}
@Override
public StringList getNames() {
StringList names = new StringList();
names.add("jexl");
return names;
}
@Override
public DataModel create() {
JexlDataModel dm = new JexlDataModel();
return dm;
}
@Override
public boolean isValidSyntax(String expr) {
try {
jexl.createExpression(expr);
return true;
} catch(JexlException e) {
return false;
}
}
@Override
public void setEvent(Event event) {
ctx.set("_event", event);
}
@Override
public Data getAsData(String content) {
try {
JexlExpression ex = jexl.createExpression(content);
return getJexlObjectAsData(ex.evaluate(ctx));
} catch(Exception e) {
}
return null;
}
@Override
public Data evalAsData(String content) {
JexlExpression expr = jexl.createExpression(content);
Data d = getJexlObjectAsData(expr.evaluate(ctx));
// d.setAtom(expr.getParsedText());
// d.setType(Data.Type.VERBATIM);
return d;
}
@Override
public boolean evalAsBool(String expr) {
try {
JexlExpression ex = jexl.createExpression("!!" + expr);
Object result = ex.evaluate(ctx);
return (Boolean) result;
} catch(Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public long getLength(String expr) {
try {
JexlExpression ex = jexl.createExpression(expr);
Object res = ex.evaluate(ctx);
return Array.getLength(res);
} catch(Exception e) {
throw new ErrorEvent("Cannot evaluate '" + expr + "' as an array: " + e.getMessage());
}
}
@Override
public void setForeach(String item, String array, String index, long iteration) {
Object res = null;
try {
JexlExpression ex = jexl.createExpression(array);
res = ex.evaluate(ctx);
} catch(Exception e) {
throw new ErrorEvent("Cannot evaluate '" + array + "' as an array: " + e.getMessage());
}
try {
JexlExpression ex = jexl.createExpression(item + "==" + item + ";");
ex.evaluate(ctx);
ctx.set(item, Array.get(res, (int) iteration));
} catch(Exception e) {
throw new ErrorEvent("Cannot set item '" + item + "' to current item: " + e.getMessage());
}
try {
if (index.length() > 0) {
ctx.set(index, iteration);
}
} catch(Exception e) {
throw new ErrorEvent("Cannot set index '" + index + "' to current index: " + e.getMessage());
}
}
@Override
public void assign(String location, Data data) {
try {
ctx.set(location, getDataAsJexlObject(data));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void init(String location, Data data) {
ctx.set(location, null);
assign(location, data);
}
@Override
public boolean isDeclared(String expr) {
try {
JexlExpression ex = jexl.createExpression(expr);
Object result = ex.evaluate(ctx);
return (Boolean) result;
} catch(JexlException e) {
}
return false;
}
protected Object evalAsObject(String expr) {
try {
JexlExpression ex = jexl.createExpression(expr);
return (ex.evaluate(ctx));
} catch(JexlException e) {
}
return null;
}
protected Data getJexlObjectAsData(Object obj) {
Data d = new Data();
if (obj.getClass().isArray()) {
int length = Array.getLength(obj);
for (int i = 0; i < length; i ++) {
d.getArray().add(getJexlObjectAsData(Array.get(obj, i)));
}
} else if (obj.getClass().isPrimitive() || isWrapperType(obj.getClass())) {
String val = obj.toString();
try {
Integer.parseInt(obj.toString());
d.setAtom(obj.toString());
d.setType(Data.Type.INTERPRETED);
return d;
} catch(NumberFormatException e) {}
try {
Double.parseDouble(obj.toString());
d.setAtom(obj.toString());
d.setType(Data.Type.INTERPRETED);
return d;
} catch(NumberFormatException e) {}
d.setAtom(obj.toString());
d.setType(Data.Type.VERBATIM);
return d;
} else {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field: fields) {
Object newObj = null;
try {
field.get(newObj);
d.getCompound().set(field.getName(), getJexlObjectAsData(newObj));
}
catch (IllegalArgumentException e) {}
catch (IllegalAccessException e) {}
}
return d;
}
return d;
}
protected Object getDataAsJexlObject(Data data) {
if (data.getAtom().length() > 0) {
if (data.getType() == Data.Type.INTERPRETED) {
try {
JexlExpression exp = jexl.createExpression(data.getAtom());
return exp.evaluate(ctx);
} catch (Exception e) {
e.printStackTrace();
}
return new String(data.getAtom());
}
try {
JexlExpression exp = jexl.createExpression(data.getAtom());
return exp.evaluate(ctx);
} catch (Exception e) {
}
return new String("\"" + data.getAtom() + "\"");
} else if (data.getCompound().size() > 0) {
StringVector keys = data.getCompoundKeys();
DataMap dataCompound = data.getCompound();
Map<String, Object> objCompound = new HashMap<String, Object>();
for (int i = 0; i < keys.size(); i++) {
objCompound.put(keys.get(i), getDataAsJexlObject(dataCompound.get(keys.get(i))));
}
return objCompound;
} else if (data.getArray().size() > 0) {
DataList dataList = data.getArray();
List<Object> objList = new ArrayList<Object>((int) data.getArray().size());
for (int i = 0; i < dataList.size(); i++) {
objList.add(i, getDataAsJexlObject(dataList.get(i)));
}
return objList;
}
return new Object();
}
protected static boolean isWrapperType(Class<?> clazz) {
return WRAPPER_TYPES.contains(clazz);
}
private static final Set<Class<?>> WRAPPER_TYPES = getWrapperTypes();
private static Set<Class<?>> getWrapperTypes() {
Set<Class<?>> ret = new HashSet<Class<?>>();
ret.add(Boolean.class);
ret.add(Character.class);
ret.add(Byte.class);
ret.add(Short.class);
ret.add(Integer.class);
ret.add(Long.class);
ret.add(Float.class);
ret.add(Double.class);
ret.add(Void.class);
return ret;
}
}
|