Search in sources :

Example 1 with Version

use of freemarker.template.Version in project spring-framework by spring-projects.

the class FreeMarkerView method getObjectWrapper.

/**
	 * Return the configured FreeMarker {@link ObjectWrapper}, or the
	 * {@link ObjectWrapper#DEFAULT_WRAPPER default wrapper} if none specified.
	 * @see freemarker.template.Configuration#getObjectWrapper()
	 */
protected ObjectWrapper getObjectWrapper() {
    ObjectWrapper ow = getConfiguration().getObjectWrapper();
    Version version = Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS;
    return (ow != null ? ow : new DefaultObjectWrapperBuilder(version).build());
}
Also used : Version(freemarker.template.Version) ObjectWrapper(freemarker.template.ObjectWrapper) DefaultObjectWrapperBuilder(freemarker.template.DefaultObjectWrapperBuilder)

Example 2 with Version

use of freemarker.template.Version in project deeplearning4j by deeplearning4j.

the class StaticPageUtil method renderHTMLContent.

public static String renderHTMLContent(Component... components) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    Configuration cfg = new Configuration(new Version(2, 3, 23));
    // Where do we load the templates from:
    cfg.setClassForTemplateLoading(StaticPageUtil.class, "");
    // Some other recommended settings:
    cfg.setIncompatibleImprovements(new Version(2, 3, 23));
    cfg.setDefaultEncoding("UTF-8");
    cfg.setLocale(Locale.US);
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    ClassPathResource cpr = new ClassPathResource("assets/dl4j-ui.js");
    String scriptContents = IOUtils.toString(cpr.getInputStream(), "UTF-8");
    Map<String, Object> pageElements = new HashMap<>();
    List<ComponentObject> list = new ArrayList<>();
    int i = 0;
    for (Component c : components) {
        list.add(new ComponentObject(String.valueOf(i), mapper.writeValueAsString(c)));
        i++;
    }
    pageElements.put("components", list);
    pageElements.put("scriptcontent", scriptContents);
    Template template = cfg.getTemplate("staticpage.ftl");
    Writer stringWriter = new StringWriter();
    template.process(pageElements, stringWriter);
    return stringWriter.toString();
}
Also used : Configuration(freemarker.template.Configuration) Template(freemarker.template.Template) StringWriter(java.io.StringWriter) Version(freemarker.template.Version) Component(org.deeplearning4j.ui.api.Component) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 3 with Version

use of freemarker.template.Version 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

Version (freemarker.template.Version)3 Configuration (freemarker.template.Configuration)2 Template (freemarker.template.Template)2 DefaultObjectWrapperBuilder (freemarker.template.DefaultObjectWrapperBuilder)1 ObjectWrapper (freemarker.template.ObjectWrapper)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 Component (org.deeplearning4j.ui.api.Component)1 ObjectMapper (org.nd4j.shade.jackson.databind.ObjectMapper)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