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