Add posting ability.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SoneTemplateFactory.java
1 /*
2  * shortener - L10nTemplateFactory.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.Reader;
21 import java.text.DateFormat;
22 import java.util.Date;
23 import java.util.Map;
24
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;
32
33 /**
34  * {@link TemplateFactory} implementation that creates {@link Template}s that
35  * have an {@link L10nFilter} added.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class SoneTemplateFactory implements TemplateFactory {
40
41         /** The base template factory. */
42         private final TemplateFactory templateFactory;
43
44         /** The L10n filter. */
45         private final L10nFilter l10nFilter;
46
47         /** The date filter. */
48         private final DateFilter dateFilter;
49
50         /** The reflection accessor. */
51         private final ReflectionAccessor reflectionAccessor = new ReflectionAccessor();
52
53         /**
54          * Creates a new Freetalk template factory.
55          *
56          * @param l10n
57          *            The L10n handler
58          */
59         public SoneTemplateFactory(BaseL10n l10n) {
60                 this(DefaultTemplateFactory.getInstance(), l10n);
61         }
62
63         /**
64          * Creates a new Freetalk template factory, retrieving templates from the
65          * given template factory, then adding all filters used by Freetalk to them.
66          *
67          * @param templateFactory
68          *            The base template factory
69          * @param l10n
70          *            The L10n handler
71          */
72         public SoneTemplateFactory(TemplateFactory templateFactory, BaseL10n l10n) {
73                 this.templateFactory = templateFactory;
74                 this.l10nFilter = new L10nFilter(l10n);
75                 this.dateFilter = new DateFilter();
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         @Override
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);
87                 return template;
88         }
89
90         /**
91          * {@link Filter} implementation replaces {@link String} values with their
92          * translated equivalents.
93          *
94          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
95          */
96         public static class L10nFilter implements Filter {
97
98                 /** The l10n handler. */
99                 private final BaseL10n l10n;
100
101                 /**
102                  * Creates a new L10n filter.
103                  *
104                  * @param l10n
105                  *            The l10n handler
106                  */
107                 public L10nFilter(BaseL10n l10n) {
108                         this.l10n = l10n;
109                 }
110
111                 /**
112                  * {@inheritDoc}
113                  */
114                 @Override
115                 public String format(DataProvider dataProvider, Object data, Map<String, String> parameters) {
116                         return l10n.getString(String.valueOf(data));
117                 }
118
119         }
120
121         /**
122          * {@link Filter} implementation that formats a date. The date may be given
123          * either as a {@link Date} or a {@link Long} object.
124          *
125          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
126          */
127         public static class DateFilter implements Filter {
128
129                 /** The date formatter. */
130                 private final DateFormat dateFormat = DateFormat.getInstance();
131
132                 /**
133                  * {@inheritDoc}
134                  */
135                 @Override
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));
141                         }
142                         return "";
143                 }
144
145         }
146
147 }