use of org.bndtools.templating.Resource in project bndtools by bndtools.
the class ReposTemplateLoaderTest method testExtendUnprocessedPatternAndIgnore.
public void testExtendUnprocessedPatternAndIgnore() throws Exception {
List<Template> templates = loader.findTemplates("test4", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
assertEquals(1, templates.size());
Template template = templates.get(0);
Map<String, List<Object>> parameters = new HashMap<>();
parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
ResourceMap outputs = template.generateOutputs(parameters);
assertEquals(1, outputs.size());
Entry<String, Resource> entry;
Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
entry = iter.next();
assertEquals("pic.xxx", entry.getKey());
// Check the digest of the pic to ensure it didn't get damaged by the templating engine
DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
IO.drain(digestStream);
byte[] digest = digestStream.getMessageDigest().digest();
assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
}
use of org.bndtools.templating.Resource in project bndtools by bndtools.
the class MustacheTemplateEngine method readSettings.
private static TemplateSettings readSettings(ResourceMap inputs) throws IOException, UnsupportedEncodingException {
Properties settingsProp = new Properties();
Resource settingsResource = inputs.remove(TEMPLATE_PROPERTIES);
if (settingsResource != null) {
if (settingsResource.getType() != ResourceType.File)
throw new IllegalArgumentException(String.format("Template settings resource %s must be a file; found resource type %s.", TEMPLATE_PROPERTIES, settingsResource.getType()));
try (Reader reader = new InputStreamReader(settingsResource.getContent(), settingsResource.getTextEncoding())) {
settingsProp.load(reader);
}
}
TemplateSettings settings = TemplateSettings.readFrom(settingsProp);
return settings;
}
use of org.bndtools.templating.Resource in project bndtools by bndtools.
the class CapabilityBasedTemplate method getInputSources.
private synchronized ResourceMap getInputSources() throws IOException {
File bundleFile = fetchBundle();
_inputResources = new ResourceMap();
try (JarInputStream in = new JarInputStream(IO.stream(bundleFile))) {
JarEntry jarEntry = in.getNextJarEntry();
while (jarEntry != null) {
String entryPath = jarEntry.getName().trim();
if (entryPath.startsWith(dir)) {
String relativePath = entryPath.substring(dir.length());
if (!relativePath.isEmpty()) {
// skip the root folder
Resource resource;
if (relativePath.endsWith("/")) {
// strip the trailing slash
relativePath.substring(0, relativePath.length());
resource = new FolderResource();
} else {
// cannot use IO.collect() because it closes the whole JarInputStream
resource = BytesResource.loadFrom(in);
}
_inputResources.put(relativePath, resource);
}
}
jarEntry = in.getNextJarEntry();
}
}
return _inputResources;
}
use of org.bndtools.templating.Resource in project bndtools by bndtools.
the class StringTemplateEngine method readSettings.
private TemplateSettings readSettings(ResourceMap inputs) throws IOException, UnsupportedEncodingException {
Properties settingsProp = new Properties();
Resource settingsResource = inputs.remove(TEMPLATE_PROPERTIES);
if (settingsResource != null) {
if (settingsResource.getType() != ResourceType.File)
throw new IllegalArgumentException(String.format("Template settings resource %s must be a file; found resource type %s.", TEMPLATE_PROPERTIES, settingsResource.getType()));
try (Reader reader = new InputStreamReader(settingsResource.getContent(), settingsResource.getTextEncoding())) {
settingsProp.load(reader);
}
}
TemplateSettings settings = TemplateSettings.readFrom(settingsProp);
return settings;
}
use of org.bndtools.templating.Resource in project bndtools by bndtools.
the class StringTemplateEngine method loadMappingTemplate.
private String loadMappingTemplate(ResourceMap inputs, TemplateSettings settings, STGroup stg) throws IOException {
StringWriter buf = new StringWriter();
try (PrintWriter bufPrint = new PrintWriter(buf)) {
for (String inputPath : inputs.getPaths()) {
if (inputPath.startsWith(TEMPLATE_DEFS_PREFIX)) {
if (inputPath.endsWith(TEMPLATE_FILE_SUFFIX)) {
// Definition... load into StringTemplate group and don't generate output
String inputPathRelative = inputPath.substring(TEMPLATE_DEFS_PREFIX.length());
Resource resource = inputs.get(inputPath);
if (resource != null && resource.getType() == ResourceType.File)
loadTemplate(stg, inputPathRelative, resource.getContent(), resource.getTextEncoding());
}
} else {
// Mapping to output file
String outputPath = inputPath;
String escapedSourcePath = escapeDelimiters(inputPath, settings);
bufPrint.printf("%s=%s%n", outputPath, escapedSourcePath);
}
}
}
String mappingTemplate = buf.toString();
return mappingTemplate;
}
Aggregations