]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/Template.java
Enforce Date-of-births to be day-only.
[gigi.git] / src / org / cacert / gigi / output / template / Template.java
1 package org.cacert.gigi.output.template;
2
3 import java.io.EOFException;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.PrintWriter;
9 import java.io.Reader;
10 import java.net.URISyntaxException;
11 import java.net.URL;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
14 import java.util.LinkedList;
15 import java.util.Map;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.cacert.gigi.localisation.Language;
20 import org.cacert.gigi.output.DateSelector;
21 import org.cacert.gigi.util.DayDate;
22 import org.cacert.gigi.util.HTMLEncoder;
23
24 public class Template implements Outputable {
25
26     private static class ParseResult {
27
28         TemplateBlock block;
29
30         String endType;
31
32         public ParseResult(TemplateBlock block, String endType) {
33             this.block = block;
34             this.endType = endType;
35         }
36
37         public String getEndType() {
38             return endType;
39         }
40
41         public TemplateBlock getBlock(String reqType) {
42             if (endType == null && reqType == null) {
43                 return block;
44             }
45             if (endType == null || reqType == null) {
46                 throw new Error("Invalid block type: " + endType);
47             }
48             if (endType.equals(reqType)) {
49                 return block;
50             }
51             throw new Error("Invalid block type: " + endType);
52         }
53     }
54
55     private TemplateBlock data;
56
57     private long lastLoaded;
58
59     private File source;
60
61     private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-zA-Z]+)\\(\\$([^)]+)\\) ?\\{ ?");
62
63     private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
64
65     public Template(URL u) {
66         try {
67             Reader r = new InputStreamReader(u.openStream(), "UTF-8");
68             try {
69                 if (u.getProtocol().equals("file")) {
70                     source = new File(u.toURI());
71                     lastLoaded = source.lastModified() + 1000;
72                 }
73             } catch (URISyntaxException e) {
74                 e.printStackTrace();
75             }
76             data = parse(r).getBlock(null);
77             r.close();
78         } catch (IOException e) {
79             throw new Error(e);
80         }
81     }
82
83     public Template(Reader r) {
84         try {
85             data = parse(r).getBlock(null);
86             r.close();
87         } catch (IOException e) {
88             throw new Error(e);
89         }
90     }
91
92     private ParseResult parse(Reader r) throws IOException {
93         LinkedList<String> splitted = new LinkedList<String>();
94         LinkedList<Outputable> commands = new LinkedList<Outputable>();
95         StringBuffer buf = new StringBuffer();
96         String blockType = null;
97         outer:
98         while (true) {
99             while ( !endsWith(buf, "<?")) {
100                 int ch = r.read();
101                 if (ch == -1) {
102                     break outer;
103                 }
104                 buf.append((char) ch);
105             }
106             buf.delete(buf.length() - 2, buf.length());
107             splitted.add(buf.toString());
108             buf.delete(0, buf.length());
109             while ( !endsWith(buf, "?>")) {
110                 int ch = r.read();
111                 if (ch == -1) {
112                     throw new EOFException();
113                 }
114                 buf.append((char) ch);
115             }
116             buf.delete(buf.length() - 2, buf.length());
117             String com = buf.toString().replace("\n", "");
118             buf.delete(0, buf.length());
119             Matcher m = CONTROL_PATTERN.matcher(com);
120             if (m.matches()) {
121                 String type = m.group(1);
122                 String variable = m.group(2);
123                 ParseResult body = parse(r);
124                 if (type.equals("if")) {
125                     if ("else".equals(body.getEndType())) {
126                         commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
127                     } else {
128                         commands.add(new IfStatement(variable, body.getBlock("}")));
129                     }
130                 } else if (type.equals("foreach")) {
131                     commands.add(new ForeachStatement(variable, body.getBlock("}")));
132                 } else {
133                     throw new IOException("Syntax error: unknown control structure: " + type);
134                 }
135                 continue;
136             } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
137                 blockType = "else";
138                 break;
139             } else if (com.matches(" ?\\} ?")) {
140                 blockType = "}";
141                 break;
142             } else {
143                 commands.add(parseCommand(com));
144             }
145         }
146         splitted.add(buf.toString());
147         return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Outputable[commands.size()])), blockType);
148     }
149
150     private boolean endsWith(StringBuffer buf, String string) {
151         return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
152     }
153
154     private Outputable parseCommand(String s2) {
155         if (s2.startsWith("=_")) {
156             final String raw = s2.substring(2);
157             if ( !s2.contains("$") && !s2.contains("!'")) {
158                 return new TranslateCommand(raw);
159             } else {
160                 return new SprintfCommand(raw);
161             }
162         } else if (s2.startsWith("=$")) {
163             final String raw = s2.substring(2);
164             return new OutputVariableCommand(raw);
165         } else {
166             throw new Error("Unknown processing instruction: " + s2);
167         }
168     }
169
170     @Override
171     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
172         if (source != null && lastLoaded < source.lastModified()) {
173             try {
174                 System.out.println("Reloading template.... " + source);
175                 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
176                 data = parse(r).getBlock(null);
177                 r.close();
178                 lastLoaded = source.lastModified() + 1000;
179             } catch (IOException e) {
180                 e.printStackTrace();
181             }
182         }
183         data.output(out, l, vars);
184     }
185
186     protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
187         Object s = vars.get(varname);
188
189         if (s == null) {
190             System.out.println("Empty variable: " + varname);
191         }
192         if (s instanceof Outputable) {
193             ((Outputable) s).output(out, l, vars);
194         } else if (s instanceof DayDate) {
195             out.print(DateSelector.getDateFormat().format(((DayDate) s).toDate()));
196         } else if (s instanceof Date) {
197             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
198             out.print(sdf.format(s));
199             out.print(" UTC");
200         } else {
201             out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));
202         }
203     }
204 }