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