Search in sources :

Example 1 with Configuration

use of org.beetl.core.Configuration in project vip by guangdada.

the class GunsTemplateEngine method initBeetlEngine.

public void initBeetlEngine() {
    Properties properties = new Properties();
    properties.put("RESOURCE.root", "");
    properties.put("DELIMITER_STATEMENT_START", "<%");
    properties.put("DELIMITER_STATEMENT_END", "%>");
    properties.put("HTML_TAG_FLAG", "##");
    Configuration cfg = null;
    try {
        cfg = new Configuration(properties);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
    groupTemplate = new GroupTemplate(resourceLoader, cfg);
    groupTemplate.registerFunctionPackage("tool", new ToolUtil());
}
Also used : ToolUtil(com.ikoori.vip.server.core.util.ToolUtil) Configuration(org.beetl.core.Configuration) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) IOException(java.io.IOException) GroupTemplate(org.beetl.core.GroupTemplate) Properties(java.util.Properties)

Example 2 with Configuration

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

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

the class Test method main.

public static void main(String[] args) throws Exception {
    ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("org/beetl/core/lab/");
    Configuration cfg = Configuration.defaultConfiguration();
    cfg.setDirectByteOutput(true);
    cfg.getResourceMap().put("tagRoot", "/");
    cfg.getPkgList().add("org.beetl.core.lab.");
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    // cfg.setStatementStart("@");
    // cfg.setStatementEnd(null);
    // cfg.setPlaceholderStart("{{");
    // cfg.setPlaceholderEnd("}}");
    // 
    gt.registerFunction("test", new TestFun());
    gt.registerTag("test", TestTag.class);
    List list = new ArrayList();
    list.add(null);
    list.add(new TestUser("abc"));
    HashMap map = new HashMap();
    map.put("key", 123);
    gt.enableStrict();
    for (int i = 0; i < 1; i++) {
        Template t = gt.getTemplate("/hello.txt");
        t.binding("user", new TestUser("jo"));
        t.binding("id", 2);
        t.binding("list", list);
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        try {
            t.renderTo(bs);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // TestUser test = new TestUser("a");
        // test.setLover(new TestUser("b"));
        // t.binding("user", test);
        System.out.println(t.render());
    }
}
Also used : Configuration(org.beetl.core.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) List(java.util.List) ArrayList(java.util.ArrayList) GroupTemplate(org.beetl.core.GroupTemplate) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 4 with Configuration

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

the class PairDLTest method getGt.

public GroupTemplate getGt() {
    ClasspathResourceLoader rs = new ClasspathResourceLoader("/template");
    Configuration cfg;
    try {
        cfg = Configuration.defaultConfiguration();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    cfg.setStatementEnd("%>");
    cfg.setStatementStart("<%");
    GroupTemplate gt = new GroupTemplate(rs, cfg);
    return gt;
}
Also used : Configuration(org.beetl.core.Configuration) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) IOException(java.io.IOException) GroupTemplate(org.beetl.core.GroupTemplate)

Example 5 with Configuration

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

Aggregations

Configuration (org.beetl.core.Configuration)23 GroupTemplate (org.beetl.core.GroupTemplate)22 Template (org.beetl.core.Template)13 ClasspathResourceLoader (org.beetl.core.resource.ClasspathResourceLoader)10 FileResourceLoader (org.beetl.core.resource.FileResourceLoader)7 IOException (java.io.IOException)6 Properties (java.util.Properties)4 HashMap (java.util.HashMap)3 StringTemplateResourceLoader (org.beetl.core.resource.StringTemplateResourceLoader)3 InputStream (java.io.InputStream)2 WebAppResourceLoader (org.beetl.core.resource.WebAppResourceLoader)2 Program (org.beetl.core.statement.Program)2 ProgramMetaData (org.beetl.core.statement.ProgramMetaData)2 Test (org.testng.annotations.Test)2 ToolUtil (com.ikoori.vip.server.core.util.ToolUtil)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1