Search in sources :

Example 66 with Template

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) {
            }
        }
    }
}
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)

Example 67 with Template

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 "";
}
Also used : TemplateException(freemarker.template.TemplateException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Template(freemarker.template.Template) PrintWriter(java.io.PrintWriter) NotNull(org.jetbrains.annotations.NotNull)

Example 68 with Template

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);
        }
    }
}
Also used : Configuration(freemarker.template.Configuration) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) FileTemplateLoader(freemarker.cache.FileTemplateLoader) JarFile(java.util.jar.JarFile) File(java.io.File) SequenceWriter(com.fasterxml.jackson.databind.SequenceWriter) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Template(freemarker.template.Template)

Example 69 with Template

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();
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TbSysTemplate(com.netsteadfast.greenstep.po.hbm.TbSysTemplate) Template(freemarker.template.Template)

Example 70 with Template

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();
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TbSysTemplate(com.netsteadfast.greenstep.po.hbm.TbSysTemplate) Template(freemarker.template.Template)

Aggregations

Template (freemarker.template.Template)72 IOException (java.io.IOException)33 StringWriter (java.io.StringWriter)32 Configuration (freemarker.template.Configuration)30 Writer (java.io.Writer)26 HashMap (java.util.HashMap)26 TemplateException (freemarker.template.TemplateException)23 OutputStreamWriter (java.io.OutputStreamWriter)13 File (java.io.File)9 Map (java.util.Map)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 JSONObject (org.json.JSONObject)6 SimpleHash (freemarker.template.SimpleHash)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 StringTemplateLoader (freemarker.cache.StringTemplateLoader)4 Environment (freemarker.core.Environment)4 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)4 BufferedWriter (java.io.BufferedWriter)4 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4