KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > parser > AddressParser


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.parser;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.LinkedList JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.regex.Matcher JavaDoc;
42 import java.util.regex.Pattern JavaDoc;
43
44 import org.columba.ristretto.message.Address;
45
46 /**
47  * Parser for mail addresses as defined in RFC 2822.
48  *
49  * @author Timo Stich <tstich@users.sourceforge.net>
50  */

51 public class AddressParser {
52     
53     private static final Pattern JavaDoc addressTokenizerPattern =
54         Pattern.compile("([^\\s]+?)(\\s|<|$)+");
55     
56     private static final Pattern JavaDoc trimPattern =
57         Pattern.compile("[\"<]?([^\"<>]*)[\">]?");
58     
59     //TODO implement Group parsing
60

61     
62     /**
63      * Parses a mailbox-list like specified in RFC 2822. The result is a List
64      * of { @link Address } objects.
65      *
66      * @param mailboxList CharSequence to parse
67      * @return Array of parsed addresses
68      * @throws ParserException
69      */

70     public static Address[] parseMailboxList(CharSequence JavaDoc mailboxList) throws ParserException {
71         List JavaDoc result = new LinkedList JavaDoc();
72         CharSequence JavaDoc[] tokens = tokenizeList(mailboxList);
73
74         for( int i=0; i< tokens.length; i++) {
75             result.add(parseAddress(tokens[i]));
76         }
77         
78         Address[] a = new Address[result.size()];
79         result.toArray((Object JavaDoc[]) a);
80         return a;
81     }
82     
83     /**
84      * Parses a address like specified in RFC 2822. The result is a
85      * of { @link Address } objects.
86      *
87      * @param address CharSequence to parse
88      * @return parsed addresses
89      * @throws ParserException
90      */

91     public static Address parseAddress(CharSequence JavaDoc address) throws ParserException {
92         Matcher JavaDoc addressTokenizer = addressTokenizerPattern.matcher(address);
93         
94         ArrayList JavaDoc tokens = new ArrayList JavaDoc();
95         
96         while (addressTokenizer.find()) {
97             tokens.add(addressTokenizer.group(1));
98         }
99         
100         if (tokens.size() < 1) {
101                     throw new ParserException("No valid EMail address", address);
102                 } else if (tokens.size() == 1) {
103             return new Address(trim((String JavaDoc)tokens.get(0)));
104         } else {
105             StringBuffer JavaDoc name = new StringBuffer JavaDoc((String JavaDoc)tokens.get(0));
106             
107             for (int i = 1; i < tokens.size() - 1; i++) {
108                 name.append(' ');
109                 name.append((String JavaDoc)tokens.get(i));
110             }
111             
112             return new Address(trim(name),
113                             trim((String JavaDoc)tokens.get(tokens.size() - 1)));
114         }
115     }
116
117     private static String JavaDoc trim(CharSequence JavaDoc input) {
118         Matcher JavaDoc trimMatcher = trimPattern.matcher(input);
119         
120         if( trimMatcher.matches() ) {
121             return trimMatcher.group(1);
122         } else {
123             return input.toString();
124         }
125     }
126     
127     private static CharSequence JavaDoc[] tokenizeList(CharSequence JavaDoc input) {
128         List JavaDoc result = new ArrayList JavaDoc();
129         boolean quoted= false;
130         
131         int start = 0;
132         int i;
133         
134         for( i=0; i<input.length(); i++) {
135             char c = input.charAt(i);
136             
137             switch( c ) {
138                 case '\"' : {
139                     quoted ^= true;
140                     break;
141                 }
142                 
143                 case ',' : {
144                     if( ! quoted ) {
145                         result.add(input.subSequence(start, i));
146                         start = i+1;
147                     }
148                     break;
149                 }
150             }
151         }
152         if( start < i ) {
153             result.add(input.subSequence(start, i));
154         }
155         
156         return (CharSequence JavaDoc[])result.toArray(new CharSequence JavaDoc[0]);
157     }
158 }
159
Popular Tags