]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/output/template/SprintfCommand.java
Merge "Update notes about password security"
[gigi.git] / src / org / cacert / gigi / output / template / SprintfCommand.java
index 47389643995640afa854a63469abfc345b45bbf9..51ea9cf209c16531a2637837c5410057aee9e011 100644 (file)
@@ -1,6 +1,9 @@
 package org.cacert.gigi.output.template;
 
 import java.io.PrintWriter;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -10,12 +13,24 @@ import java.util.regex.Pattern;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.util.HTMLEncoder;
 
-public final class SprintfCommand implements Outputable {
+/**
+ * A pattern that is to be translated before variables are inserted.
+ */
+public final class SprintfCommand implements Translatable {
 
     private final String text;
 
     private final String[] store;
 
+    /**
+     * Creates a new SprintfCommand based on its pre-parsed contents.
+     * 
+     * @param text
+     *            a string with <code>{0},{1},..</code> as placeholders.
+     * @param store
+     *            the data to put into the placeholders: ${var}, $!{var},
+     *            !'plain'.
+     */
     public SprintfCommand(String text, List<String> store) {
         this.text = text;
         this.store = store.toArray(new String[store.size()]);
@@ -25,7 +40,13 @@ public final class SprintfCommand implements Outputable {
 
     private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'");
 
-    public SprintfCommand(String content) {
+    /**
+     * Creates a new SprintfCommand that is parsed as from template source.
+     * 
+     * @param content
+     *            the part from the template that is to be parsed.
+     */
+    protected SprintfCommand(String content) {
         StringBuffer raw = new StringBuffer();
         List<String> var = new LinkedList<String>();
         int counter = 0;
@@ -49,7 +70,7 @@ public final class SprintfCommand implements Outputable {
         store = var.toArray(new String[var.size()]);
     }
 
-    private final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}");
+    private static final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}");
 
     @Override
     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
@@ -73,4 +94,30 @@ public final class SprintfCommand implements Outputable {
         }
         out.print(HTMLEncoder.encodeHTML(parts.substring(pos)));
     }
+
+    @Override
+    public void addTranslations(Collection<String> s) {
+        s.add(text);
+    }
+
+    /**
+     * Creates a simple {@link SprintfCommand} wrapped in a {@link Scope} to fit
+     * in now constant variables into this template.
+     * 
+     * @param msg
+     *            the message (to be translated) with <code>{0},{1},...</code>
+     *            as placeholders.
+     * @param vars
+     *            the variables to put into the placeholders.
+     * @return the constructed {@link Outputable}.
+     */
+    public static Outputable createSimple(String msg, Object... vars) {
+        HashMap<String, Object> scope = new HashMap<>();
+        String[] store = new String[vars.length];
+        for (int i = 0; i < vars.length; i++) {
+            scope.put("autoVar" + i, vars[i]);
+            store[i] = "${autoVar" + i + "}";
+        }
+        return new Scope(new SprintfCommand(msg, Arrays.asList(store)), scope);
+    }
 }