io.inugami.commons.files.TemplateRendering Maven / Gradle / Ivy
/* --------------------------------------------------------------------
* Inugami
* --------------------------------------------------------------------
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package io.inugami.commons.files;
import java.io.File;
import java.io.IOException;
import java.util.Map;
/**
* TemplateRendering
*
* @author patrick_guillerm
* @since 23 juin 2017
*/
public class TemplateRendering {
// =========================================================================
// render
// =========================================================================
public String render(final File template, final Map properties) throws IOException {
FilesUtils.assertIsFile(template);
FilesUtils.assertCanRead(template);
String content = "";
final byte[] data = FilesUtils.readBytes(template);
if (data != null) {
content = new String(data);
}
if (!properties.isEmpty()) {
for (final Map.Entry entry : properties.entrySet()) {
content = applyProperty(content, entry.getKey(), entry.getValue());
}
}
return content;
}
// =========================================================================
// applyProperty
// =========================================================================
public String applyProperty(final String content, final String key, final String value) {
//@formatter:off
final String regex = new StringBuilder()
.append("[$][{]")
.append(key)
.append("[}]")
.toString();
//@formatter:on
return content.replaceAll(regex, value);
}
}