]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/Template.java
5498ab0e04eade63e04e52d3a69c64d6e13a772c
[gigi.git] / src / org / cacert / gigi / output / Template.java
1 package org.cacert.gigi.output;
2
3 import java.io.PrintWriter;
4 import java.io.Reader;
5 import java.util.LinkedList;
6 import java.util.Map;
7 import java.util.Scanner;
8 import java.util.regex.Pattern;
9
10 import org.cacert.gigi.Language;
11
12 public class Template implements Outputable {
13         String[] contents;
14
15         public Template(Reader r) {
16                 LinkedList<String> splitted = new LinkedList<String>();
17                 Scanner sc = new Scanner(r);
18                 Pattern p1 = Pattern.compile("([^<]|<[^?])*<\\?");
19                 Pattern p2 = Pattern.compile("([^<]|<[^?])*\\?>");
20                 while (true) {
21                         String s1 = sc.findWithinHorizon(p1, 0);
22                         if (s1 == null) {
23                                 break;
24                         }
25                         s1 = s1.substring(0, s1.length() - 2);
26                         splitted.add(s1);
27                         String s2 = sc.findWithinHorizon(p2, 0);
28                         s2 = s2.substring(0, s2.length() - 2);
29                         splitted.add(s2);
30                 }
31                 sc.useDelimiter("\0");
32                 if (sc.hasNext()) {
33                         splitted.add(sc.next());
34                 }
35                 sc.close();
36                 contents = splitted.toArray(new String[splitted.size()]);
37         }
38         public void output(PrintWriter out, Language l, Map<String, Object> vars) {
39                 LinkedList<String> store = new LinkedList<String>();
40                 for (int i = 0; i < contents.length; i++) {
41                         if (i % 2 == 0) {
42                                 out.print(contents[i]);
43                         } else if (contents[i].startsWith("=_")) {
44                                 out.print(l.getTranslation(contents[i].substring(2)));
45                         } else if (contents[i].startsWith("=$")) {
46                                 outputVar(out, l, vars, contents[i].substring(2));
47                         } else if (contents[i].startsWith("=s,")) {
48                                 String command = contents[i].substring(3);
49                                 store.clear();
50                                 while (command.startsWith("$")) {
51                                         int idx = command.indexOf(",");
52                                         store.add(command.substring(0, idx));
53                                         command = command.substring(idx + 1);
54                                 }
55                                 String[] parts = l.getTranslation(command).split("%s");
56                                 String[] myvars = store.toArray(new String[store.size()]);
57                                 out.print(parts[0]);
58                                 for (int j = 1; j < parts.length; j++) {
59                                         outputVar(out, l, vars, myvars[j - 1].substring(1));
60                                         out.print(parts[j]);
61                                 }
62                         } else {
63                                 System.out.println("Unknown processing instruction: "
64                                                 + contents[i]);
65                         }
66                 }
67         }
68         private void outputVar(PrintWriter out, Language l,
69                         Map<String, Object> vars, String varname) {
70                 Object s = vars.get(varname);
71
72                 if (s == null) {
73                         System.out.println("Empty variable: " + varname);
74                 }
75                 if (s instanceof Outputable) {
76                         ((Outputable) s).output(out, l, vars);
77                 } else {
78                         out.print(s);
79                 }
80         }
81 }