Search in sources :

Example 56 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class WhileStatement method execute.

@Override
public void execute(Context ctx) {
    if (this.hasGoto) {
        while (true) {
            Object result = exp.evaluate(ctx);
            if (result instanceof Boolean) {
                if ((Boolean) result) {
                    whileBody.execute(ctx);
                    switch(ctx.gotoFlag) {
                        case IGoto.NORMAL:
                            break;
                        case IGoto.CONTINUE:
                            ctx.gotoFlag = IGoto.NORMAL;
                            continue;
                        case IGoto.RETURN:
                            return;
                        case IGoto.BREAK:
                            ctx.gotoFlag = IGoto.NORMAL;
                            return;
                    }
                } else {
                    break;
                }
            } else {
                BeetlException be = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR);
                be.pushToken(exp.token);
                throw be;
            }
        }
    } else {
        while (true) {
            Object result = exp.evaluate(ctx);
            if (result instanceof Boolean) {
                if ((Boolean) result) {
                    whileBody.execute(ctx);
                } else {
                    break;
                }
            } else {
                BeetlException be = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR);
                be.pushToken(exp.token);
                throw be;
            }
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException)

Example 57 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class SwitchStatement method execute.

@Override
public void execute(Context ctx) {
    Object o = value.evaluate(ctx);
    if (o == null) {
        BeetlException ex = new BeetlException(BeetlException.NULL);
        ex.pushToken(value.token);
        throw ex;
    }
    boolean isMatch = false;
    for (Expression exp : condtionsList) {
        if (isMatch || ALU.equals(o, exp.evaluate(ctx))) {
            isMatch = true;
            BlockStatement block = map.get(exp);
            if (block != null) {
                block.execute(ctx);
                switch(ctx.gotoFlag) {
                    case IGoto.NORMAL:
                        break;
                    case IGoto.RETURN:
                        return;
                    case IGoto.BREAK:
                        ctx.gotoFlag = IGoto.NORMAL;
                        return;
                }
            } else {
                // 匹配下一个Block
                continue;
            }
        } else {
            continue;
        }
    }
    if (!isMatch && defaultBlock != null) {
        defaultBlock.execute(ctx);
        switch(ctx.gotoFlag) {
            case IGoto.NORMAL:
                break;
            case IGoto.RETURN:
                return;
            case IGoto.BREAK:
                ctx.gotoFlag = IGoto.NORMAL;
                return;
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException)

Example 58 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class TagVarBindingStatement method runTag.

@Override
protected void runTag(Tag tag, Context ctx) {
    try {
        if (tag instanceof HTMLTagVarBindingWrapper) {
            HTMLTagVarBindingWrapper htmlTag = (HTMLTagVarBindingWrapper) tag;
            // 初始化
            Object[] vars = htmlTag.bindVars();
            if (vars != null) {
                for (int i = 0; i < vars.length; i++) {
                    ctx.vars[varIndexs[i].getVarIndex()] = vars[i];
                }
            }
            LinkedHashMap<String, Integer> indexMap = new LinkedHashMap<String, Integer>(this.varIndexs.length);
            for (VarDefineNode node : this.varIndexs) {
                indexMap.put(node.token.text, node.varIndex);
            }
            htmlTag.mapName2Index(indexMap);
        } else {
            BeetlException be = new BeetlException(BeetlException.ERROR, "tag必须是HTMLTagVarBindingWrapper");
            be.pushToken(this.token);
            throw be;
        }
        tag.render();
    } catch (BeetlException ex) {
        // BeetlException异常时不要设置token,因为抛出的地方已经设置了
        throw ex;
    } catch (RuntimeException ex) {
        BeetlException be = new BeetlException(BeetlException.ERROR, "tag执行抛错", ex);
        be.pushToken(token);
        throw be;
    }
}
Also used : HTMLTagVarBindingWrapper(org.beetl.ext.tag.HTMLTagVarBindingWrapper) BeetlException(org.beetl.core.exception.BeetlException) LinkedHashMap(java.util.LinkedHashMap)

Example 59 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class TernaryExpression method evaluate.

public Object evaluate(Context ctx) {
    Object value = condtion.evaluate(ctx);
    if (value == null) {
        BeetlException be = new BeetlException(BeetlException.NULL);
        be.pushToken(condtion.token);
        throw be;
    } else if (!(value instanceof Boolean)) {
        BeetlException be = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR);
        be.pushToken(condtion.token);
        throw be;
    }
    boolean cond = (Boolean) value;
    if (cond) {
        return a.evaluate(ctx);
    } else {
        if (b != null) {
            return b.evaluate(ctx);
        } else {
            return null;
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException)

Example 60 with BeetlException

use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.

the class VarRef method evaluateUntilLast.

/**
 * 计算所有表达式,知道最后一值,用于a.b[xx].c = 1  赋值,只计算到a.b[xx]
 * @param ctx
 * @return
 */
public Object evaluateUntilLast(Context ctx) {
    if (attributes.length == 0) {
        // 不可能发生,除非beetl写错了,先放在着
        throw new RuntimeException();
    }
    Object value = ctx.vars[varIndex];
    if (value == Context.NOT_EXIST_OBJECT) {
        BeetlException ex = new BeetlException(BeetlException.VAR_NOT_DEFINED);
        ex.pushToken(this.firstToken);
        throw ex;
    }
    if (value == null) {
        BeetlException ex = new BeetlException(BeetlException.NULL);
        ex.pushToken(this.firstToken);
        throw ex;
    }
    for (int i = 0; i < attributes.length - 1; i++) {
        VarAttribute attr = attributes[i];
        if (value == null) {
            BeetlException be = new BeetlException(BeetlException.NULL, "空指针");
            if (i == 0) {
                be.pushToken(this.firstToken);
            } else {
                be.pushToken(attributes[i - 1].token);
            }
            throw be;
        }
        try {
            value = attr.evaluate(ctx, value);
        } catch (BeetlException ex) {
            ex.pushToken(attr.token);
            throw ex;
        } catch (RuntimeException ex) {
            BeetlException be = new BeetlException(BeetlException.ATTRIBUTE_INVALID, "属性访问出错", ex);
            be.pushToken(attr.token);
            throw be;
        }
    }
    return value;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException)

Aggregations

BeetlException (org.beetl.core.exception.BeetlException)60 GrammarToken (org.beetl.core.statement.GrammarToken)10 IOException (java.io.IOException)7 BeetlParserException (org.beetl.core.exception.BeetlParserException)6 Expression (org.beetl.core.statement.Expression)6 AndExpression (org.beetl.core.statement.AndExpression)5 ArthExpression (org.beetl.core.statement.ArthExpression)5 CompareExpression (org.beetl.core.statement.CompareExpression)5 ContentBodyExpression (org.beetl.core.statement.ContentBodyExpression)5 FormatExpression (org.beetl.core.statement.FormatExpression)5 FunctionExpression (org.beetl.core.statement.FunctionExpression)5 IncDecExpression (org.beetl.core.statement.IncDecExpression)5 JsonArrayExpression (org.beetl.core.statement.JsonArrayExpression)5 JsonMapExpression (org.beetl.core.statement.JsonMapExpression)5 NativeCallExpression (org.beetl.core.statement.NativeCallExpression)5 NegExpression (org.beetl.core.statement.NegExpression)5 NotBooleanExpression (org.beetl.core.statement.NotBooleanExpression)5 OrExpression (org.beetl.core.statement.OrExpression)5 StatementExpression (org.beetl.core.statement.StatementExpression)5 TernaryExpression (org.beetl.core.statement.TernaryExpression)5