use of org.bndtools.templating.StringResource in project bndtools by bndtools.
the class MustacheTemplateEngine method generateOutputs.
@Override
public ResourceMap generateOutputs(ResourceMap inputs, Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
TemplateSettings settings = readSettings(inputs);
Properties defaults = readDefaults(inputs);
ResourceMap outputs = new ResourceMap();
final Map<String, Object> flattenedParams = flattenParameters(parameters);
applyDefaults(defaults, flattenedParams);
DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory();
mustacheFactory.setObjectHandler(new CheckMissingObjectHandler(mustacheFactory.getObjectHandler()));
for (Entry<String, Resource> entry : inputs.entries()) {
String inputPath = entry.getKey();
Resource source = entry.getValue();
StringWriter writer = new StringWriter();
mustacheFactory.compile(new StringReader(inputPath), "mapping", settings.leftDelim, settings.rightDelim).execute(writer, flattenedParams);
String outputPath = writer.toString();
if (settings.ignore == null || !settings.ignore.matches(inputPath)) {
Resource output;
switch(source.getType()) {
case Folder:
output = source;
break;
case File:
if (settings.preprocessMatch.matches(inputPath)) {
// This file should be processed with the template engine
InputStreamReader reader = new InputStreamReader(source.getContent(), source.getTextEncoding());
StringWriter rendered = new StringWriter();
mustacheFactory.compile(reader, outputPath, settings.leftDelim, settings.rightDelim).execute(rendered, flattenedParams);
output = new StringResource(rendered.toString());
} else {
// This file should be directly copied
output = source;
}
break;
default:
throw new IllegalArgumentException("Unknown resource type " + source.getType());
}
outputs.put(outputPath, output);
}
}
return outputs;
}
use of org.bndtools.templating.StringResource 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.bndtools.templating.StringResource 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.bndtools.templating.StringResource in project bndtools by bndtools.
the class MustacheTemplateEngineTest method testGetParamNames.
@Test
public void testGetParamNames() throws Exception {
MustacheTemplateEngine engine = new MustacheTemplateEngine();
ResourceMap input = new ResourceMap();
input.put("readme.txt", new StringResource("Blah {{fish}} blah {{battleship}} blah {{antidisestablishmentarianism}}"));
Map<String, String> params = engine.getTemplateParameters(input, new NullProgressMonitor());
assertTrue(params.containsKey("fish"));
assertTrue(params.containsKey("battleship"));
assertTrue(params.containsKey("antidisestablishmentarianism"));
}
use of org.bndtools.templating.StringResource in project bndtools by bndtools.
the class MustacheTemplateEngineTest method testNoProcessDefaultPattern.
@Test
public void testNoProcessDefaultPattern() throws Exception {
MustacheTemplateEngine engine = new MustacheTemplateEngine();
ResourceMap input = new ResourceMap();
input.put("{{srcDir}}/", new FolderResource());
input.put("{{srcDir}}/{{packageDir}}/", new FolderResource());
input.put("{{srcDir}}/{{packageDir}}/package-info.java", new StringResource("package {{packageName}};"));
input.put("{{srcDir}}/{{packageDir}}/package-info.jpg", new StringResource("package {{packageName}};"));
Map<String, List<Object>> params = new HashMap<>();
params.put("srcDir", Collections.<Object>singletonList("src"));
params.put("packageDir", Collections.<Object>singletonList("org/example/foo"));
params.put("packageName", Collections.<Object>singletonList("org.example.foo"));
ResourceMap output = engine.generateOutputs(input, params, new NullProgressMonitor());
assertEquals(4, output.size());
assertEquals("package org.example.foo;", IO.collect(output.get("src/org/example/foo/package-info.java").getContent()));
assertEquals("package {{packageName}};", IO.collect(output.get("src/org/example/foo/package-info.jpg").getContent()));
}
Aggregations