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 ****************************************************************/
019
020
021
022 package org.apache.mailet.base;
023
024 import org.apache.mailet.MailetConfig;
025
026
027 /**
028 * Collects utility methods.
029 */
030 public class MailetUtil {
031
032 /**
033 * <p>This takes the subject string and reduces (normailzes) it.
034 * Multiple "Re:" entries are reduced to one, and capitalized. The
035 * prefix is always moved/placed at the beginning of the line, and
036 * extra blanks are reduced, so that the output is always of the
037 * form:</p>
038 * <code>
039 * <prefix> + <one-optional-"Re:"*gt; + <remaining subject>
040 * </code>
041 * <p>I have done extensive testing of this routine with a standalone
042 * driver, and am leaving the commented out debug messages so that
043 * when someone decides to enhance this method, it can be yanked it
044 * from this file, embedded it with a test driver, and the comments
045 * enabled.</p>
046 */
047 public static String normalizeSubject(String subj, String prefix) {
048 StringBuilder subject = new StringBuilder(subj);
049 int prefixLength = prefix.length();
050
051 // If the "prefix" is not at the beginning the subject line, remove it
052 int index = subject.indexOf(prefix);
053 if (index != 0) {
054
055 if (index > 0) {
056 subject.delete(index, index + prefixLength);
057 }
058 subject.insert(0, prefix); // insert prefix at the front
059 }
060
061 // Replace Re: with RE:
062 String match = "Re:";
063 index = subject.indexOf(match, prefixLength);
064
065 while(index > -1) {
066 subject.replace(index, index + match.length(), "RE:");
067 index = subject.indexOf(match, prefixLength);
068 }
069
070 // Reduce them to one at the beginning
071 match ="RE:";
072 int indexRE = subject.indexOf(match, prefixLength) + match.length();
073 index = subject.indexOf(match, indexRE);
074 while(index > 0) {
075 subject.delete(index, index + match.length());
076 index = subject.indexOf(match, indexRE);
077 }
078
079 // Reduce blanks
080 match = " ";
081 index = subject.indexOf(match, prefixLength);
082 while(index > -1) {
083 subject.replace(index, index + match.length(), " ");
084 index = subject.indexOf(match, prefixLength);
085 }
086 return subject.toString();
087 }
088
089
090 /**
091 * <p>Gets a boolean valued init parameter.</p>
092 * @param config not null
093 * @param name name of the init parameter to be queried
094 * @param defaultValue this value will be substituted when the named value
095 * cannot be parse or when the init parameter is absent
096 * @return true when the init parameter is <code>true</code> (ignoring case);
097 * false when the init parameter is <code>false</code> (ignoring case);
098 * otherwise the default value
099 */
100 public static boolean getInitParameter(MailetConfig config, String name, boolean defaultValue) {
101 final String value = config.getInitParameter(name);
102 final boolean result;
103 if ("true".equalsIgnoreCase(value)) {
104 result = true;
105 } else if ("false".equalsIgnoreCase(value)){
106 result = false;
107 } else {
108 result = defaultValue;
109 }
110 return result;
111 }
112 }