com.lianggzone.freemarkerutils.utils.FreeMarkerFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.lianggzone.freemarkerutils.utils.FreeMarkerFactory.java

Source

/*
 * Copyright 2013-2015 lianggzone all rights reserved.
 * @license http://www.lianggzone.com/about
 */
package com.lianggzone.freemarkerutils.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * FreeMarker
 * @author 
 * @since 
 * <p>: 2015911  v0.1</p><p>: </p>
 */
public class FreeMarkerFactory {

    /**
     * ?ftl?,???HTML
     * @param ftlPath   FTL?,["c:/liang/template.ftl"]
     * @param filePath  ?HMTL["d:/liang/lianggzone.html"]
     * @param data      Map?
     * @return
     */
    public static boolean createHTML(String ftlPath, String filePath, Map<String, Object> data) throws IOException {
        return createHTML(ftlPath, filePath, data, true);
    }

    /**
     * ?ftl?,???HTML
     * @param ftlPath   FTL?,["c:/liang/template.ftl"]
     * @param filePath  ?HMTL["d:/liang/lianggzone.html"]
     * @param data      Map?
     * @param isCreate4NoExists      ??
     * @return
     */
    public static boolean createHTML(String ftlPath, String filePath, Map<String, Object> data,
            boolean isCreate4NoExists) throws IOException {
        String fileDir = StringUtils.substringBeforeLast(filePath, "/"); // ?HMTL
        //      String fileName = StringUtils.substringAfterLast(filePath, "/");  // ?HMTL??
        String ftlDir = StringUtils.substringBeforeLast(ftlPath, "/"); // ?FTL
        String ftlName = StringUtils.substringAfterLast(ftlPath, "/"); // ?FTL?? 

        //?
        if (isCreate4NoExists) {
            File realDirectory = new File(fileDir);
            if (!realDirectory.exists()) {
                realDirectory.mkdirs();
            }
        }

        // step1 ?freemarker?
        Configuration freemarkerCfg = new Configuration(Configuration.VERSION_2_3_23);
        // step2 freemarker??()
        freemarkerCfg.setDirectoryForTemplateLoading(new File(ftlDir));
        // step3 freemarker??
        freemarkerCfg.setEncoding(Locale.getDefault(), CharEncoding.UTF_8);
        // step4 freemarker?
        Template template = freemarkerCfg.getTemplate(ftlName, CharEncoding.UTF_8);
        // step5 ?IO?
        try (Writer writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(new File(filePath)), CharEncoding.UTF_8))) {
            writer.flush();
            // step6 ??
            template.process(data, writer);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}