Search in sources :

Example 1 with Expr_constantContext

use of jetbrick.template.parser.grammer.JetTemplateParser.Expr_constantContext in project jetbrick-template-1x by subchen.

the class JetTemplateCodeVisitor method visitInclude_directive.

@Override
public Code visitInclude_directive(Include_directiveContext ctx) {
    Expression_listContext expression_list = ctx.expression_list();
    SegmentListCode childrenCode = (SegmentListCode) expression_list.accept(this);
    if (childrenCode.size() > 2) {
        throw reportError("Arguments mismatch for #include directive.", ctx);
    }
    // argument 1: file
    SegmentCode fileCode = childrenCode.getChild(0);
    ExpressionContext fileExpression = expression_list.expression(0);
    if (!String.class.equals(fileCode.getKlass())) {
        throw reportError("Type mismatch: the first argument cannot convert from " + fileCode.getKlassName() + " to String", fileExpression);
    }
    // argument 2: parameters
    SegmentCode parametersCode = null;
    if (childrenCode.size() > 1) {
        parametersCode = childrenCode.getChild(1);
        if (!(Map.class.equals(parametersCode.getKlass()))) {
            throw reportError("Type mismatch: the second argument cannot convert from " + parametersCode.getKlassName() + " to Map", expression_list.expression(1));
        }
    }
    // 如果 file 是常量,那么进行 file.exists() 校验
    if (fileExpression instanceof Expr_constantContext) {
        String file = fileCode.toString();
        file = file.substring(1, file.length() - 1);
        file = StringEscapeUtils.unescapeJava(file);
        file = PathUtils.getAbsolutionName(resource.getName(), file);
        if (!engine.lookupResource(file)) {
            throw reportError("FileNotFoundException: " + file, fileExpression);
        }
    }
    // 生成代码
    StringBuilder source = new StringBuilder();
    source.append("JetUtils.asInclude($ctx, ");
    source.append(fileCode.toString());
    source.append(", (Map<String, Object>)");
    source.append((parametersCode != null) ? parametersCode.toString() : "null");
    source.append("); // line: ");
    source.append(ctx.getStart().getLine());
    return scopeCode.createLineCode(source.toString());
}
Also used : SegmentCode(jetbrick.template.parser.code.SegmentCode) ExpressionContext(jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext) Expression_listContext(jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext) Expr_constantContext(jetbrick.template.parser.grammer.JetTemplateParser.Expr_constantContext) SegmentListCode(jetbrick.template.parser.code.SegmentListCode)

Aggregations

SegmentCode (jetbrick.template.parser.code.SegmentCode)1 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)1 Expr_constantContext (jetbrick.template.parser.grammer.JetTemplateParser.Expr_constantContext)1 ExpressionContext (jetbrick.template.parser.grammer.JetTemplateParser.ExpressionContext)1 Expression_listContext (jetbrick.template.parser.grammer.JetTemplateParser.Expression_listContext)1