use of pcgen.io.freemarker.PCVarFunction 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) {
}
}
}
}
Aggregations