1 package org.cacert.gigi.output.template;
3 import java.io.IOException;
4 import java.io.PrintWriter;
6 import java.io.StringReader;
7 import java.io.StringWriter;
9 import java.util.Collection;
12 import org.cacert.gigi.email.SendMail;
13 import org.cacert.gigi.localisation.Language;
15 public class MailTemplate extends Template {
17 private static final Template FOOTER = new Template(MailTemplate.class.getResource("MailFooter.templ"));
19 public static final String SUBJECT_TAG = "[SomeCA] ";
21 private TemplateBlock subjectBlock;
23 public MailTemplate(URL u) {
27 public MailTemplate(Reader r) {
32 protected ParseResult parse(Reader r) throws IOException {
33 StringBuilder strb = new StringBuilder();
36 while ((c = r.read()) > 0) {
45 strb.append((char) c);
47 String[] lines = strb.toString().split("\n");
48 for (int i = 0; i < lines.length; i++) {
49 String[] lineParts = lines[i].split(": ", 2);
50 if (lineParts.length != 2) {
51 throw new IOException("Mail template header malformed.");
53 if (lineParts[0].equals("Subject")) {
54 subjectBlock = parseContent(new StringReader(lineParts[1])).getBlock(null);
57 if (subjectBlock == null) {
58 throw new IOException("Mail template without subject line.");
60 return parseContent(r);
63 public void sendMail(Language l, Map<String, Object> vars, String to) throws IOException {
65 vars.put(Outputable.OUT_KEY_PLAIN, true);
67 String body = runTemplate(this, l, vars);
68 body += runTemplate(FOOTER, l, vars);
69 String subject = runTemplate(subjectBlock, l, vars);
71 SendMail.getInstance().sendMail(to, SUBJECT_TAG + subject, body, null, null, null, null, false);
74 private static String runTemplate(Outputable toRun, Language l, Map<String, Object> vars) {
75 StringWriter sw = new StringWriter();
76 PrintWriter pw = new PrintWriter(sw);
77 toRun.output(pw, l, vars);
83 public void addTranslations(Collection<String> s) {
84 subjectBlock.addTranslations(s);
85 super.addTranslations(s);