use of org.apache.commons.jexl3.JexlFeatures in project commons-jexl by apache.
the class LexicalTest method testInternalLexicalFeatures.
@Test
public void testInternalLexicalFeatures() throws Exception {
final String str = "42";
final JexlFeatures f = new JexlFeatures();
f.lexical(true);
f.lexicalShade(true);
final JexlEngine jexl = new JexlBuilder().features(f).create();
final JexlScript e = jexl.createScript(str);
final VarContext vars = new VarContext();
final JexlOptions opts = vars.getEngineOptions();
// so we can see the effect of features on options
opts.setSharedInstance(true);
final Script script = (Script) e;
final JexlFeatures features = script.getFeatures();
Assert.assertTrue(features.isLexical());
Assert.assertTrue(features.isLexicalShade());
final Object result = e.execute(vars);
Assert.assertEquals(42, result);
Assert.assertTrue(opts.isLexical());
Assert.assertTrue(opts.isLexicalShade());
}
use of org.apache.commons.jexl3.JexlFeatures in project commons-jexl by apache.
the class Util method debuggerCheck.
/**
* Will force testing the debugger for each derived test class by
* recreating each expression from the JexlNode in the JexlEngine cache &
* testing them for equality with the origin.
* @throws Exception
*/
public static void debuggerCheck(final JexlEngine ijexl) throws Exception {
final Engine jexl = (Engine) ijexl;
// without a cache, nothing to check
if (jexl == null || jexl.cache == null) {
return;
}
final Engine jdbg = new Engine();
jdbg.parser.allowRegisters(true);
final Debugger dbg = new Debugger();
// iterate over all expression in
for (final Map.Entry<Source, ASTJexlScript> entry : jexl.cache.entries()) {
final JexlNode node = entry.getValue();
// recreate expr string from AST
dbg.debug(node);
final String expressiondbg = dbg.toString();
final JexlFeatures features = entry.getKey().getFeatures();
// recreate expr from string
try {
final Script exprdbg = jdbg.createScript(features, null, expressiondbg, null);
// make arg cause become the root cause
JexlNode root = exprdbg.script;
while (root.jjtGetParent() != null) {
root = root.jjtGetParent();
}
// test equality
final String reason = checkEquals(root, node);
if (reason != null) {
throw new RuntimeException("check equal failed: " + expressiondbg + " /**** " + reason + " **** */ " + entry.getKey());
}
} catch (final JexlException xjexl) {
throw new RuntimeException("check parse failed: " + expressiondbg + " /*********/ " + entry.getKey(), xjexl);
}
}
}
use of org.apache.commons.jexl3.JexlFeatures in project commons-jexl by apache.
the class Engine method parse.
/**
* Parses an expression.
*
* @param info information structure
* @param parsingf the set of parsing features
* @param src the expression to parse
* @param scope the script frame
* @return the parsed tree
* @throws JexlException if any error occurred during parsing
*/
protected ASTJexlScript parse(final JexlInfo info, final JexlFeatures parsingf, final String src, final Scope scope) {
final boolean cached = src.length() < cacheThreshold && cache != null;
JexlFeatures features = parsingf != null ? parsingf : DEFAULT_FEATURES;
// if (features.getNameSpaces().isEmpty() && !functions.isEmpty()) {
// features = new JexlFeatures(features).nameSpaces(functions.keySet());
// }
final Source source = cached ? new Source(features, src) : null;
ASTJexlScript script;
if (source != null) {
script = cache.get(source);
if (script != null) {
final Scope f = script.getScope();
if ((f == null && scope == null) || (f != null && f.equals(scope))) {
return script;
}
}
}
final JexlInfo ninfo = info == null && debug ? createInfo() : info;
// if parser not in use...
if (parsing.compareAndSet(false, true)) {
try {
// lets parse
script = parser.parse(ninfo, features, src, scope);
} finally {
// no longer in use
parsing.set(false);
}
} else {
// ...otherwise parser was in use, create a new temporary one
final Parser lparser = new Parser(new StringProvider(";"));
script = lparser.parse(ninfo, features, src, scope);
}
if (source != null) {
cache.put(source, script);
}
return script;
}
use of org.apache.commons.jexl3.JexlFeatures in project commons-jexl by apache.
the class Engine method createScript.
@Override
public Script createScript(final JexlFeatures features, final JexlInfo info, final String scriptText, final String... names) {
if (scriptText == null) {
throw new NullPointerException("source is null");
}
final String source = trimSource(scriptText);
final Scope scope = names == null || names.length == 0 ? null : new Scope(null, names);
final JexlFeatures ftrs = features == null ? scriptFeatures : features;
final ASTJexlScript tree = parse(info, ftrs, source, scope);
return new Script(this, source, tree);
}
use of org.apache.commons.jexl3.JexlFeatures in project commons-jexl by apache.
the class JexlTest method testAssignment.
/**
* Test assignment.
* @throws Exception
*/
@Test
public void testAssignment() throws Exception {
final JexlContext jc = new MapContext();
jc.set("aString", "Hello");
final Foo foo = new Foo();
jc.set("foo", foo);
final Parser parser = new Parser(";");
parser.parse(null, new JexlFeatures().register(false), "aString = 'World';", null);
assertExpression(jc, "hello = 'world'", "world");
Assert.assertEquals("hello variable not changed", "world", jc.get("hello"));
assertExpression(jc, "result = 1 + 1", new Integer(2));
Assert.assertEquals("result variable not changed", new Integer(2), jc.get("result"));
// todo: make sure properties can be assigned to, fall back to flat var if no property
// assertExpression(jc, "foo.property1 = '99'", "99");
// Assert.assertEquals("property not set", "99", foo.getProperty1());
}
Aggregations