Search in sources :

Example 41 with BeetlException

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

the class ALU method isTrue.

/**
 * @param o
 * @param node
 * @return
 */
public static Boolean isTrue(final Object o, ASTNode node) {
    if (o == null) {
        BeetlException be = new BeetlException(BeetlException.NULL);
        be.pushToken(node.token);
        throw be;
    }
    if (Boolean.TRUE == o) {
        return Boolean.TRUE;
    } else if (Boolean.FALSE == o) {
        return Boolean.FALSE;
    } else if (o instanceof Boolean) {
        return ((Boolean) o);
    } else {
        BeetlException ex = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR, o.getClass().toString());
        ex.pushToken(node.token);
        throw ex;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException)

Example 42 with BeetlException

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

the class WebRender method render.

/**
 * @param key 模板资源id
 * @param request
 * @param response
 * @param args 其他参数,将会传给modifyTemplate方法
 */
public void render(String key, HttpServletRequest request, HttpServletResponse response, Object... args) {
    Writer writer = null;
    OutputStream os = null;
    String ajaxId = null;
    Template template = null;
    boolean isError = false;
    try {
        // response.setContentType(contentType);
        int ajaxIdIndex = key.lastIndexOf("#");
        if (ajaxIdIndex != -1) {
            ajaxId = key.substring(ajaxIdIndex + 1);
            key = key.substring(0, ajaxIdIndex);
            template = gt.getAjaxTemplate(key, ajaxId);
        } else {
            template = gt.getTemplate(key);
        }
        Enumeration<String> attrs = request.getAttributeNames();
        while (attrs.hasMoreElements()) {
            String attrName = attrs.nextElement();
            template.binding(attrName, request.getAttribute(attrName));
        }
        WebVariable webVariable = new WebVariable();
        webVariable.setRequest(request);
        webVariable.setResponse(response);
        template.binding(WebVariable.SESSION, new SessionWrapper(request, request.getSession(false)));
        template.binding(WebVariable.SERVLET, webVariable);
        template.binding(WebVariable.REQUEST, request);
        template.binding("ctxPath", request.getContextPath());
        template.binding("$page", new HashMap());
        template.binding("parameter", new ParameterWrapper(request));
        template.binding("cookie", new CookieFunction());
        modifyTemplate(template, key, request, response, args);
        String strWebAppExt = gt.getConf().getWebAppExt();
        if (strWebAppExt != null) {
            WebRenderExt renderExt = this.getWebRenderExt(strWebAppExt);
            renderExt.modify(template, gt, request, response);
        }
        if (gt.getConf().isDirectByteOutput()) {
            os = response.getOutputStream();
            template.renderTo(os);
        } else {
            writer = response.getWriter();
            template.renderTo(writer);
        }
    } catch (IOException e) {
        isError = true;
        handleClientError(e);
    } catch (BeetlException e) {
        isError = true;
        // response.setStatus(500);
        // 
        handleBeetlException(e);
    } finally {
        try {
            if (!isError && writer != null)
                writer.flush();
            if (!isError && os != null) {
                os.flush();
            }
        } catch (IOException e) {
            handleClientError(e);
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) IOException(java.io.IOException) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template) Writer(java.io.Writer) CookieFunction(org.beetl.ext.fn.CookieFunction)

Example 43 with BeetlException

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

the class Printf method call.

public String call(Object[] paras, Context ctx) {
    String template = (String) paras[0];
    Object[] args = new Object[paras.length - 1];
    for (int i = 0; i < args.length; i++) {
        args[i] = paras[i + 1];
    }
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    f.format(template, args);
    try {
        ctx.byteWriter.writeString(sb.toString());
    } catch (IOException e) {
        BeetlException be = new BeetlException(BeetlException.CLIENT_IO_ERROR_ERROR);
        throw be;
    }
    return "";
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) Formatter(java.util.Formatter) IOException(java.io.IOException)

Example 44 with BeetlException

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

the class Println method call.

public String call(Object[] paras, Context ctx) {
    try {
        ByteWriter w = ctx.byteWriter;
        if (paras.length == 0) {
            w.writeString(ctx.template.program.metaData.lineSeparator);
            return "";
        }
        Object o = paras[0];
        if (o != null) {
            w.writeString(o.toString());
            w.writeString(ctx.template.program.metaData.lineSeparator);
        }
    } catch (IOException e) {
        BeetlException be = new BeetlException(BeetlException.CLIENT_IO_ERROR_ERROR);
        throw be;
    }
    return "";
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) ByteWriter(org.beetl.core.ByteWriter) IOException(java.io.IOException)

Example 45 with BeetlException

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

the class ObjectAA method value.

@Override
public Object value(Object o, Object name) {
    if (o instanceof Map) {
        return ((Map) o).get(name);
    } else if (o instanceof List) {
        try {
            return ((List) o).get(((Number) name).intValue());
        } catch (ClassCastException ex) {
            throw new ClassCastException("目标类型为java.util.List,无此属性:" + name);
        }
    } else if (o.getClass().isArray()) {
        try {
            if (o.getClass().getComponentType().isPrimitive()) {
                return PrimitiveArrayUtil.getObject(o, (((Number) name).intValue()));
            } else {
                return ((Object[]) o)[(((Number) name).intValue())];
            }
        } catch (ClassCastException ex) {
            throw new ClassCastException("目标类型为数组,无此属性:" + name);
        }
    } else {
        Class c = o.getClass();
        MethodInvoker invoker = ObjectUtil.getInvokder(c, (String) name);
        if (invoker != null) {
            return invoker.get(o);
        } else {
            BeetlException ex = new BeetlException(BeetlException.ATTRIBUTE_NOT_FOUND, (String) name);
            throw ex;
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) List(java.util.List) Map(java.util.Map)

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