01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
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;
19: import java.util.List;
20: import java.util.StringTokenizer;
21: import java.util.Vector;
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<String> normalizeRCPTVector(List<String> list) {
49: if (list == null)
50: throw new IllegalArgumentException("list == null");
51:
52: String mailaddress = "";
53: String new_address = "";
54: List<String> out = new Vector<String>();
55:
56: for (Iterator it = list.iterator(); it.hasNext();) {
57: mailaddress = (String) it.next();
58:
59: // skip
60: if ((mailaddress == null) || (mailaddress.length() == 0)) {
61: continue;
62: }
63:
64: StringTokenizer strToken = new StringTokenizer(mailaddress,
65: "<");
66:
67: if (strToken.countTokens() == 2) {
68: // the first token is irrelevant
69: strToken.nextToken();
70:
71: // the next token is an token with the whole Mailaddress
72: new_address = "<" + strToken.nextToken();
73: } else {
74: // just look if the first character alrady an <
75: // so can use this mailaddress as the correct address
76: if (mailaddress.charAt(0) == '<') {
77: new_address = mailaddress;
78: } else {
79: new_address = "<" + mailaddress + ">";
80: }
81: }
82:
83: out.add(new_address);
84: new_address = "";
85: }
86:
87: return out;
88: }
89: }
|