*/
public class Template implements Outputable {
- private static class ParseResult {
+ protected static class ParseResult {
TemplateBlock block;
* 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 (Reader r = new InputStreamReader(u.openStream(), "UTF-8")) {
try {
if (u.getProtocol().equals("file")) {
source = new File(u.toURI());
e.printStackTrace();
}
data = parse(r).getBlock(null);
- r.close();
} catch (IOException e) {
throw new Error(e);
}
}
}
- private ParseResult parse(Reader r) throws IOException {
+ protected ParseResult parse(Reader r) throws IOException {
+ return parseContent(r);
+ }
+
+ protected ParseResult parseContent(Reader r) throws IOException {
LinkedList<String> splitted = new LinkedList<String>();
LinkedList<Translatable> commands = new LinkedList<Translatable>();
StringBuffer buf = new StringBuffer();
break outer;
}
buf.append((char) ch);
+ if (endsWith(buf, "\\\n")) {
+ buf.delete(buf.length() - 2, buf.length());
+ }
}
buf.delete(buf.length() - 2, buf.length());
splitted.add(buf.toString());
if (m.matches()) {
String type = m.group(1);
String variable = m.group(2);
- ParseResult body = parse(r);
+ ParseResult body = parseContent(r);
if (type.equals("if")) {
if ("else".equals(body.getEndType())) {
- commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}")));
+ commands.add(new IfStatement(variable, body.getBlock("else"), parseContent(r).getBlock("}")));
} else {
commands.add(new IfStatement(variable, body.getBlock("}")));
}
@Override
public void output(PrintWriter out, Language l, Map<String, Object> vars) {
+ tryReload();
+ data.output(out, l, vars);
+ }
+
+ protected void tryReload() {
if (source != null && lastLoaded < source.lastModified()) {
try {
System.out.println("Reloading template.... " + source);
e.printStackTrace();
}
}
- data.output(out, l, vars);
}
protected static void outputVar(PrintWriter out, Language l, Map<String, Object> vars, String varname, boolean unescaped) {
+ if (vars.containsKey(Outputable.OUT_KEY_PLAIN)) {
+ unescaped = true;
+ }
Object s = vars.get(varname);
if (s == null) {
} else if (s instanceof DayDate) {
out.print(DateSelector.getDateFormat().format(((DayDate) s).toDate()));
} else if (s instanceof Date) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- out.print(sdf.format(s));
- out.print(" UTC");
+ SimpleDateFormat sdfUI = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+ out.print("<time datetime=\"" + sdf.format(s) + "\">");
+ out.print(sdfUI.format(s));
+ out.print(" UTC</time>");
} else {
out.print(s == null ? "null" : (unescaped ? s.toString() : HTMLEncoder.encodeHTML(s.toString())));
}