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 package info.magnolia.freemarker;
35
36 import info.magnolia.freemarker.models.MagnoliaModelFactory;
37 import info.magnolia.freemarker.models.MagnoliaObjectWrapper;
38
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 import javax.inject.Singleton;
45
46 import freemarker.cache.ClassTemplateLoader;
47 import freemarker.cache.MultiTemplateLoader;
48 import freemarker.cache.TemplateLoader;
49 import freemarker.template.ObjectWrapper;
50 import freemarker.template.TemplateExceptionHandler;
51 import freemarker.template.TemplateModel;
52 import freemarker.template.TemplateModelException;
53
54
55
56
57
58
59
60
61
62
63
64 @Singleton
65 public class FreemarkerConfig {
66
67
68
69
70 private List<MagnoliaModelFactory> registeredModelFactories = new ArrayList<MagnoliaModelFactory>();
71 private List<TemplateLoader> templateLoaders = new ArrayList<TemplateLoader>();
72 private Map<String, TemplateModel> sharedVariables = new HashMap<String, TemplateModel>();
73 private TemplateExceptionHandler templateExceptionHandler = TemplateExceptionHandler.IGNORE_HANDLER;
74
75 private ObjectWrapper objectWrapper;
76 private TemplateLoader multiTL;
77
78 public FreemarkerConfig() {
79 }
80
81 public ObjectWrapper getObjectWrapper() {
82 if (objectWrapper == null) {
83 objectWrapper = newObjectWrapper();
84 }
85 return objectWrapper;
86 }
87
88 protected ObjectWrapper newObjectWrapper() {
89 return new MagnoliaObjectWrapper(this);
90 }
91
92 public TemplateLoader getTemplateLoader() {
93
94 if (multiTL == null) {
95
96 final List<TemplateLoader> loaders = getTemplateLoaders();
97
98 if (loaders.isEmpty()) {
99 multiTL = new ClassTemplateLoader(getClass(), "/");
100 } else {
101 final TemplateLoader[] tl = loaders.toArray(new TemplateLoader[loaders.size()]);
102 multiTL = new MultiTemplateLoader(tl);
103 }
104 }
105 return multiTL;
106 }
107
108 public List<MagnoliaModelFactory> getModelFactories() {
109 return registeredModelFactories;
110 }
111
112 public void setModelFactories(List<MagnoliaModelFactory> registeredModelFactories) {
113 this.registeredModelFactories.addAll(registeredModelFactories);
114 }
115
116 public List<TemplateLoader> getTemplateLoaders() {
117 return templateLoaders;
118 }
119
120 public void setTemplateLoaders(List<TemplateLoader> templateLoaders) {
121 this.templateLoaders.addAll(templateLoaders);
122 }
123
124 public Map<String, TemplateModel> getSharedVariables() {
125 return sharedVariables;
126 }
127
128 public void setSharedVariables(Map<String, Object> configuredSharedVariables) throws TemplateModelException {
129 for (Map.Entry<String, Object> sv : configuredSharedVariables.entrySet()) {
130 sharedVariables.put(sv.getKey(), getObjectWrapper().wrap(sv.getValue()));
131 }
132 }
133
134
135 void addSharedVariable(String name, Object value) throws TemplateModelException {
136 sharedVariables.put(name, getObjectWrapper().wrap(value));
137 }
138
139 public TemplateExceptionHandler getTemplateExceptionHandler() {
140 return templateExceptionHandler;
141 }
142
143 public void setTemplateExceptionHandler(TemplateExceptionHandler templateExceptionHandler) {
144 this.templateExceptionHandler = templateExceptionHandler;
145 }
146 }