1 /**************************************************************** 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 ****************************************************************/ 19 20 21 22 package org.apache.mailet.base; 23 24 import org.apache.mailet.MailetConfig; 25 26 27 /** 28 * Collects utility methods. 29 */ 30 public class MailetUtil { 31 32 /** 33 * <p>This takes the subject string and reduces (normailzes) it. 34 * Multiple "Re:" entries are reduced to one, and capitalized. The 35 * prefix is always moved/placed at the beginning of the line, and 36 * extra blanks are reduced, so that the output is always of the 37 * form:</p> 38 * <code> 39 * <prefix> + <one-optional-"Re:"*gt; + <remaining subject> 40 * </code> 41 * <p>I have done extensive testing of this routine with a standalone 42 * driver, and am leaving the commented out debug messages so that 43 * when someone decides to enhance this method, it can be yanked it 44 * from this file, embedded it with a test driver, and the comments 45 * enabled.</p> 46 */ 47 public static String normalizeSubject(String subj, String prefix) { 48 StringBuffer subject = new StringBuffer(subj); 49 int prefixLength = prefix.length(); 50 51 // If the "prefix" is not at the beginning the subject line, remove it 52 int index = subject.indexOf(prefix); 53 if (index != 0) { 54 55 if (index > 0) { 56 subject.delete(index, index + prefixLength); 57 } 58 subject.insert(0, prefix); // insert prefix at the front 59 } 60 61 // Replace Re: with RE: 62 String match = "Re:"; 63 index = subject.indexOf(match, prefixLength); 64 65 while(index > -1) { 66 subject.replace(index, index + match.length(), "RE:"); 67 index = subject.indexOf(match, prefixLength); 68 } 69 70 // Reduce them to one at the beginning 71 match ="RE:"; 72 int indexRE = subject.indexOf(match, prefixLength) + match.length(); 73 index = subject.indexOf(match, indexRE); 74 while(index > 0) { 75 subject.delete(index, index + match.length()); 76 index = subject.indexOf(match, indexRE); 77 } 78 79 // Reduce blanks 80 match = " "; 81 index = subject.indexOf(match, prefixLength); 82 while(index > -1) { 83 subject.replace(index, index + match.length(), " "); 84 index = subject.indexOf(match, prefixLength); 85 } 86 return subject.toString(); 87 } 88 89 90 /** 91 * <p>Gets a boolean valued init parameter.</p> 92 * @param config not null 93 * @param name name of the init parameter to be queried 94 * @param defaultValue this value will be substituted when the named value 95 * cannot be parse or when the init parameter is absent 96 * @return true when the init parameter is <code>true</code> (ignoring case); 97 * false when the init parameter is <code>false</code> (ignoring case); 98 * otherwise the default value 99 */ 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 }