com.bsb.cms.commons.template.freemarker.util.FreemarkerUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.bsb.cms.commons.template.freemarker.util.FreemarkerUtils.java

Source

/**
 * @(#)FreemarkerUtils.java 2014-7-7
 *
 * Copyright (c) 2014-2015 BuShangBan (China) Int'l Co., Ltd
 * yongtai Road. pu dong District.Shanghai China
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of BuShangBan (China)
 *  Int'l Co., Ltd ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with BuShangBan (China).
 */
package com.bsb.cms.commons.template.freemarker.util;

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

import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModelException;

/**
 * Freemarker?.
 * @author hongjian.liu
 * @version 1.0.0 2014-6-13
 * @since 1.0
 */
public class FreemarkerUtils {
    /**
     * ???
     */
    public static final boolean IS_DEV_MODE = false;

    /**
     * 
     * @param message
     */
    public static void myOut(String message) {
        if (IS_DEV_MODE) {
            System.out.println(message);
        }
    }

    /**
     * ?? 
     * @param cfg
     * @throws TemplateModelException 
     */
    public static void setTemplateEnv(Configuration cfg) {
        cfg.setClassicCompatible(true);// ???null??$(empty!"emptyValue of fbysss"}??)

        //cfg.setTemplateExceptionHandler(new com.iss.pms.core.freemarker.method.DefaultTemplateException());
        setGolbalVariable(cfg); //???
    }

    //?
    /**
     * ???global 
     */
    private static void setGolbalVariable(Configuration cfg) {
        try {
            BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
            TemplateHashModel staticClass = wrapper.getStaticModels();
            TemplateHashModel apacheStringUtils = (TemplateHashModel) staticClass
                    .get("com.bsb.cms.commons.utils.StringUtils");
            cfg.setSharedVariable("StringUtils", apacheStringUtils);//??

        } catch (TemplateModelException e) {
            e.printStackTrace();
        }
    }

    /**
     * ??
     * @param map
     */
    public static void getMap(Map<String, Object> map) {
        //map.put("contextPath", "");//web_base
    }

    /**
     * ?spring (org.springframework.ui.freemarker.FreeMarkerTemplateUtils)
     * 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();
    }

    /**
     * ?????
     * @param env
     * @param name
     * @param value
     * @throws Exception
     */
    public static void setVariable(Environment env, String name, Object value) throws Exception {
        env.setVariable(name, ObjectWrapper.DEFAULT_WRAPPER.wrap(value));
        // env.setLocalVariable("codes", codes);
    }
}