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