Search in sources :

Example 1 with AjaxStatement

use of org.beetl.core.statement.AjaxStatement in project beetl2.0 by javamonkey.

the class AntlrProgramBuilder method parseAjax.

protected AjaxStatement parseAjax(AjaxStContext ajaxCtx) {
    GrammarToken token = null;
    String flag = "render";
    List<TerminalNode> nodes = ajaxCtx.Identifier();
    if (nodes.size() == 1) {
        token = this.getBTToken(nodes.get(0).getSymbol());
    } else {
        token = this.getBTToken(nodes.get(1).getSymbol());
        flag = nodes.get(0).getSymbol().getText();
        if (!(flag.equals("render") || flag.equals("norender"))) {
            BeetlException be = new BeetlException(BeetlException.AJAX_PROPERTY_ERROR, "expect render or norender ,but " + flag);
            be.pushToken(token);
            throw be;
        }
    }
    BlockContext blockCtx = ajaxCtx.block();
    BlockStatement block = (BlockStatement) this.parseBlock(blockCtx.statement(), blockCtx);
    AjaxStatement ajaxStat = new AjaxStatement(block, token, flag.equals("render"));
    if (this.data.ajaxs == null) {
        this.data.ajaxs = new HashMap<String, AjaxStatement>();
    }
    String anchor = ajaxStat.token.text;
    if (this.data.ajaxs.containsKey(anchor)) {
        GrammarToken lastToken = this.data.ajaxs.get(anchor).token;
        BeetlException ex = new BeetlException(BeetlException.AJAX_ALREADY_DEFINED, "已经在第" + lastToken.line + "行定义");
        ex.pushToken(token);
        throw ex;
    }
    this.data.ajaxs.put(anchor, ajaxStat);
    return ajaxStat;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) BlockStatement(org.beetl.core.statement.BlockStatement) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) GrammarToken(org.beetl.core.statement.GrammarToken) AjaxStatement(org.beetl.core.statement.AjaxStatement)

Example 2 with AjaxStatement

use of org.beetl.core.statement.AjaxStatement in project beetl2.0 by javamonkey.

the class Template method renderTo.

public void renderTo(ByteWriter byteWriter) {
    try {
        ctx.byteWriter = byteWriter;
        ctx.byteOutputMode = cf.directByteOutput;
        ctx.gt = this.gt;
        ctx.template = this;
        if (gt.sharedVars != null) {
            for (Entry<String, Object> entry : gt.sharedVars.entrySet()) {
                ctx.set(entry.getKey(), entry.getValue());
            }
        }
        program.metaData.initContext(ctx);
        if (ajaxId != null) {
            AjaxStatement ajax = program.metaData.getAjax(ajaxId);
            if (ajax == null) {
                BeetlException be = new BeetlException(BeetlException.AJAX_NOT_FOUND);
                be.pushToken(new GrammarToken(ajaxId, 0, 0));
                throw be;
            }
            ajax.execute(ctx);
        } else {
            program.execute(ctx);
        }
        if (isRoot) {
            byteWriter.flush();
        }
    } catch (BeetlException e) {
        if (!(program instanceof ErrorGrammarProgram)) {
            e.pushResource(this.program.res);
        }
        // 是否打印异常,只有根模板才能打印异常
        if (!isRoot)
            throw e;
        if (e.detailCode == BeetlException.CLIENT_IO_ERROR_ERROR && ctx.gt.conf.isIgnoreClientIOError) {
            return;
        }
        Writer w = BeetlUtil.getWriterByByteWriter(ctx.byteWriter);
        e.gt = this.program.gt;
        e.cr = this.program.metaData.lineSeparator;
        ErrorHandler errorHandler = this.gt.getErrorHandler();
        if (errorHandler == null) {
            throw e;
        }
        errorHandler.processExcption(e, w);
        try {
            ctx.byteWriter.flush();
        } catch (IOException e1) {
        // 输出到客户端
        }
    } catch (IOException e) {
        if (!ctx.gt.conf.isIgnoreClientIOError) {
            BeetlException be = new BeetlException(BeetlException.CLIENT_IO_ERROR_ERROR, e.getMessage(), e);
            be.pushResource(this.program.res);
            be.pushToken(new GrammarToken(this.program.res.id, 0, 0));
            ErrorHandler errorHandler = this.gt.getErrorHandler();
            if (errorHandler == null) {
                throw be;
            }
            Writer w = BeetlUtil.getWriterByByteWriter(ctx.byteWriter);
            errorHandler.processExcption(be, w);
            try {
                ctx.byteWriter.flush();
            } catch (IOException e1) {
            // 输出到客户端
            }
        } else {
        // do  nothing ,just ignore
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) IOException(java.io.IOException) ErrorGrammarProgram(org.beetl.core.statement.ErrorGrammarProgram) AjaxStatement(org.beetl.core.statement.AjaxStatement) GrammarToken(org.beetl.core.statement.GrammarToken) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

BeetlException (org.beetl.core.exception.BeetlException)2 AjaxStatement (org.beetl.core.statement.AjaxStatement)2 GrammarToken (org.beetl.core.statement.GrammarToken)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 BlockContext (org.beetl.core.parser.BeetlParser.BlockContext)1 BlockStatement (org.beetl.core.statement.BlockStatement)1 ErrorGrammarProgram (org.beetl.core.statement.ErrorGrammarProgram)1