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.localisation.Language;
19 import org.cacert.gigi.util.HTMLEncoder;
21 public class Template implements Outputable {
23 private TemplateBlock data;
25 private long lastLoaded;
29 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?");
31 public Template(URL u) {
33 Reader r = new InputStreamReader(u.openStream(), "UTF-8");
35 if (u.getProtocol().equals("file") && DevelLauncher.DEVEL) {
36 source = new File(u.toURI());
37 lastLoaded = source.lastModified() + 1000;
39 } catch (URISyntaxException e) {
44 } catch (IOException e) {
49 public Template(Reader r) {
53 } catch (IOException e) {
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();
64 while ( !endsWith(buf, "<?")) {
69 buf.append((char) ch);
71 buf.delete(buf.length() - 2, buf.length());
72 splitted.add(buf.toString());
73 buf.delete(0, buf.length());
74 while ( !endsWith(buf, "?>")) {
77 throw new EOFException();
79 buf.append((char) ch);
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);
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));
94 throw new IOException("Syntax error: unknown control structure: " + type);
98 if (com.matches(" ?\\} ?")) {
101 commands.add(parseCommand(com));
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);
109 private boolean endsWith(StringBuffer buf, String string) {
110 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
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("!\"")) {
125 if (command.startsWith("\"") || command.startsWith("!\"")) {
126 idx = command.indexOf("\"", command.charAt(0) == '!' ? 2 : 1) + 1;
127 store.add(command.substring(0, idx - 1));
129 idx = command.indexOf(",");
130 store.add(command.substring(0, idx));
132 command = command.substring(idx + 1);
134 final String text = command;
135 return new SprintfCommand(text, store);
137 System.out.println("Unknown processing instruction: " + s2);
143 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
144 if (source != null && DevelLauncher.DEVEL) {
145 if (lastLoaded < source.lastModified()) {
147 System.out.println("Reloading template.... " + source);
148 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
151 lastLoaded = source.lastModified() + 1000;
152 } catch (IOException e) {
157 data.output(out, l, vars);
160 protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
161 Object s = vars.get(varname);
164 System.out.println("Empty variable: " + varname);
166 if (s instanceof Outputable) {
167 ((Outputable) s).output(out, l, vars);
169 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));