Java Code Examples for freemarker.template.Configuration#VERSION_2_3_22

The following examples show how to use freemarker.template.Configuration#VERSION_2_3_22 . 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: TemplateProcessor.java    From contribution with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Generates output file with release notes using FreeMarker.
 *
 * @param variables the map which represents template variables.
 * @param outputFile output file.
 * @param templateFileName the optional file name of the template.
 * @param defaultResource the resource file name to use if no file name was given.
 * @throws IOException if I/O error occurs.
 * @throws TemplateException if an error occurs while generating freemarker template.
 */
public static void generateWithFreemarker(Map<String, Object> variables, String outputFile,
        String templateFileName, String defaultResource) throws IOException, TemplateException {

    final Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
    configuration.setDefaultEncoding("UTF-8");
    configuration.setLocale(Locale.US);
    configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    configuration.setNumberFormat("0.######");

    final StringTemplateLoader loader = new StringTemplateLoader();
    loader.putTemplate(TEMPLATE_NAME, loadTemplate(templateFileName, defaultResource));
    configuration.setTemplateLoader(loader);

    final Template template = configuration.getTemplate(TEMPLATE_NAME);
    try (Writer fileWriter = new OutputStreamWriter(
            new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {
        template.process(variables, fileWriter);
    }
}
 
Example 2
Source File: ViewFreemarker.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@PostConstruct
public void init()
{
  Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
  
  cfg.setDefaultEncoding("UTF-8");
  
  String templatePath = _config.get("view.freemarker.templates",
                                   "classpath:/templates");
  
  ClassLoader loader = Thread.currentThread().getContextClassLoader();
  
  if (templatePath.startsWith("classpath:")) {
    String path = templatePath.substring(("classpath:").length());
    
    cfg.setClassLoaderForTemplateLoading(loader, path);
  }
  else {
    throw new UnsupportedOperationException();
  }
  
  cfg.setAutoFlush(false);
  
  cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
  
  _cfg = cfg;
}
 
Example 3
Source File: MailService.java    From abixen-platform with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Async
public void sendMail(String to, Map<String, String> parameters, String template, String subject) {
    log.debug("sendMail() - to: " + to);
    MimeMessage message = mailSender.createMimeMessage();
    try {
        String stringDir = MailService.class.getResource("/templates/freemarker").getPath();
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
        cfg.setDefaultEncoding("UTF-8");

        File dir = new File(stringDir);
        cfg.setDirectoryForTemplateLoading(dir);
        cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_22));

        StringBuffer content = new StringBuffer();
        Template temp = cfg.getTemplate(template + ".ftl");
        content.append(FreeMarkerTemplateUtils.processTemplateIntoString(temp, parameters));

        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(content.toString(), "text/html;charset=\"UTF-8\"");

        MimeMultipart multipart = new MimeMultipart("related");
        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);
        message.setFrom(new InternetAddress(platformMailConfigurationProperties.getUser().getUsername(), platformMailConfigurationProperties.getUser().getName()));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(subject);
        mailSender.send(message);

        log.info("Message has been sent to: " + to);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: Freemarker.java    From template-benchmark with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Setup
public void setup() throws IOException {
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
    configuration.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));
    template = configuration.getTemplate("templates/stocks.freemarker.html");
    this.context = getContext();
}
 
Example 5
Source File: WelcomePage.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
/**
 * 创建 Configuration 实例.
 *
 * @return
 * @throws IOException
 */
private Configuration config(String ftlDir) throws IOException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    cfg.setDirectoryForTemplateLoading(new File(ftlDir));
    cfg.setDefaultEncoding(StandardCharsets.UTF_8.name());
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    return cfg;
}
 
Example 6
Source File: HtmlTriplePatternFragmentWriterImpl.java    From Server.Java with MIT License 5 votes vote down vote up
/**
 *
 * @param prefixes
 * @param datasources
 * @throws IOException
 */
public HtmlTriplePatternFragmentWriterImpl(Map<String, String> prefixes, HashMap<String, IDataSource> datasources) throws IOException {
    super(prefixes, datasources);
    
    cfg = new Configuration(Configuration.VERSION_2_3_22);
    cfg.setClassForTemplateLoading(getClass(), "/views");
    cfg.setDefaultEncoding("UTF-8");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    
    indexTemplate = cfg.getTemplate("index.ftl.html");
    datasourceTemplate = cfg.getTemplate("datasource.ftl.html");
    notfoundTemplate = cfg.getTemplate("notfound.ftl.html");
    errorTemplate = cfg.getTemplate("error.ftl.html");
}
 
Example 7
Source File: DocUtil.java    From OASystem with MIT License 4 votes vote down vote up
public DocUtil() {
	configure = new Configuration(Configuration.VERSION_2_3_22);
	configure.setDefaultEncoding("utf-8");
}
 
Example 8
Source File: FreemarkerConfigBuilder.java    From ymate-platform-v2 with Apache License 2.0 4 votes vote down vote up
public Version getVersion() {
    return version != null ? version : Configuration.VERSION_2_3_22;
}
 
Example 9
Source File: FreeMarkerRender.java    From hangout with MIT License 4 votes vote down vote up
public FreeMarkerRender(String template, String templateName)
        throws IOException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    this.t = new Template(templateName, template, cfg);
}