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