1 package org.cacert.gigi.output.template;
3 import java.io.EOFException;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.PrintWriter;
10 import java.net.URISyntaxException;
12 import java.text.SimpleDateFormat;
13 import java.util.Collection;
14 import java.util.Date;
15 import java.util.LinkedList;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import org.cacert.gigi.localisation.Language;
21 import org.cacert.gigi.output.DateSelector;
22 import org.cacert.gigi.util.DayDate;
23 import org.cacert.gigi.util.HTMLEncoder;
26 * Represents a loaded template file.
28 public class Template implements Outputable {
30 protected static class ParseResult {
36 public ParseResult(TemplateBlock block, String endType) {
38 this.endType = endType;
41 public String getEndType() {
45 public TemplateBlock getBlock(String reqType) {
46 if (endType == null && reqType == null) {
49 if (endType == null || reqType == null) {
50 throw new Error("Invalid block type: " + endType);
52 if (endType.equals(reqType)) {
55 throw new Error("Invalid block type: " + endType);
59 private TemplateBlock data;
61 private long lastLoaded;
65 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-zA-Z]+)\\(\\$([^)]+)\\) ?\\{ ?");
67 private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
70 * Creates a new template by parsing the contents from the given URL. This
71 * constructor will fail on syntax error. When the URL points to a file,
72 * {@link File#lastModified()} is monitored for changes of the template.
75 * the URL to load the template from. UTF-8 is chosen as charset.
77 public Template(URL u) {
78 try (Reader r = new InputStreamReader(u.openStream(), "UTF-8")) {
80 if (u.getProtocol().equals("file")) {
81 source = new File(u.toURI());
82 lastLoaded = source.lastModified() + 1000;
84 } catch (URISyntaxException e) {
87 data = parse(r).getBlock(null);
88 } catch (IOException e) {
94 * Creates a new template by parsing the contents from the given reader.
95 * This constructor will fail on syntax error.
98 * the Reader containing the data.
100 public Template(Reader r) {
102 data = parse(r).getBlock(null);
104 } catch (IOException e) {
109 protected ParseResult parse(Reader r) throws IOException {
110 return parseContent(r);
113 protected ParseResult parseContent(Reader r) throws IOException {
114 LinkedList<String> splitted = new LinkedList<String>();
115 LinkedList<Translatable> commands = new LinkedList<Translatable>();
116 StringBuffer buf = new StringBuffer();
117 String blockType = null;
120 while ( !endsWith(buf, "<?")) {
125 buf.append((char) ch);
126 if (endsWith(buf, "\\\n")) {
127 buf.delete(buf.length() - 2, buf.length());
130 buf.delete(buf.length() - 2, buf.length());
131 splitted.add(buf.toString());
132 buf.delete(0, buf.length());
133 while ( !endsWith(buf, "?>")) {
136 throw new EOFException();
138 buf.append((char) ch);
140 buf.delete(buf.length() - 2, buf.length());
141 String com = buf.toString().replace("\n", "");
142 buf.delete(0, buf.length());
143 Matcher m = CONTROL_PATTERN.matcher(com);
145 String type = m.group(1);
146 String variable = m.group(2);
147 ParseResult body = parseContent(r);
148 if (type.equals("if")) {
149 if ("else".equals(body.getEndType())) {
150 commands.add(new IfStatement(variable, body.getBlock("else"), parseContent(r).getBlock("}")));
152 commands.add(new IfStatement(variable, body.getBlock("}")));
154 } else if (type.equals("foreach")) {
155 commands.add(new ForeachStatement(variable, body.getBlock("}")));
157 throw new IOException("Syntax error: unknown control structure: " + type);
160 } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
163 } else if (com.matches(" ?\\} ?")) {
167 commands.add(parseCommand(com));
170 splitted.add(buf.toString());
171 return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Translatable[commands.size()])), blockType);
174 private boolean endsWith(StringBuffer buf, String string) {
175 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
178 private Translatable parseCommand(String s2) {
179 if (s2.startsWith("=_")) {
180 final String raw = s2.substring(2);
181 if ( !s2.contains("$") && !s2.contains("!'")) {
182 return new TranslateCommand(raw);
184 return new SprintfCommand(raw);
186 } else if (s2.startsWith("=$")) {
187 final String raw = s2.substring(2);
188 return new OutputVariableCommand(raw);
190 throw new Error("Unknown processing instruction: " + s2);
195 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
197 data.output(out, l, vars);
200 protected void tryReload() {
201 if (source != null && lastLoaded < source.lastModified()) {
203 System.out.println("Reloading template.... " + source);
204 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
205 data = parse(r).getBlock(null);
207 lastLoaded = source.lastModified() + 1000;
208 } catch (IOException e) {
214 protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
215 if (vars.containsKey(Outputable.OUT_KEY_PLAIN)) {
218 Object s = vars.get(varname);
221 System.out.println("Empty variable: " + varname);
223 if (s instanceof Outputable) {
224 ((Outputable) s).output(out, l, vars);
225 } else if (s instanceof DayDate) {
226 out.print(DateSelector.getDateFormat().format(((DayDate) s).toDate()));
227 } else if (s instanceof Date) {
228 SimpleDateFormat sdfUI = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
229 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
230 out.print("<time datetime=\"" + sdf.format(s) + "\">");
231 out.print(sdfUI.format(s));
232 out.print(" UTC</time>");
234 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));
238 public void addTranslations(Collection<String> s) {
239 data.addTranslations(s);