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