X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2Ftemplate%2FSprintfCommand.java;h=47389643995640afa854a63469abfc345b45bbf9;hb=b207e1989b166619e924946731e40ccf98457fae;hp=9cafef4787f58fd9f7451cdf1ceae85e455e09db;hpb=480cb29387c76ccc19f8fa8fb0abe8ae1b069730;p=gigi.git diff --git a/src/org/cacert/gigi/output/template/SprintfCommand.java b/src/org/cacert/gigi/output/template/SprintfCommand.java index 9cafef47..47389643 100644 --- a/src/org/cacert/gigi/output/template/SprintfCommand.java +++ b/src/org/cacert/gigi/output/template/SprintfCommand.java @@ -2,36 +2,75 @@ package org.cacert.gigi.output.template; import java.io.PrintWriter; 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.output.Outputable; import org.cacert.gigi.util.HTMLEncoder; public final class SprintfCommand implements Outputable { private final String text; - private final LinkedList store; + private final String[] store; - public SprintfCommand(String text, LinkedList store) { + public SprintfCommand(String text, List store) { this.text = text; - this.store = store; + 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 + ")|(!'[^{}'\\$]*)'"); + + public 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 final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}"); + @Override public void output(PrintWriter out, Language l, Map vars) { - String[] parts = l.getTranslation(text).split("%s"); - String[] myvars = store.toArray(new String[store.size()]); - 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); + 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("$")) { + 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))); } }