001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.parser;
019:
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Vector;
023:
024: /**
025: * Provides parsers for creating String representations from List objects and
026: * vice versa.
027: *
028: * @author fdietz
029: */
030: public class ListParser {
031:
032: public final static char SEPARATOR_CHAR = ',';
033:
034: public final static String SEPARATOR_STRING = ",";
035:
036: public ListParser() {
037: }
038:
039: /**
040: * Create list from String containing semicolon-separated email addresses.
041: *
042: * @param str
043: * semicolon separated email address list
044: *
045: * @return list list of email addresses, never <code>null</code>
046: */
047: public static List<String> createListFromString(String str) {
048: if (str == null)
049: throw new IllegalArgumentException("str == null");
050:
051: List<String> result = new Vector<String>();
052: if (str.length() == 0)
053: return result;
054:
055: int pos = 0;
056: boolean bracket = false;
057:
058: // Remove the ending separator and whitespace, if any exist
059: str = str.trim();
060: if (str.endsWith(SEPARATOR_STRING))
061: str = str.substring(0, str.length() - 1);
062:
063: StringBuffer buf = new StringBuffer();
064: while (pos < str.length()) {
065: char ch = str.charAt(pos);
066:
067: if ((ch == SEPARATOR_CHAR) && (bracket == false)) {
068: // found new message
069: String address = buf.toString().trim();
070: result.add(address);
071:
072: buf = new StringBuffer();
073: } else if (ch == '"') {
074: // Remove the double-quote characters from around the addresses in the string
075: bracket = !bracket;
076: } else {
077: buf.append(ch);
078: }
079: pos++;
080: }
081:
082: String address = buf.toString().trim();
083: // remove whitespaces
084: address = address.trim();
085: result.add(address);
086:
087: return result;
088: }
089:
090: /**
091: * Create list from String containing semicolon-separated email addresses.
092: *
093: * @param str
094: * semicolon separated email address list
095: *
096: * @return list list of email addresses, never <code>null</code>
097: public static List<String> createListFromString(String str) {
098: if (str == null)
099: 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 createStringFromList(List<String> list,
150: String separator) {
151: if (list == null)
152: throw new IllegalArgumentException("list == null");
153: if (separator == null)
154: throw new IllegalArgumentException("separator == null");
155:
156: StringBuffer output = new StringBuffer();
157:
158: for (Iterator it = list.iterator(); it.hasNext();) {
159: String address = (String) it.next();
160: if (address == null) {
161: continue;
162: }
163:
164: // Remote double-quotes
165: StringBuffer addrSB = new StringBuffer(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: }
|