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.Date;
13 import java.util.LinkedList;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.cacert.gigi.localisation.Language;
19 import org.cacert.gigi.output.DateSelector;
20 import org.cacert.gigi.util.HTMLEncoder;
22 public class Template implements Outputable {
30 public ParseResult(TemplateBlock block, String endType) {
32 this.endType = endType;
35 public String getEndType() {
39 public TemplateBlock getBlock(String reqType) {
40 if (endType == null && reqType == null) {
43 if (endType == null || reqType == null) {
44 throw new Error("Invalid block type: " + endType);
46 if (endType.equals(reqType)) {
49 throw new Error("Invalid block type: " + endType);
53 private TemplateBlock data;
55 private long lastLoaded;
59 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-zA-Z]+)\\(\\$([^)]+)\\) ?\\{ ?");
61 private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
63 public Template(URL u) {
65 Reader r = new InputStreamReader(u.openStream(), "UTF-8");
67 if (u.getProtocol().equals("file")) {
68 source = new File(u.toURI());
69 lastLoaded = source.lastModified() + 1000;
71 } catch (URISyntaxException e) {
74 data = parse(r).getBlock(null);
76 } catch (IOException e) {
81 public Template(Reader r) {
83 data = parse(r).getBlock(null);
85 } catch (IOException e) {
90 private ParseResult parse(Reader r) throws IOException {
91 LinkedList<String> splitted = new LinkedList<String>();
92 LinkedList<Outputable> commands = new LinkedList<Outputable>();
93 StringBuffer buf = new StringBuffer();
94 String blockType = null;
97 while ( !endsWith(buf, "<?")) {
102 buf.append((char) ch);
104 buf.delete(buf.length() - 2, buf.length());
105 splitted.add(buf.toString());
106 buf.delete(0, buf.length());
107 while ( !endsWith(buf, "?>")) {
110 throw new EOFException();
112 buf.append((char) ch);
114 buf.delete(buf.length() - 2, buf.length());
115 String com = buf.toString().replace("\n", "");
116 buf.delete(0, buf.length());
117 Matcher m = CONTROL_PATTERN.matcher(com);
119 String type = m.group(1);
120 String variable = m.group(2);
121 ParseResult body = parse(r);
122 if (type.equals("if")) {
123 if ("else".equals(body.getEndType())) {
124 commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
126 commands.add(new IfStatement(variable, body.getBlock("}")));
128 } else if (type.equals("foreach")) {
129 commands.add(new ForeachStatement(variable, body.getBlock("}")));
131 throw new IOException("Syntax error: unknown control structure: " + type);
134 } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
137 } else if (com.matches(" ?\\} ?")) {
141 commands.add(parseCommand(com));
144 splitted.add(buf.toString());
145 return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Outputable[commands.size()])), blockType);
148 private boolean endsWith(StringBuffer buf, String string) {
149 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
152 private Outputable parseCommand(String s2) {
153 if (s2.startsWith("=_")) {
154 final String raw = s2.substring(2);
155 if ( !s2.contains("$") && !s2.contains("!'")) {
156 return new TranslateCommand(raw);
158 return new SprintfCommand(raw);
160 } else if (s2.startsWith("=$")) {
161 final String raw = s2.substring(2);
162 return new OutputVariableCommand(raw);
164 throw new Error("Unknown processing instruction: " + s2);
169 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
170 if (source != null && lastLoaded < source.lastModified()) {
172 System.out.println("Reloading template.... " + source);
173 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
174 data = parse(r).getBlock(null);
176 lastLoaded = source.lastModified() + 1000;
177 } catch (IOException e) {
181 data.output(out, l, vars);
184 protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
185 Object s = vars.get(varname);
188 System.out.println("Empty variable: " + varname);
190 if (s instanceof Outputable) {
191 ((Outputable) s).output(out, l, vars);
192 } else if (s instanceof Date) {
193 out.print(DateSelector.getDateFormat().format(s));
195 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));