Add AJAX handler to get a rendered post.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / WebInterface.java
1 /*
2  * FreenetSone - WebInterface.java - Copyright © 2010 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 import net.pterodactylus.sone.core.Core;
33 import net.pterodactylus.sone.core.CoreListener;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.Reply;
36 import net.pterodactylus.sone.data.Sone;
37 import net.pterodactylus.sone.freenet.L10nFilter;
38 import net.pterodactylus.sone.freenet.wot.Identity;
39 import net.pterodactylus.sone.main.SonePlugin;
40 import net.pterodactylus.sone.notify.ListNotification;
41 import net.pterodactylus.sone.template.CollectionAccessor;
42 import net.pterodactylus.sone.template.CssClassNameFilter;
43 import net.pterodactylus.sone.template.GetPagePlugin;
44 import net.pterodactylus.sone.template.IdentityAccessor;
45 import net.pterodactylus.sone.template.NotificationManagerAccessor;
46 import net.pterodactylus.sone.template.PostAccessor;
47 import net.pterodactylus.sone.template.ReplyAccessor;
48 import net.pterodactylus.sone.template.RequestChangeFilter;
49 import net.pterodactylus.sone.template.SoneAccessor;
50 import net.pterodactylus.sone.template.SubstringFilter;
51 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
52 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
53 import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage;
54 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
55 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
56 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
57 import net.pterodactylus.sone.web.ajax.GetLikesAjaxPage;
58 import net.pterodactylus.sone.web.ajax.GetPostAjaxPage;
59 import net.pterodactylus.sone.web.ajax.GetReplyAjaxPage;
60 import net.pterodactylus.sone.web.ajax.GetStatusAjaxPage;
61 import net.pterodactylus.sone.web.ajax.GetTranslationPage;
62 import net.pterodactylus.sone.web.ajax.LikeAjaxPage;
63 import net.pterodactylus.sone.web.ajax.LockSoneAjaxPage;
64 import net.pterodactylus.sone.web.ajax.UnfollowSoneAjaxPage;
65 import net.pterodactylus.sone.web.ajax.UnlikeAjaxPage;
66 import net.pterodactylus.sone.web.ajax.UnlockSoneAjaxPage;
67 import net.pterodactylus.sone.web.page.PageToadlet;
68 import net.pterodactylus.sone.web.page.PageToadletFactory;
69 import net.pterodactylus.sone.web.page.StaticPage;
70 import net.pterodactylus.util.logging.Logging;
71 import net.pterodactylus.util.notify.NotificationManager;
72 import net.pterodactylus.util.notify.TemplateNotification;
73 import net.pterodactylus.util.template.DateFilter;
74 import net.pterodactylus.util.template.DefaultTemplateFactory;
75 import net.pterodactylus.util.template.MatchFilter;
76 import net.pterodactylus.util.template.PaginationPlugin;
77 import net.pterodactylus.util.template.ReflectionAccessor;
78 import net.pterodactylus.util.template.Template;
79 import net.pterodactylus.util.template.TemplateException;
80 import net.pterodactylus.util.template.TemplateFactory;
81 import net.pterodactylus.util.template.TemplateProvider;
82 import net.pterodactylus.util.template.XmlFilter;
83 import net.pterodactylus.util.thread.Ticker;
84 import freenet.clients.http.SessionManager;
85 import freenet.clients.http.ToadletContainer;
86 import freenet.l10n.BaseL10n;
87
88 /**
89  * Bundles functionality that a web interface of a Freenet plugin needs, e.g.
90  * references to l10n helpers.
91  *
92  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
93  */
94 public class WebInterface implements CoreListener {
95
96         /** The logger. */
97         private static final Logger logger = Logging.getLogger(WebInterface.class);
98
99         /** The notification manager. */
100         private final NotificationManager notificationManager = new NotificationManager();
101
102         /** The Sone plugin. */
103         private final SonePlugin sonePlugin;
104
105         /** The registered toadlets. */
106         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
107
108         /** The form password. */
109         private final String formPassword;
110
111         /** The template factory. */
112         private DefaultTemplateFactory templateFactory;
113
114         /** The “new Sone” notification. */
115         private final ListNotification<Sone> newSoneNotification;
116
117         /** The “new post” notification. */
118         private final ListNotification<Post> newPostNotification;
119
120         /** The “new reply” notification. */
121         private final ListNotification<Reply> newReplyNotification;
122
123         /** The “rescuing Sone” notification. */
124         private final ListNotification<Sone> rescuingSonesNotification;
125
126         /** The “Sone rescued” notification. */
127         private final ListNotification<Sone> sonesRescuedNotification;
128
129         /**
130          * Creates a new web interface.
131          *
132          * @param sonePlugin
133          *            The Sone plugin
134          */
135         public WebInterface(SonePlugin sonePlugin) {
136                 this.sonePlugin = sonePlugin;
137                 formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
138
139                 templateFactory = new DefaultTemplateFactory();
140                 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
141                 templateFactory.addAccessor(Collection.class, new CollectionAccessor());
142                 templateFactory.addAccessor(Sone.class, new SoneAccessor(getCore()));
143                 templateFactory.addAccessor(Post.class, new PostAccessor(getCore()));
144                 templateFactory.addAccessor(Reply.class, new ReplyAccessor(getCore()));
145                 templateFactory.addAccessor(Identity.class, new IdentityAccessor(getCore()));
146                 templateFactory.addAccessor(NotificationManager.class, new NotificationManagerAccessor());
147                 templateFactory.addFilter("date", new DateFilter());
148                 templateFactory.addFilter("l10n", new L10nFilter(getL10n()));
149                 templateFactory.addFilter("substring", new SubstringFilter());
150                 templateFactory.addFilter("xml", new XmlFilter());
151                 templateFactory.addFilter("change", new RequestChangeFilter());
152                 templateFactory.addFilter("match", new MatchFilter());
153                 templateFactory.addFilter("css", new CssClassNameFilter());
154                 templateFactory.addPlugin("getpage", new GetPagePlugin());
155                 templateFactory.addPlugin("paginate", new PaginationPlugin());
156                 templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory));
157                 templateFactory.addTemplateObject("formPassword", formPassword);
158
159                 /* create notifications. */
160                 Template newSoneNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newSoneNotification.html"));
161                 newSoneNotification = new ListNotification<Sone>("new-sone-notification", "sones", newSoneNotificationTemplate);
162
163                 Template newPostNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newPostNotification.html"));
164                 newPostNotification = new ListNotification<Post>("new-post-notification", "posts", newPostNotificationTemplate);
165
166                 Template newReplyNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/newReplyNotification.html"));
167                 newReplyNotification = new ListNotification<Reply>("new-replies-notification", "replies", newReplyNotificationTemplate);
168
169                 Template rescuingSonesTemplate = templateFactory.createTemplate(createReader("/templates/notify/rescuingSonesNotification.html"));
170                 rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
171
172                 Template sonesRescuedTemplate = templateFactory.createTemplate(createReader("/templates/notify/sonesRescuedNotification.html"));
173                 sonesRescuedNotification = new ListNotification<Sone>("sones-rescued-notification", "sones", sonesRescuedTemplate);
174         }
175
176         //
177         // ACCESSORS
178         //
179
180         /**
181          * Returns the Sone core used by the Sone plugin.
182          *
183          * @return The Sone core
184          */
185         public Core getCore() {
186                 return sonePlugin.core();
187         }
188
189         /**
190          * Returns the notification manager.
191          *
192          * @return The notification manager
193          */
194         public NotificationManager getNotifications() {
195                 return notificationManager;
196         }
197
198         /**
199          * Returns the l10n helper of the node.
200          *
201          * @return The node’s l10n helper
202          */
203         public BaseL10n getL10n() {
204                 return sonePlugin.l10n().getBase();
205         }
206
207         /**
208          * Returns the session manager of the node.
209          *
210          * @return The node’s session manager
211          */
212         public SessionManager getSessionManager() {
213                 return sonePlugin.pluginRespirator().getSessionManager("Sone");
214         }
215
216         /**
217          * Returns the node’s form password.
218          *
219          * @return The form password
220          */
221         public String getFormPassword() {
222                 return formPassword;
223         }
224
225         /**
226          * Returns the posts that have been announced as new in the
227          * {@link #newPostNotification}.
228          *
229          * @return The new posts
230          */
231         public Set<Post> getNewPosts() {
232                 return new HashSet<Post>(newPostNotification.getElements());
233         }
234
235         /**
236          * Returns the replies that have been announced as new in the
237          * {@link #newReplyNotification}.
238          *
239          * @return The new replies
240          */
241         public Set<Reply> getNewReplies() {
242                 return new HashSet<Reply>(newReplyNotification.getElements());
243         }
244
245         //
246         // ACTIONS
247         //
248
249         /**
250          * Starts the web interface and registers all toadlets.
251          */
252         public void start() {
253                 registerToadlets();
254
255                 /* notification templates. */
256                 Template startupNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/startupNotification.html"));
257
258                 final TemplateNotification startupNotification = new TemplateNotification("startup-notification", startupNotificationTemplate);
259                 notificationManager.addNotification(startupNotification);
260
261                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (120 * 1000), new Runnable() {
262
263                         @Override
264                         public void run() {
265                                 startupNotification.dismiss();
266                         }
267                 }, "Sone Startup Notification Remover");
268
269                 Template wotMissingNotificationTemplate = templateFactory.createTemplate(createReader("/templates/notify/wotMissingNotification.html"));
270                 final TemplateNotification wotMissingNotification = new TemplateNotification("wot-missing-notification", wotMissingNotificationTemplate);
271                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), new Runnable() {
272
273                         @Override
274                         @SuppressWarnings("synthetic-access")
275                         public void run() {
276                                 if (getCore().getIdentityManager().isConnected()) {
277                                         wotMissingNotification.dismiss();
278                                 } else {
279                                         notificationManager.addNotification(wotMissingNotification);
280                                 }
281                                 Ticker.getInstance().registerEvent(System.currentTimeMillis() + (15 * 1000), this, "Sone WoT Connector Checker");
282                         }
283
284                 }, "Sone WoT Connector Checker");
285         }
286
287         /**
288          * Stops the web interface and unregisters all toadlets.
289          */
290         public void stop() {
291                 unregisterToadlets();
292                 Ticker.getInstance().stop();
293         }
294
295         //
296         // PRIVATE METHODS
297         //
298
299         /**
300          * Register all toadlets.
301          */
302         private void registerToadlets() {
303                 Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html"));
304                 Template indexTemplate = templateFactory.createTemplate(createReader("/templates/index.html"));
305                 Template knownSonesTemplate = templateFactory.createTemplate(createReader("/templates/knownSones.html"));
306                 Template createSoneTemplate = templateFactory.createTemplate(createReader("/templates/createSone.html"));
307                 Template createPostTemplate = templateFactory.createTemplate(createReader("/templates/createPost.html"));
308                 Template createReplyTemplate = templateFactory.createTemplate(createReader("/templates/createReply.html"));
309                 Template editProfileTemplate = templateFactory.createTemplate(createReader("/templates/editProfile.html"));
310                 Template viewSoneTemplate = templateFactory.createTemplate(createReader("/templates/viewSone.html"));
311                 Template viewPostTemplate = templateFactory.createTemplate(createReader("/templates/viewPost.html"));
312                 Template likePostTemplate = templateFactory.createTemplate(createReader("/templates/like.html"));
313                 Template unlikePostTemplate = templateFactory.createTemplate(createReader("/templates/unlike.html"));
314                 Template deletePostTemplate = templateFactory.createTemplate(createReader("/templates/deletePost.html"));
315                 Template deleteReplyTemplate = templateFactory.createTemplate(createReader("/templates/deleteReply.html"));
316                 Template lockSoneTemplate = templateFactory.createTemplate(createReader("/templates/lockSone.html"));
317                 Template unlockSoneTemplate = templateFactory.createTemplate(createReader("/templates/unlockSone.html"));
318                 Template followSoneTemplate = templateFactory.createTemplate(createReader("/templates/followSone.html"));
319                 Template unfollowSoneTemplate = templateFactory.createTemplate(createReader("/templates/unfollowSone.html"));
320                 Template deleteSoneTemplate = templateFactory.createTemplate(createReader("/templates/deleteSone.html"));
321                 Template noPermissionTemplate = templateFactory.createTemplate(createReader("/templates/noPermission.html"));
322                 Template dismissNotificationTemplate = templateFactory.createTemplate(createReader("/templates/dismissNotification.html"));
323                 Template logoutTemplate = templateFactory.createTemplate(createReader("/templates/logout.html"));
324                 Template optionsTemplate = templateFactory.createTemplate(createReader("/templates/options.html"));
325                 Template aboutTemplate = templateFactory.createTemplate(createReader("/templates/about.html"));
326                 Template postTemplate = templateFactory.createTemplate(createReader("/templates/include/viewPost.html"));
327                 Template replyTemplate = templateFactory.createTemplate(createReader("/templates/include/viewReply.html"));
328
329                 PageToadletFactory pageToadletFactory = new PageToadletFactory(sonePlugin.pluginRespirator().getHLSimpleClient(), "/Sone/");
330                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
331                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateSonePage(createSoneTemplate, this), "CreateSone"));
332                 pageToadlets.add(pageToadletFactory.createPageToadlet(new KnownSonesPage(knownSonesTemplate, this), "KnownSones"));
333                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditProfilePage(editProfileTemplate, this), "EditProfile"));
334                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostPage(createPostTemplate, this)));
335                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyPage(createReplyTemplate, this)));
336                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewSonePage(viewSoneTemplate, this)));
337                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ViewPostPage(viewPostTemplate, this)));
338                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikePage(likePostTemplate, this)));
339                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikePage(unlikePostTemplate, this)));
340                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostPage(deletePostTemplate, this)));
341                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyPage(deleteReplyTemplate, this)));
342                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSonePage(lockSoneTemplate, this)));
343                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSonePage(unlockSoneTemplate, this)));
344                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSonePage(followSoneTemplate, this)));
345                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSonePage(unfollowSoneTemplate, this)));
346                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteSonePage(deleteSoneTemplate, this), "DeleteSone"));
347                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LoginPage(loginTemplate, this), "Login"));
348                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LogoutPage(logoutTemplate, this), "Logout"));
349                 pageToadlets.add(pageToadletFactory.createPageToadlet(new OptionsPage(optionsTemplate, this), "Options"));
350                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About"));
351                 pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this)));
352                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(dismissNotificationTemplate, this)));
353                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css")));
354                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("javascript/", "/static/javascript/", "text/javascript")));
355                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("images/", "/static/images/", "image/png")));
356                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetTranslationPage(this)));
357                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetStatusAjaxPage(this)));
358                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationAjaxPage(this)));
359                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreatePostAjaxPage(this)));
360                 pageToadlets.add(pageToadletFactory.createPageToadlet(new CreateReplyAjaxPage(this)));
361                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetReplyAjaxPage(this, replyTemplate)));
362                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetPostAjaxPage(this, postTemplate)));
363                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeletePostAjaxPage(this)));
364                 pageToadlets.add(pageToadletFactory.createPageToadlet(new DeleteReplyAjaxPage(this)));
365                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LockSoneAjaxPage(this)));
366                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
367                 pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
368                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
369                 pageToadlets.add(pageToadletFactory.createPageToadlet(new LikeAjaxPage(this)));
370                 pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlikeAjaxPage(this)));
371                 pageToadlets.add(pageToadletFactory.createPageToadlet(new GetLikesAjaxPage(this)));
372
373                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
374                 toadletContainer.getPageMaker().addNavigationCategory("/Sone/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", sonePlugin);
375                 for (PageToadlet toadlet : pageToadlets) {
376                         String menuName = toadlet.getMenuName();
377                         if (menuName != null) {
378                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
379                         } else {
380                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
381                         }
382                 }
383         }
384
385         /**
386          * Unregisters all toadlets.
387          */
388         private void unregisterToadlets() {
389                 ToadletContainer toadletContainer = sonePlugin.pluginRespirator().getToadletContainer();
390                 for (PageToadlet pageToadlet : pageToadlets) {
391                         toadletContainer.unregister(pageToadlet);
392                 }
393                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
394         }
395
396         /**
397          * Creates a {@link Reader} from the {@link InputStream} for the resource
398          * with the given name.
399          *
400          * @param resourceName
401          *            The name of the resource
402          * @return A {@link Reader} for the resource
403          */
404         private Reader createReader(String resourceName) {
405                 try {
406                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
407                 } catch (UnsupportedEncodingException uee1) {
408                         return null;
409                 }
410         }
411
412         //
413         // CORELISTENER METHODS
414         //
415
416         /**
417          * {@inheritDoc}
418          */
419         @Override
420         public void rescuingSone(Sone sone) {
421                 rescuingSonesNotification.add(sone);
422                 notificationManager.addNotification(rescuingSonesNotification);
423         }
424
425         /**
426          * {@inheritDoc}
427          */
428         @Override
429         public void rescuedSone(Sone sone) {
430                 rescuingSonesNotification.remove(sone);
431                 sonesRescuedNotification.add(sone);
432                 notificationManager.addNotification(sonesRescuedNotification);
433         }
434
435         /**
436          * {@inheritDoc}
437          */
438         @Override
439         public void newSoneFound(Sone sone) {
440                 newSoneNotification.add(sone);
441                 notificationManager.addNotification(newSoneNotification);
442         }
443
444         /**
445          * {@inheritDoc}
446          */
447         @Override
448         public void newPostFound(Post post) {
449                 newPostNotification.add(post);
450                 notificationManager.addNotification(newPostNotification);
451         }
452
453         /**
454          * {@inheritDoc}
455          */
456         @Override
457         public void newReplyFound(Reply reply) {
458                 if (reply.getPost().getSone() == null) {
459                         return;
460                 }
461                 newReplyNotification.add(reply);
462                 notificationManager.addNotification(newReplyNotification);
463         }
464
465         /**
466          * {@inheritDoc}
467          */
468         @Override
469         public void markSoneKnown(Sone sone) {
470                 newSoneNotification.remove(sone);
471         }
472
473         /**
474          * {@inheritDoc}
475          */
476         @Override
477         public void markPostKnown(Post post) {
478                 newPostNotification.remove(post);
479         }
480
481         /**
482          * {@inheritDoc}
483          */
484         @Override
485         public void markReplyKnown(Reply reply) {
486                 newReplyNotification.remove(reply);
487         }
488
489         /**
490          * Template provider implementation that uses
491          * {@link WebInterface#createReader(String)} to load templates for
492          * inclusion.
493          *
494          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
495          */
496         private class ClassPathTemplateProvider implements TemplateProvider {
497
498                 /** The template factory. */
499                 @SuppressWarnings("hiding")
500                 private final TemplateFactory templateFactory;
501
502                 /**
503                  * Creates a new template provider that locates templates on the
504                  * classpath.
505                  *
506                  * @param templateFactory
507                  *            The template factory to create the templates
508                  */
509                 public ClassPathTemplateProvider(TemplateFactory templateFactory) {
510                         this.templateFactory = templateFactory;
511                 }
512
513                 /**
514                  * {@inheritDoc}
515                  */
516                 @Override
517                 @SuppressWarnings("synthetic-access")
518                 public Template getTemplate(String templateName) {
519                         Reader templateReader = createReader("/templates/" + templateName);
520                         if (templateReader == null) {
521                                 return null;
522                         }
523                         Template template = templateFactory.createTemplate(templateReader);
524                         try {
525                                 template.parse();
526                         } catch (TemplateException te1) {
527                                 logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1);
528                         }
529                         return template;
530                 }
531
532         }
533
534 }