1 package org.cacert.gigi.output.template;
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;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.util.HTMLEncoder;
17 * A pattern that is to be translated before variables are inserted.
19 public final class SprintfCommand implements Translatable {
21 private final String text;
23 private final String[] store;
26 * Creates a new SprintfCommand based on its pre-parsed contents.
29 * a string with <code>{0},{1},..</code> as placeholders.
31 * the data to put into the placeholders: ${var}, $!{var},
34 public SprintfCommand(String text, List<String> store) {
36 this.store = store.toArray(new String[store.size()]);
39 private static final String VARIABLE = "\\$!?\\{[a-zA-Z0-9_-]+\\}";
41 private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'");
44 * Creates a new SprintfCommand that is parsed as from template source.
47 * the part from the template that is to be parsed.
49 protected SprintfCommand(String content) {
50 StringBuffer raw = new StringBuffer();
51 List<String> var = new LinkedList<String>();
53 Matcher m = processingInstruction.matcher(content);
56 raw.append(content.substring(last, m.start()));
58 if ((group = m.group(1)) != null) {
60 } else if ((group = m.group(2)) != null) {
63 throw new Error("Regex is broken??");
66 raw.append("{" + (counter++) + "}");
68 raw.append(content.substring(last));
69 text = raw.toString();
70 store = var.toArray(new String[var.size()]);
73 private static final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}");
76 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
77 String parts = l.getTranslation(text);
78 Matcher m = replacant.matcher(parts);
81 out.print(escape(vars, parts.substring(pos, m.start())));
82 String var = store[Integer.parseInt(m.group(1))];
83 if (var.startsWith("$!")) {
84 Template.outputVar(out, l, vars, var.substring(3, var.length() - 1), true);
85 } else if (var.startsWith("!'")) {
86 out.print(var.substring(2));
87 } else if (var.startsWith("$")) {
88 Template.outputVar(out, l, vars, var.substring(2, var.length() - 1), false);
90 throw new Error("Processing error in template.");
95 out.print(escape(vars, parts.substring(pos)));
98 private String escape(Map<String, Object> vars, String target) {
99 if (vars.containsKey(OUT_KEY_PLAIN)) {
102 return HTMLEncoder.encodeHTML(target);
106 public void addTranslations(Collection<String> s) {
111 * Creates a simple {@link SprintfCommand} wrapped in a {@link Scope} to fit
112 * in now constant variables into this template.
115 * the message (to be translated) with <code>{0},{1},...</code>
118 * the variables to put into the placeholders.
119 * @return the constructed {@link Outputable}.
121 public static Outputable createSimple(String msg, Object... vars) {
122 HashMap<String, Object> scope = new HashMap<>();
123 String[] store = new String[vars.length];
124 for (int i = 0; i < vars.length; i++) {
125 scope.put("autoVar" + i, vars[i]);
126 store[i] = "${autoVar" + i + "}";
128 return new Scope(new SprintfCommand(msg, Arrays.asList(store)), scope);