Search in sources :

Example 1 with Program

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

the class GroupTemplate method getTemplateByLoader.

private Template getTemplateByLoader(String key, ResourceLoader loader, boolean isTextTemplate) {
    key = key.intern();
    Program program = (Program) this.programCache.get(key);
    if (program == null) {
        synchronized (key) {
            if (program == null) {
                Resource resource = loader.getResource(key);
                program = this.loadTemplate(resource, isTextTemplate);
                this.programCache.set(key, program);
                return new Template(this, program, this.conf);
            }
        }
    }
    if (resourceLoader.isModified(program.rs)) {
        synchronized (key) {
            Resource resource = loader.getResource(key);
            program = this.loadTemplate(resource, isTextTemplate);
            this.programCache.set(key, program);
        }
    }
    return new Template(this, program, this.conf);
}
Also used : Program(org.beetl.core.statement.Program) ErrorGrammarProgram(org.beetl.core.statement.ErrorGrammarProgram)

Example 2 with Program

use of org.beetl.core.statement.Program 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 3 with Program

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

the class DefaultTemplateEngine method createProgram.

@Override
public Program createProgram(Resource resource, Reader reader, Map<Integer, String> textMap, String cr, GroupTemplate gt) {
    ANTLRInputStream input;
    try {
        input = new ANTLRInputStream(reader);
    } catch (IOException e) {
        // 不可能发生
        throw new RuntimeException(e);
    }
    BeetlLexer lexer = new BeetlLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(syntaxError);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    BeetlParser parser = new BeetlParser(tokens);
    // 测试代码
    parser.setErrorHandler(antlrErrorStrategy);
    // 
    ProgContext tree = parser.prog();
    // begin parsing at init rule
    AntlrProgramBuilder pb = getAntlrBuilder(gt);
    ProgramMetaData data = pb.build(tree);
    Program program = new Program();
    program.metaData = data;
    program.res = resource;
    program.rs = resource;
    program.gt = gt;
    program.metaData.staticTextArray = new Object[textMap.size()];
    program.metaData.lineSeparator = cr;
    int i = 0;
    Configuration conf = gt.getConf();
    String charset = conf.getCharset();
    boolean byteOut = conf.isDirectByteOutput();
    for (Entry<Integer, String> entry : textMap.entrySet()) {
        if (byteOut) {
            try {
                program.metaData.staticTextArray[i++] = entry.getValue().getBytes(charset);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        } else {
            program.metaData.staticTextArray[i++] = entry.getValue().toCharArray();
        }
    }
    return program;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Program(org.beetl.core.statement.Program) Configuration(org.beetl.core.Configuration) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) BeetlLexer(org.beetl.core.parser.BeetlLexer) BeetlParser(org.beetl.core.parser.BeetlParser) ProgContext(org.beetl.core.parser.BeetlParser.ProgContext) ProgramMetaData(org.beetl.core.statement.ProgramMetaData) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) AntlrProgramBuilder(org.beetl.core.AntlrProgramBuilder)

Example 4 with Program

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

the class MetaCopyTest method main.

public static void main(String[] args) throws Exception {
    String home = System.getProperty("user.dir") + File.separator + "template" + File.separator;
    Configuration cf = Configuration.defaultConfiguration();
    FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
    GroupTemplate gt = new GroupTemplate(rs, cf);
    Template t = gt.getTemplate("/helloworld.html");
    Program p = gt.getProgram("/helloworld.html");
    ProgramMetaData old = p.metaData;
    ProgramMetaData copy = old.copy();
    System.out.println("ok");
}
Also used : Program(org.beetl.core.statement.Program) Configuration(org.beetl.core.Configuration) ProgramMetaData(org.beetl.core.statement.ProgramMetaData) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 5 with Program

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

the class FastRuntimeEngine method createProgram.

@Override
public Program createProgram(Resource rs, Reader reader, Map<Integer, String> textMap, String cr, GroupTemplate gt) {
    Program orginalProgram = super.createProgram(rs, reader, textMap, cr, gt);
    // copy 一个出来分析
    ProgramMetaData metaData = orginalProgram.metaData.copy();
    Program copyProgram = new Program();
    copyProgram.metaData = metaData;
    copyProgram.res = orginalProgram.res;
    copyProgram.gt = orginalProgram.gt;
    copyProgram.rs = orginalProgram.rs;
    Probe nextFilter = new BasicProgramOptProbe();
    nextFilter.setProgram(copyProgram);
    TypeBindingProbe firstFilter = new TypeBindingProbe(copyProgram, nextFilter);
    // 在能优化前,按照原有的program执行,但每次都会执行fitler以看看是否能优化
    FilterProgram filterProgram = new FilterProgram(orginalProgram, copyProgram, firstFilter);
    return filterProgram;
}
Also used : Program(org.beetl.core.statement.Program) ProgramMetaData(org.beetl.core.statement.ProgramMetaData)

Aggregations

Program (org.beetl.core.statement.Program)7 ErrorGrammarProgram (org.beetl.core.statement.ErrorGrammarProgram)4 ProgramMetaData (org.beetl.core.statement.ProgramMetaData)3 IOException (java.io.IOException)2 Reader (java.io.Reader)2 Configuration (org.beetl.core.Configuration)2 BeetlException (org.beetl.core.exception.BeetlException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 AntlrProgramBuilder (org.beetl.core.AntlrProgramBuilder)1 GroupTemplate (org.beetl.core.GroupTemplate)1 Template (org.beetl.core.Template)1 HTMLTagParserException (org.beetl.core.exception.HTMLTagParserException)1 BeetlLexer (org.beetl.core.parser.BeetlLexer)1 BeetlParser (org.beetl.core.parser.BeetlParser)1 ProgContext (org.beetl.core.parser.BeetlParser.ProgContext)1 FileResourceLoader (org.beetl.core.resource.FileResourceLoader)1