X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2Ftemplate%2FSprintfCommand.java;h=51ea9cf209c16531a2637837c5410057aee9e011;hp=e2ff0ba6ce6395c4bfd3e3e8c587bc3c572c13fa;hb=d23d7a6fa9dc38c6193fea70017e0bff11257be5;hpb=cd14b85c60f736a643842b421b11f41d8fca86c7
diff --git a/src/org/cacert/gigi/output/template/SprintfCommand.java b/src/org/cacert/gigi/output/template/SprintfCommand.java
index e2ff0ba6..51ea9cf2 100644
--- a/src/org/cacert/gigi/output/template/SprintfCommand.java
+++ b/src/org/cacert/gigi/output/template/SprintfCommand.java
@@ -1,40 +1,123 @@
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;
+import java.util.regex.Matcher;
+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 {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()]);
}
+ private static final String VARIABLE = "\\$!?\\{[a-zA-Z0-9_-]+\\}";
+
+ private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'");
+
+ /**
+ * 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;
+ Matcher m = processingInstruction.matcher(content);
+ int last = 0;
+ while (m.find()) {
+ raw.append(content.substring(last, m.start()));
+ String group = null;
+ if ((group = m.group(1)) != null) {
+ var.add(group);
+ } else if ((group = m.group(2)) != null) {
+ var.add(group);
+ } else {
+ throw new Error("Regex is broken??");
+ }
+ last = m.end();
+ raw.append("{" + (counter++) + "}");
+ }
+ raw.append(content.substring(last));
+ text = raw.toString();
+ store = var.toArray(new String[var.size()]);
+ }
+
+ private static final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}");
+
@Override
public void output(PrintWriter out, Language l, Map vars) {
- String[] parts = l.getTranslation(text).split("%s", -1);
- String[] myvars = store;
- out.print(HTMLEncoder.encodeHTML(parts[0]));
- for (int j = 1; j < parts.length; j++) {
- String var = myvars[j - 1];
+ String parts = l.getTranslation(text);
+ Matcher m = replacant.matcher(parts);
+ int pos = 0;
+ while (m.find()) {
+ out.print(HTMLEncoder.encodeHTML(parts.substring(pos, m.start())));
+ String var = store[Integer.parseInt(m.group(1))];
if (var.startsWith("$!")) {
- Template.outputVar(out, l, vars, myvars[j - 1].substring(2), true);
- } else if (var.startsWith("!\"")) {
+ Template.outputVar(out, l, vars, var.substring(3, var.length() - 1), true);
+ } else if (var.startsWith("!'")) {
out.print(var.substring(2));
- } else if (var.startsWith("\"")) {
- out.print(HTMLEncoder.encodeHTML(var.substring(1)));
+ } else if (var.startsWith("$")) {
+ Template.outputVar(out, l, vars, var.substring(2, var.length() - 1), false);
} else {
- Template.outputVar(out, l, vars, myvars[j - 1].substring(1), false);
+ throw new Error("Processing error in template.");
}
- out.print(HTMLEncoder.encodeHTML(parts[j]));
+ pos = m.end();
+
+ }
+ out.print(HTMLEncoder.encodeHTML(parts.substring(pos)));
+ }
+
+ @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);
}
}