X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2Ftemplate%2FTemplate.java;h=fc3bf7854dda9142d05f0e30cc75c5be82935ee3;hp=ac59f3135ef75a11639710a81983be75ab990c53;hb=9c05ed99bc13cd875e16f1f8c72376ed29f57498;hpb=fb38a9c8b9d86289213a36bd3d2afddc58ec7d3f diff --git a/src/org/cacert/gigi/output/template/Template.java b/src/org/cacert/gigi/output/template/Template.java index ac59f313..fc3bf785 100644 --- a/src/org/cacert/gigi/output/template/Template.java +++ b/src/org/cacert/gigi/output/template/Template.java @@ -9,38 +9,69 @@ import java.io.PrintWriter; import java.io.Reader; import java.net.URISyntaxException; import java.net.URL; +import java.text.SimpleDateFormat; +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.util.HTMLEncoder; 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 ?\\{ ?"); 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); @@ -49,17 +80,18 @@ public class Template implements Outputable { 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(); StringBuffer buf = new StringBuffer(); + String blockType = null; outer: while (true) { while ( !endsWith(buf, " 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 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); @@ -160,6 +189,10 @@ public class Template implements Outputable { } if (s instanceof Outputable) { ((Outputable) s).output(out, l, vars); + } else if (s instanceof Date) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + out.print(sdf.format(s)); + out.print(" UTC"); } else { out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString()))); }