001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.mailets;
019:
020: import org.apache.avalon.framework.configuration.ConfigurationException;
021: import org.apache.james.util.XMLResources;
022: import org.apache.oro.text.regex.MalformedPatternException;
023: import org.apache.oro.text.regex.Pattern;
024: import org.apache.oro.text.regex.Perl5Compiler;
025: import org.apache.oro.text.regex.Perl5Matcher;
026: import org.apache.oro.text.regex.StringSubstitution;
027: import org.apache.oro.text.regex.Util;
028:
029: import javax.mail.MessagingException;
030:
031: /**
032: * CommandListservFooter is based on the AddFooter mailet.
033: *
034: * It is used by the {@link CommandListservProcessor} to inject a footer into mailing list.
035: * <br />
036: * <br />
037: *
038: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
039: * @since 2.2.0
040: * @see XMLResources
041: */
042: public class CommandListservFooter extends AbstractAddFooter {
043:
044: protected String footerText;
045: protected String footerHtml;
046:
047: /**
048: * The list serv manager
049: */
050: protected ICommandListservManager commandListservManager;
051:
052: /**
053: * For matching
054: */
055: protected Perl5Compiler perl5Compiler = new Perl5Compiler();
056: protected Pattern insertPattern;
057: protected Pattern newlinePattern;
058:
059: //For resources
060: protected XMLResources[] xmlResources = new XMLResources[2];
061:
062: protected static final int TEXT_PLAIN = 0;
063: protected static final int TEXT_HTML = 1;
064:
065: public CommandListservFooter(
066: ICommandListservManager commandListservManager) {
067: this .commandListservManager = commandListservManager;
068: try {
069: insertPattern = perl5Compiler.compile("</body>\\s*</html>",
070: Perl5Compiler.CASE_INSENSITIVE_MASK);
071: newlinePattern = perl5Compiler.compile("\r\n|\n",
072: Perl5Compiler.CASE_INSENSITIVE_MASK);
073: } catch (MalformedPatternException e) {
074: throw new IllegalStateException("Unable to parse regexps: "
075: + e.getMessage());
076: }
077: }
078:
079: /**
080: * Initialize the mailet
081: */
082: public void init() throws MessagingException {
083: try {
084: xmlResources = commandListservManager
085: .initXMLResources(new String[] { "footer",
086: "footer_html" });
087: } catch (ConfigurationException e) {
088: throw new MessagingException(e.getMessage(), e);
089: }
090: }
091:
092: /**
093: * Return a string describing this mailet.
094: *
095: * @return a string describing this mailet
096: */
097: public String getMailetInfo() {
098: return "CommandListservFooter Mailet";
099: }
100:
101: /**
102: * Get and cache the footer text
103: *
104: * @return the footer text
105: * @see XMLResources
106: */
107: protected String getFooterText() {
108: if (footerText == null) {
109: footerText = getFormattedText(TEXT_PLAIN);
110: }
111: return footerText;
112: }
113:
114: /**
115: * Get and cache the footer html text
116: *
117: * @return the footer text
118: * @see XMLResources
119: */
120: protected String getFooterHTML() {
121: if (footerHtml == null) {
122: String footerText = getFormattedText(TEXT_HTML);
123: footerHtml = Util.substitute(new Perl5Matcher(),
124: newlinePattern, new StringSubstitution(" <br />"),
125: footerText, Util.SUBSTITUTE_ALL);
126: }
127: return footerHtml;
128: }
129:
130: /**
131: * @see XMLResources#getString
132: * @param index either {@link #TEXT_PLAIN} or {@link #TEXT_HTML}
133: * @return a formatted text with the proper list and domain
134: */
135: protected String getFormattedText(int index) {
136: return xmlResources[index].getString("text");
137: }
138: }
|