use of org.apache.commons.jexl3.internal.Script in project nexus-public by sonatype.
the class DatastoreCselToSqlTest method orTest.
@Test
public void orTest() {
final ASTJexlScript script = jexlEngine.parseExpression("a==\"woof\" || b==\"meow\"");
script.childrenAccept(underTest, builder);
assertThat(builder.getQueryString(), is("a_alias = :param_0 or b_alias = :param_1"));
assertThat(builder.getQueryParameters().size(), is(2));
assertThat(builder.getQueryParameters().get("param_0"), is("woof"));
assertThat(builder.getQueryParameters().get("param_1"), is("meow"));
}
use of org.apache.commons.jexl3.internal.Script in project nexus-public by sonatype.
the class DatastoreCselToSqlTest method notEqualTest.
@Test
public void notEqualTest() {
final ASTJexlScript script = jexlEngine.parseExpression("a != \"woof\"");
script.childrenAccept(underTest, builder);
assertThat(builder.getQueryString(), is("(a_alias is null or a_alias <> :param_0)"));
assertThat(builder.getQueryParameters().size(), is(1));
assertThat(builder.getQueryParameters().get("param_0"), is("woof"));
}
use of org.apache.commons.jexl3.internal.Script in project nexus-public by sonatype.
the class DatastoreCselToSqlTest method andTest.
@Test
public void andTest() {
final ASTJexlScript script = jexlEngine.parseExpression("a==\"woof\" && b==\"meow\"");
script.childrenAccept(underTest, builder);
assertThat(builder.getQueryString(), is("a_alias = :param_0 and b_alias = :param_1"));
assertThat(builder.getQueryParameters().size(), is(2));
assertThat(builder.getQueryParameters().get("param_0"), is("woof"));
assertThat(builder.getQueryParameters().get("param_1"), is("meow"));
}
use of org.apache.commons.jexl3.internal.Script 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.internal.Script in project commons-jexl by apache.
the class Engine method setProperty.
@Override
public void setProperty(JexlContext context, final Object bean, final String expr, final Object value) {
// synthesize expr using register
String src = trimSource(expr);
src = "#0" + (src.charAt(0) == '[' ? "" : ".") + src + "=" + "#1";
try {
final Scope scope = new Scope(null, "#0", "#1");
final ASTJexlScript script = parse(null, PROPERTY_FEATURES, src, scope);
final JexlNode node = script.jjtGetChild(0);
final Frame frame = script.createFrame(bean, value);
final Interpreter interpreter = createInterpreter(context != null ? context : EMPTY_CONTEXT, frame, options);
interpreter.visitLexicalNode(node, null);
} catch (final JexlException xjexl) {
if (silent) {
if (logger.isWarnEnabled()) {
logger.warn(xjexl.getMessage(), xjexl.getCause());
}
return;
}
throw xjexl.clean();
}
}
Aggregations