cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cn.ksource.core.util;
 
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.List;
import java.util.Locale;
import java.util.Map;
 
 
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.HashAdapter;
import freemarker.ext.beans.MapModel;
import freemarker.template.Configuration;
import freemarker.template.SimpleHash;
import freemarker.template.Template;
import freemarker.template.TemplateHashModel;
 
public class FreeMarkerUtil {
 
    public static Configuration getConfiguration(Class clazz, String templatePath){
        Configuration cfg = new Configuration();
        try {
//            File templateDir = new File(templatePath);
//            cfg.setDirectoryForTemplateLoading(templateDir);
            cfg.setClassForTemplateLoading(clazz, templatePath);
            cfg.setLocale(Locale.CHINA);
            cfg.setDefaultEncoding("utf-8");
            cfg.setClassicCompatible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cfg;
    }
 
    
    /**
     * 判断给定的类,是否为SimpleHash
     * @param object
     * @return
     * 作者:杨凯
     */
    public static boolean isSimpleHash(Object object){
        if (object instanceof HashAdapter) {
            return true;
        }
        return false;
    }
    
    
 
    /**
     * 功能描述:将SimpleHash转换为Map<BR>
     * @param sh
     * @return
     * @author:杨凯<BR>
     * 时间:Jul 29, 2009 11:14:33 AM<BR>
     */
    public static Map simpleHash2Map(SimpleHash sh){
        if (sh == null) {
            return null;
        }
        Map map = null;
        try {
            map = sh.toMap();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return map;
        }
    }
    
    /**
     * 功能描述:将MapModel转换为Map<BR>
     * @param mapModel
     * @return
     * @author:杨凯<BR>
     * 时间:Aug 10, 2009 11:28:35 AM<BR>
     */
    public static Map mapModel2Map(MapModel mapModel){
        return (Map)mapModel.getWrappedObject();
    }
    
    /**
     * @Description:list.get(i)<BR>
     * @Title: getMapFromList
     * @param list
     * @param i
     * @return Map
     * @author huxiao ybhuxiao@gmail.com<BR>
     * Time:2009-8-19  下午04:02:07<BR>
     */
    public static Map getMapFromList(List<Map> list, int i){
        return list.get(i);
    }
}