]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/Template.java
Fix templating. Add language to IterableDataset.
[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 CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?");
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                 outer: while (true) {
61                         while (!endsWith(buf, "<?")) {
62                                 int ch = r.read();
63                                 if (ch == -1) {
64                                         break outer;
65                                 }
66                                 buf.append((char) ch);
67                         }
68                         buf.delete(buf.length() - 2, buf.length());
69                         splitted.add(buf.toString());
70                         buf.delete(0, buf.length());
71                         while (!endsWith(buf, "?>")) {
72                                 int ch = r.read();
73                                 if (ch == -1) {
74                                         throw new EOFException();
75                                 }
76                                 buf.append((char) ch);
77                         }
78                         buf.delete(buf.length() - 2, buf.length());
79                         String com = buf.toString().replace("\n", "");
80                         buf.delete(0, buf.length());
81                         Matcher m = CONTROL_PATTERN.matcher(com);
82                         if (m.matches()) {
83                                 String type = m.group(1);
84                                 String variable = m.group(2);
85                                 TemplateBlock body = parse(r);
86                                 if (type.equals("if")) {
87                                         commands.add(new IfStatement(variable, body));
88                                 } else if (type.equals("foreach")) {
89                                         commands.add(new ForeachStatement(variable, body));
90                                 } else {
91                                         throw new IOException("Syntax error: unknown control structure: " + type);
92                                 }
93                                 continue;
94                         }
95                         if (com.matches(" ?\\} ?")) {
96                                 break;
97                         }
98                         commands.add(parseCommand(com));
99                 }
100                 splitted.add(buf.toString());
101                 String[] contents = splitted.toArray(new String[splitted.size()]);
102                 Outputable[] vars = commands.toArray(new Outputable[commands.size()]);
103                 return new TemplateBlock(contents, vars);
104         }
105
106         private boolean endsWith(StringBuffer buf, String string) {
107                 return buf.length() >= string.length()
108                         && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
109         }
110
111         private Outputable parseCommand(String s2) {
112                 if (s2.startsWith("=_")) {
113                         final String raw = s2.substring(2);
114                         return new TranslateCommand(raw);
115                 } else if (s2.startsWith("=$")) {
116                         final String raw = s2.substring(2);
117                         return new OutputVariableCommand(raw);
118                 } else if (s2.startsWith("=s,")) {
119                         String command = s2.substring(3);
120                         final LinkedList<String> store = new LinkedList<String>();
121                         while (command.startsWith("$")) {
122                                 int idx = command.indexOf(",");
123                                 store.add(command.substring(0, idx));
124                                 command = command.substring(idx + 1);
125                         }
126                         final String text = command;
127                         return new SprintfCommand(text, store);
128                 } else {
129                         System.out.println("Unknown processing instruction: " + s2);
130                 }
131                 return null;
132         }
133
134         public void output(PrintWriter out, Language l, Map<String, Object> vars) {
135                 if (source != null && DevelLauncher.DEVEL) {
136                         if (lastLoaded < source.lastModified()) {
137                                 try {
138                                         System.out.println("Reloading template.... " + source);
139                                         InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
140                                         data = parse(r);
141                                         r.close();
142                                         lastLoaded = source.lastModified() + 1000;
143                                 } catch (IOException e) {
144                                         e.printStackTrace();
145                                 }
146                         }
147                 }
148                 data.output(out, l, vars);
149         }
150
151         protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname) {
152                 Object s = vars.get(varname);
153
154                 if (s == null) {
155                         System.out.println("Empty variable: " + varname);
156                 }
157                 if (s instanceof Outputable) {
158                         ((Outputable) s).output(out, l, vars);
159                 } else {
160                         out.print(s);
161                 }
162         }
163 }