All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hibegin.template.BasicTemplateRender Maven / Gradle / Ivy

Go to download

Simple, flexible, less dependent, more extended. Less memory footprint, can quickly build Web project. Can quickly run embedded, Android devices

There is a newer version: 0.3.162
Show newest version
package com.hibegin.template;

import com.hibegin.common.util.IOUtil;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class BasicTemplateRender implements TemplateRender {

    private final Map map;
    private final Class clazzLoader;

    public BasicTemplateRender(Map map, Class clazzLoader) {
        this.map = map;
        this.clazzLoader = clazzLoader;
    }

    public String render(InputStream in) {
        if (in == null) {
            return "InputStream in null";
        }
        return render(new String(IOUtil.getByteByInputStream(in), StandardCharsets.UTF_8));
    }

    public String render(String templateStr) {
        String renderResult = templateStr;
        for (Map.Entry entry : map.entrySet()) {
            renderResult = renderResult.replace("${" + entry.getKey() + "}", entry.getValue().toString());
        }
        return renderResult;
    }

    @Override
    public String renderByTemplateName(String templateName) {
        return render(clazzLoader.getResourceAsStream(templateName));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy