Search in sources :

Example 1 with ErrorGrammarProgram

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

the class GroupTemplate method loadTemplate.

private Program loadTemplate(Resource res, boolean isTextTemplate) {
    Transformator sf = null;
    try {
        Reader reader = res.openReader();
        if (isTextTemplate) {
            sf = new Transformator(conf.placeholderStart, conf.placeholderEnd, conf.statementStart, conf.statementEnd);
        } else {
            if (conf.isHasFunctionLimiter()) {
                sf = new Transformator(conf.placeholderStart, conf.placeholderEnd, conf.functionLimiterStart, conf.functionLimiterEnd);
            } else {
                sf = new Transformator(conf.placeholderStart, conf.placeholderEnd, conf.statementStart, conf.statementEnd);
            }
        }
        if (this.conf.isHtmlTagSupport()) {
            sf.enableHtmlTagSupport(conf.getHtmlTagStart(), conf.getHtmlTagEnd(), this.conf.getHtmlTagBindingAttribute());
        }
        Reader scriptReader;
        scriptReader = sf.transform(reader);
        Program program = engine.createProgram(res, scriptReader, sf.textMap, sf.lineSeparator, this);
        return program;
    } catch (HTMLTagParserException e) {
        ErrorGrammarProgram ep = new ErrorGrammarProgram(res, this, sf.lineSeparator);
        ep.setException(e);
        e.pushResource(res);
        return ep;
    } catch (IOException e) {
        ErrorGrammarProgram ep = new ErrorGrammarProgram(res, this, sf.lineSeparator);
        BeetlException ex = new BeetlException(BeetlException.TEMPLATE_LOAD_ERROR);
        ex.pushResource(res);
        ep.setException(ex);
        return ep;
    } catch (BeetlException ex) {
        ErrorGrammarProgram ep = new ErrorGrammarProgram(res, this, sf != null ? sf.lineSeparator : null);
        ex.pushResource(res);
        ep.setException(ex);
        return ep;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) Program(org.beetl.core.statement.Program) ErrorGrammarProgram(org.beetl.core.statement.ErrorGrammarProgram) Reader(java.io.Reader) IOException(java.io.IOException) HTMLTagParserException(org.beetl.core.exception.HTMLTagParserException) ErrorGrammarProgram(org.beetl.core.statement.ErrorGrammarProgram)

Example 2 with ErrorGrammarProgram

use of org.beetl.core.statement.ErrorGrammarProgram 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)

Example 3 with ErrorGrammarProgram

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

the class Template method validate.

/**
 * 语法校验,如果返回BeetlException,则表示语法有错,返回null,语法无错误
 * @return
 */
public BeetlException validate() {
    if (!(program instanceof ErrorGrammarProgram)) {
        return null;
    }
    ErrorGrammarProgram error = (ErrorGrammarProgram) program;
    BeetlException exception = error.getException();
    return exception;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) ErrorGrammarProgram(org.beetl.core.statement.ErrorGrammarProgram)

Example 4 with ErrorGrammarProgram

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

the class GroupTemplate method loadScript.

private Program loadScript(Resource res) {
    try {
        Reader scriptReader = res.openReader();
        Program program = engine.createProgram(res, scriptReader, Collections.EMPTY_MAP, System.getProperty("line.separator"), this);
        return program;
    } catch (BeetlException ex) {
        ErrorGrammarProgram ep = new ErrorGrammarProgram(res, this, System.getProperty("line.separator"));
        ex.pushResource(res);
        ep.setException(ex);
        return ep;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) Program(org.beetl.core.statement.Program) ErrorGrammarProgram(org.beetl.core.statement.ErrorGrammarProgram) Reader(java.io.Reader) ErrorGrammarProgram(org.beetl.core.statement.ErrorGrammarProgram)

Aggregations

BeetlException (org.beetl.core.exception.BeetlException)4 ErrorGrammarProgram (org.beetl.core.statement.ErrorGrammarProgram)4 IOException (java.io.IOException)2 Reader (java.io.Reader)2 Program (org.beetl.core.statement.Program)2 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 HTMLTagParserException (org.beetl.core.exception.HTMLTagParserException)1 AjaxStatement (org.beetl.core.statement.AjaxStatement)1 GrammarToken (org.beetl.core.statement.GrammarToken)1