Search in sources :

Example 11 with BeetlException

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

the class VarAttributeNodeListener method onEvent.

@Override
public Object onEvent(Event e) {
    Stack stack = (Stack) e.getEventTaget();
    Object o = stack.peek();
    if (o instanceof VarRef) {
        VarRef ref = (VarRef) o;
        VarAttribute[] attrs = ref.attributes;
        for (int i = 0; i < attrs.length; i++) {
            GroupTemplate gt = (GroupTemplate) ((Map) stack.get(0)).get("groupTemplate");
            VarAttribute attr = attrs[i];
            if (attr.getClass() == VarAttribute.class) {
                Type type = attr.type;
                String name = attr.token != null ? attr.token.text : null;
                // 换成速度较快的属性访问类
                try {
                    AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(type.cls, name, gt);
                    attr.aa = aa;
                } catch (BeetlException ex) {
                    ex.pushToken(attr.token);
                    throw ex;
                }
            } else if (attr.getClass() == VarSquareAttribute.class) {
                Type type = attr.type;
                Class c = type.cls;
                if (Map.class.isAssignableFrom(c)) {
                    attr.setAA(gt.getAttributeAccessFactory().getMapAA());
                } else if (List.class.isAssignableFrom(c) || Set.class.isAssignableFrom(c)) {
                    attr.setAA(gt.getAttributeAccessFactory().getListAA());
                } else if (c.isArray()) {
                    attr.setAA(gt.getAttributeAccessFactory().getArrayAA());
                } else {
                    Expression exp = ((VarSquareAttribute) attr).exp;
                    if (exp instanceof Literal) {
                        Literal literal = (Literal) exp;
                        if (literal.obj instanceof String) {
                            try {
                                String attributeName = (String) literal.obj;
                                AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(c, attributeName, gt);
                                ref.attributes[i] = new VarSquareAttribute2((VarSquareAttribute) attrs[i], attributeName, aa);
                            } catch (BeetlException ex) {
                                ex.pushToken(attr.token);
                                throw ex;
                            }
                        }
                    }
                }
            } else if (attr.getClass() == VarVirtualAttribute.class) {
                // 对虚拟属性~size做优化
                if (attr.token.text.equals("size")) {
                    // 优化
                    Class c = attr.type.cls;
                    if (Map.class.isAssignableFrom(c)) {
                        ref.attributes[i] = new MapSizeVirtualAttribute((VarVirtualAttribute) attr);
                    } else if (Collection.class.isAssignableFrom(c)) {
                        ref.attributes[i] = new CollectionSizeVirtualAttribute((VarVirtualAttribute) attr);
                    } else if (c.isArray()) {
                        ref.attributes[i] = new ArraySizeVirtualAttribute((VarVirtualAttribute) attr);
                    }
                }
            }
        }
    }
    return null;
}
Also used : VarRef(org.beetl.core.statement.VarRef) VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) BeetlException(org.beetl.core.exception.BeetlException) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) Set(java.util.Set) GroupTemplate(org.beetl.core.GroupTemplate) AttributeAccess(org.beetl.core.om.AttributeAccess) Stack(java.util.Stack) Type(org.beetl.core.statement.Type) Expression(org.beetl.core.statement.Expression) VarAttribute(org.beetl.core.statement.VarAttribute) Literal(org.beetl.core.statement.Literal) Collection(java.util.Collection) List(java.util.List) Map(java.util.Map)

Example 12 with BeetlException

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

the class FileFunctionWrapper method call.

@Override
public Object call(Object[] paras, Context ctx) {
    try {
        Template template = ctx.gt.getHtmlFunctionOrTagTemplate(this.resourceId);
        // 能显示异常
        template.isRoot = false;
        template.binding(ctx.globalVar);
        for (int i = 0; i < paras.length; i++) {
            template.binding("para".concat(String.valueOf(i)), paras[i]);
        }
        template.renderTo(ctx.byteWriter);
        Object[] vars = template.getCtx().vars;
        Object o = vars[vars.length - 1];
        if (o != Context.NOT_EXIST_OBJECT) {
            return o;
        } else {
            return null;
        }
    } catch (BeetlException ex) {
        throw ex;
    } catch (Exception ex) {
        BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "调用方法出错 " + this.resourceId, ex);
        throw be;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlException(org.beetl.core.exception.BeetlException) Template(org.beetl.core.Template)

Example 13 with BeetlException

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

the class StatementParser method exec.

protected void exec(Object astNode, Class[] matchClasses, Stack stack) {
    stack.push(astNode);
    Class astNodeClass = astNode.getClass();
    Field[] fields = astNodeClass.getFields();
    for (Field f : fields) {
        if (f.getModifiers() != Modifier.PUBLIC)
            continue;
        Class target = null;
        Class c = f.getType();
        if (c.isArray()) {
            target = c.getComponentType();
        } else {
            target = c;
        }
        // 只解析含有ASTNode的字段
        if (!ASTNode.class.isAssignableFrom(target))
            continue;
        Object values;
        try {
            // 需要判断的节点
            values = f.get(astNode);
            if (values == null)
                continue;
            // 具体类型
            Class target2 = values.getClass();
            if (target2.isArray()) {
                Object[] array = (Object[]) values;
                if (array.length == 0)
                    continue;
                for (int i = 0; i < array.length; i++) {
                    Object item = array[i];
                    if (item == null)
                        continue;
                    Class target3 = item.getClass();
                    if (match(target3, matchClasses)) {
                        stack.push(item);
                        Listener ls = this.listeners.get(target3);
                        NodeEvent e = new NodeEvent(stack);
                        Object newASTNode = ls.onEvent(e);
                        if (newASTNode != null) {
                            stack.pop();
                            stack.push(newASTNode);
                            // 替换原有节点
                            array[i] = newASTNode;
                            item = newASTNode;
                        }
                        // 继续遍历子节点
                        this.exec(item, matchClasses, stack);
                        stack.pop();
                    } else {
                        ASTNode node = (ASTNode) item;
                        this.exec(node, matchClasses, stack);
                    }
                }
            } else {
                if (match(target2, matchClasses)) {
                    stack.push(values);
                    Listener ls = this.listeners.get(target2);
                    NodeEvent e = new NodeEvent(stack);
                    Object newASTNode = ls.onEvent(e);
                    if (newASTNode != null) {
                        stack.pop();
                        stack.push(newASTNode);
                        // 替换原有节点
                        try {
                            f.set(astNode, newASTNode);
                        } catch (Exception ex) {
                            BeetlException be = new BeetlException(BeetlException.ERROR, "替换ASTNode错", ex);
                            be.pushToken(((ASTNode) newASTNode).token);
                            throw be;
                        }
                        values = newASTNode;
                    }
                    this.exec(values, matchClasses, stack);
                    stack.pop();
                    continue;
                } else {
                    ASTNode node = (ASTNode) values;
                    this.exec(node, matchClasses, stack);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    stack.pop();
}
Also used : Field(java.lang.reflect.Field) BeetlException(org.beetl.core.exception.BeetlException) Listener(org.beetl.core.Listener) ASTNode(org.beetl.core.statement.ASTNode) BeetlException(org.beetl.core.exception.BeetlException)

Example 14 with BeetlException

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

the class MutipleFunctionWrapper method call.

@Override
public Object call(Object[] paras, Context ctx) {
    Class[] parameterType = null;
    try {
        // 比较慢的情况,要考虑到底哪个方法适合调用
        Class[] parameterContextType = null;
        Class[] parameterNoContextType = null;
        for (MethodContext mc : mcs) {
            if (mc.contextRequired) {
                if (parameterContextType == null) {
                    parameterContextType = new Class[paras.length + 1];
                    int i = 0;
                    for (Object para : paras) {
                        parameterContextType[i++] = para != null ? para.getClass() : null;
                    }
                    parameterContextType[i] = Context.class;
                }
                parameterType = parameterContextType;
            } else {
                if (parameterNoContextType == null) {
                    parameterNoContextType = new Class[paras.length];
                    int i = 0;
                    for (Object para : paras) {
                        parameterNoContextType[i++] = para != null ? para.getClass() : null;
                    }
                }
                parameterType = parameterNoContextType;
            }
            ObjectMethodMatchConf conf = ObjectUtil.match(mc.m, parameterType);
            if (conf == null) {
                continue;
            }
            Object[] newParas = null;
            if (mc.contextRequired) {
                newParas = this.getContextParas(paras, ctx);
            } else {
                newParas = paras;
            }
            newParas = conf.convert(newParas);
            return mc.m.invoke(target, newParas);
        }
    } catch (IllegalArgumentException e) {
        BeetlException ex = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "参数不匹配 " + this.functionName, e);
        throw ex;
    } catch (IllegalAccessException e) {
        BeetlException ex = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "非法访问方法 " + this.functionName, e);
        throw ex;
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        if (t instanceof BeetlException) {
            throw (BeetlException) t;
        } else {
            BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "调用方法出错 " + this.functionName, t);
            throw be;
        }
    }
    BeetlException ex = new BeetlException(BeetlException.NATIVE_CALL_INVALID, "找不到方法 " + this.functionName + BeetlUtil.getParameterDescription(parameterType));
    throw ex;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) ObjectMethodMatchConf(org.beetl.core.om.ObjectMethodMatchConf) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 15 with BeetlException

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

the class ALU method div.

// /
// @todo: 对0的判断
public static Object div(final Object o1, final Object o2, final ASTNode node1, final ASTNode node2) {
    if (o1 != null && o2 != null) {
        switch(getBaseType(o1, o2)) {
            case INTEGER:
            case LONG:
            case DOUBLE:
            case FLOAT:
            case SHORT:
                double c = ((Number) o2).doubleValue();
                if (c == 0) {
                    BeetlException ex = new BeetlException(BeetlException.DIV_ZERO_ERROR);
                    ex.pushToken(node2.token);
                    throw ex;
                }
                double a = ((Number) o1).doubleValue() / ((Number) o2).doubleValue();
                return trim(a, (Number) o1, (Number) o2);
            case HS:
                BigDecimal b1 = getBigDecimal(o1), b2 = getBigDecimal(o2);
                BigDecimal b = b1.divide(b2, scale, round);
                return b.stripTrailingZeros();
            // return b1.divide(b2);
            default:
                throw UnsupportedTypeException(o1, o2, node1, node2, "/");
        }
    } else {
        throw valueIsNullException(o1, o2, node1, node2);
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BigDecimal(java.math.BigDecimal)

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