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