X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2FTemplate.java;h=b6fb111e20e5419d31651a78a81df2429e59399f;hb=005ed4d2e8fa57b7ede403a310771c9fdf4a9578;hp=5498ab0e04eade63e04e52d3a69c64d6e13a772c;hpb=e6ea6f8f9af7b4b24c58626f8749cfda3dcb2240;p=gigi.git diff --git a/src/org/cacert/gigi/output/Template.java b/src/org/cacert/gigi/output/Template.java index 5498ab0e..b6fb111e 100644 --- a/src/org/cacert/gigi/output/Template.java +++ b/src/org/cacert/gigi/output/Template.java @@ -1,67 +1,114 @@ package org.cacert.gigi.output; +import java.io.EOFException; +import java.io.IOException; import java.io.PrintWriter; import java.io.Reader; import java.util.LinkedList; import java.util.Map; -import java.util.Scanner; -import java.util.regex.Pattern; import org.cacert.gigi.Language; public class Template implements Outputable { String[] contents; + Outputable[] vars; public Template(Reader r) { - LinkedList splitted = new LinkedList(); - Scanner sc = new Scanner(r); - Pattern p1 = Pattern.compile("([^<]|<[^?])*<\\?"); - Pattern p2 = Pattern.compile("([^<]|<[^?])*\\?>"); - while (true) { - String s1 = sc.findWithinHorizon(p1, 0); - if (s1 == null) { - break; + try { + LinkedList splitted = new LinkedList(); + LinkedList commands = new LinkedList(); + StringBuffer buf = new StringBuffer(); + int ch = r.read(); + outer : while (true) { + while (!endsWith(buf, "")) { + buf.append((char) ch); + ch = r.read(); + if (ch == -1) { + throw new EOFException(); + } + } + buf.delete(buf.length() - 2, buf.length()); + commands.add(parseCommand(buf.toString())); + buf.delete(0, buf.length()); } - s1 = s1.substring(0, s1.length() - 2); - splitted.add(s1); - String s2 = sc.findWithinHorizon(p2, 0); - s2 = s2.substring(0, s2.length() - 2); - splitted.add(s2); + splitted.add(buf.toString()); + contents = splitted.toArray(new String[splitted.size()]); + vars = commands.toArray(new Outputable[commands.size()]); + r.close(); + } catch (IOException e) { + throw new Error(e); } - sc.useDelimiter("\0"); - if (sc.hasNext()) { - splitted.add(sc.next()); + } + private boolean endsWith(StringBuffer buf, String string) { + return buf.length() >= string.length() + && buf.substring(buf.length() - string.length(), buf.length()) + .equals(string); + } + private Outputable parseCommand(String s2) { + s2 = s2.replace("\n", ""); + if (s2.startsWith("=_")) { + final String raw = s2.substring(2); + return new Outputable() { + + @Override + public void output(PrintWriter out, Language l, + Map vars) { + out.print(l.getTranslation(raw)); + } + }; + } else if (s2.startsWith("=$")) { + final String raw = s2.substring(2); + return new Outputable() { + + @Override + public void output(PrintWriter out, Language l, + Map vars) { + outputVar(out, l, vars, raw); + } + }; + } else if (s2.startsWith("=s,")) { + String command = s2.substring(3); + final LinkedList store = new LinkedList(); + while (command.startsWith("$")) { + int idx = command.indexOf(","); + store.add(command.substring(0, idx)); + command = command.substring(idx + 1); + } + final String text = command; + return new Outputable() { + + @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(parts[0]); + for (int j = 1; j < parts.length; j++) { + outputVar(out, l, vars, myvars[j - 1].substring(1)); + out.print(parts[j]); + } + } + }; + } else { + System.out.println("Unknown processing instruction: " + s2); } - sc.close(); - contents = splitted.toArray(new String[splitted.size()]); + return null; } public void output(PrintWriter out, Language l, Map vars) { - LinkedList store = new LinkedList(); for (int i = 0; i < contents.length; i++) { - if (i % 2 == 0) { - out.print(contents[i]); - } else if (contents[i].startsWith("=_")) { - out.print(l.getTranslation(contents[i].substring(2))); - } else if (contents[i].startsWith("=$")) { - outputVar(out, l, vars, contents[i].substring(2)); - } else if (contents[i].startsWith("=s,")) { - String command = contents[i].substring(3); - store.clear(); - while (command.startsWith("$")) { - int idx = command.indexOf(","); - store.add(command.substring(0, idx)); - command = command.substring(idx + 1); - } - String[] parts = l.getTranslation(command).split("%s"); - String[] myvars = store.toArray(new String[store.size()]); - out.print(parts[0]); - for (int j = 1; j < parts.length; j++) { - outputVar(out, l, vars, myvars[j - 1].substring(1)); - out.print(parts[j]); - } - } else { - System.out.println("Unknown processing instruction: " - + contents[i]); + out.print(contents[i]); + if (i < this.vars.length) { + this.vars[i].output(out, l, vars); } } }