]> WPIA git - gigi.git/commitdiff
Implement template if-else statements
authorFelix Dörre <felix@dogcraft.de>
Wed, 31 Dec 2014 01:21:05 +0000 (02:21 +0100)
committerJanis Streib <janis@dogcraft.de>
Wed, 31 Dec 2014 01:36:14 +0000 (02:36 +0100)
src/org/cacert/gigi/output/template/IfStatement.java
src/org/cacert/gigi/output/template/Template.java
tests/org/cacert/gigi/template/TestTemplate.java

index db0cbe11f07f20132ebdad1503ed82695686f849..fbd83bcfc799c70059b6f226f225dd84f5bab6e2 100644 (file)
@@ -9,18 +9,29 @@ public final class IfStatement implements Outputable {
 
     private final String variable;
 
-    private final TemplateBlock body;
+    private final TemplateBlock iftrue;
+
+    private final TemplateBlock iffalse;
 
     public IfStatement(String variable, TemplateBlock body) {
         this.variable = variable;
-        this.body = body;
+        this.iftrue = body;
+        iffalse = null;
+    }
+
+    public IfStatement(String variable, TemplateBlock iftrue, TemplateBlock iffalse) {
+        this.variable = variable;
+        this.iftrue = iftrue;
+        this.iffalse = iffalse;
     }
 
     @Override
     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
         Object o = vars.get(variable);
         if ( !(o == Boolean.FALSE || o == null)) {
-            body.output(out, l, vars);
+            iftrue.output(out, l, vars);
+        } else if (iffalse != null) {
+            iffalse.output(out, l, vars);
         }
     }
 }
index 0bd9cce170096b4d4088ed158edca8c4f9dfb459..b345a059f36873a53b8d2eb5eb3e87763aac71cf 100644 (file)
@@ -20,6 +20,33 @@ import org.cacert.gigi.util.HTMLEncoder;
 
 public class Template implements Outputable {
 
+    class ParseResult {
+
+        TemplateBlock block;
+
+        String endType;
+
+        public ParseResult(TemplateBlock block, String endType) {
+            this.block = block;
+            this.endType = endType;
+        }
+
+        public String getEndType() {
+            return endType;
+        }
+
+        public TemplateBlock getBlock(String reqType) {
+            if (endType == null && reqType == null)
+                return block;
+            if (endType == null || reqType == null) {
+                throw new Error("Invalid block type: " + endType);
+            }
+            if (endType.equals(reqType))
+                return block;
+            throw new Error("Invalid block type: " + endType);
+        }
+    }
+
     private TemplateBlock data;
 
     private long lastLoaded;
@@ -28,6 +55,8 @@ public class Template implements Outputable {
 
     private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?");
 
+    private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?");
+
     public Template(URL u) {
         try {
             Reader r = new InputStreamReader(u.openStream(), "UTF-8");
@@ -39,7 +68,7 @@ public class Template implements Outputable {
             } catch (URISyntaxException e) {
                 e.printStackTrace();
             }
-            data = parse(r);
+            data = parse(r).getBlock(null);
             r.close();
         } catch (IOException e) {
             throw new Error(e);
@@ -48,17 +77,18 @@ public class Template implements Outputable {
 
     public Template(Reader r) {
         try {
-            data = parse(r);
+            data = parse(r).getBlock(null);
             r.close();
         } catch (IOException e) {
             throw new Error(e);
         }
     }
 
-    private TemplateBlock parse(Reader r) throws IOException {
+    private ParseResult parse(Reader r) throws IOException {
         LinkedList<String> splitted = new LinkedList<String>();
         LinkedList<Outputable> commands = new LinkedList<Outputable>();
         StringBuffer buf = new StringBuffer();
+        String blockType = null;
         outer:
         while (true) {
             while ( !endsWith(buf, "<?")) {
@@ -85,25 +115,31 @@ public class Template implements Outputable {
             if (m.matches()) {
                 String type = m.group(1);
                 String variable = m.group(2);
-                TemplateBlock body = parse(r);
+                ParseResult body = parse(r);
                 if (type.equals("if")) {
-                    commands.add(new IfStatement(variable, body));
+                    if ("else".equals(body.getEndType())) {
+                        commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
+                    } else {
+                        commands.add(new IfStatement(variable, body.getBlock("}")));
+                    }
                 } else if (type.equals("foreach")) {
-                    commands.add(new ForeachStatement(variable, body));
+                    commands.add(new ForeachStatement(variable, body.getBlock("}")));
                 } else {
                     throw new IOException("Syntax error: unknown control structure: " + type);
                 }
                 continue;
-            }
-            if (com.matches(" ?\\} ?")) {
+            } else if ((m = ELSE_PATTERN.matcher(com)).matches()) {
+                blockType = "else";
+                break;
+            } else if (com.matches(" ?\\} ?")) {
+                blockType = "}";
                 break;
+            } else {
+                commands.add(parseCommand(com));
             }
-            commands.add(parseCommand(com));
         }
         splitted.add(buf.toString());
-        String[] contents = splitted.toArray(new String[splitted.size()]);
-        Outputable[] vars = commands.toArray(new Outputable[commands.size()]);
-        return new TemplateBlock(contents, vars);
+        return new ParseResult(new TemplateBlock(splitted.toArray(new String[splitted.size()]), commands.toArray(new Outputable[commands.size()])), blockType);
     }
 
     private boolean endsWith(StringBuffer buf, String string) {
@@ -146,7 +182,7 @@ public class Template implements Outputable {
                 try {
                     System.out.println("Reloading template.... " + source);
                     InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8");
-                    data = parse(r);
+                    data = parse(r).getBlock(null);
                     r.close();
                     lastLoaded = source.lastModified() + 1000;
                 } catch (IOException e) {
index fff3ecba06a61cbd47a47ff92376dd410dbf822a..1e5a9fb6823215d4a5b0e762b7cbbba2b21ad3c6 100644 (file)
@@ -115,4 +115,26 @@ public class TestTemplate {
 
     }
 
+    @Test
+    public void testInvalidBracketContent() {
+        try {
+            assertEquals("", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<? } ?>"));
+            fail("should throw an error");
+        } catch (Error e) {
+
+        }
+    }
+
+    @Test
+    public void testIfElse() {
+        vars.put("b", Boolean.TRUE);
+        assertEquals("true", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<? if($b){ ?>true<? } else{?>false<?}?>"));
+        vars.put("b", Boolean.FALSE);
+        assertEquals("false", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<? if($b){ ?>true<? } else{?>false<?}?>"));
+
+        vars.put("b", new Object());
+        assertEquals("true", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<? if($b){ ?>true<? } else{?>false<?}?>"));
+        vars.put("b", null);
+        assertEquals("false", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<? if($b){ ?>true<? } else{?>false<?}?>"));
+    }
 }