]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/output/template/Template.java
add: mail templates support
[gigi.git] / src / org / cacert / gigi / output / template / Template.java
index 15f3408d28ce41d10d55b4f9491fdaf223cb0d84..8ad47bc2876a271dea1cce221c4a732cc2907190 100644 (file)
@@ -27,7 +27,7 @@ import org.cacert.gigi.util.HTMLEncoder;
  */
 public class Template implements Outputable {
 
-    private static class ParseResult {
+    protected static class ParseResult {
 
         TemplateBlock block;
 
@@ -75,8 +75,7 @@ public class Template implements Outputable {
      *            the URL to load the template from. UTF-8 is chosen as charset.
      */
     public Template(URL u) {
-        try {
-            Reader r = new InputStreamReader(u.openStream(), "UTF-8");
+        try (Reader r = new InputStreamReader(u.openStream(), "UTF-8")) {
             try {
                 if (u.getProtocol().equals("file")) {
                     source = new File(u.toURI());
@@ -86,7 +85,6 @@ public class Template implements Outputable {
                 e.printStackTrace();
             }
             data = parse(r).getBlock(null);
-            r.close();
         } catch (IOException e) {
             throw new Error(e);
         }
@@ -108,7 +106,11 @@ public class Template implements Outputable {
         }
     }
 
-    private ParseResult parse(Reader r) throws IOException {
+    protected ParseResult parse(Reader r) throws IOException {
+        return parseContent(r);
+    }
+
+    protected ParseResult parseContent(Reader r) throws IOException {
         LinkedList<String> splitted = new LinkedList<String>();
         LinkedList<Translatable> commands = new LinkedList<Translatable>();
         StringBuffer buf = new StringBuffer();
@@ -139,10 +141,10 @@ public class Template implements Outputable {
             if (m.matches()) {
                 String type = m.group(1);
                 String variable = m.group(2);
-                ParseResult body = parse(r);
+                ParseResult body = parseContent(r);
                 if (type.equals("if")) {
                     if ("else".equals(body.getEndType())) {
-                        commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
+                        commands.add(new IfStatement(variable, body.getBlock("else"), parseContent(r).getBlock("}")));
                     } else {
                         commands.add(new IfStatement(variable, body.getBlock("}")));
                     }
@@ -188,6 +190,11 @@ public class Template implements Outputable {
 
     @Override
     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
+        tryReload();
+        data.output(out, l, vars);
+    }
+
+    protected void tryReload() {
         if (source != null && lastLoaded < source.lastModified()) {
             try {
                 System.out.println("Reloading template.... " + source);
@@ -199,10 +206,12 @@ public class Template implements Outputable {
                 e.printStackTrace();
             }
         }
-        data.output(out, l, vars);
     }
 
     protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
+        if (vars.containsKey(Outputable.OUT_KEY_PLAIN)) {
+            unescaped = true;
+        }
         Object s = vars.get(varname);
 
         if (s == null) {