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.localisation.Language;
18 import org.cacert.gigi.util.HTMLEncoder;
20 public class Template implements Outputable {
28 public ParseResult(TemplateBlock block, String endType) {
30 this.endType = endType;
33 public String getEndType() {
37 public TemplateBlock getBlock(String reqType) {
38 if (endType == null && reqType == null) {
41 if (endType == null || reqType == null) {
42 throw new Error("Invalid block type: " + endType);
44 if (endType.equals(reqType)) {
47 throw new Error("Invalid block type: " + endType);
51 private TemplateBlock data;
53 private long lastLoaded;
57 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?");
59 private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
61 public Template(URL u) {
63 Reader r = new InputStreamReader(u.openStream(), "UTF-8");
65 if (u.getProtocol().equals("file")) {
66 source = new File(u.toURI());
67 lastLoaded = source.lastModified() + 1000;
69 } catch (URISyntaxException e) {
72 data = parse(r).getBlock(null);
74 } catch (IOException e) {
79 public Template(Reader r) {
81 data = parse(r).getBlock(null);
83 } catch (IOException e) {
88 private ParseResult parse(Reader r) throws IOException {
89 LinkedList<String> splitted = new LinkedList<String>();
90 LinkedList<Outputable> commands = new LinkedList<Outputable>();
91 StringBuffer buf = new StringBuffer();
92 String blockType = null;
95 while ( !endsWith(buf, "<?")) {
100 buf.append((char) ch);
102 buf.delete(buf.length() - 2, buf.length());
103 splitted.add(buf.toString());
104 buf.delete(0, buf.length());
105 while ( !endsWith(buf, "?>")) {
108 throw new EOFException();
110 buf.append((char) ch);
112 buf.delete(buf.length() - 2, buf.length());
113 String com = buf.toString().replace("\n", "");
114 buf.delete(0, buf.length());
115 Matcher m = CONTROL_PATTERN.matcher(com);
117 String type = m.group(1);
118 String variable = m.group(2);
119 ParseResult body = parse(r);
120 if (type.equals("if")) {
121 if ("else".equals(body.getEndType())) {
122 commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
124 commands.add(new IfStatement(variable, body.getBlock("}")));
126 } else if (type.equals("foreach")) {
127 commands.add(new ForeachStatement(variable, body.getBlock("}")));
129 throw new IOException("Syntax error: unknown control structure: " + type);
132 } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
135 } else if (com.matches(" ?\\} ?")) {
139 commands.add(parseCommand(com));
142 splitted.add(buf.toString());
143 return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Outputable[commands.size()])), blockType);
146 private boolean endsWith(StringBuffer buf, String string) {
147 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
150 private Outputable parseCommand(String s2) {
151 if (s2.startsWith("=_")) {
152 final String raw = s2.substring(2);
153 if ( !s2.contains("$") && !s2.contains("!'")) {
154 return new TranslateCommand(raw);
156 return new SprintfCommand(raw);
158 } else if (s2.startsWith("=$")) {
159 final String raw = s2.substring(2);
160 return new OutputVariableCommand(raw);
162 throw new Error("Unknown processing instruction: " + s2);
167 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
168 if (source != null && lastLoaded < source.lastModified()) {
170 System.out.println("Reloading template.... " + source);
171 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
172 data = parse(r).getBlock(null);
174 lastLoaded = source.lastModified() + 1000;
175 } catch (IOException e) {
179 data.output(out, l, vars);
182 protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
183 Object s = vars.get(varname);
186 System.out.println("Empty variable: " + varname);
188 if (s instanceof Outputable) {
189 ((Outputable) s).output(out, l, vars);
191 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));