1 package club.wpia.gigi.output.template;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.PrintWriter;
9 import java.net.URISyntaxException;
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;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
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;
27 * Represents a loaded template file.
29 public class Template implements Outputable {
31 protected static class ParseResult {
37 public ParseResult(TemplateBlock block, String endType) {
39 this.endType = endType;
42 public String getEndType() {
46 public TemplateBlock getBlock(String reqType) {
47 if (endType == null && reqType == null) {
50 if (endType == null || reqType == null) {
51 throw new Error("Invalid block type: " + endType);
53 if (endType.equals(reqType)) {
56 throw new Error("Invalid block type: " + endType);
60 private TemplateBlock data;
62 private long lastLoaded;
66 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-zA-Z]+)\\(\\$([^)]+)\\) ?\\{ ?");
68 private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
70 private static final String[] POSSIBLE_CONTROL_PATTERNS = new String[] {
71 "if", "else", "foreach"
74 private static final String UNKOWN_CONTROL_STRUCTURE_MSG = "Unknown control structure \"%s\", did you mean \"%s\"?";
76 public static final String UTC_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
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.
84 * the URL to load the template from. UTF-8 is chosen as charset.
86 public Template(URL u) {
87 try (Reader r = new InputStreamReader(u.openStream(), "UTF-8")) {
89 if (u.getProtocol().equals("file")) {
90 source = new File(u.toURI());
91 lastLoaded = source.lastModified() + 1000;
93 } catch (URISyntaxException e) {
96 data = parse(r).getBlock(null);
97 } catch (IOException e) {
103 * Creates a new template by parsing the contents from the given reader.
104 * This constructor will fail on syntax error.
107 * the Reader containing the data.
109 public Template(Reader r) {
111 data = parse(r).getBlock(null);
113 } catch (IOException e) {
118 protected ParseResult parse(Reader r) throws IOException {
119 return parseContent(r);
122 protected ParseResult parseContent(Reader r) throws IOException {
123 ParseContext context = new ParseContext(r);
124 ParseResult result = parseContent(context);
125 if (context.parseException.isEmpty()) {
128 while (context.curChar != -1) {
129 parseContent(context);
131 throw context.parseException;
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;
142 if (tContext != null) {
143 context.merge(tContext);
145 while ( !endsWith(buf, "<?")) {
146 int ch = context.read();
150 buf.append((char) ch);
151 if (endsWith(buf, "\\\n")) {
152 buf.delete(buf.length() - 2, buf.length());
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();
162 context.addError("Expected \"?>\"");
165 buf.append((char) ch);
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);
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);
182 commands.add(new IfStatement(variable, body.getBlock("}")));
184 } else if (type.equals("foreach")) {
185 commands.add(new ForeachStatement(variable, body.getBlock("}")));
187 String bestMatching = EditDistance.getBestMatchingStringByEditDistance(type, POSSIBLE_CONTROL_PATTERNS);
188 tContext.addError(String.format(UNKOWN_CONTROL_STRUCTURE_MSG, type, bestMatching));
190 tContext.merge(bodyContext);
192 } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
195 } else if (com.matches(" ?\\} ?")) {
199 commands.add(parseCommand(com, tContext));
202 if (tContext != null) {
203 context.merge(tContext);
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);
210 private boolean endsWith(StringBuffer buf, String string) {
211 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
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);
220 return new SprintfCommand(raw);
222 } else if (s2.startsWith("=$")) {
223 final String raw = s2.substring(2);
224 return new OutputVariableCommand(raw);
226 context.addError("Unknown processing instruction \"" + s2 + "\"," + " did you mean \"" + EditDistance.getBestMatchingStringByEditDistance(s2, new String[] {
234 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
236 data.output(out, l, vars);
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);
245 lastLoaded = source.lastModified() + 1000;
246 } catch (IOException e) {
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)) {
256 Object s = vars.get(varname);
259 System.err.println("Empty variable: " + varname);
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));
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>");
278 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));
282 public void addTranslations(Collection<String> s) {
283 data.addTranslations(s);
286 private class ParseContext {
288 public static final int CONTEXT_LENGTH = 20;
290 private Reader reader;
292 public final TemplateParseException parseException = new TemplateParseException(source);
298 private int curChar = -1;
300 private int[] charContext = new int[CONTEXT_LENGTH];
302 protected int contextPosition = 0;
304 public ParseContext(Reader reader) {
305 this.reader = reader;
308 public void addError(String message) {
309 addError(line, column, message);
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");
320 charContextBuffer.appendCodePoint(charContext[j]);
323 j = (j + 1) % CONTEXT_LENGTH;
325 parseException.addError(line, column, message, charContextBuffer.toString());
328 public void merge(ParseContext other) {
330 column = other.column;
334 public void append(ParseContext other) {
335 parseException.append(other.parseException);
338 public int read() throws IOException {
340 while ((ch = reader.read()) == '\r') {
350 charContext[contextPosition] = ch;
351 contextPosition = (contextPosition + 1) % CONTEXT_LENGTH;
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;