]> WPIA git - gigi.git/blob - src/club/wpia/gigi/output/template/Template.java
d3f5a011aa6448a8e2aae66e5a7055eb6942e844
[gigi.git] / src / club / wpia / gigi / output / template / Template.java
1 package club.wpia.gigi.output.template;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.PrintWriter;
8 import java.io.Reader;
9 import java.net.URISyntaxException;
10 import java.net.URL;
11 import java.text.SimpleDateFormat;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.Date;
15 import java.util.LinkedList;
16 import java.util.Map;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import club.wpia.gigi.localisation.Language;
21 import club.wpia.gigi.output.DateSelector;
22 import club.wpia.gigi.util.DayDate;
23 import club.wpia.gigi.util.EditDistance;
24 import club.wpia.gigi.util.HTMLEncoder;
25
26 /**
27  * Represents a loaded template file.
28  */
29 public class Template implements Outputable {
30
31     protected static class ParseResult {
32
33         TemplateBlock block;
34
35         String endType;
36
37         public ParseResult(TemplateBlock block, String endType) {
38             this.block = block;
39             this.endType = endType;
40         }
41
42         public String getEndType() {
43             return endType;
44         }
45
46         public TemplateBlock getBlock(String reqType) {
47             if (endType == null && reqType == null) {
48                 return block;
49             }
50             if (endType == null || reqType == null) {
51                 throw new Error("Invalid block type: " + endType);
52             }
53             if (endType.equals(reqType)) {
54                 return block;
55             }
56             throw new Error("Invalid block type: " + endType);
57         }
58     }
59
60     private TemplateBlock data;
61
62     private long lastLoaded;
63
64     private File source;
65
66     private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-zA-Z]+)\\(\\$([^)]+)\\) ?\\{ ?");
67
68     private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
69
70     private static final String[] POSSIBLE_CONTROL_PATTERNS = new String[] {
71             "if", "else", "foreach"
72     };
73
74     private static final String UNKOWN_CONTROL_STRUCTURE_MSG = "Unknown control structure \"%s\", did you mean \"%s\"?";
75
76     /**
77      * Creates a new template by parsing the contents from the given URL. This
78      * constructor will fail on syntax error. When the URL points to a file,
79      * {@link File#lastModified()} is monitored for changes of the template.
80      * 
81      * @param u
82      *            the URL to load the template from. UTF-8 is chosen as charset.
83      */
84     public Template(URL u) {
85         try (Reader r = new InputStreamReader(u.openStream(), "UTF-8")) {
86             try {
87                 if (u.getProtocol().equals("file")) {
88                     source = new File(u.toURI());
89                     lastLoaded = source.lastModified() + 1000;
90                 }
91             } catch (URISyntaxException e) {
92                 e.printStackTrace();
93             }
94             data = parse(r).getBlock(null);
95         } catch (IOException e) {
96             throw new Error(e);
97         }
98     }
99
100     /**
101      * Creates a new template by parsing the contents from the given reader.
102      * This constructor will fail on syntax error.
103      * 
104      * @param r
105      *            the Reader containing the data.
106      */
107     public Template(Reader r) {
108         try {
109             data = parse(r).getBlock(null);
110             r.close();
111         } catch (IOException e) {
112             throw new Error(e);
113         }
114     }
115
116     protected ParseResult parse(Reader r) throws IOException {
117         return parseContent(r);
118     }
119
120     protected ParseResult parseContent(Reader r) throws IOException {
121         ParseContext context = new ParseContext(r);
122         ParseResult result = parseContent(context);
123         if (context.parseException.isEmpty()) {
124             return result;
125         }
126         while (context.curChar != -1) {
127             parseContent(context);
128         }
129         throw context.parseException;
130     }
131
132     protected ParseResult parseContent(ParseContext context) throws IOException {
133         LinkedList<String> splitted = new LinkedList<String>();
134         LinkedList<Translatable> commands = new LinkedList<Translatable>();
135         StringBuffer buf = new StringBuffer();
136         String blockType = null;
137         ParseContext tContext = null;
138         outer:
139         while (true) {
140             if (tContext != null) {
141                 context.merge(tContext);
142             }
143             while ( !endsWith(buf, "<?")) {
144                 int ch = context.read();
145                 if (ch == -1) {
146                     break outer;
147                 }
148                 buf.append((char) ch);
149                 if (endsWith(buf, "\\\n")) {
150                     buf.delete(buf.length() - 2, buf.length());
151                 }
152             }
153             tContext = context.copy();
154             buf.delete(buf.length() - 2, buf.length());
155             splitted.add(buf.toString());
156             buf.delete(0, buf.length());
157             while ( !endsWith(buf, "?>")) {
158                 int ch = context.read();
159                 if (ch == -1) {
160                     context.addError("Expected \"?>\"");
161                     return null;
162                 }
163                 buf.append((char) ch);
164             }
165             buf.delete(buf.length() - 2, buf.length());
166             String com = buf.toString().replace("\n", "");
167             buf.delete(0, buf.length());
168             Matcher m = CONTROL_PATTERN.matcher(com);
169             if (m.matches()) {
170                 String type = m.group(1);
171                 String variable = m.group(2);
172                 ParseContext bodyContext = tContext.copy();
173                 ParseResult body = parseContent(bodyContext);
174                 if (type.equals("if")) {
175                     if ("else".equals(body.getEndType())) {
176                         ParseContext bodyContext2 = bodyContext.copy();
177                         commands.add(new IfStatement(variable, body.getBlock("else"), parseContent(bodyContext).getBlock("}")));
178                         bodyContext.merge(bodyContext2);
179                     } else {
180                         commands.add(new IfStatement(variable, body.getBlock("}")));
181                     }
182                 } else if (type.equals("foreach")) {
183                     commands.add(new ForeachStatement(variable, body.getBlock("}")));
184                 } else {
185                     String bestMatching = EditDistance.getBestMatchingStringByEditDistance(type, POSSIBLE_CONTROL_PATTERNS);
186                     tContext.addError(String.format(UNKOWN_CONTROL_STRUCTURE_MSG, type, bestMatching));
187                 }
188                 tContext.merge(bodyContext);
189                 continue;
190             } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
191                 blockType = "else";
192                 break;
193             } else if (com.matches(" ?\\} ?")) {
194                 blockType = "}";
195                 break;
196             } else {
197                 commands.add(parseCommand(com, tContext));
198             }
199         }
200         if (tContext != null) {
201             context.merge(tContext);
202         }
203         splitted.add(buf.toString());
204         ParseResult result = new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Translatable[commands.size()])), blockType);
205         return result;
206     }
207
208     private boolean endsWith(StringBuffer buf, String string) {
209         return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
210     }
211
212     private Translatable parseCommand(String s2, ParseContext context) {
213         if (s2.startsWith("=_")) {
214             final String raw = s2.substring(2);
215             if ( !s2.contains("$") && !s2.contains("!'")) {
216                 return new TranslateCommand(raw);
217             } else {
218                 return new SprintfCommand(raw);
219             }
220         } else if (s2.startsWith("=$")) {
221             final String raw = s2.substring(2);
222             return new OutputVariableCommand(raw);
223         } else {
224             context.addError("Unknown processing instruction \"" + s2 + "\"," + " did you mean \"" + EditDistance.getBestMatchingStringByEditDistance(s2, new String[] {
225                     "=_", "=$"
226             }) + "\"?");
227             return null;
228         }
229     }
230
231     @Override
232     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
233         tryReload();
234         data.output(out, l, vars);
235     }
236
237     protected void tryReload() {
238         if (source != null && lastLoaded < source.lastModified()) {
239             System.out.println("Reloading template.... " + source);
240             try (InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8")) {
241                 data = parse(r).getBlock(null);
242                 r.close();
243                 lastLoaded = source.lastModified() + 1000;
244             } catch (IOException e) {
245                 e.printStackTrace();
246             }
247         }
248     }
249
250     protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
251         if (vars.containsKey(Outputable.OUT_KEY_PLAIN)) {
252             unescaped = true;
253         }
254         Object s = vars.get(varname);
255
256         if (s == null) {
257             System.err.println("Empty variable: " + varname);
258         }
259         if (s instanceof Outputable) {
260             ((Outputable) s).output(out, l, vars);
261         } else if (s instanceof DayDate) {
262             out.print(DateSelector.getDateFormat().format(((DayDate) s).toDate()));
263         } else if (s instanceof Boolean) {
264             out.print(((Boolean) s) ? l.getTranslation("yes") : l.getTranslation("no"));
265         } else if (s instanceof Date) {
266             SimpleDateFormat sdfUI = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
267             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
268             out.print("<time datetime=\"" + sdf.format(s) + "\">");
269             out.print(sdfUI.format(s));
270             out.print(" UTC</time>");
271         } else {
272             out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));
273         }
274     }
275
276     public void addTranslations(Collection<String> s) {
277         data.addTranslations(s);
278     }
279
280     private class ParseContext {
281
282         public static final int CONTEXT_LENGTH = 20;
283
284         private Reader reader;
285
286         public final TemplateParseException parseException = new TemplateParseException(source);
287
288         int line = 1;
289
290         int column = 0;
291
292         private int curChar = -1;
293
294         private int[] charContext = new int[CONTEXT_LENGTH];
295
296         protected int contextPosition = 0;
297
298         public ParseContext(Reader reader) {
299             this.reader = reader;
300         }
301
302         public void addError(String message) {
303             addError(line, column, message);
304         }
305
306         public void addError(int line, int column, String message) {
307             StringBuffer charContextBuffer = new StringBuffer();
308             int j = contextPosition;
309             for (int i = 0; i < CONTEXT_LENGTH; i++) {
310                 if (charContext[j] != 0) {
311                     if (charContext[j] == '\n') {
312                         charContextBuffer.append("\\n");
313                     } else {
314                         charContextBuffer.appendCodePoint(charContext[j]);
315                     }
316                 }
317                 j = (j + 1) % CONTEXT_LENGTH;
318             }
319             parseException.addError(line, column, message, charContextBuffer.toString());
320         }
321
322         public void merge(ParseContext other) {
323             line = other.line;
324             column = other.column;
325             append(other);
326         }
327
328         public void append(ParseContext other) {
329             parseException.append(other.parseException);
330         }
331
332         public int read() throws IOException {
333             int ch;
334             while ((ch = reader.read()) == '\r') {
335             }
336             curChar = ch;
337             if (ch == '\n') {
338                 line++;
339                 column = 0;
340             } else {
341                 column++;
342             }
343             if (ch != -1) {
344                 charContext[contextPosition] = ch;
345                 contextPosition = (contextPosition + 1) % CONTEXT_LENGTH;
346             }
347             return ch;
348         }
349
350         public ParseContext copy() {
351             ParseContext newParseContext = new ParseContext(reader);
352             newParseContext.line = line;
353             newParseContext.column = column;
354             newParseContext.charContext = Arrays.copyOf(charContext, charContext.length);
355             newParseContext.contextPosition = contextPosition;
356             return newParseContext;
357         }
358     }
359 }