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