1 package org.cacert.gigi.output.template;
3 import java.io.EOFException;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.PrintWriter;
10 import java.net.URISyntaxException;
12 import java.util.LinkedList;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
17 import org.cacert.gigi.DevelLauncher;
18 import org.cacert.gigi.Language;
19 import org.cacert.gigi.output.Outputable;
21 public class Template implements Outputable {
22 static class TemplateBlock implements Outputable {
26 public TemplateBlock(String[] contents, Outputable[] vars) {
27 this.contents = contents;
32 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
33 for (int i = 0; i < contents.length; i++) {
34 out.print(contents[i]);
35 if (i < this.vars.length) {
36 this.vars[i].output(out, l, vars);
48 private static final Pattern IF_PATTERN = Pattern.compile(" ?if\\(\\$([^)]+)\\) ?\\{ ?");
50 public Template(URL u) {
52 Reader r = new InputStreamReader(u.openStream(), "UTF-8");
54 if (u.getProtocol().equals("file") && DevelLauncher.DEVEL) {
55 source = new File(u.toURI());
56 lastLoaded = source.lastModified() + 1000;
58 } catch (URISyntaxException e) {
63 } catch (IOException e) {
68 public Template(Reader r) {
72 } catch (IOException e) {
77 private TemplateBlock parse(Reader r) throws IOException {
78 LinkedList<String> splitted = new LinkedList<String>();
79 LinkedList<Outputable> commands = new LinkedList<Outputable>();
80 StringBuffer buf = new StringBuffer();
83 while (!endsWith(buf, "<?")) {
87 buf.append((char) ch);
90 buf.delete(buf.length() - 2, buf.length());
91 splitted.add(buf.toString());
92 buf.delete(0, buf.length());
93 while (!endsWith(buf, "?>")) {
94 buf.append((char) ch);
97 throw new EOFException();
100 buf.delete(buf.length() - 2, buf.length());
101 String com = buf.toString().replace("\n", "");
102 buf.delete(0, buf.length());
103 Matcher m = IF_PATTERN.matcher(com);
105 final String variable = m.group(1);
106 final TemplateBlock body = parse(r);
107 commands.add(new Outputable() {
110 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
111 Object o = vars.get(variable);
112 if (o instanceof Boolean && o == Boolean.TRUE) {
113 body.output(out, l, vars);
119 if (com.matches(" ?\\} ?")) {
122 commands.add(parseCommand(com));
124 splitted.add(buf.toString());
125 String[] contents = splitted.toArray(new String[splitted.size()]);
126 Outputable[] vars = commands.toArray(new Outputable[commands.size()]);
127 return new TemplateBlock(contents, vars);
130 private boolean endsWith(StringBuffer buf, String string) {
131 return buf.length() >= string.length()
132 && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
135 private Outputable parseCommand(String s2) {
136 if (s2.startsWith("=_")) {
137 final String raw = s2.substring(2);
138 return new Outputable() {
141 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
142 out.print(l.getTranslation(raw));
145 } else if (s2.startsWith("=$")) {
146 final String raw = s2.substring(2);
147 return new Outputable() {
150 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
151 outputVar(out, l, vars, raw);
154 } else if (s2.startsWith("=s,")) {
155 String command = s2.substring(3);
156 final LinkedList<String> store = new LinkedList<String>();
157 while (command.startsWith("$")) {
158 int idx = command.indexOf(",");
159 store.add(command.substring(0, idx));
160 command = command.substring(idx + 1);
162 final String text = command;
163 return new Outputable() {
166 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
167 String[] parts = l.getTranslation(text).split("%s");
168 String[] myvars = store.toArray(new String[store.size()]);
170 for (int j = 1; j < parts.length; j++) {
171 outputVar(out, l, vars, myvars[j - 1].substring(1));
177 System.out.println("Unknown processing instruction: " + s2);
182 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
183 if (source != null && DevelLauncher.DEVEL) {
184 if (lastLoaded < source.lastModified()) {
186 System.out.println("Reloading template.... " + source);
187 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
190 lastLoaded = source.lastModified() + 1000;
191 } catch (IOException e) {
196 data.output(out, l, vars);
199 private void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname) {
200 Object s = vars.get(varname);
203 System.out.println("Empty variable: " + varname);
205 if (s instanceof Outputable) {
206 ((Outputable) s).output(out, l, vars);