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)
41 if (endType == null || reqType == null) {
42 throw new Error("Invalid block type: " + endType);
44 if (endType.equals(reqType))
46 throw new Error("Invalid block type: " + endType);
50 private TemplateBlock data;
52 private long lastLoaded;
56 private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?");
58 private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
60 public Template(URL u) {
62 Reader r = new InputStreamReader(u.openStream(), "UTF-8");
64 if (u.getProtocol().equals("file") && DevelLauncher.DEVEL) {
65 source = new File(u.toURI());
66 lastLoaded = source.lastModified() + 1000;
68 } catch (URISyntaxException e) {
71 data = parse(r).getBlock(null);
73 } catch (IOException e) {
78 public Template(Reader r) {
80 data = parse(r).getBlock(null);
82 } catch (IOException e) {
87 private ParseResult parse(Reader r) throws IOException {
88 LinkedList<String> splitted = new LinkedList<String>();
89 LinkedList<Outputable> commands = new LinkedList<Outputable>();
90 StringBuffer buf = new StringBuffer();
91 String blockType = null;
94 while ( !endsWith(buf, "<?")) {
99 buf.append((char) ch);
101 buf.delete(buf.length() - 2, buf.length());
102 splitted.add(buf.toString());
103 buf.delete(0, buf.length());
104 while ( !endsWith(buf, "?>")) {
107 throw new EOFException();
109 buf.append((char) ch);
111 buf.delete(buf.length() - 2, buf.length());
112 String com = buf.toString().replace("\n", "");
113 buf.delete(0, buf.length());
114 Matcher m = CONTROL_PATTERN.matcher(com);
116 String type = m.group(1);
117 String variable = m.group(2);
118 ParseResult body = parse(r);
119 if (type.equals("if")) {
120 if ("else".equals(body.getEndType())) {
121 commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
123 commands.add(new IfStatement(variable, body.getBlock("}")));
125 } else if (type.equals("foreach")) {
126 commands.add(new ForeachStatement(variable, body.getBlock("}")));
128 throw new IOException("Syntax error: unknown control structure: " + type);
131 } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
134 } else if (com.matches(" ?\\} ?")) {
138 commands.add(parseCommand(com));
141 splitted.add(buf.toString());
142 return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Outputable[commands.size()])), blockType);
145 private boolean endsWith(StringBuffer buf, String string) {
146 return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
149 private Outputable parseCommand(String s2) {
150 if (s2.startsWith("=_")) {
151 final String raw = s2.substring(2);
152 return new TranslateCommand(raw);
153 } else if (s2.startsWith("=$")) {
154 final String raw = s2.substring(2);
155 return new OutputVariableCommand(raw);
156 } else if (s2.startsWith("=s,")) {
157 String command = s2.substring(3);
158 final LinkedList<String> store = new LinkedList<String>();
159 while (command.startsWith("$") || command.startsWith("\"") || command.startsWith("!\"")) {
161 if (command.startsWith("\"") || command.startsWith("!\"")) {
162 idx = command.indexOf("\"", command.charAt(0) == '!' ? 2 : 1) + 1;
163 store.add(command.substring(0, idx - 1));
165 idx = command.indexOf(",");
166 store.add(command.substring(0, idx));
168 command = command.substring(idx + 1);
170 final String text = command;
171 return new SprintfCommand(text, store);
173 System.out.println("Unknown processing instruction: " + s2);
179 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
180 if (source != null && DevelLauncher.DEVEL) {
181 if (lastLoaded < source.lastModified()) {
183 System.out.println("Reloading template.... " + source);
184 InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
185 data = parse(r).getBlock(null);
187 lastLoaded = source.lastModified() + 1000;
188 } catch (IOException e) {
193 data.output(out, l, vars);
196 protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
197 Object s = vars.get(varname);
200 System.out.println("Empty variable: " + varname);
202 if (s instanceof Outputable) {
203 ((Outputable) s).output(out, l, vars);
205 out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));