X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2Ftemplate%2FTemplate.java;h=15f3408d28ce41d10d55b4f9491fdaf223cb0d84;hp=dca6568b7432847f4db4cca56f9840e9bc9cab41;hb=2e2a2b842beac5c70b53ae379e97c8a1e688e12f;hpb=7e1a029d9df4c731a2087e58f774e6d8737aace3 diff --git a/src/org/cacert/gigi/output/template/Template.java b/src/org/cacert/gigi/output/template/Template.java index dca6568b..15f3408d 100644 --- a/src/org/cacert/gigi/output/template/Template.java +++ b/src/org/cacert/gigi/output/template/Template.java @@ -9,57 +9,110 @@ import java.io.PrintWriter; import java.io.Reader; import java.net.URISyntaxException; import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.Date; import java.util.LinkedList; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.cacert.gigi.DevelLauncher; import org.cacert.gigi.localisation.Language; -import org.cacert.gigi.output.Outputable; +import org.cacert.gigi.output.DateSelector; +import org.cacert.gigi.util.DayDate; import org.cacert.gigi.util.HTMLEncoder; +/** + * Represents a loaded template file. + */ public class Template implements Outputable { + private static class ParseResult { + + TemplateBlock block; + + String endType; + + public ParseResult(TemplateBlock block, String endType) { + this.block = block; + this.endType = endType; + } + + public String getEndType() { + return endType; + } + + public TemplateBlock getBlock(String reqType) { + if (endType == null && reqType == null) { + return block; + } + if (endType == null || reqType == null) { + throw new Error("Invalid block type: " + endType); + } + if (endType.equals(reqType)) { + return block; + } + throw new Error("Invalid block type: " + endType); + } + } + private TemplateBlock data; private long lastLoaded; private File source; - private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-z]+)\\(\\$([^)]+)\\) ?\\{ ?"); + private static final Pattern CONTROL_PATTERN = Pattern.compile(" ?([a-zA-Z]+)\\(\\$([^)]+)\\) ?\\{ ?"); + private static final Pattern ELSE_PATTERN = Pattern.compile(" ?\\} ?else ?\\{ ?"); + + /** + * Creates a new template by parsing the contents from the given URL. This + * constructor will fail on syntax error. When the URL points to a file, + * {@link File#lastModified()} is monitored for changes of the template. + * + * @param u + * the URL to load the template from. UTF-8 is chosen as charset. + */ public Template(URL u) { try { Reader r = new InputStreamReader(u.openStream(), "UTF-8"); try { - if (u.getProtocol().equals("file") && DevelLauncher.DEVEL) { + if (u.getProtocol().equals("file")) { source = new File(u.toURI()); lastLoaded = source.lastModified() + 1000; } } catch (URISyntaxException e) { e.printStackTrace(); } - data = parse(r); + data = parse(r).getBlock(null); r.close(); } catch (IOException e) { throw new Error(e); } } + /** + * Creates a new template by parsing the contents from the given reader. + * This constructor will fail on syntax error. + * + * @param r + * the Reader containing the data. + */ public Template(Reader r) { try { - data = parse(r); + data = parse(r).getBlock(null); r.close(); } catch (IOException e) { throw new Error(e); } } - private TemplateBlock parse(Reader r) throws IOException { + private ParseResult parse(Reader r) throws IOException { LinkedList splitted = new LinkedList(); - LinkedList commands = new LinkedList(); + LinkedList commands = new LinkedList(); StringBuffer buf = new StringBuffer(); + String blockType = null; outer: while (true) { while ( !endsWith(buf, "= string.length() && buf.substring(buf.length() - string.length(), buf.length()).equals(string); } - private Outputable parseCommand(String s2) { + private Translatable parseCommand(String s2) { if (s2.startsWith("=_")) { final String raw = s2.substring(2); - return new TranslateCommand(raw); + if ( !s2.contains("$") && !s2.contains("!'")) { + return new TranslateCommand(raw); + } else { + return new SprintfCommand(raw); + } } else if (s2.startsWith("=$")) { final String raw = s2.substring(2); return new OutputVariableCommand(raw); - } else if (s2.startsWith("=s,")) { - String command = s2.substring(3); - final LinkedList store = new LinkedList(); - while (command.startsWith("$") || command.startsWith("\"") || command.startsWith("!\"")) { - int idx; - if (command.startsWith("\"") || command.startsWith("!\"")) { - idx = command.indexOf("\"", command.charAt(0) == '!' ? 2 : 1) + 1; - store.add(command.substring(0, idx - 1)); - } else { - idx = command.indexOf(","); - store.add(command.substring(0, idx)); - } - command = command.substring(idx + 1); - } - final String text = command; - return new SprintfCommand(text, store); } else { - System.out.println("Unknown processing instruction: " + s2); + throw new Error("Unknown processing instruction: " + s2); } - return null; } @Override public void output(PrintWriter out, Language l, Map vars) { - if (source != null && DevelLauncher.DEVEL) { - if (lastLoaded < source.lastModified()) { - try { - System.out.println("Reloading template.... " + source); - InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8"); - data = parse(r); - r.close(); - lastLoaded = source.lastModified() + 1000; - } catch (IOException e) { - e.printStackTrace(); - } + if (source != null && lastLoaded < source.lastModified()) { + try { + System.out.println("Reloading template.... " + source); + InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8"); + data = parse(r).getBlock(null); + r.close(); + lastLoaded = source.lastModified() + 1000; + } catch (IOException e) { + e.printStackTrace(); } } data.output(out, l, vars); @@ -166,8 +210,20 @@ public class Template implements Outputable { } if (s instanceof Outputable) { ((Outputable) s).output(out, l, vars); + } else if (s instanceof DayDate) { + out.print(DateSelector.getDateFormat().format(((DayDate) s).toDate())); + } else if (s instanceof Date) { + SimpleDateFormat sdfUI = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + out.print(""); } else { out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString()))); } } + + public void addTranslations(Collection s) { + data.addTranslations(s); + } }