Search in sources :

Example 1 with PCHasVarFunction

use of pcgen.io.freemarker.PCHasVarFunction 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) {
            }
        }
    }
}
Also used : EquipSetLoopDirective(pcgen.io.freemarker.EquipSetLoopDirective) LoopDirective(pcgen.io.freemarker.LoopDirective) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) EquipSetLoopDirective(pcgen.io.freemarker.EquipSetLoopDirective) ParseException(java.text.ParseException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) PCTemplate(pcgen.core.PCTemplate) Template(freemarker.template.Template) GameMode(pcgen.core.GameMode) PCBooleanFunction(pcgen.io.freemarker.PCBooleanFunction) Version(freemarker.template.Version) PCStringDirective(pcgen.io.freemarker.PCStringDirective) PCVarFunction(pcgen.io.freemarker.PCVarFunction) PCHasVarFunction(pcgen.io.freemarker.PCHasVarFunction) PObject(pcgen.core.PObject) CDOMObject(pcgen.cdom.base.CDOMObject)

Aggregations

Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 Version (freemarker.template.Version)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 CDOMObject (pcgen.cdom.base.CDOMObject)1 GameMode (pcgen.core.GameMode)1 PCTemplate (pcgen.core.PCTemplate)1 PObject (pcgen.core.PObject)1 EquipSetLoopDirective (pcgen.io.freemarker.EquipSetLoopDirective)1 LoopDirective (pcgen.io.freemarker.LoopDirective)1 PCBooleanFunction (pcgen.io.freemarker.PCBooleanFunction)1 PCHasVarFunction (pcgen.io.freemarker.PCHasVarFunction)1 PCStringDirective (pcgen.io.freemarker.PCStringDirective)1 PCVarFunction (pcgen.io.freemarker.PCVarFunction)1