KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > parser > NormalizeRecipientListParser


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.parser;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 /**
24  * Parsers for email address in RFC822 format.
25  *
26  * @author fdietz
27  */

28 public class NormalizeRecipientListParser {
29
30     /**
31      * Normalize Mail-addresses given in an Vector in
32      * <p>
33      * For example there ar mails as strings in the list with following formats:
34      * <p>
35      * <ul>
36      * <li>Firstname Lastname <mail@mail.org></li>
37      * <li>"Lastname, Firstname" <mail@mail.org></li>
38      * <li>mail@mail.org</li>
39      * <li><mail@mail.org></li>
40      * </ul>
41      * <p>
42      * These formats must be normalized to <mail@mail.org>.
43      *
44      * @param list
45      * List of Strings with mailaddresses in any format
46      * @return List of Strings with mailaddress in format <mail@mail.org>, never <code>null</code>
47      */

48     public List JavaDoc<String JavaDoc> normalizeRCPTVector(List JavaDoc<String JavaDoc> list) {
49         if (list == null)
50             throw new IllegalArgumentException JavaDoc("list == null");
51
52         String JavaDoc mailaddress = "";
53         String JavaDoc new_address = "";
54         List JavaDoc<String JavaDoc> out = new Vector JavaDoc<String JavaDoc>();
55
56         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
57             mailaddress = (String JavaDoc) it.next();
58
59             // skip
60
if ((mailaddress == null) || (mailaddress.length() == 0)) {
61                 continue;
62             }
63
64             StringTokenizer JavaDoc strToken = new StringTokenizer JavaDoc(mailaddress, "<");
65
66             if (strToken.countTokens() == 2) {
67                 // the first token is irrelevant
68
strToken.nextToken();
69
70                 // the next token is an token with the whole Mailaddress
71
new_address = "<" + strToken.nextToken();
72             } else {
73                 // just look if the first character alrady an <
74
// so can use this mailaddress as the correct address
75
if (mailaddress.charAt(0) == '<') {
76                     new_address = mailaddress;
77                 } else {
78                     new_address = "<" + mailaddress + ">";
79                 }
80             }
81
82             out.add(new_address);
83             new_address = "";
84         }
85
86         return out;
87     }
88 }
Popular Tags