2 * shortener - L10nTemplateFactory.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web;
20 import java.io.Reader;
21 import java.text.DateFormat;
22 import java.util.Date;
25 import net.pterodactylus.util.template.DataProvider;
26 import net.pterodactylus.util.template.DefaultTemplateFactory;
27 import net.pterodactylus.util.template.Filter;
28 import net.pterodactylus.util.template.ReflectionAccessor;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateFactory;
31 import freenet.l10n.BaseL10n;
34 * {@link TemplateFactory} implementation that creates {@link Template}s that
35 * have an {@link L10nFilter} added.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class SoneTemplateFactory implements TemplateFactory {
41 /** The base template factory. */
42 private final TemplateFactory templateFactory;
44 /** The L10n filter. */
45 private final L10nFilter l10nFilter;
47 /** The date filter. */
48 private final DateFilter dateFilter;
50 /** The reflection accessor. */
51 private final ReflectionAccessor reflectionAccessor = new ReflectionAccessor();
54 * Creates a new Freetalk template factory.
59 public SoneTemplateFactory(BaseL10n l10n) {
60 this(DefaultTemplateFactory.getInstance(), l10n);
64 * Creates a new Freetalk template factory, retrieving templates from the
65 * given template factory, then adding all filters used by Freetalk to them.
67 * @param templateFactory
68 * The base template factory
72 public SoneTemplateFactory(TemplateFactory templateFactory, BaseL10n l10n) {
73 this.templateFactory = templateFactory;
74 this.l10nFilter = new L10nFilter(l10n);
75 this.dateFilter = new DateFilter();
82 public Template createTemplate(Reader templateSource) {
83 Template template = templateFactory.createTemplate(templateSource);
84 template.addAccessor(Object.class, reflectionAccessor);
85 template.addFilter("l10n", l10nFilter);
86 template.addFilter("date", dateFilter);
91 * {@link Filter} implementation replaces {@link String} values with their
92 * translated equivalents.
94 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
96 public static class L10nFilter implements Filter {
98 /** The l10n handler. */
99 private final BaseL10n l10n;
102 * Creates a new L10n filter.
107 public L10nFilter(BaseL10n l10n) {
115 public String format(DataProvider dataProvider, Object data, Map<String, String> parameters) {
116 return l10n.getString(String.valueOf(data));
122 * {@link Filter} implementation that formats a date. The date may be given
123 * either as a {@link Date} or a {@link Long} object.
125 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
127 public static class DateFilter implements Filter {
129 /** The date formatter. */
130 private final DateFormat dateFormat = DateFormat.getInstance();
136 public String format(DataProvider dataProvider, Object data, Map<String, String> parameters) {
137 if (data instanceof Date) {
138 return dateFormat.format((Date) data);
139 } else if (data instanceof Long) {
140 return dateFormat.format(new Date((Long) data));