]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/SprintfCommand.java
Merge remote-tracking branch 'origin/libs/scrypt/local'
[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.LinkedList;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import org.cacert.gigi.localisation.Language;
11 import org.cacert.gigi.util.HTMLEncoder;
12
13 public final class SprintfCommand implements Outputable {
14
15     private final String text;
16
17     private final String[] store;
18
19     public SprintfCommand(String text, List<String> store) {
20         this.text = text;
21         this.store = store.toArray(new String[store.size()]);
22     }
23
24     private static final String VARIABLE = "\\$!?\\{[a-zA-Z0-9_-]+\\}";
25
26     private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'");
27
28     public SprintfCommand(String content) {
29         StringBuffer raw = new StringBuffer();
30         List<String> var = new LinkedList<String>();
31         int counter = 0;
32         Matcher m = processingInstruction.matcher(content);
33         int last = 0;
34         while (m.find()) {
35             raw.append(content.substring(last, m.start()));
36             String group = null;
37             if ((group = m.group(1)) != null) {
38                 var.add(group);
39             } else if ((group = m.group(2)) != null) {
40                 var.add(group);
41             } else {
42                 throw new Error("Regex is broken??");
43             }
44             last = m.end();
45             raw.append("{" + (counter++) + "}");
46         }
47         raw.append(content.substring(last));
48         text = raw.toString();
49         store = var.toArray(new String[var.size()]);
50     }
51
52     private final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}");
53
54     @Override
55     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
56         String parts = l.getTranslation(text);
57         Matcher m = replacant.matcher(parts);
58         int pos = 0;
59         while (m.find()) {
60             out.print(HTMLEncoder.encodeHTML(parts.substring(pos, m.start())));
61             String var = store[Integer.parseInt(m.group(1))];
62             if (var.startsWith("$!")) {
63                 Template.outputVar(out, l, vars, var.substring(3, var.length() - 1), true);
64             } else if (var.startsWith("!'")) {
65                 out.print(var.substring(2));
66             } else if (var.startsWith("$")) {
67                 Template.outputVar(out, l, vars, var.substring(2, var.length() - 1), false);
68             } else {
69                 throw new Error("Processing error in template.");
70             }
71             pos = m.end();
72
73         }
74         out.print(HTMLEncoder.encodeHTML(parts.substring(pos)));
75     }
76 }