use of freemarker.template.Template in project pcgen by PCGen.
the class ExportHandler method exportCharacterUsingFreemarker.
/**
* Produce an output file for a character using a FreeMarker template.
*
* @param aPC The character being output.
* @param outputWriter The destination for the output.
* @throws ExportException If the export fails.
*/
private void exportCharacterUsingFreemarker(PlayerCharacter aPC, BufferedWriter outputWriter) throws ExportException {
try {
// Set Directory for templates
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(templateFile.getParentFile());
cfg.setIncompatibleImprovements(new Version("2.3.20"));
// load template
Template template = cfg.getTemplate(templateFile.getName());
// Configure our custom directives and functions.
cfg.setSharedVariable("pcstring", new PCStringDirective(aPC, this));
cfg.setSharedVariable("pcvar", new PCVarFunction(aPC));
cfg.setSharedVariable("pcboolean", new PCBooleanFunction(aPC, this));
cfg.setSharedVariable("pchasvar", new PCHasVarFunction(aPC, this));
cfg.setSharedVariable("loop", new LoopDirective());
cfg.setSharedVariable("equipsetloop", new EquipSetLoopDirective(aPC));
GameMode gamemode = SettingsHandler.getGame();
// data-model
Map<String, Object> pc = OutputDB.buildDataModel(aPC.getCharID());
Map<String, Object> mode = OutputDB.buildModeDataModel(gamemode);
Map<String, Object> input = new HashMap<>();
input.put("pcgen", OutputDB.getGlobal());
input.put("pc", ObjectWrapper.DEFAULT_WRAPPER.wrap(pc));
input.put("gamemode", mode);
input.put("gamemodename", gamemode.getName());
// Process the template
template.process(input, outputWriter);
} catch (IOException | TemplateException exc) {
String message = "Error exporting character using template " + templateFile;
Logging.errorPrint(message, exc);
throw new ExportException(message + " : " + exc.getLocalizedMessage(), exc);
} finally {
if (outputWriter != null) {
try {
outputWriter.flush();
} catch (Exception ignored) {
}
}
}
}
use of freemarker.template.Template in project intellij-plugins by StepicOrg.
the class Templater method processTemplate.
@NotNull
public static String processTemplate(@NotNull String templateName, @NotNull Map<String, Object> map) {
if (config == null) {
initConfig();
}
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
Template template = config.getTemplate(templateName + ".ftl");
template.process(map, new PrintWriter(out));
return out.toString();
} catch (TemplateException | IOException e) {
logger.warn(e);
}
return "";
}
use of freemarker.template.Template in project asterixdb by apache.
the class GenerateFileMojo method generateFiles.
private void generateFiles() throws TemplateException, IOException {
Map<String, Object> props = getProperties();
Configuration config = new Configuration();
config.setTemplateLoader(new FileTemplateLoader(templateRootDir));
for (GeneratedFile generation : generatedFiles) {
Template template = config.getTemplate(generation.getTemplate(), StandardCharsets.UTF_8.name());
if (template == null) {
throw new IOException("Could not load template " + generation.getTemplate());
}
outputDir.mkdirs();
final File file = new File(outputDir, generation.getOutputFile());
getLog().info("Writing " + file + "...");
try (final FileOutputStream fos = new FileOutputStream(file);
final Writer writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
template.process(props, writer);
}
}
}
use of freemarker.template.Template in project bamboobsc by billchen198318.
the class TemplateUtils method processTemplate.
private static String processTemplate(String resource, Map<String, Object> params) throws Exception {
StringTemplateLoader templateLoader = new StringTemplateLoader();
templateLoader.putTemplate("sysTemplate", resource);
Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
cfg.setTemplateLoader(templateLoader);
Template template = cfg.getTemplate("sysTemplate", Constants.BASE_ENCODING);
Writer out = new StringWriter();
template.process(params, out);
return out.toString();
}
use of freemarker.template.Template in project bamboobsc by billchen198318.
the class TemplateUtils method processTemplate.
/**
* 單獨提供單處理 template 取出結果
*
* @param name
* @param classLoader
* @param templateResource
* @param parameter
* @return
* @throws Exception
*/
public static String processTemplate(String name, ClassLoader classLoader, String templateResource, Map<String, Object> parameter) throws Exception {
StringTemplateLoader templateLoader = new StringTemplateLoader();
templateLoader.putTemplate("resourceTemplate", getResourceSrc(classLoader, templateResource));
Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
cfg.setTemplateLoader(templateLoader);
Template template = cfg.getTemplate("resourceTemplate", Constants.BASE_ENCODING);
Writer out = new StringWriter();
template.process(parameter, out);
return out.toString();
}
Aggregations