Search in sources :

Example 16 with BeetlException

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

the class ALU method UnsupportedTypeException.

private static RuntimeException UnsupportedTypeException(final Object o1, final Object o2, final ASTNode node1, final ASTNode node2, String type) {
    BeetlException ex = new BeetlException(BeetlException.EXPRESSION_NOT_COMPATIBLE, o1.getClass() + type + o2.getClass());
    GrammarToken token = GrammarToken.createToken(node1.token.text + type + node2.token.text, node1.token.line);
    ex.pushToken(token);
    throw ex;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 17 with BeetlException

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

the class MapResourceLoader method getResource.

public Resource getResource(final String key) {
    return new Resource(key, this) {

        public Reader openReader() {
            String val = get(key);
            if (val == null) {
                BeetlException ex = new BeetlException(BeetlException.TEMPLATE_LOAD_ERROR);
                ex.pushResource(this);
                throw ex;
            }
            return new StringReader(val);
        }

        public boolean isModified() {
            return !autoCheck;
        }
    };
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) Resource(org.beetl.core.Resource) StringReader(java.io.StringReader)

Example 18 with BeetlException

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

the class CacheTag method render.

@Override
public void render() {
    try {
        String key = null;
        long refreshPeriod = 0;
        BodyContent cahcedObject = null;
        key = (String) this.args[0];
        if (this.args.length == 3) {
            boolean refreshNow = ((Boolean) this.args[2]).booleanValue();
            if (refreshNow) {
                cahcedObject = super.getBodyContent();
                cacheManager.setObject(key, cahcedObject, refreshPeriod);
                cahcedObject.fill(bw);
                return;
            }
        }
        if (this.args.length >= 2) {
            refreshPeriod = ((Number) this.args[1]).longValue();
        } else {
            // 默认1小时刷新一次
            refreshPeriod = 60 * 60;
        }
        if (refreshPeriod < 0) {
            cahcedObject = super.getBodyContent();
            cacheManager.setObject(key, cahcedObject, refreshPeriod);
            cahcedObject.fill(bw);
        } else {
            cahcedObject = (BodyContent) cacheManager.getObject(key);
            if (cahcedObject == null) {
                cahcedObject = super.getBodyContent();
                cacheManager.setObject(key, cahcedObject, refreshPeriod);
            }
            cahcedObject.fill(this.bw);
        }
        return;
    } catch (IOException ex) {
        if (!ctx.gt.getConf().isIgnoreClientIOError()) {
            throw new BeetlException(BeetlException.CLIENT_IO_ERROR_ERROR, "IO Error", ex);
        }
    }
}
Also used : BodyContent(org.beetl.core.BodyContent) BeetlException(org.beetl.core.exception.BeetlException) IOException(java.io.IOException)

Example 19 with BeetlException

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

the class ForStatement method execute.

public final void execute(Context ctx) {
    // idNode 是其后设置的
    int varIndex = ((IVarIndex) idNode).getVarIndex();
    Object collection = exp.evaluate(ctx);
    IteratorStatus it = null;
    if (collection == null) {
        if (!this.hasSafe) {
            BeetlException ex = new BeetlException(BeetlException.NULL);
            ex.pushToken(exp.token);
            throw ex;
        } else {
            it = new IteratorStatus(Collections.EMPTY_LIST);
        }
    } else {
        it = IteratorStatus.getIteratorStatusByType(collection, itType);
        if (it == null) {
            BeetlParserException ex = new BeetlParserException(BeetlParserException.COLLECTION_EXPECTED_ERROR, "实际类型是:" + collection.getClass());
            ex.pushToken(exp.token);
            throw ex;
        }
    }
    ctx.vars[varIndex + 1] = it;
    // 
    if (this.hasGoto) {
        while (it.hasNext()) {
            ctx.vars[varIndex] = it.next();
            forPart.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;
            }
        }
        if (!it.hasData()) {
            if (elseforPart != null)
                elseforPart.execute(ctx);
        }
        return;
    } else {
        while (it.hasNext()) {
            ctx.vars[varIndex] = it.next();
            forPart.execute(ctx);
        }
        if (!it.hasData()) {
            if (elseforPart != null)
                elseforPart.execute(ctx);
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlParserException(org.beetl.core.exception.BeetlParserException) IteratorStatus(org.beetl.core.IteratorStatus)

Example 20 with BeetlException

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

the class FunctionExpression method infer.

public void infer(InferContext inferCtx) {
    Function fn = inferCtx.gt.getFunction(name);
    if (fn == null) {
        Resource resource = getResource(inferCtx.gt, name);
        if (resource == null) {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_NOT_FOUND);
            ex.pushToken(token);
            throw ex;
        } else {
            fn = new FileFunctionWrapper(resource.getId());
        }
    }
    for (Expression exp : exps) {
        exp.infer(inferCtx);
    }
    // return type;
    Class c = null;
    if (fn instanceof SingleFunctionWrapper) {
        SingleFunctionWrapper singleWrapper = (SingleFunctionWrapper) fn;
        c = singleWrapper.getReturnType();
    } else if (fn instanceof MutipleFunctionWrapper) {
        try {
            Class[] parasType = new Class[exps.length];
            int i = 0;
            for (Expression exp : exps) {
                exp.infer(inferCtx);
                parasType[i++] = exp.getType().cls;
            }
            c = ((MutipleFunctionWrapper) fn).getReturnType(parasType);
        } catch (BeetlException ex) {
            ex.pushToken(token);
            throw ex;
        }
    } else {
        Method call = null;
        try {
            call = fn.getClass().getMethod("call", Object[].class, Context.class);
            c = call.getReturnType();
        } catch (NoSuchMethodException e) {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_INVALID);
            ex.pushToken(token);
            throw ex;
        } catch (SecurityException e) {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_INVALID);
            ex.pushToken(token);
            throw ex;
        }
    }
    Type lastType = new Type(c);
    if (vas == null) {
        this.type = lastType;
        return;
    } else {
        Type t = null;
        for (VarAttribute attr : vas) {
            inferCtx.temp = lastType;
            attr.infer(inferCtx);
            t = lastType;
            lastType = attr.type;
            attr.type = t;
        }
        this.type = lastType;
    }
    if (safeExp != null) {
        safeExp.infer(inferCtx);
        if (!safeExp.type.equals(this.type)) {
            this.type = Type.ObjectType;
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) FileFunctionWrapper(org.beetl.core.fun.FileFunctionWrapper) SingleFunctionWrapper(org.beetl.core.fun.SingleFunctionWrapper) Resource(org.beetl.core.Resource) Method(java.lang.reflect.Method) Function(org.beetl.core.Function) MutipleFunctionWrapper(org.beetl.core.fun.MutipleFunctionWrapper)

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