Search in sources :

Example 11 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project commons-jexl by apache.

the class OptionalTest method test342.

@Test
public void test342() {
    JexlBuilder builder = new JexlBuilder();
    JexlUberspect uber = builder.create().getUberspect();
    JexlEngine jexl = builder.uberspect(new ReferenceUberspect(uber)).safe(false).create();
    JexlInfo info = new JexlInfo("test352", 1, 1);
    Thing thing = new Thing();
    JexlScript script;
    script = jexl.createScript(info.at(53, 1), "thing.name.length()", "thing");
    Object result = script.execute(null, thing);
    Assert.assertNull(result);
    thing.name = "foo";
    result = script.execute(null, thing);
    Assert.assertEquals(3, result);
    try {
        script = jexl.createScript(info.at(62, 1), "thing.name.size()", "thing");
        result = script.execute(null, thing);
        Assert.fail("should have thrown");
    } catch (JexlException.Method xmethod) {
        Assert.assertEquals("size", xmethod.getDetail());
        Assert.assertEquals("test352@62:11 unsolvable function/method 'size'", xmethod.getMessage());
    }
    try {
        script = jexl.createScript(info.at(71, 1), "thing.name?.size()", "thing");
        result = script.execute(null, thing);
    } catch (JexlException.Method xmethod) {
        Assert.fail("should not have thrown");
    }
    thing.name = null;
    script = jexl.createScript(info, "thing.names.size()", "thing");
    result = script.execute(null, thing);
    Assert.assertNull(result);
    thing.name = "froboz";
    script = jexl.createScript(info, "thing.names", "thing");
    result = script.execute(null, thing);
    Assert.assertNotNull(result);
    script = jexl.createScript(info, "thing.names.size()", "thing");
    result = script.execute(null, thing);
    Assert.assertEquals(1, result);
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlException(org.apache.commons.jexl3.JexlException) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlUberspect(org.apache.commons.jexl3.introspection.JexlUberspect) JexlScript(org.apache.commons.jexl3.JexlScript) JexlInfo(org.apache.commons.jexl3.JexlInfo) Test(org.junit.Test)

Example 12 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project commons-jexl by apache.

the class JexlParser method throwAmbiguousException.

/**
 * Throws Ambiguous exception.
 * <p>Seeks the end of the ambiguous statement to recover.
 * @param node the first token in ambiguous expression
 * @throws JexlException.Ambiguous in all cases
 */
protected void throwAmbiguousException(final JexlNode node) {
    final JexlInfo begin = node.jexlInfo();
    final Token t = getToken(0);
    final JexlInfo end = info.at(t.beginLine, t.endColumn);
    final String msg = readSourceLine(source, end.getLine());
    throw new JexlException.Ambiguous(begin, end, msg);
}
Also used : JexlInfo(org.apache.commons.jexl3.JexlInfo)

Example 13 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project commons-jexl by apache.

the class JexlParser method throwFeatureException.

/**
 * Throws a feature exception.
 * @param feature the feature code
 * @param trigger the token that triggered it
 * @throws JexlException.Parsing if actual error token can not be found
 * @throws JexlException.Feature in all other cases
 */
protected void throwFeatureException(final int feature, Token trigger) {
    Token token = trigger;
    if (token == null) {
        token = this.getToken(0);
        if (token == null) {
            throw new JexlException.Parsing(null, JexlFeatures.stringify(feature));
        }
    }
    final JexlInfo xinfo = info.at(token.beginLine, token.beginColumn);
    throwFeatureException(feature, xinfo);
}
Also used : JexlInfo(org.apache.commons.jexl3.JexlInfo)

Example 14 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project commons-jexl by apache.

the class JexlNode method jexlInfo.

/**
 * Gets the associated JexlInfo instance.
 *
 * @return the info
 */
public JexlInfo jexlInfo() {
    JexlInfo info = null;
    JexlNode node = this;
    while (node != null) {
        if (node.jjtGetValue() instanceof JexlInfo) {
            info = (JexlInfo) node.jjtGetValue();
            break;
        }
        node = node.jjtGetParent();
    }
    if (lc >= 0) {
        final int c = lc & 0xfff;
        final int l = lc >> 0xc;
        // at least an info with line/column number
        return info != null ? info.at(info.getLine() + l - 1, c) : new JexlInfo(null, l, c);
    }
    // weird though; no jjSetFirstToken(...) ever called?
    return info;
}
Also used : JexlInfo(org.apache.commons.jexl3.JexlInfo)

Example 15 with JexlInfo

use of org.apache.commons.jexl3.JexlInfo in project commafeed by Athou.

the class FeedEntryFilteringService method initEngine.

private static JexlEngine initEngine() {
    // classloader that prevents object creation
    ClassLoader cl = new ClassLoader() {

        @Override
        protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
            return null;
        }
    };
    // uberspect that prevents access to .class and .getClass()
    Uberspect uberspect = new UberspectImpl(LogFactory.getLog(JexlEngine.class)) {

        @Override
        public JexlPropertyGet getPropertyGet(Object obj, Object identifier, JexlInfo info) {
            if ("class".equals(identifier)) {
                return null;
            }
            return super.getPropertyGet(obj, identifier, info);
        }

        @Override
        public JexlMethod getMethod(Object obj, String method, Object[] args, JexlInfo info) {
            if ("getClass".equals(method)) {
                return null;
            }
            return super.getMethod(obj, method, args, info);
        }
    };
    JexlEngine engine = new JexlEngine(uberspect, null, null, null);
    engine.setStrict(true);
    engine.setClassLoader(cl);
    return engine;
}
Also used : UberspectImpl(org.apache.commons.jexl2.introspection.UberspectImpl) JexlEngine(org.apache.commons.jexl2.JexlEngine) JexlInfo(org.apache.commons.jexl2.JexlInfo) Uberspect(org.apache.commons.jexl2.introspection.Uberspect)

Aggregations

JexlInfo (org.apache.commons.jexl3.JexlInfo)13 JexlException (org.apache.commons.jexl3.JexlException)7 IOException (java.io.IOException)2 JexlFeatures (org.apache.commons.jexl3.JexlFeatures)2 JexlScript (org.apache.commons.jexl3.JexlScript)2 JexlMethod (org.apache.commons.jexl3.introspection.JexlMethod)2 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)2 Test (org.junit.Test)2 Matcher (java.util.regex.Matcher)1 JexlEngine (org.apache.commons.jexl2.JexlEngine)1 JexlInfo (org.apache.commons.jexl2.JexlInfo)1 Uberspect (org.apache.commons.jexl2.introspection.Uberspect)1 UberspectImpl (org.apache.commons.jexl2.introspection.UberspectImpl)1 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)1 JexlEngine (org.apache.commons.jexl3.JexlEngine)1 LexicalScope (org.apache.commons.jexl3.internal.LexicalScope)1 Scope (org.apache.commons.jexl3.internal.Scope)1 JexlUberspect (org.apache.commons.jexl3.introspection.JexlUberspect)1 JexlNode (org.apache.commons.jexl3.parser.JexlNode)1 Parser (org.apache.commons.jexl3.parser.Parser)1