]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/template/SprintfCommand.java
Merge "Suggestions to enhance the SQL call pattern."
[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 /**
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(HTMLEncoder.encodeHTML(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(HTMLEncoder.encodeHTML(parts.substring(pos)));
96     }
97
98     @Override
99     public void addTranslations(Collection<String> s) {
100         s.add(text);
101     }
102
103     /**
104      * Creates a simple {@link SprintfCommand} wrapped in a {@link Scope} to fit
105      * in now constant variables into this template.
106      * 
107      * @param msg
108      *            the message (to be translated) with <code>{0},{1},...</code>
109      *            as placeholders.
110      * @param vars
111      *            the variables to put into the placeholders.
112      * @return the constructed {@link Outputable}.
113      */
114     public static Outputable createSimple(String msg, Object... vars) {
115         HashMap<String, Object> scope = new HashMap<>();
116         String[] store = new String[vars.length];
117         for (int i = 0; i < vars.length; i++) {
118             scope.put("autoVar" + i, vars[i]);
119             store[i] = "${autoVar" + i + "}";
120         }
121         return new Scope(new SprintfCommand(msg, Arrays.asList(store)), scope);
122     }
123 }