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