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.DayDate;
22 import org.cacert.gigi.util.HTMLEncoder;
24 public class Template implements Outputable {
26 private static class ParseResult {
32 public ParseResult(TemplateBlock block, String endType) {
34 this.endType = endType;
37 public String getEndType() {
41 public TemplateBlock getBlock(String reqType) {
42 if (endType == null && reqType == null) {
45 if (endType == null || reqType == null) {
46 throw new Error("Invalid block type: " + endType);
48 if (endType.equals(reqType)) {
51 throw new Error("Invalid block type: " + endType);
55 private TemplateBlock data;
57 private long lastLoaded;
61 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-zA-Z]+)\\(\\$([^)]+)\\) ?\\{ ?");
63 private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
65 public Template(URL u) {
67 Reader r = new InputStreamReader(u.openStream(), "UTF-8");
69 if (u.getProtocol().equals("file")) {
70 source = new File(u.toURI());
71 lastLoaded = source.lastModified() + 1000;
73 } catch (URISyntaxException e) {
76 data = parse(r).getBlock(null);
78 } catch (IOException e) {
83 public Template(Reader r) {
85 data = parse(r).getBlock(null);
87 } catch (IOException e) {
92 private ParseResult parse(Reader r) throws IOException {
93 LinkedList<String> splitted = new LinkedList<String>();
94 LinkedList<Outputable> commands = new LinkedList<Outputable>();
95 StringBuffer buf = new StringBuffer();
96 String blockType = null;
99 while ( !endsWith(buf, "<?")) {
104 buf.append((char) ch);
106 buf.delete(buf.length() - 2, buf.length());
107 splitted.add(buf.toString());
108 buf.delete(0, buf.length());
109 while ( !endsWith(buf, "?>")) {
112 throw new EOFException();
114 buf.append((char) ch);
116 buf.delete(buf.length() - 2, buf.length());
117 String com = buf.toString().replace("\n", "");
118 buf.delete(0, buf.length());
119 Matcher m = CONTROL_PATTERN.matcher(com);
121 String type = m.group(1);
122 String variable = m.group(2);
123 ParseResult body = parse(r);
124 if (type.equals("if")) {
125 if ("else".equals(body.getEndType())) {
126 commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
128 commands.add(new IfStatement(variable, body.getBlock("}")));
130 } else if (type.equals("foreach")) {
131 commands.add(new ForeachStatement(variable, body.getBlock("}")));
133 throw new IOException("Syntax error: unknown control structure: " + type);
136 } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
139 } else if (com.matches(" ?\\} ?")) {
143 commands.add(parseCommand(com));
146 splitted.add(buf.toString());
147 return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Outputable[commands.size()])), blockType);
150 private boolean endsWith(StringBuffer buf, String string) {
151 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
154 private Outputable parseCommand(String s2) {
155 if (s2.startsWith("=_")) {
156 final String raw = s2.substring(2);
157 if ( !s2.contains("$") && !s2.contains("!'")) {
158 return new TranslateCommand(raw);
160 return new SprintfCommand(raw);
162 } else if (s2.startsWith("=$")) {
163 final String raw = s2.substring(2);
164 return new OutputVariableCommand(raw);
166 throw new Error("Unknown processing instruction: " + s2);
171 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
172 if (source != null && lastLoaded < source.lastModified()) {
174 System.out.println("Reloading template.... " + source);
175 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
176 data = parse(r).getBlock(null);
178 lastLoaded = source.lastModified() + 1000;
179 } catch (IOException e) {
183 data.output(out, l, vars);
186 protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
187 Object s = vars.get(varname);
190 System.out.println("Empty variable: " + varname);
192 if (s instanceof Outputable) {
193 ((Outputable) s).output(out, l, vars);
194 } else if (s instanceof DayDate) {
195 out.print(DateSelector.getDateFormat().format(((DayDate) s).toDate()));
196 } else if (s instanceof Date) {
197 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
198 out.print(sdf.format(s));
201 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));