Search in sources :

Example 1 with ANTLRInputStream

use of st4hidden.org.antlr.runtime.ANTLRInputStream in project bndtools by bndtools.

the class StringTemplateEngine method loadRawTemplate.

private CompiledST loadRawTemplate(STGroup stg, String name, Resource resource) throws IOException {
    if (resource.getType() != ResourceType.File)
        throw new IllegalArgumentException(String.format("Cannot build resource from resource of type %s (name='%s').", resource.getType(), name));
    try (InputStream is = resource.getContent()) {
        ANTLRInputStream templateStream = new ANTLRInputStream(is, resource.getTextEncoding());
        String template = templateStream.substring(0, templateStream.size() - 1);
        CompiledST impl = new Compiler(stg).compile(name, template);
        CommonToken nameT = new CommonToken(STLexer.SEMI);
        nameT.setInputStream(templateStream);
        stg.rawDefineTemplate("/" + name, impl, nameT);
        impl.defineImplicitlyDefinedTemplates(stg);
        return impl;
    }
}
Also used : Compiler(org.stringtemplate.v4.compiler.Compiler) ANTLRInputStream(st4hidden.org.antlr.runtime.ANTLRInputStream) InputStream(java.io.InputStream) CommonToken(st4hidden.org.antlr.runtime.CommonToken) ANTLRInputStream(st4hidden.org.antlr.runtime.ANTLRInputStream) CompiledST(org.stringtemplate.v4.compiler.CompiledST)

Example 2 with ANTLRInputStream

use of st4hidden.org.antlr.runtime.ANTLRInputStream in project bndtools by bndtools.

the class StringTemplateEngine method loadTemplate.

private void loadTemplate(STGroup stg, String fileName, InputStream is, String encoding) throws IOException {
    ANTLRInputStream charStream = new ANTLRInputStream(is, encoding);
    charStream.name = fileName;
    try {
        stg.loadTemplateFile("/", fileName, charStream);
    } catch (NullPointerException e) {
        throw new IOException(String.format("Error loading template file: %s. Ensure the file contains a template definition matching the file name.", fileName), e);
    }
}
Also used : IOException(java.io.IOException) ANTLRInputStream(st4hidden.org.antlr.runtime.ANTLRInputStream)

Aggregations

ANTLRInputStream (st4hidden.org.antlr.runtime.ANTLRInputStream)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CompiledST (org.stringtemplate.v4.compiler.CompiledST)1 Compiler (org.stringtemplate.v4.compiler.Compiler)1 CommonToken (st4hidden.org.antlr.runtime.CommonToken)1