
/**
 * 
 */
package org.tio.sitexxx.view.http;

import freemarker.template.Configuration;
import org.tio.http.common.HttpConfig;
import org.tio.http.common.view.freemarker.ConfigurationCreater;
import org.tio.utils.freemarker.ShortMessageTemplateExceptionHandler;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

/**
 * @author tanyaowu
 *
 */
public class FreemarkerConfigurationCreater implements ConfigurationCreater {

	public static final FreemarkerConfigurationCreater me = new FreemarkerConfigurationCreater();

	@Override
	public Configuration createConfiguration(HttpConfig httpConfig, String root) throws IOException {
		Configuration cfg = new Configuration(Configuration.getVersion());

		if (httpConfig.isPageInClasspath()) {
			cfg.setClassForTemplateLoading(this.getClass(), root);
		} else {
			cfg.setDirectoryForTemplateLoading(new File(root));
		}

		cfg.setDefaultEncoding(httpConfig.getCharset());

		//开发模式
		cfg.setLogTemplateExceptions(WebViewInit.devMode == 1);

		cfg.setWrapUncheckedExceptions(true);
		cfg.setTemplateExceptionHandler(ShortMessageTemplateExceptionHandler.me);
		cfg.setLocale(Locale.SIMPLIFIED_CHINESE);
		cfg.setNumberFormat("#");
		return cfg;
	}
}
