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.text.SimpleDateFormat;
13 import java.util.Date;
14 import java.util.LinkedList;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
19 import org.cacert.gigi.localisation.Language;
20 import org.cacert.gigi.output.DateSelector;
21 import org.cacert.gigi.util.HTMLEncoder;
23 public class Template implements Outputable {
25 private static class ParseResult {
31 public ParseResult(TemplateBlock block, String endType) {
33 this.endType = endType;
36 public String getEndType() {
40 public TemplateBlock getBlock(String reqType) {
41 if (endType == null && reqType == null) {
44 if (endType == null || reqType == null) {
45 throw new Error("Invalid block type: " + endType);
47 if (endType.equals(reqType)) {
50 throw new Error("Invalid block type: " + endType);
54 private TemplateBlock data;
56 private long lastLoaded;
60 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-zA-Z]+)\\(\\$([^)]+)\\) ?\\{ ?");
62 private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
64 public Template(URL u) {
66 Reader r = new InputStreamReader(u.openStream(), "UTF-8");
68 if (u.getProtocol().equals("file")) {
69 source = new File(u.toURI());
70 lastLoaded = source.lastModified() + 1000;
72 } catch (URISyntaxException e) {
75 data = parse(r).getBlock(null);
77 } catch (IOException e) {
82 public Template(Reader r) {
84 data = parse(r).getBlock(null);
86 } catch (IOException e) {
91 private ParseResult parse(Reader r) throws IOException {
92 LinkedList<String> splitted = new LinkedList<String>();
93 LinkedList<Outputable> commands = new LinkedList<Outputable>();
94 StringBuffer buf = new StringBuffer();
95 String blockType = null;
98 while ( !endsWith(buf, "<?")) {
103 buf.append((char) ch);
105 buf.delete(buf.length() - 2, buf.length());
106 splitted.add(buf.toString());
107 buf.delete(0, buf.length());
108 while ( !endsWith(buf, "?>")) {
111 throw new EOFException();
113 buf.append((char) ch);
115 buf.delete(buf.length() - 2, buf.length());
116 String com = buf.toString().replace("\n", "");
117 buf.delete(0, buf.length());
118 Matcher m = CONTROL_PATTERN.matcher(com);
120 String type = m.group(1);
121 String variable = m.group(2);
122 ParseResult body = parse(r);
123 if (type.equals("if")) {
124 if ("else".equals(body.getEndType())) {
125 commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
127 commands.add(new IfStatement(variable, body.getBlock("}")));
129 } else if (type.equals("foreach")) {
130 commands.add(new ForeachStatement(variable, body.getBlock("}")));
132 throw new IOException("Syntax error: unknown control structure: " + type);
135 } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
138 } else if (com.matches(" ?\\} ?")) {
142 commands.add(parseCommand(com));
145 splitted.add(buf.toString());
146 return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Outputable[commands.size()])), blockType);
149 private boolean endsWith(StringBuffer buf, String string) {
150 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
153 private Outputable parseCommand(String s2) {
154 if (s2.startsWith("=_")) {
155 final String raw = s2.substring(2);
156 if ( !s2.contains("$") && !s2.contains("!'")) {
157 return new TranslateCommand(raw);
159 return new SprintfCommand(raw);
161 } else if (s2.startsWith("=$")) {
162 final String raw = s2.substring(2);
163 return new OutputVariableCommand(raw);
165 throw new Error("Unknown processing instruction: " + s2);
170 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
171 if (source != null && lastLoaded < source.lastModified()) {
173 System.out.println("Reloading template.... " + source);
174 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
175 data = parse(r).getBlock(null);
177 lastLoaded = source.lastModified() + 1000;
178 } catch (IOException e) {
182 data.output(out, l, vars);
185 protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
186 Object s = vars.get(varname);
189 System.out.println("Empty variable: " + varname);
191 if (s instanceof Outputable) {
192 ((Outputable) s).output(out, l, vars);
193 } else if (s instanceof java.sql.Date) {
194 out.print(DateSelector.getDateFormat().format(s));
195 } else if (s instanceof Date) {
196 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
197 out.print(sdf.format(s));
200 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));