码迷,mamicode.com
首页 > 编程语言 > 详细

java工具类-FreeMarker

时间:2014-07-29 12:27:06      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:java   os   io   for   ar   new   res   app   

package com.huawei.it.citools.utils;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

import org.apache.log4j.Logger;

import com.huawei.it.citools.commons.ApplicationException;
import com.huawei.it.citools.commons.CiConstants;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
 
public class FreeMarkerUtils
{

    private static Logger LOGGER = Logger.getLogger (FreeMarkerUtils.class);

    private static FreeMarkerUtils freemarkerUtils = new FreeMarkerUtils();

    private Configuration configuration;

    private FreeMarkerUtils()
    {
        try
        {
            File currFile = new File ("");
            String templatePath = currFile.getCanonicalPath() + "/template/";

            LOGGER.info ("freemarker template file path : " + templatePath);

            configuration = new Configuration();
            File path = new File (templatePath);
            configuration.setDirectoryForTemplateLoading (path);
            configuration.setDefaultEncoding ("UTF-8");
        }
        catch (Exception e)
        {
            LOGGER.info ("freemarker configuration fail....." + e);

            throw new ApplicationException ("freemarker configuration fail.....", e);
        }
    }
 
    public String process (Map rootMap, String ftlName) throws IOException, TemplateException
    {
        StringWriter sw = new StringWriter();
        try {
            Template template = configuration.getTemplate (ftlName);
            template.process (rootMap, sw);
            String result = sw.toString();
            return result;
        } finally {
            sw.close();
        }
    }
    public static FreeMarkerUtils newInstance()
    {
        return freemarkerUtils;
    }
}

java工具类-FreeMarker,布布扣,bubuko.com

java工具类-FreeMarker

标签:java   os   io   for   ar   new   res   app   

原文地址:http://www.cnblogs.com/heling/p/3874719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!