Search in sources :

Example 1 with Debugger

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

the class TemplateDebugger method debug.

/**
 * Position the debugger on the root of a template script.
 * @param jt the template
 * @return true if the template was a {@link TemplateScript} instance, false otherwise
 */
public boolean debug(final JxltEngine.Template jt) {
    if (!(jt instanceof TemplateScript)) {
        return false;
    }
    final TemplateScript ts = (TemplateScript) jt;
    // ensure expr is not null for templates
    this.exprs = ts.getExpressions() == null ? new TemplateExpression[0] : ts.getExpressions();
    this.script = ts.getScript();
    start = 0;
    end = 0;
    indentLevel = 0;
    builder.setLength(0);
    cause = script;
    final int num = script.jjtGetNumChildren();
    for (int i = 0; i < num; ++i) {
        final JexlNode child = script.jjtGetChild(i);
        acceptStatement(child, null);
    }
    // the last line
    if (builder.length() > 0 && builder.charAt(builder.length() - 1) != '\n') {
        builder.append('\n');
    }
    end = builder.length();
    return end > 0;
}
Also used : TemplateExpression(org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression) JexlNode(org.apache.commons.jexl3.parser.JexlNode)

Example 2 with Debugger

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

the class JXLTTest method testDbgEscapes.

@Test
public void testDbgEscapes() throws Exception {
    String[] srcs = new String[] { "jexl:print('hello\\'\\nworld')", "'hello\\tworld'", "'hello\\nworld'", "'hello\\fworld'", "'hello\\rworld'" };
    for (String src : srcs) {
        JexlScript script = ENGINE.createScript(src);
        Debugger dbg = new Debugger();
        dbg.debug(script);
        String msrc = dbg.toString();
        Assert.assertEquals(src, msrc);
    }
}
Also used : TemplateDebugger(org.apache.commons.jexl3.internal.TemplateDebugger) Debugger(org.apache.commons.jexl3.internal.Debugger) Test(org.junit.Test)

Example 3 with Debugger

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

the class PropertyAccessTest method testStringIdentifier.

@Test
public void testStringIdentifier() throws Exception {
    final Map<String, String> foo = new HashMap<String, String>();
    final JexlContext jc = new MapContext();
    jc.set("foo", foo);
    foo.put("q u u x", "456");
    JexlExpression e = JEXL.createExpression("foo.\"q u u x\"");
    Object result = e.evaluate(jc);
    Assert.assertEquals("456", result);
    e = JEXL.createExpression("foo.'q u u x'");
    result = e.evaluate(jc);
    Assert.assertEquals("456", result);
    JexlScript s = JEXL.createScript("foo.\"q u u x\"");
    result = s.execute(jc);
    Assert.assertEquals("456", result);
    s = JEXL.createScript("foo.'q u u x'");
    result = s.execute(jc);
    Assert.assertEquals("456", result);
    final Debugger dbg = new Debugger();
    dbg.debug(e);
    final String dbgdata = dbg.toString();
    Assert.assertEquals("foo.'q u u x'", dbgdata);
}
Also used : Debugger(org.apache.commons.jexl3.internal.Debugger) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with Debugger

use of org.apache.commons.jexl3.internal.Debugger 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);
        }
    }
}
Also used : ASTJexlScript(org.apache.commons.jexl3.parser.ASTJexlScript) JexlScript(org.apache.commons.jexl3.JexlScript) ASTJexlScript(org.apache.commons.jexl3.parser.ASTJexlScript) JexlException(org.apache.commons.jexl3.JexlException) JexlNode(org.apache.commons.jexl3.parser.JexlNode) JexlFeatures(org.apache.commons.jexl3.JexlFeatures) Map(java.util.Map) JexlEngine(org.apache.commons.jexl3.JexlEngine)

Example 5 with Debugger

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

the class JXLTTest method testInheritedDebugger.

@Test
public void testInheritedDebugger() throws Exception {
    final String src = "if ($A) { $B + 1; } else { $C - 2 }";
    final JexlEngine jexl = JXLT.getEngine();
    final JexlScript script = jexl.createScript(src);
    final Debugger sd = new Debugger();
    final String rscript = sd.debug(script) ? sd.toString() : null;
    Assert.assertNotNull(rscript);
    final TemplateDebugger td = new TemplateDebugger();
    final String refactored = td.debug(script) ? td.toString() : null;
    Assert.assertNotNull(refactored);
    Assert.assertEquals(refactored, rscript);
}
Also used : TemplateDebugger(org.apache.commons.jexl3.internal.TemplateDebugger) Debugger(org.apache.commons.jexl3.internal.Debugger) TemplateDebugger(org.apache.commons.jexl3.internal.TemplateDebugger) Test(org.junit.Test)

Aggregations

Debugger (org.apache.commons.jexl3.internal.Debugger)3 Test (org.junit.Test)3 TemplateDebugger (org.apache.commons.jexl3.internal.TemplateDebugger)2 JexlNode (org.apache.commons.jexl3.parser.JexlNode)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JexlEngine (org.apache.commons.jexl3.JexlEngine)1 JexlException (org.apache.commons.jexl3.JexlException)1 JexlFeatures (org.apache.commons.jexl3.JexlFeatures)1 JexlScript (org.apache.commons.jexl3.JexlScript)1 TemplateExpression (org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression)1 ASTJexlScript (org.apache.commons.jexl3.parser.ASTJexlScript)1