View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.transport.mailets;
19  
20  import org.apache.avalon.framework.configuration.ConfigurationException;
21  import org.apache.james.util.XMLResources;
22  import org.apache.oro.text.regex.MalformedPatternException;
23  import org.apache.oro.text.regex.Pattern;
24  import org.apache.oro.text.regex.Perl5Compiler;
25  import org.apache.oro.text.regex.Perl5Matcher;
26  import org.apache.oro.text.regex.StringSubstitution;
27  import org.apache.oro.text.regex.Util;
28  
29  import javax.mail.MessagingException;
30  
31  
32  /***
33   * CommandListservFooter is based on the AddFooter mailet.
34   *
35   * It is used by the {@link CommandListservProcessor} to inject a footer into mailing list.
36   * <br />
37   * <br />
38   *
39   * @version CVS $Revision: 413813 $ $Date: 2006-06-13 06:09:36 +0000 (mar, 13 giu 2006) $
40   * @since 2.2.0
41   * @see XMLResources
42   */
43  public class CommandListservFooter extends AbstractAddFooter {
44  
45      protected String footerText;
46      protected String footerHtml;
47  
48      /***
49       * The list serv manager
50       */
51      protected ICommandListservManager commandListservManager;
52  
53      /***
54       * For matching
55       */
56      protected Perl5Compiler perl5Compiler = new Perl5Compiler();
57      protected Pattern insertPattern;
58      protected Pattern newlinePattern;
59  
60      //For resources
61      protected XMLResources[] xmlResources = new XMLResources[2];
62  
63      protected static final int TEXT_PLAIN = 0;
64      protected static final int TEXT_HTML = 1;
65  
66      public CommandListservFooter(ICommandListservManager commandListservManager) {
67          this.commandListservManager = commandListservManager;
68          try {
69              insertPattern = perl5Compiler.compile("</body>//s*</html>", Perl5Compiler.CASE_INSENSITIVE_MASK);
70              newlinePattern = perl5Compiler.compile("\r\n|\n", Perl5Compiler.CASE_INSENSITIVE_MASK);
71          } catch (MalformedPatternException e) {
72              throw new IllegalStateException("Unable to parse regexps: " + e.getMessage());
73          }
74      }
75  
76      /***
77       * Initialize the mailet
78       */
79      public void init() throws MessagingException {
80          try {
81              xmlResources = commandListservManager.initXMLResources(new String[]{"footer", "footer_html"});
82          } catch (ConfigurationException e) {
83              throw new MessagingException(e.getMessage(), e);
84          }
85      }
86  
87      /***
88       * Return a string describing this mailet.
89       *
90       * @return a string describing this mailet
91       */
92      public String getMailetInfo() {
93          return "CommandListservFooter Mailet";
94      }
95  
96  
97      /***
98       * Get and cache the footer text
99       *
100      * @return the footer text
101      * @see XMLResources
102      */
103     protected String getFooterText() {
104         if (footerText == null) {
105             footerText = getFormattedText(TEXT_PLAIN);
106         }
107         return footerText;
108     }
109 
110     /***
111      * Get and cache the footer html text
112      *
113      * @return the footer text
114      * @see XMLResources
115      */
116     protected String getFooterHTML() {
117         if (footerHtml == null) {
118             String footerText = getFormattedText(TEXT_HTML);
119             footerHtml = Util.substitute(new Perl5Matcher(),
120                     newlinePattern,
121                     new StringSubstitution(" <br />"),
122                     footerText,
123                     Util.SUBSTITUTE_ALL);
124         }
125         return footerHtml;
126     }
127 
128     /***
129      * @see XMLResources#getString
130      * @param index either {@link #TEXT_PLAIN} or {@link #TEXT_HTML}
131      * @return a formatted text with the proper list and domain
132      */
133     protected String getFormattedText(int index) {
134         return xmlResources[index].getString("text");
135     }
136 }