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;
}
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);
}
}
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);
}
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);
}
}
}
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);
}
Aggregations