Search in sources :

Example 1 with HTMLTagParserException

use of org.beetl.core.exception.HTMLTagParserException 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 HTMLTagParserException

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

the class LineStatus method transform.

public Reader transform(String str) throws IOException, HTMLTagParserException {
    cs = str.toCharArray();
    // 找到回车换行符号
    findCR();
    checkAppendCR();
    parser();
    if (this.isSupportHtmlTag && this.htmlTagStack.size() != 0) {
        String tagName = (String) htmlTagStack.peek();
        GrammarToken token = GrammarToken.createToken(tagName, this.totalLineCount + 1);
        HTMLTagParserException ex = new HTMLTagParserException("解析html tag 标签出错,未找到匹配结束标签 " + tagName);
        ex.pushToken(token);
        ex.line = totalLineCount + 1;
        this.clear();
        throw ex;
    }
    return new StringReader(sb.toString());
}
Also used : StringReader(java.io.StringReader) HTMLTagParserException(org.beetl.core.exception.HTMLTagParserException) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 3 with HTMLTagParserException

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

the class LineStatus method readHTMLTagEnd.

public void readHTMLTagEnd() throws HTMLTagParserException {
    String tagName = null;
    try {
        HTMLTagParser html = new HTMLTagParser(cs, index, htmlTagBindingAttribute, false);
        html.parser();
        tagName = html.getTagName();
        if (htmlTagStack.empty()) {
            throw new RuntimeException("解析html tag出错");
        }
        String lastTag = (String) this.htmlTagStack.peek();
        if (tagName.equals(lastTag)) {
            this.htmlTagStack.pop();
            sb.append("}");
        } else {
            throw new RuntimeException("解析html tag出错,期望匹配标签" + lastTag);
        }
        this.index = html.getIndex();
        status = 1;
        this.lineStatus.setStatment();
    } catch (RuntimeException re) {
        if (tagName == null) {
            tagName = "未知标签";
        }
        GrammarToken token = GrammarToken.createToken(tagName, this.totalLineCount + 1);
        HTMLTagParserException ex = new HTMLTagParserException(re.getMessage());
        ex.pushToken(token);
        // ex.token = token;
        ex.line = totalLineCount + 1;
        throw ex;
    }
}
Also used : HTMLTagParserException(org.beetl.core.exception.HTMLTagParserException) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 4 with HTMLTagParserException

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

the class LineStatus method readHTMLTagBegin.

public void readHTMLTagBegin() throws HTMLTagParserException {
    String tagName = null;
    try {
        StringBuilder script = new StringBuilder();
        HTMLTagParser html = new HTMLTagParser(cs, index, htmlTagBindingAttribute, true);
        html.parser();
        if (html.hasVarBinding) {
            script.append("htmltagvar");
        } else {
            script.append("htmltag");
        }
        tagName = html.getTagName();
        script.append("('").append(tagName).append("',");
        Map<String, String> map = html.getExpMap();
        Map<String, Character> quat = html.getQuatMap();
        if (map.size() != 0) {
            script.append("{");
        }
        for (Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (html.crKey.contains(key)) {
                script.append(this.lineSeparator);
            }
            script.append(key).append(":");
            String attrValue = this.parseAttr(quat.get(key), value);
            script.append(attrValue);
            script.append(",");
        }
        script.setLength(script.length() - 1);
        if (map.size() != 0) {
            script.append("}");
        }
        if (html.hasVarBinding) {
            if (map.size() == 0) {
                // 保持三个参数,第一个为标签函数名,第二个为属性,第三个为申明的变量
                script.append(",{}");
            }
            if (html.varBidingStr.trim().length() == 0) {
                String defaultVarName = null;
                int index = tagName.lastIndexOf(":");
                if (index == -1) {
                    defaultVarName = tagName;
                } else {
                    defaultVarName = tagName.substring(index + 1);
                }
                script.append(",").append("'").append(defaultVarName).append("'");
            } else {
                script.append(",").append("'").append(html.varBidingStr).append("'");
            }
        }
        script.append("){");
        if (html.isEmptyTag()) {
            script.append("}");
        } else {
            htmlTagStack.push(tagName);
        }
        sb.append(script);
        this.index = html.getIndex();
        status = 1;
        this.lineStatus.setStatment();
    } catch (RuntimeException re) {
        if (tagName == null) {
            tagName = "未知标签";
        }
        GrammarToken token = GrammarToken.createToken(tagName, this.totalLineCount + 1);
        HTMLTagParserException ex = new HTMLTagParserException(re.getMessage());
        ex.pushToken(token);
        // todo
        // ex.token = token;
        ex.line = totalLineCount + 1;
        throw ex;
    }
}
Also used : HTMLTagParserException(org.beetl.core.exception.HTMLTagParserException) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 5 with HTMLTagParserException

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

the class LineStatus method transform.

public Reader transform(Reader orginal) throws IOException, HTMLTagParserException {
    StringBuilder temp = new StringBuilder();
    int bufSzie = 1024;
    cs = new char[bufSzie];
    int len = -1;
    while ((len = orginal.read(cs)) != -1) {
        temp.append(cs, 0, len);
    }
    cs = temp.toString().toCharArray();
    // 找到回车换行符号
    findCR();
    checkAppendCR();
    parser();
    if (this.isSupportHtmlTag && this.htmlTagStack.size() != 0) {
        String tagName = (String) htmlTagStack.peek();
        GrammarToken token = GrammarToken.createToken(tagName, this.totalLineCount + 1);
        HTMLTagParserException ex = new HTMLTagParserException("解析html tag 标签出错,未找到匹配结束标签 " + tagName);
        ex.pushToken(token);
        ex.line = totalLineCount + 1;
        this.clear();
        throw ex;
    }
    orginal.close();
    return new StringReader(sb.toString());
}
Also used : StringReader(java.io.StringReader) HTMLTagParserException(org.beetl.core.exception.HTMLTagParserException) GrammarToken(org.beetl.core.statement.GrammarToken)

Aggregations

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