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.output.Outputable;
20 import org.cacert.gigi.util.HTMLEncoder;
22 public class Template implements Outputable {
30 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?");
32 public Template(URL u) {
34 Reader r = new InputStreamReader(u.openStream(), "UTF-8");
36 if (u.getProtocol().equals("file") && DevelLauncher.DEVEL) {
37 source = new File(u.toURI());
38 lastLoaded = source.lastModified() + 1000;
40 } catch (URISyntaxException e) {
45 } catch (IOException e) {
50 public Template(Reader r) {
54 } catch (IOException e) {
59 private TemplateBlock parse(Reader r) throws IOException {
60 LinkedList<String> splitted = new LinkedList<String>();
61 LinkedList<Outputable> commands = new LinkedList<Outputable>();
62 StringBuffer buf = new StringBuffer();
65 while ( !endsWith(buf, "<?")) {
70 buf.append((char) ch);
72 buf.delete(buf.length() - 2, buf.length());
73 splitted.add(buf.toString());
74 buf.delete(0, buf.length());
75 while ( !endsWith(buf, "?>")) {
78 throw new EOFException();
80 buf.append((char) ch);
82 buf.delete(buf.length() - 2, buf.length());
83 String com = buf.toString().replace("\n", "");
84 buf.delete(0, buf.length());
85 Matcher m = CONTROL_PATTERN.matcher(com);
87 String type = m.group(1);
88 String variable = m.group(2);
89 TemplateBlock body = parse(r);
90 if (type.equals("if")) {
91 commands.add(new IfStatement(variable, body));
92 } else if (type.equals("foreach")) {
93 commands.add(new ForeachStatement(variable, body));
95 throw new IOException("Syntax error: unknown control structure: " + type);
99 if (com.matches(" ?\\} ?")) {
102 commands.add(parseCommand(com));
104 splitted.add(buf.toString());
105 String[] contents = splitted.toArray(new String[splitted.size()]);
106 Outputable[] vars = commands.toArray(new Outputable[commands.size()]);
107 return new TemplateBlock(contents, vars);
110 private boolean endsWith(StringBuffer buf, String string) {
111 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
114 private Outputable parseCommand(String s2) {
115 if (s2.startsWith("=_")) {
116 final String raw = s2.substring(2);
117 return new TranslateCommand(raw);
118 } else if (s2.startsWith("=$")) {
119 final String raw = s2.substring(2);
120 return new OutputVariableCommand(raw);
121 } else if (s2.startsWith("=s,")) {
122 String command = s2.substring(3);
123 final LinkedList<String> store = new LinkedList<String>();
124 while (command.startsWith("$")) {
125 int idx = command.indexOf(",");
126 store.add(command.substring(0, idx));
127 command = command.substring(idx + 1);
129 final String text = command;
130 return new SprintfCommand(text, store);
132 System.out.println("Unknown processing instruction: " + s2);
137 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
138 if (source != null && DevelLauncher.DEVEL) {
139 if (lastLoaded < source.lastModified()) {
141 System.out.println("Reloading template.... " + source);
142 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
145 lastLoaded = source.lastModified() + 1000;
146 } catch (IOException e) {
151 data.output(out, l, vars);
154 protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
155 Object s = vars.get(varname);
158 System.out.println("Empty variable: " + varname);
160 if (s instanceof Outputable) {
161 ((Outputable) s).output(out, l, vars);
163 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));