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