KickJava   Java API By Example, From Geeks To Geeks.

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


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

30 public class ListParser {
31
32     public final static char SEPARATOR_CHAR = ',';
33
34     public final static String JavaDoc SEPARATOR_STRING = ",";
35
36     public ListParser() {
37     }
38
39     /**
40      * Create list from String containing semicolon-separated email addresses.
41      *
42      * @param str
43      * semicolon separated email address list
44      *
45      * @return list list of email addresses, never <code>null</code>
46      */

47     public static List JavaDoc<String JavaDoc> createListFromString(String JavaDoc str) {
48         if (str == null)
49             throw new IllegalArgumentException JavaDoc("str == null");
50
51         List JavaDoc<String JavaDoc> result = new Vector JavaDoc<String JavaDoc>();
52         if (str.length() == 0)
53             return result;
54
55         int pos = 0;
56         boolean bracket = false;
57         
58         // Remove the ending separator and whitespace, if any exist
59
str = str.trim();
60         if (str.endsWith(SEPARATOR_STRING))
61             str = str.substring(0, str.length()-1);
62
63         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
64         while (pos < str.length()) {
65             char ch = str.charAt(pos);
66
67             if ((ch == SEPARATOR_CHAR) && (bracket == false)) {
68                 // found new message
69
String JavaDoc address = buf.toString().trim();
70                 result.add(address);
71
72                 buf = new StringBuffer JavaDoc();
73             } else if (ch == '"') {
74                 // Remove the double-quote characters from around the addresses in the string
75
bracket = !bracket;
76             } else {
77                 buf.append(ch);
78             }
79             pos++;
80         }
81
82         String JavaDoc address = buf.toString().trim();
83         // remove whitespaces
84
address = address.trim();
85         result.add(address);
86
87         return result;
88     }
89
90     /**
91      * Create list from String containing semicolon-separated email addresses.
92      *
93      * @param str
94      * semicolon separated email address list
95      *
96      * @return list list of email addresses, never <code>null</code>
97     public static List<String> createListFromString(String str) {
98         if (str == null)
99             throw new IllegalArgumentException("str == null");
100
101         List<String> result = new Vector<String>();
102         if (str.length() == 0)
103             return result;
104
105         int pos = 0;
106         boolean bracket = false;
107         StringBuffer buf = new StringBuffer();
108         int listLength = str.length();
109
110         while (pos < listLength) {
111             char ch = str.charAt(pos);
112
113             if ((ch == SEPARATOR_CHAR) && (bracket == false)) {
114                 // found new message
115                 String address = buf.toString();
116                 result.add(address);
117
118                 buf = new StringBuffer();
119                 pos++;
120             } else if (ch == '"') {
121                 buf.append(ch);
122
123                 pos++;
124
125                 bracket = !bracket;
126             } else {
127                 buf.append(ch);
128
129                 pos++;
130             }
131         }
132
133         String address = buf.toString();
134         // remove whitespaces
135         address = address.trim();
136         result.add(address);
137
138         return result;
139     }
140      */

141
142     /**
143      * Create comma-separated String representation of a list of String objects.
144      *
145      * @param list
146      * list containing String objects
147      * @return String representation, never <code>null</code
148      */

149     public static String JavaDoc createStringFromList(List JavaDoc<String JavaDoc> list,
150             String JavaDoc separator) {
151         if (list == null)
152             throw new IllegalArgumentException JavaDoc("list == null");
153         if (separator == null)
154             throw new IllegalArgumentException JavaDoc("separator == null");
155
156         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
157
158         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
159             String JavaDoc address = (String JavaDoc) it.next();
160             if (address == null) {
161                 continue;
162             }
163             
164             // Remote double-quotes
165
StringBuffer JavaDoc addrSB = new StringBuffer JavaDoc(address);
166             while (true) {
167                 int doubleQuote = addrSB.indexOf("\"");
168                 if (doubleQuote >= 0)
169                     addrSB.deleteCharAt(doubleQuote);
170                 else
171                     break;
172             }
173             
174             // If address contains a comma, enclose the display name portion in double-quotes
175
int comma = addrSB.indexOf(",");
176             int endDoubleQuote = addrSB.length();
177             if (comma >= 0) {
178                 int addrStart = addrSB.indexOf(" <");
179                 if (addrStart >= 0)
180                     endDoubleQuote = addrStart;
181                 addrSB.insert(endDoubleQuote, '"');
182                 addrSB.insert(0, '"');
183             }
184
185             address = addrSB.toString();
186             
187             output.append(address);
188             output.append(separator);
189             output.append(" ");
190         }
191
192         /*
193         if (output.length() > 0) {
194             output.deleteCharAt(output.length() - 1);
195         }
196         */

197
198         return output.toString();
199     }
200 }
Popular Tags