1 package org.cacert.gigi.output.template;
3 import java.io.PrintWriter;
4 import java.util.Collection;
7 import org.cacert.gigi.localisation.Language;
10 * One ore two {@link Outputable}s that are emitted conditionally if a given
11 * variable is neither <code>null</code> nor {@link Boolean#FALSE}.
13 public final class IfStatement implements Translatable {
15 private final String variable;
17 private final TemplateBlock iftrue;
19 private final TemplateBlock iffalse;
22 * Creates a new {@link IfStatement} with an empty else-part.
25 * the variable to check.
27 * the body to emit conditionally.
29 public IfStatement(String variable, TemplateBlock body) {
30 this.variable = variable;
36 * Creates a new {@link IfStatement} with an else-block.
39 * the variable to check.
41 * the block to emit if the check succeeds.
43 * the block to emit if the check fails.
45 public IfStatement(String variable, TemplateBlock iftrue, TemplateBlock iffalse) {
46 this.variable = variable;
48 this.iffalse = iffalse;
52 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
53 Object o = vars.get(variable);
55 if ( !(o == null || Boolean.FALSE.equals(o))) {
56 iftrue.output(out, l, vars);
57 } else if (iffalse != null) {
58 iffalse.output(out, l, vars);
63 public void addTranslations(Collection<String> s) {
64 iftrue.addTranslations(s);
65 if (iffalse != null) {
66 iffalse.addTranslations(s);