]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/SprintfCommand.java
fix: use sprintf-translation correctly
[gigi.git] / src / org / cacert / gigi / output / template / SprintfCommand.java
1 package org.cacert.gigi.output.template;
2
3 import java.io.PrintWriter;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.LinkedList;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
12
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.util.HTMLEncoder;
15
16 public final class SprintfCommand implements Translatable {
17
18     private final String text;
19
20     private final String[] store;
21
22     public SprintfCommand(String text, List<String> store) {
23         this.text = text;
24         this.store = store.toArray(new String[store.size()]);
25     }
26
27     private static final String VARIABLE = "\\$!?\\{[a-zA-Z0-9_-]+\\}";
28
29     private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'");
30
31     public SprintfCommand(String content) {
32         StringBuffer raw = new StringBuffer();
33         List<String> var = new LinkedList<String>();
34         int counter = 0;
35         Matcher m = processingInstruction.matcher(content);
36         int last = 0;
37         while (m.find()) {
38             raw.append(content.substring(last, m.start()));
39             String group = null;
40             if ((group = m.group(1)) != null) {
41                 var.add(group);
42             } else if ((group = m.group(2)) != null) {
43                 var.add(group);
44             } else {
45                 throw new Error("Regex is broken??");
46             }
47             last = m.end();
48             raw.append("{" + (counter++) + "}");
49         }
50         raw.append(content.substring(last));
51         text = raw.toString();
52         store = var.toArray(new String[var.size()]);
53     }
54
55     private static final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}");
56
57     @Override
58     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
59         String parts = l.getTranslation(text);
60         Matcher m = replacant.matcher(parts);
61         int pos = 0;
62         while (m.find()) {
63             out.print(HTMLEncoder.encodeHTML(parts.substring(pos, m.start())));
64             String var = store[Integer.parseInt(m.group(1))];
65             if (var.startsWith("$!")) {
66                 Template.outputVar(out, l, vars, var.substring(3, var.length() - 1), true);
67             } else if (var.startsWith("!'")) {
68                 out.print(var.substring(2));
69             } else if (var.startsWith("$")) {
70                 Template.outputVar(out, l, vars, var.substring(2, var.length() - 1), false);
71             } else {
72                 throw new Error("Processing error in template.");
73             }
74             pos = m.end();
75
76         }
77         out.print(HTMLEncoder.encodeHTML(parts.substring(pos)));
78     }
79
80     @Override
81     public void addTranslations(Collection<String> s) {
82         s.add(text);
83     }
84
85     public static Outputable createSimple(String msg, String... vars) {
86         HashMap<String, Object> scope = new HashMap<>();
87         String[] store = new String[vars.length];
88         for (int i = 0; i < vars.length; i++) {
89             scope.put("autoVar" + i, vars[i]);
90             store[i] = "${autoVar" + i + "}";
91         }
92         return new Scope(new SprintfCommand(msg, Arrays.asList(store)), scope);
93     }
94 }