Search in sources :

Example 1 with TemplateExpression

use of org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression 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 TemplateExpression

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

the class TemplateDebugger method acceptStatement.

@Override
protected Object acceptStatement(final JexlNode child, final Object data) {
    // if not really a template, must use super impl
    if (exprs == null) {
        return super.acceptStatement(child, data);
    }
    final TemplateExpression te = getPrintStatement(child);
    if (te != null) {
        // if statement is a jexl:print(...), may need to prepend '\n'
        newJxltLine();
        return visit(te, data);
    }
    // if statement is not a jexl:print(...), need to prepend '$$'
    newJexlLine();
    return super.acceptStatement(child, data);
}
Also used : TemplateExpression(org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression)

Example 3 with TemplateExpression

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

the class TemplateInterpreter method printComposite.

/**
 * Prints a composite expression.
 * @param composite the composite expression
 */
private void printComposite(final TemplateEngine.CompositeExpression composite) {
    final TemplateEngine.TemplateExpression[] cexprs = composite.exprs;
    Object value;
    for (final TemplateExpression cexpr : cexprs) {
        value = cexpr.evaluate(this);
        doPrint(cexpr.getInfo(), value);
    }
}
Also used : TemplateExpression(org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression)

Example 4 with TemplateExpression

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

the class TemplateScript method prepare.

@Override
public TemplateScript prepare(final JexlContext context) {
    final Engine jexl = jxlt.getEngine();
    final JexlOptions options = jexl.evalOptions(script, context);
    final Frame frame = script.createFrame((Object[]) null);
    final TemplateInterpreter.Arguments targs = new TemplateInterpreter.Arguments(jxlt.getEngine()).context(context).options(options).frame(frame);
    final Interpreter interpreter = jexl.createTemplateInterpreter(targs);
    final TemplateExpression[] immediates = new TemplateExpression[exprs.length];
    for (int e = 0; e < exprs.length; ++e) {
        try {
            immediates[e] = exprs[e].prepare(interpreter);
        } catch (final JexlException xjexl) {
            final JexlException xuel = TemplateEngine.createException(xjexl.getInfo(), "prepare", exprs[e], xjexl);
            if (jexl.isSilent()) {
                if (jexl.logger.isWarnEnabled()) {
                    jexl.logger.warn(xuel.getMessage(), xuel.getCause());
                }
                return null;
            }
            throw xuel;
        }
    }
    return new TemplateScript(jxlt, prefix, source, script, immediates);
}
Also used : JexlException(org.apache.commons.jexl3.JexlException) JexlOptions(org.apache.commons.jexl3.JexlOptions) TemplateExpression(org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression) JxltEngine(org.apache.commons.jexl3.JxltEngine)

Example 5 with TemplateExpression

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

the class TemplateDebugger method getPrintStatement.

/**
 * In a template, any statement that is not 'jexl:print(n)' must be prefixed by "$$".
 * @param child the node to check
 * @return the expression number or -1 if the node is not a jexl:print
 */
private TemplateExpression getPrintStatement(final JexlNode child) {
    if (exprs != null && child instanceof ASTFunctionNode) {
        final ASTFunctionNode node = (ASTFunctionNode) child;
        final ASTIdentifier ns = (ASTIdentifier) node.jjtGetChild(0);
        final JexlNode args = node.jjtGetChild(1);
        if ("jexl".equals(ns.getNamespace()) && "print".equals(ns.getName()) && args.jjtGetNumChildren() == 1 && args.jjtGetChild(0) instanceof ASTNumberLiteral) {
            final ASTNumberLiteral exprn = (ASTNumberLiteral) args.jjtGetChild(0);
            final int n = exprn.getLiteral().intValue();
            if (n >= 0 && n < exprs.length) {
                return exprs[n];
            }
        }
    }
    return null;
}
Also used : ASTFunctionNode(org.apache.commons.jexl3.parser.ASTFunctionNode) ASTIdentifier(org.apache.commons.jexl3.parser.ASTIdentifier) JexlNode(org.apache.commons.jexl3.parser.JexlNode) ASTNumberLiteral(org.apache.commons.jexl3.parser.ASTNumberLiteral)

Aggregations

TemplateExpression (org.apache.commons.jexl3.internal.TemplateEngine.TemplateExpression)4 JexlException (org.apache.commons.jexl3.JexlException)2 JexlNode (org.apache.commons.jexl3.parser.JexlNode)2 IOException (java.io.IOException)1 JexlInfo (org.apache.commons.jexl3.JexlInfo)1 JexlOptions (org.apache.commons.jexl3.JexlOptions)1 JxltEngine (org.apache.commons.jexl3.JxltEngine)1 ASTFunctionNode (org.apache.commons.jexl3.parser.ASTFunctionNode)1 ASTIdentifier (org.apache.commons.jexl3.parser.ASTIdentifier)1 ASTNumberLiteral (org.apache.commons.jexl3.parser.ASTNumberLiteral)1