Search in sources :

Example 11 with Resource

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());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) DigestInputStream(java.security.DigestInputStream) ProgressMonitorReporter(org.bndtools.utils.progress.ProgressMonitorReporter) HashMap(java.util.HashMap) Resource(org.bndtools.templating.Resource) Template(org.bndtools.templating.Template) ResourceMap(org.bndtools.templating.ResourceMap) Entry(java.util.Map.Entry) List(java.util.List)

Example 12 with Resource

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Properties(java.util.Properties)

Example 13 with Resource

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;
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) JarInputStream(java.util.jar.JarInputStream) BytesResource(org.bndtools.templating.BytesResource) Resource(org.bndtools.templating.Resource) FolderResource(org.bndtools.templating.FolderResource) FolderResource(org.bndtools.templating.FolderResource) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 14 with Resource

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Properties(java.util.Properties)

Example 15 with Resource

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;
}
Also used : StringWriter(java.io.StringWriter) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) PrintWriter(java.io.PrintWriter)

Aggregations

Resource (org.bndtools.templating.Resource)17 ResourceMap (org.bndtools.templating.ResourceMap)10 StringResource (org.bndtools.templating.StringResource)10 HashMap (java.util.HashMap)8 StringReader (java.io.StringReader)7 Properties (java.util.Properties)7 List (java.util.List)6 Template (org.bndtools.templating.Template)6 InputStreamReader (java.io.InputStreamReader)5 Entry (java.util.Map.Entry)5 ProgressMonitorReporter (org.bndtools.utils.progress.ProgressMonitorReporter)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 Reader (java.io.Reader)3 StringWriter (java.io.StringWriter)3 DigestInputStream (java.security.DigestInputStream)3 DefaultMustacheFactory (com.github.mustachejava.DefaultMustacheFactory)2 File (java.io.File)2 InputStream (java.io.InputStream)2 Enumeration (java.util.Enumeration)2 BuiltInTemplate (org.bndtools.core.ui.wizards.shared.BuiltInTemplate)2