]> WPIA git - gigi.git/blob - src/club/wpia/gigi/output/template/SprintfCommand.java
6b40c8fd0ab49bd88b6f4c6f4dba4aca934b4dfc
[gigi.git] / src / club / wpia / gigi / output / template / SprintfCommand.java
1 package club.wpia.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 club.wpia.gigi.localisation.Language;
14 import club.wpia.gigi.util.HTMLEncoder;
15
16 /**
17  * A pattern that is to be translated before variables are inserted.
18  */
19 public final class SprintfCommand implements Translatable {
20
21     private final String text;
22
23     private final String[] store;
24
25     /**
26      * Creates a new SprintfCommand based on its pre-parsed contents.
27      * 
28      * @param text
29      *            a string with <code>{0},{1},..</code> as placeholders.
30      * @param store
31      *            the data to put into the placeholders: ${var}, $!{var},
32      *            !'plain'.
33      */
34     public SprintfCommand(String text, List<String> store) {
35         this.text = text;
36         this.store = store.toArray(new String[store.size()]);
37     }
38
39     private static final String VARIABLE = "\\$!?\\{[a-zA-Z0-9_-]+\\}";
40
41     private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'");
42
43     /**
44      * Creates a new SprintfCommand that is parsed as from template source.
45      * 
46      * @param content
47      *            the part from the template that is to be parsed.
48      */
49     protected SprintfCommand(String content) {
50         StringBuffer raw = new StringBuffer();
51         List<String> var = new LinkedList<String>();
52         int counter = 0;
53         Matcher m = processingInstruction.matcher(content);
54         int last = 0;
55         while (m.find()) {
56             raw.append(content.substring(last, m.start()));
57             String group = null;
58             if ((group = m.group(1)) != null) {
59                 var.add(group);
60             } else if ((group = m.group(2)) != null) {
61                 var.add(group);
62             } else {
63                 throw new Error("Regex is broken??");
64             }
65             last = m.end();
66             raw.append("{" + (counter++) + "}");
67         }
68         raw.append(content.substring(last));
69         text = raw.toString();
70         store = var.toArray(new String[var.size()]);
71     }
72
73     private static final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}");
74
75     @Override
76     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
77         String parts = l.getTranslation(text);
78         Matcher m = replacant.matcher(parts);
79         int pos = 0;
80         while (m.find()) {
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);
89             } else {
90                 throw new Error("Processing error in template.");
91             }
92             pos = m.end();
93
94         }
95         out.print(escape(vars, parts.substring(pos)));
96     }
97
98     private String escape(Map<String, Object> vars, String target) {
99         if (vars.containsKey(OUT_KEY_PLAIN)) {
100             return target;
101         }
102         return HTMLEncoder.encodeHTML(target);
103     }
104
105     @Override
106     public void addTranslations(Collection<String> s) {
107         s.add(text);
108     }
109
110     /**
111      * Creates a simple {@link SprintfCommand} wrapped in a {@link Scope} to fit
112      * in now constant variables into this template.
113      * 
114      * @param msg
115      *            the message (to be translated) with <code>{0},{1},...</code>
116      *            as placeholders.
117      * @param vars
118      *            the variables to put into the placeholders.
119      * @return the constructed {@link Outputable}.
120      */
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 + "}";
127         }
128         return new Scope(new SprintfCommand(msg, Arrays.asList(store)), scope);
129     }
130 }