X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2Ftemplate%2FSprintfCommand.java;h=64ede7823776692b11f7e2ba6100e1e07d9d6a3d;hb=98dc0c64072a6f7f7916471f378cabf2d6c4fb87;hp=07a0b8cc303175ce0ac0e855d3be58ddaba24a1a;hpb=ec155eec51606c19970c9099ef23f700fb6aa53a;p=gigi.git
diff --git a/src/org/cacert/gigi/output/template/SprintfCommand.java b/src/org/cacert/gigi/output/template/SprintfCommand.java
index 07a0b8cc..64ede782 100644
--- a/src/org/cacert/gigi/output/template/SprintfCommand.java
+++ b/src/org/cacert/gigi/output/template/SprintfCommand.java
@@ -1,7 +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;
@@ -11,12 +13,24 @@ import java.util.regex.Pattern;
import org.cacert.gigi.localisation.Language;
import org.cacert.gigi.util.HTMLEncoder;
+/**
+ * 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 {0},{1},..
as placeholders.
+ * @param store
+ * the data to put into the placeholders: ${var}, $!{var},
+ * !'plain'.
+ */
public SprintfCommand(String text, List store) {
this.text = text;
this.store = store.toArray(new String[store.size()]);
@@ -26,7 +40,13 @@ public final class SprintfCommand implements Translatable {
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 var = new LinkedList();
int counter = 0;
@@ -50,7 +70,7 @@ public final class SprintfCommand implements Translatable {
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 vars) {
@@ -58,7 +78,7 @@ public final class SprintfCommand implements Translatable {
Matcher m = replacant.matcher(parts);
int pos = 0;
while (m.find()) {
- out.print(HTMLEncoder.encodeHTML(parts.substring(pos, m.start())));
+ out.print(escape(vars, parts.substring(pos, m.start())));
String var = store[Integer.parseInt(m.group(1))];
if (var.startsWith("$!")) {
Template.outputVar(out, l, vars, var.substring(3, var.length() - 1), true);
@@ -72,11 +92,39 @@ public final class SprintfCommand implements Translatable {
pos = m.end();
}
- out.print(HTMLEncoder.encodeHTML(parts.substring(pos)));
+ out.print(escape(vars, parts.substring(pos)));
+ }
+
+ private String escape(Map vars, String target) {
+ if (vars.containsKey(OUT_KEY_PLAIN)) {
+ return target;
+ }
+ return HTMLEncoder.encodeHTML(target);
}
@Override
public void addTranslations(Collection 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 {0},{1},...
+ * 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 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);
+ }
}