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