]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/ForeachStatement.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / src / org / cacert / gigi / output / template / ForeachStatement.java
1 package org.cacert.gigi.output.template;
2
3 import java.io.PrintWriter;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.cacert.gigi.localisation.Language;
9
10 /**
11  * Outputs an {@link Outputable} multiple times based on a given
12  * {@link IterableDataset}.
13  */
14 public final class ForeachStatement implements Translatable {
15
16     private final String variable;
17
18     private final TemplateBlock body;
19
20     /**
21      * Creates a new {@link ForeachStatement}.
22      * 
23      * @param variable
24      *            the variable to take the {@link IterableDataset} from.
25      * @param body
26      *            the body to output multiple times.
27      */
28     public ForeachStatement(String variable, TemplateBlock body) {
29         this.variable = variable;
30         this.body = body;
31     }
32
33     @Override
34     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
35         Object o = vars.get(variable);
36         if (o instanceof IterableDataset) {
37             IterableDataset id = (IterableDataset) o;
38             Map<String, Object> subcontext = new HashMap<String, Object>(vars);
39             while (id.next(l, subcontext)) {
40                 body.output(out, l, subcontext);
41             }
42         }
43     }
44
45     @Override
46     public void addTranslations(Collection<String> s) {
47         body.addTranslations(s);
48     }
49 }