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.util.HTMLEncoder;
21 public class Template implements Outputable {
29 public ParseResult(TemplateBlock block, String endType) {
31 this.endType = endType;
34 public String getEndType() {
38 public TemplateBlock getBlock(String reqType) {
39 if (endType == null && reqType == null) {
42 if (endType == null || reqType == null) {
43 throw new Error("Invalid block type: " + endType);
45 if (endType.equals(reqType)) {
48 throw new Error("Invalid block type: " + endType);
52 private TemplateBlock data;
54 private long lastLoaded;
58 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?");
60 private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
62 public Template(URL u) {
64 Reader r = new InputStreamReader(u.openStream(), "UTF-8");
66 if (u.getProtocol().equals("file") && DevelLauncher.DEVEL) {
67 source = new File(u.toURI());
68 lastLoaded = source.lastModified() + 1000;
70 } catch (URISyntaxException e) {
73 data = parse(r).getBlock(null);
75 } catch (IOException e) {
80 public Template(Reader r) {
82 data = parse(r).getBlock(null);
84 } catch (IOException e) {
89 private ParseResult parse(Reader r) throws IOException {
90 LinkedList<String> splitted = new LinkedList<String>();
91 LinkedList<Outputable> commands = new LinkedList<Outputable>();
92 StringBuffer buf = new StringBuffer();
93 String blockType = null;
96 while ( !endsWith(buf, "<?")) {
101 buf.append((char) ch);
103 buf.delete(buf.length() - 2, buf.length());
104 splitted.add(buf.toString());
105 buf.delete(0, buf.length());
106 while ( !endsWith(buf, "?>")) {
109 throw new EOFException();
111 buf.append((char) ch);
113 buf.delete(buf.length() - 2, buf.length());
114 String com = buf.toString().replace("\n", "");
115 buf.delete(0, buf.length());
116 Matcher m = CONTROL_PATTERN.matcher(com);
118 String type = m.group(1);
119 String variable = m.group(2);
120 ParseResult body = parse(r);
121 if (type.equals("if")) {
122 if ("else".equals(body.getEndType())) {
123 commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
125 commands.add(new IfStatement(variable, body.getBlock("}")));
127 } else if (type.equals("foreach")) {
128 commands.add(new ForeachStatement(variable, body.getBlock("}")));
130 throw new IOException("Syntax error: unknown control structure: " + type);
133 } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
136 } else if (com.matches(" ?\\} ?")) {
140 commands.add(parseCommand(com));
143 splitted.add(buf.toString());
144 return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Outputable[commands.size()])), blockType);
147 private boolean endsWith(StringBuffer buf, String string) {
148 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
151 private Outputable parseCommand(String s2) {
152 if (s2.startsWith("=_")) {
153 final String raw = s2.substring(2);
154 if ( !s2.contains("$") && !s2.contains("!'")) {
155 return new TranslateCommand(raw);
157 return new SprintfCommand(raw);
159 } else if (s2.startsWith("=$")) {
160 final String raw = s2.substring(2);
161 return new OutputVariableCommand(raw);
163 throw new Error("Unknown processing instruction: " + s2);
168 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
169 if (source != null && DevelLauncher.DEVEL) {
170 if (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) {
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);
194 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));