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);
}
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();
}
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;
}
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;
}
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;
}
}
Aggregations