]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/Template.java
Merge remote-tracking branch 'origin/libs/scrypt/local'
[gigi.git] / src / org / cacert / gigi / output / template / Template.java
1 package org.cacert.gigi.output.template;
2
3 import java.io.EOFException;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.io.PrintWriter;
9 import java.io.Reader;
10 import java.net.URISyntaxException;
11 import java.net.URL;
12 import java.util.LinkedList;
13 import java.util.Map;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 import org.cacert.gigi.DevelLauncher;
18 import org.cacert.gigi.localisation.Language;
19 import org.cacert.gigi.util.HTMLEncoder;
20
21 public class Template implements Outputable {
22
23     class ParseResult {
24
25         TemplateBlock block;
26
27         String endType;
28
29         public ParseResult(TemplateBlock block, String endType) {
30             this.block = block;
31             this.endType = endType;
32         }
33
34         public String getEndType() {
35             return endType;
36         }
37
38         public TemplateBlock getBlock(String reqType) {
39             if (endType == null && reqType == null) {
40                 return block;
41             }
42             if (endType == null || reqType == null) {
43                 throw new Error("Invalid block type: " + endType);
44             }
45             if (endType.equals(reqType)) {
46                 return block;
47             }
48             throw new Error("Invalid block type: " + endType);
49         }
50     }
51
52     private TemplateBlock data;
53
54     private long lastLoaded;
55
56     private File source;
57
58     private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?");
59
60     private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
61
62     public Template(URL u) {
63         try {
64             Reader r = new InputStreamReader(u.openStream(), "UTF-8");
65             try {
66                 if (u.getProtocol().equals("file") && DevelLauncher.DEVEL) {
67                     source = new File(u.toURI());
68                     lastLoaded = source.lastModified() + 1000;
69                 }
70             } catch (URISyntaxException e) {
71                 e.printStackTrace();
72             }
73             data = parse(r).getBlock(null);
74             r.close();
75         } catch (IOException e) {
76             throw new Error(e);
77         }
78     }
79
80     public Template(Reader r) {
81         try {
82             data = parse(r).getBlock(null);
83             r.close();
84         } catch (IOException e) {
85             throw new Error(e);
86         }
87     }
88
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;
94         outer:
95         while (true) {
96             while ( !endsWith(buf, "<?")) {
97                 int ch = r.read();
98                 if (ch == -1) {
99                     break outer;
100                 }
101                 buf.append((char) ch);
102             }
103             buf.delete(buf.length() - 2, buf.length());
104             splitted.add(buf.toString());
105             buf.delete(0, buf.length());
106             while ( !endsWith(buf, "?>")) {
107                 int ch = r.read();
108                 if (ch == -1) {
109                     throw new EOFException();
110                 }
111                 buf.append((char) ch);
112             }
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);
117             if (m.matches()) {
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("}")));
124                     } else {
125                         commands.add(new IfStatement(variable, body.getBlock("}")));
126                     }
127                 } else if (type.equals("foreach")) {
128                     commands.add(new ForeachStatement(variable, body.getBlock("}")));
129                 } else {
130                     throw new IOException("Syntax error: unknown control structure: " + type);
131                 }
132                 continue;
133             } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
134                 blockType = "else";
135                 break;
136             } else if (com.matches(" ?\\} ?")) {
137                 blockType = "}";
138                 break;
139             } else {
140                 commands.add(parseCommand(com));
141             }
142         }
143         splitted.add(buf.toString());
144         return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Outputable[commands.size()])), blockType);
145     }
146
147     private boolean endsWith(StringBuffer buf, String string) {
148         return buf.length() >= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string);
149     }
150
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);
156             } else {
157                 return new SprintfCommand(raw);
158             }
159         } else if (s2.startsWith("=$")) {
160             final String raw = s2.substring(2);
161             return new OutputVariableCommand(raw);
162         } else {
163             throw new Error("Unknown processing instruction: " + s2);
164         }
165     }
166
167     @Override
168     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
169         if (source != null && DevelLauncher.DEVEL) {
170             if (lastLoaded < source.lastModified()) {
171                 try {
172                     System.out.println("Reloading template.... " + source);
173                     InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
174                     data = parse(r).getBlock(null);
175                     r.close();
176                     lastLoaded = source.lastModified() + 1000;
177                 } catch (IOException e) {
178                     e.printStackTrace();
179                 }
180             }
181         }
182         data.output(out, l, vars);
183     }
184
185     protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
186         Object s = vars.get(varname);
187
188         if (s == null) {
189             System.out.println("Empty variable: " + varname);
190         }
191         if (s instanceof Outputable) {
192             ((Outputable) s).output(out, l, vars);
193         } else {
194             out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));
195         }
196     }
197 }