1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.transport.mailets;
23
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.james.util.XMLResources;
26 import org.apache.oro.text.regex.MalformedPatternException;
27 import org.apache.oro.text.regex.Pattern;
28 import org.apache.oro.text.regex.Perl5Compiler;
29 import org.apache.oro.text.regex.Perl5Matcher;
30 import org.apache.oro.text.regex.StringSubstitution;
31 import org.apache.oro.text.regex.Util;
32
33 import javax.mail.MessagingException;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class CommandListservFooter extends AbstractAddFooter {
48
49 protected String footerText;
50 protected String footerHtml;
51
52
53
54
55 protected ICommandListservManager commandListservManager;
56
57
58
59
60 protected Perl5Compiler perl5Compiler = new Perl5Compiler();
61 protected Pattern insertPattern;
62 protected Pattern newlinePattern;
63
64
65 protected XMLResources[] xmlResources = new XMLResources[2];
66
67 protected static final int TEXT_PLAIN = 0;
68 protected static final int TEXT_HTML = 1;
69
70 public CommandListservFooter(ICommandListservManager commandListservManager) {
71 this.commandListservManager = commandListservManager;
72 try {
73 insertPattern = perl5Compiler.compile("</body>\\s*</html>", Perl5Compiler.CASE_INSENSITIVE_MASK);
74 newlinePattern = perl5Compiler.compile("\r\n|\n", Perl5Compiler.CASE_INSENSITIVE_MASK);
75 } catch (MalformedPatternException e) {
76 throw new IllegalStateException("Unable to parse regexps: " + e.getMessage());
77 }
78 }
79
80
81
82
83 public void init() throws MessagingException {
84 try {
85 xmlResources = commandListservManager.initXMLResources(new String[]{"footer", "footer_html"});
86 } catch (ConfigurationException e) {
87 throw new MessagingException(e.getMessage(), e);
88 }
89 }
90
91
92
93
94
95
96 public String getMailetInfo() {
97 return "CommandListservFooter Mailet";
98 }
99
100
101
102
103
104
105
106
107 protected String getFooterText() {
108 if (footerText == null) {
109 footerText = getFormattedText(TEXT_PLAIN);
110 }
111 return footerText;
112 }
113
114
115
116
117
118
119
120 protected String getFooterHTML() {
121 if (footerHtml == null) {
122 String footerText = getFormattedText(TEXT_HTML);
123 footerHtml = Util.substitute(new Perl5Matcher(),
124 newlinePattern,
125 new StringSubstitution(" <br />"),
126 footerText,
127 Util.SUBSTITUTE_ALL);
128 }
129 return footerHtml;
130 }
131
132
133
134
135
136
137 protected String getFormattedText(int index) {
138 return xmlResources[index].getString("text");
139 }
140 }