/** * Return a new Configuration object. Subclasses can override this for custom * initialization (e.g. specifying a FreeMarker compatibility level which is a * new feature in FreeMarker 2.3.21), or for using a mock object for testing. * <p>Called by {@code createConfiguration()}. * @return the Configuration object * @throws IOException if a config file wasn't found * @throws TemplateException on FreeMarker initialization failure * @see #createConfiguration() */ protected Configuration newConfiguration() throws IOException, TemplateException { return new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); }
/** * 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 = obtainConfiguration().getObjectWrapper(); Version version = Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS; return (ow != null ? ow : new DefaultObjectWrapperBuilder(version).build()); }
public FreemarkerTemplateFormatter() { configuration.setLocale(Locale.CHINA); configuration.setDefaultEncoding("UTF-8"); configuration.setTemplateLoader(templateLoader); configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23)); }
/** * Process the FreeMarker template to the servlet response. * <p>Can be overridden to customize the behavior. * @param template the template to process * @param model the model for the template * @param response servlet response (use this to get the OutputStream or Writer) * @throws IOException if the template file could not be retrieved * @throws TemplateException if thrown by FreeMarker * @see freemarker.template.Template#process(Object, java.io.Writer) */ protected void processTemplate(Template template, SimpleHash model, HttpServletResponse response) throws IOException, TemplateException { template.process(model, response.getWriter()); }
/** * Retrieve the FreeMarker template specified by the given name, * using the encoding specified by the "encoding" bean property. * <p>Can be called by subclasses to retrieve a specific template, * for example to render multiple templates into a single view. * @param name the file name of the desired template * @param locale the current locale * @return the FreeMarker template * @throws IOException if the template file could not be retrieved */ protected Template getTemplate(String name, Locale locale) throws IOException { return (getEncoding() != null ? obtainConfiguration().getTemplate(name, locale, getEncoding()) : obtainConfiguration().getTemplate(name, locale)); }
private static void addKeys(Set set, SimpleSequence keySeq, TemplateHashModelEx hash) throws TemplateModelException { TemplateModelIterator it = hash.keys().iterator(); while (it.hasNext()) { TemplateScalarModel tsm = (TemplateScalarModel) it.next(); if (set.add(tsm.getAsString())) { // The first occurence of the key decides the index; // this is consisten with stuff like java.util.LinkedHashSet. keySeq.add(tsm); } } }
/** * Build a FreeMarker template model for the given model Map. * <p>The default implementation builds a {@link SimpleHash}. * @param model the model to use for rendering * @param exchange current exchange * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof */ protected SimpleHash getTemplateModel(Map<String, Object> model, ServerWebExchange exchange) { SimpleHash fmModel = new SimpleHash(getObjectWrapper()); fmModel.putAll(model); return fmModel; }
public TemplateModel get(String key) throws TemplateModelException { TemplateModel value = rootDataModel.get(key); return value != null ? value : configuration.getSharedVariable(key); } };
/** * @param heading should the heading at the top be printed * @param ftlStackTrace should the FTL stack trace be printed * @param javaStackTrace should the Java stack trace be printed * * @since 2.3.20 */ public void printStackTrace(PrintWriter out, boolean heading, boolean ftlStackTrace, boolean javaStackTrace) { synchronized (out) { printStackTrace(new PrintWriterStackTraceWriter(out), heading, ftlStackTrace, javaStackTrace); } }
/** * @param heading should the heading at the top be printed * @param ftlStackTrace should the FTL stack trace be printed * @param javaStackTrace should the Java stack trace be printed * * @since 2.3.20 */ public void printStackTrace(PrintStream out, boolean heading, boolean ftlStackTrace, boolean javaStackTrace) { synchronized (out) { printStackTrace(new PrintStreamStackTraceWriter(out), heading, ftlStackTrace, javaStackTrace); } }
@Override TemplateModel calculateResult(TemplateNodeModel nodeModel, Environment env) throws TemplateModelException { return new SimpleScalar(nodeModel.getNodeType()); } }
private TemplateModel emptyResult(boolean seq) { return seq ? (_TemplateAPI.getTemplateLanguageVersionAsInt(this) < _TemplateAPI.VERSION_INT_2_3_21 ? new SimpleSequence(Collections.EMPTY_LIST, null) : Constants.EMPTY_SEQUENCE) : TemplateScalarModel.EMPTY_STRING; }
/** * A prime constructor to which all other constructors should * delegate directly or indirectly. */ private Template(String name, String sourceName, Configuration cfg, ParserConfiguration customParserConfiguration) { super(toNonNull(cfg)); this.name = name; this.sourceName = sourceName; this.templateLanguageVersion = normalizeTemplateLanguageVersion(toNonNull(cfg).getIncompatibleImprovements()); this.parserConfiguration = customParserConfiguration != null ? customParserConfiguration : getConfiguration(); }
protected DefaultObjectWrapperConfiguration(Version incompatibleImprovements) { super(DefaultObjectWrapper.normalizeIncompatibleImprovementsVersion(incompatibleImprovements), true); useAdaptersForContainers = getIncompatibleImprovements().intValue() >= _TemplateAPI.VERSION_INT_2_3_22; forceLegacyNonListCollections = true; // [2.4]: = IcI < _TemplateAPI.VERSION_INT_2_4_0; }
public FreemarkerTemplateFormatter() { configuration.setLocale(Locale.CHINA); configuration.setDefaultEncoding("UTF-8"); configuration.setTemplateLoader(templateLoader); configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23)); }
/** * Process the specified FreeMarker template with the given model and write * the result to the given Writer. * <p>When using this method to prepare a text for a mail to be sent with Spring's * mail support, consider wrapping IO/TemplateException in MailPreparationException. * @param model the model object, typically a Map that contains model names * as keys and model objects as values * @return the result as String * @throws IOException if the template wasn't found or couldn't be read * @throws freemarker.template.TemplateException if rendering failed * @see org.springframework.mail.MailPreparationException */ public static String processTemplateIntoString(Template template, Object model) throws IOException, TemplateException { StringWriter result = new StringWriter(); template.process(model, result); return result.toString(); }
/** * Retrieve the FreeMarker template for the given locale, * to be rendering by this view. * <p>By default, the template specified by the "url" bean property * will be retrieved. * @param locale the current locale * @return the FreeMarker template to render */ protected Template getTemplate(Locale locale) throws IOException { return (getEncoding() != null ? obtainConfiguration().getTemplate(getUrl(), locale, getEncoding()) : obtainConfiguration().getTemplate(getUrl(), locale)); }
/** * 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 = obtainConfiguration().getObjectWrapper(); return (ow != null ? ow : new DefaultObjectWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS).build()); }