Java Code Examples for freemarker.template.Configuration#setOutputFormat()

The following examples show how to use freemarker.template.Configuration#setOutputFormat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrillRestServer.java    From Bats with Apache License 2.0 7 votes vote down vote up
/**
 * Creates freemarker configuration settings,
 * default output format to trigger auto-escaping policy
 * and template loaders.
 *
 * @param servletContext servlet context
 * @return freemarker configuration settings
 */
private Configuration getFreemarkerConfiguration(ServletContext servletContext) {
  Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
  configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);

  List<TemplateLoader> loaders = new ArrayList<>();
  loaders.add(new WebappTemplateLoader(servletContext));
  loaders.add(new ClassTemplateLoader(DrillRestServer.class, "/"));
  try {
    loaders.add(new FileTemplateLoader(new File("/")));
  } catch (IOException e) {
    logger.error("Could not set up file template loader.", e);
  }
  configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
  return configuration;
}
 
Example 2
Source File: FreemarkerTemplateEngineFactory.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
    configuration.setDefaultEncoding("utf-8");
    configuration.setLogTemplateExceptions(true);
    configuration.setNumberFormat("computer");
    configuration.setOutputFormat(XHTMLOutputFormat.INSTANCE);
    configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    configuration.setTemplateLoader(new SpringTemplateLoader(this.resourceLoader, this.resourceLoaderPath));

    if (this.shouldCheckForTemplateModifications) {
        configuration.setTemplateUpdateDelayMilliseconds(1000);
    } else {
        configuration.setTemplateUpdateDelayMilliseconds(Long.MAX_VALUE);
    }

    this.configuration = configuration;
}
 
Example 3
Source File: FreeMarkerService.java    From freemarker-online-tester with Apache License 2.0 5 votes vote down vote up
private FreeMarkerService(Builder bulder) {
     maxOutputLength = bulder.getMaxOutputLength();
     maxThreads = bulder.getMaxThreads();
     maxQueueLength = bulder.getMaxQueueLength();
     maxTemplateExecutionTime = bulder.getMaxTemplateExecutionTime();

     int actualMaxQueueLength = maxQueueLength != null
             ? maxQueueLength
             : Math.max(
                     MIN_DEFAULT_MAX_QUEUE_LENGTH,
                     (int) (MAX_DEFAULT_MAX_QUEUE_LENGTH_MILLISECONDS / maxTemplateExecutionTime));
     ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
             maxThreads, maxThreads,
             THREAD_KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
             new BlockingArrayQueue<Runnable>(actualMaxQueueLength));
     threadPoolExecutor.allowCoreThreadTimeOut(true);
     templateExecutor = threadPoolExecutor;

     // Avoid ERROR log for using the actual current version. This application is special in that regard.
     Version latestVersion = new Version(Configuration.getVersion().toString());

     freeMarkerConfig = new Configuration(latestVersion);
     freeMarkerConfig.setNewBuiltinClassResolver(TemplateClassResolver.ALLOWS_NOTHING_RESOLVER);
     freeMarkerConfig.setObjectWrapper(new SimpleObjectWrapperWithXmlSupport(latestVersion));
     freeMarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
     freeMarkerConfig.setLogTemplateExceptions(false);
     freeMarkerConfig.setAttemptExceptionReporter(new AttemptExceptionReporter() {
@Override
public void report(TemplateException te, Environment env) {
	// Suppress it
}
     });
     freeMarkerConfig.setLocale(AllowedSettingValues.DEFAULT_LOCALE);
     freeMarkerConfig.setTimeZone(AllowedSettingValues.DEFAULT_TIME_ZONE);
     freeMarkerConfig.setOutputFormat(AllowedSettingValues.DEFAULT_OUTPUT_FORMAT);
     freeMarkerConfig.setOutputEncoding("UTF-8");
 }
 
Example 4
Source File: FreeMarkerUtil.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private Template getTemplate(String templateName, Theme theme) throws IOException {
    Configuration cfg = new Configuration();

    // Assume *.ftl files are html.  This lets freemarker know how to
    // sanitize and prevent XSS attacks.
    if (templateName.toLowerCase().endsWith(".ftl")) {
        cfg.setOutputFormat(HTMLOutputFormat.INSTANCE);
    }
    
    cfg.setTemplateLoader(new ThemeTemplateLoader(theme));
    return cfg.getTemplate(templateName, "UTF-8");
}
 
Example 5
Source File: RestServerV2.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private Configuration getFreemarkerConfiguration() {
  Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
  configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);
  configuration.setClassForTemplateLoading(getClass(), "/");
  return configuration;
}