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 fr.paris.lutece.portal.service.template;
35
36 import fr.paris.lutece.portal.service.datastore.DatastoreService;
37 import fr.paris.lutece.portal.service.i18n.I18nService;
38 import fr.paris.lutece.portal.service.i18n.I18nTemplateMethod;
39 import fr.paris.lutece.portal.service.plugin.Plugin;
40 import fr.paris.lutece.portal.service.plugin.PluginService;
41 import fr.paris.lutece.portal.service.util.AppLogService;
42 import fr.paris.lutece.util.html.HtmlTemplate;
43
44 import java.util.Locale;
45 import java.util.Map;
46
47
48
49
50
51 public final class AppTemplateService
52 {
53
54 private static String _strTemplateDefaultPath;
55 private static IFreeMarkerTemplateService _freeMarkerTemplateService;
56
57
58
59
60 private AppTemplateService( )
61 {
62 }
63
64
65
66
67
68
69
70 public static void init( String strTemplatePath )
71 {
72 _strTemplateDefaultPath = strTemplatePath;
73 getFreeMarkerTemplateService( ).setSharedVariable( "i18n", new I18nTemplateMethod( ) );
74 }
75
76
77
78
79 public static void initMacros( )
80 {
81
82 Plugin corePlugin = PluginService.getCore( );
83 addPluginAutoIncludes( corePlugin );
84 addPluginAutoImports( corePlugin );
85
86
87 for ( Plugin plugin : PluginService.getPluginList( ) )
88 {
89 addPluginAutoIncludes( plugin );
90 addPluginAutoImports( plugin );
91 }
92
93
94 CommonsService.activateCommons( CommonsService.getCurrentCommonsKey( ) );
95 }
96
97
98
99
100
101
102
103 private static void addPluginAutoIncludes( Plugin plugin )
104 {
105 for ( String strFileName : plugin.getFreeMarkerAutoIncludes( ) )
106 {
107 AppLogService.info( "New freemarker autoinclude : {} from {}", strFileName, plugin.getName( ) );
108 getFreeMarkerTemplateService( ).addPluginAutoInclude( strFileName );
109 }
110 }
111
112
113
114
115
116
117
118 private static void addPluginAutoImports( Plugin plugin )
119 {
120 for ( Map.Entry<String, String> importEntry : plugin.getFreeMarkerAutoImports( ).entrySet( ) )
121 {
122 AppLogService.info( "New freemarker autoimport : {} as {} from {}", importEntry.getValue( ), importEntry.getKey( ), plugin.getName( ) );
123 getFreeMarkerTemplateService( ).addPluginAutoImport( importEntry.getKey( ), importEntry.getValue( ) );
124 }
125 }
126
127
128
129
130 public static void resetCache( )
131 {
132 getFreeMarkerTemplateService( ).resetCache( );
133 }
134
135
136
137
138 public static void resetConfiguration( )
139 {
140 getFreeMarkerTemplateService( ).resetConfiguration( );
141 }
142
143
144
145
146
147
148
149
150 public static HtmlTemplate getTemplate( String strTemplate )
151 {
152 return getTemplate( strTemplate, _strTemplateDefaultPath );
153 }
154
155
156
157
158
159
160
161
162
163
164
165 public static HtmlTemplate getTemplate( String strTemplate, String strPath )
166 {
167 return getTemplate( strTemplate, strPath, null, null );
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 public static HtmlTemplate getTemplate( String strTemplate, Locale locale )
184 {
185 return getTemplate( strTemplate, _strTemplateDefaultPath, locale, null );
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200 public static HtmlTemplate getTemplate( String strTemplate, Locale locale, Object model )
201 {
202 HtmlTemplate template;
203
204
205 template = getTemplate( strTemplate, _strTemplateDefaultPath, locale, model );
206
207 return template;
208 }
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 public static HtmlTemplate getTemplate( String strTemplate, String strPath, Locale locale, Object model )
225 {
226 HtmlTemplate template;
227
228
229 template = loadTemplate( strPath, strTemplate, locale, model );
230
231 return template;
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateData, Locale locale, Object model )
248 {
249 HtmlTemplate template = getFreeMarkerTemplateService( ).loadTemplateFromStringFtl( strFreemarkerTemplateData, locale, model );
250
251 if ( locale != null )
252 {
253 String strLocalized = I18nService.localize( template.getHtml( ), locale );
254 template = new HtmlTemplate( strLocalized );
255 }
256 return template;
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 public static HtmlTemplate getTemplateFromStringFtl( String strFreemarkerTemplateName,String strFreemarkerTemplateData, Locale locale, Object model,boolean bResetCache )
275 {
276 HtmlTemplate template = getFreeMarkerTemplateService( ).loadTemplateFromStringFtl( strFreemarkerTemplateName,strFreemarkerTemplateData, locale, model,bResetCache );
277
278 if ( locale != null )
279 {
280 String strLocalized = I18nService.localize( template.getHtml( ), locale );
281 template = new HtmlTemplate( strLocalized );
282 }
283 return template;
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 private static HtmlTemplate loadTemplate( String strPath, String strTemplate, Locale locale, Object model )
300 {
301 HtmlTemplate template;
302 template = getFreeMarkerTemplateService( ).loadTemplate( strPath, strTemplate, locale, model );
303
304 if ( locale != null )
305 {
306 String strLocalized = I18nService.localize( template.getHtml( ), locale );
307 template = new HtmlTemplate( strLocalized );
308 }
309
310 template = new HtmlTemplate( DatastoreService.replaceKeys( template.getHtml( ) ) );
311
312 return template;
313 }
314
315
316
317
318
319
320 private static IFreeMarkerTemplateService getFreeMarkerTemplateService( )
321 {
322 if ( _freeMarkerTemplateService == null )
323 {
324 _freeMarkerTemplateService = FreeMarkerTemplateService.getInstance( );
325 _freeMarkerTemplateService.init( _strTemplateDefaultPath );
326 }
327
328 return _freeMarkerTemplateService;
329 }
330 }