Search in sources :

Example 11 with STGroup

use of org.stringtemplate.v4.STGroup in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class StringTemplate method compileTemplate.

private ST compileTemplate(String row, char delimeterStartChar, char delimeterEndChar) {
    STGroup group = new STGroup(delimeterStartChar, delimeterEndChar);
    prepareGroup(group);
    group.defineTemplate(TEMPLATE_NAME, MODEL_VARIABLE + ',' + CONTENT_VARIABLE, row);
    return group.getInstanceOf(TEMPLATE_NAME);
}
Also used : STGroup(org.stringtemplate.v4.STGroup)

Example 12 with STGroup

use of org.stringtemplate.v4.STGroup in project uPortal by Jasig.

the class EmailPasswordResetNotificationImpl method formatBody.

/**
     * Get the body content of the email.
     *
     * @param resetUrl the password reset URL
     * @param account the user account that has had its password reset
     * @param locale the locale of the user who reset the password
     * @return The message body as a string.
     */
private String formatBody(URL resetUrl, ILocalAccountPerson account, Locale locale) {
    final STGroup group = new STGroupDir(templateDir, '$', '$');
    final ST template = group.getInstanceOf(templateName);
    String name = findDisplayNameFromLocalAccountPerson(account);
    template.add("displayName", name);
    template.add("url", resetUrl.toString());
    return template.render();
}
Also used : ST(org.stringtemplate.v4.ST) STGroupDir(org.stringtemplate.v4.STGroupDir) STGroup(org.stringtemplate.v4.STGroup)

Example 13 with STGroup

use of org.stringtemplate.v4.STGroup in project bndtools by bndtools.

the class StringTemplateEngine method getTemplateParameters.

@Override
public Map<String, String> getTemplateParameters(ResourceMap inputs, IProgressMonitor monitor) throws Exception {
    Map<String, String> params = new HashMap<>();
    // Initialise the engine
    TemplateSettings settings = readSettings(inputs);
    STGroup stg = new STGroup(settings.leftDelim, settings.rightDelim);
    // Assemble a mapping properties file of outputPath=sourcePath
    String mappingTemplate = loadMappingTemplate(inputs, settings, stg);
    extractAttrs(compile(stg, "_mapping", new StringResource(mappingTemplate)), params);
    // Iterate the entries
    Properties contentProps = new Properties();
    contentProps.load(new StringReader(mappingTemplate));
    @SuppressWarnings("unchecked") Enumeration<String> contentEnum = (Enumeration<String>) contentProps.propertyNames();
    while (contentEnum.hasMoreElements()) {
        String outputPath = contentEnum.nextElement().trim();
        String sourcePath = contentProps.getProperty(outputPath);
        Resource source = inputs.get(sourcePath);
        if (source == null)
            throw new RuntimeException(String.format("Internal error in template engine: could not find input resource '%s'", sourcePath));
        if (settings.ignore == null || !settings.ignore.matches(sourcePath)) {
            if (source.getType() == ResourceType.File) {
                if (settings.preprocessMatch.matches(sourcePath)) {
                    extractAttrs(compile(stg, sourcePath, source), params);
                }
            }
        }
    }
    return params;
}
Also used : Enumeration(java.util.Enumeration) STGroup(org.stringtemplate.v4.STGroup) HashMap(java.util.HashMap) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) Properties(java.util.Properties) StringResource(org.bndtools.templating.StringResource) StringReader(java.io.StringReader)

Example 14 with STGroup

use of org.stringtemplate.v4.STGroup in project bndtools by bndtools.

the class StringTemplateEngine method generateOutputs.

@Override
public ResourceMap generateOutputs(ResourceMap inputs, Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
    TemplateSettings settings = readSettings(inputs);
    STGroup stg = new STGroup(settings.leftDelim, settings.rightDelim);
    // Assemble a mapping properties file of outputPath=sourcePath
    String mappingTemplate = loadMappingTemplate(inputs, settings, stg);
    String renderedMapping = render(compile(stg, "_mapping", new StringResource(mappingTemplate)), parameters);
    Properties contentProps = new Properties();
    contentProps.load(new StringReader(renderedMapping));
    // Iterate the content entries
    ResourceMap outputs = new ResourceMap();
    @SuppressWarnings("unchecked") Enumeration<String> contentEnum = (Enumeration<String>) contentProps.propertyNames();
    while (contentEnum.hasMoreElements()) {
        String outputName = contentEnum.nextElement().trim();
        String sourceName = contentProps.getProperty(outputName, "").trim();
        Resource source = inputs.get(sourceName);
        if (source == null)
            throw new RuntimeException(String.format("Internal error in template engine: could not find input resource '%s'", sourceName));
        Resource output;
        if (settings.ignore == null || !settings.ignore.matches(sourceName)) {
            if (source.getType() == ResourceType.Folder) {
                output = source;
            } else if (settings.preprocessMatch.matches(sourceName)) {
                // This file is a candidate for preprocessing with ST
                String rendered = render(compile(stg, sourceName, source), parameters);
                output = new StringResource(rendered);
            } else {
                // This file should be directly copied
                output = source;
            }
            outputs.put(outputName, output);
        }
    }
    return outputs;
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) StringResource(org.bndtools.templating.StringResource) Enumeration(java.util.Enumeration) STGroup(org.stringtemplate.v4.STGroup) StringReader(java.io.StringReader) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) Properties(java.util.Properties)

Example 15 with STGroup

use of org.stringtemplate.v4.STGroup 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)

Aggregations

STGroup (org.stringtemplate.v4.STGroup)26 ST (org.stringtemplate.v4.ST)14 ATNFactory (org.antlr.v4.automata.ATNFactory)6 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)6 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)6 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)6 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)6 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 Grammar (org.antlr.v4.tool.Grammar)6 LexerGrammar (org.antlr.v4.tool.LexerGrammar)6 STGroupString (org.stringtemplate.v4.STGroupString)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 STGroupFile (org.stringtemplate.v4.STGroupFile)5 StringRenderer (org.stringtemplate.v4.StringRenderer)5 NumberRenderer (org.stringtemplate.v4.NumberRenderer)3 STErrorListener (org.stringtemplate.v4.STErrorListener)3 STMessage (org.stringtemplate.v4.misc.STMessage)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 URL (java.net.URL)2