]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/Template.java
8c633c422934d415b7da3b813bc5d44512f00753
[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.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 org.cacert.gigi.localisation.Language;
21 import org.cacert.gigi.output.DateSelector;
22 import org.cacert.gigi.util.DayDate;
23 import org.cacert.gigi.util.HTMLEncoder;
24
25 /**
26  * Represents a loaded template file.
27  */
28 public class Template implements Outputable {
29
30     protected static class ParseResult {
31
32         TemplateBlock block;
33
34         String endType;
35
36         public ParseResult(TemplateBlock block, String endType) {
37             this.block = block;
38             this.endType = endType;
39         }
40
41         public String getEndType() {
42             return endType;
43         }
44
45         public TemplateBlock getBlock(String reqType) {
46             if (endType == null && reqType == null) {
47                 return block;
48             }
49             if (endType == null || reqType == null) {
50                 throw new Error("Invalid block type: " + endType);
51             }
52             if (endType.equals(reqType)) {
53                 return block;
54             }
55             throw new Error("Invalid block type: " + endType);
56         }
57     }
58
59     private TemplateBlock data;
60
61     private long lastLoaded;
62
63     private File source;
64
65     private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-zA-Z]+)\\(\\$([^)]+)\\) ?\\{ ?");
66
67     private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
68
69     /**
70      * Creates a new template by parsing the contents from the given URL. This
71      * constructor will fail on syntax error. When the URL points to a file,
72      * {@link File#lastModified()} is monitored for changes of the template.
73      * 
74      * @param u
75      *            the URL to load the template from. UTF-8 is chosen as charset.
76      */
77     public Template(URL u) {
78         try (Reader r = new InputStreamReader(u.openStream(), "UTF-8")) {
79             try {
80                 if (u.getProtocol().equals("file")) {
81                     source = new File(u.toURI());
82                     lastLoaded = source.lastModified() + 1000;
83                 }
84             } catch (URISyntaxException e) {
85                 e.printStackTrace();
86             }
87             data = parse(r).getBlock(null);
88         } catch (IOException e) {
89             throw new Error(e);
90         }
91     }
92
93     /**
94      * Creates a new template by parsing the contents from the given reader.
95      * This constructor will fail on syntax error.
96      * 
97      * @param r
98      *            the Reader containing the data.
99      */
100     public Template(Reader r) {
101         try {
102             data = parse(r).getBlock(null);
103             r.close();
104         } catch (IOException e) {
105             throw new Error(e);
106         }
107     }
108
109     protected ParseResult parse(Reader r) throws IOException {
110         return parseContent(r);
111     }
112
113     protected ParseResult parseContent(Reader r) throws IOException {
114         LinkedList<String> splitted = new LinkedList<String>();
115         LinkedList<Translatable> commands = new LinkedList<Translatable>();
116         StringBuffer buf = new StringBuffer();
117         String blockType = null;
118         outer:
119         while (true) {
120             while ( !endsWith(buf, "<?")) {
121                 int ch = r.read();
122                 if (ch == -1) {
123                     break outer;
124                 }
125                 buf.append((char) ch);
126                 if (endsWith(buf, "\\\n")) {
127                     buf.delete(buf.length() - 2, buf.length());
128                 }
129             }
130             buf.delete(buf.length() - 2, buf.length());
131             splitted.add(buf.toString());
132             buf.delete(0, buf.length());
133             while ( !endsWith(buf, "?>")) {
134                 int ch = r.read();
135                 if (ch == -1) {
136                     throw new EOFException();
137                 }
138                 buf.append((char) ch);
139             }
140             buf.delete(buf.length() - 2, buf.length());
141             String com = buf.toString().replace("\n", "");
142             buf.delete(0, buf.length());
143             Matcher m = CONTROL_PATTERN.matcher(com);
144             if (m.matches()) {
145                 String type = m.group(1);
146                 String variable = m.group(2);
147                 ParseResult body = parseContent(r);
148                 if (type.equals("if")) {
149                     if ("else".equals(body.getEndType())) {
150                         commands.add(new IfStatement(variable, body.getBlock("else"), parseContent(r).getBlock("}")));
151                     } else {
152                         commands.add(new IfStatement(variable, body.getBlock("}")));
153                     }
154                 } else if (type.equals("foreach")) {
155                     commands.add(new ForeachStatement(variable, body.getBlock("}")));
156                 } else {
157                     throw new IOException("Syntax error: unknown control structure: " + type);
158                 }
159                 continue;
160             } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
161                 blockType = "else";
162                 break;
163             } else if (com.matches(" ?\\} ?")) {
164                 blockType = "}";
165                 break;
166             } else {
167                 commands.add(parseCommand(com));
168             }
169         }
170         splitted.add(buf.toString());
171         return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Translatable[commands.size()])), blockType);
172     }
173
174     private boolean endsWith(StringBuffer buf, String string) {
175         return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
176     }
177
178     private Translatable parseCommand(String s2) {
179         if (s2.startsWith("=_")) {
180             final String raw = s2.substring(2);
181             if ( !s2.contains("$") && !s2.contains("!'")) {
182                 return new TranslateCommand(raw);
183             } else {
184                 return new SprintfCommand(raw);
185             }
186         } else if (s2.startsWith("=$")) {
187             final String raw = s2.substring(2);
188             return new OutputVariableCommand(raw);
189         } else {
190             throw new Error("Unknown processing instruction: " + s2);
191         }
192     }
193
194     @Override
195     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
196         tryReload();
197         data.output(out, l, vars);
198     }
199
200     protected void tryReload() {
201         if (source != null && lastLoaded < source.lastModified()) {
202             try {
203                 System.out.println("Reloading template.... " + source);
204                 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
205                 data = parse(r).getBlock(null);
206                 r.close();
207                 lastLoaded = source.lastModified() + 1000;
208             } catch (IOException e) {
209                 e.printStackTrace();
210             }
211         }
212     }
213
214     protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
215         if (vars.containsKey(Outputable.OUT_KEY_PLAIN)) {
216             unescaped = true;
217         }
218         Object s = vars.get(varname);
219
220         if (s == null) {
221             System.out.println("Empty variable: " + varname);
222         }
223         if (s instanceof Outputable) {
224             ((Outputable) s).output(out, l, vars);
225         } else if (s instanceof DayDate) {
226             out.print(DateSelector.getDateFormat().format(((DayDate) s).toDate()));
227         } else if (s instanceof Boolean) {
228             out.print(((Boolean) s) ? l.getTranslation("yes") : l.getTranslation("no"));
229         } else if (s instanceof Date) {
230             SimpleDateFormat sdfUI = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
231             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
232             out.print("<time datetime=\"" + sdf.format(s) + "\">");
233             out.print(sdfUI.format(s));
234             out.print(" UTC</time>");
235         } else {
236             out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));
237         }
238     }
239
240     public void addTranslations(Collection<String> s) {
241         data.addTranslations(s);
242     }
243 }