use of org.beetl.core.statement.ProgramMetaData 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;
}
use of org.beetl.core.statement.ProgramMetaData 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");
}
use of org.beetl.core.statement.ProgramMetaData 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;
}
Aggregations