From e5be49e076586ad54d3ea11863de6b42578f0d6e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Wed, 31 Dec 2014 02:21:05 +0100 Subject: [PATCH] Implement template if-else statements --- .../gigi/output/template/IfStatement.java | 17 ++++- .../cacert/gigi/output/template/Template.java | 62 +++++++++++++++---- .../cacert/gigi/template/TestTemplate.java | 22 +++++++ 3 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/org/cacert/gigi/output/template/IfStatement.java b/src/org/cacert/gigi/output/template/IfStatement.java index db0cbe11..fbd83bcf 100644 --- a/src/org/cacert/gigi/output/template/IfStatement.java +++ b/src/org/cacert/gigi/output/template/IfStatement.java @@ -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 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); } } } diff --git a/src/org/cacert/gigi/output/template/Template.java b/src/org/cacert/gigi/output/template/Template.java index 0bd9cce1..b345a059 100644 --- a/src/org/cacert/gigi/output/template/Template.java +++ b/src/org/cacert/gigi/output/template/Template.java @@ -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 splitted = new LinkedList(); LinkedList commands = new LinkedList(); StringBuffer buf = new StringBuffer(); + String blockType = null; outer: while (true) { while ( !endsWith(buf, "")); + 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, "truefalse")); + vars.put("b", Boolean.FALSE); + assertEquals("false", testExecute(Language.getInstance(Locale.ENGLISH), vars, "truefalse")); + + vars.put("b", new Object()); + assertEquals("true", testExecute(Language.getInstance(Locale.ENGLISH), vars, "truefalse")); + vars.put("b", null); + assertEquals("false", testExecute(Language.getInstance(Locale.ENGLISH), vars, "truefalse")); + } } -- 2.39.2