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