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