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:
17: package org.columba.mail.main;
18:
19: import java.util.ArrayList;
20: import java.util.Hashtable;
21: import java.util.List;
22: import java.util.Map;
23:
24: public class MessageOptionParser {
25:
26: private static final int KEY = 0;
27:
28: private static final int VALUE = 1;
29:
30: private static final int QUOTED_VALUE = 2;
31:
32: public static CharSequence[] tokenizeList(CharSequence input) {
33: if (input == null)
34: throw new IllegalArgumentException("input == null");
35:
36: List<CharSequence> result = new ArrayList<CharSequence>();
37: boolean quoted = false;
38:
39: int start = 0;
40: int i;
41:
42: for (i = 0; i < input.length(); i++) {
43: char c = input.charAt(i);
44:
45: switch (c) {
46: case '\"': {
47: quoted ^= true;
48: break;
49: }
50:
51: case ',': {
52: if (!quoted) {
53: result.add(input.subSequence(start, i));
54: start = i + 1;
55: }
56: break;
57: }
58: }
59: }
60: if (start < i) {
61: result.add(input.subSequence(start, i));
62: }
63:
64: return (CharSequence[]) result.toArray(new CharSequence[0]);
65: }
66:
67: public static Map<String, String> parse(String in) {
68: if (in == null)
69: throw new IllegalArgumentException("in == null");
70:
71: Hashtable<String, String> map = new Hashtable<String, String>();
72: CharSequence[] sequence = tokenizeList(in);
73: for (int i = 0; i < sequence.length; i++) {
74: String s = (String) sequence[i];
75: // remove whitespaces
76: s = s.trim();
77:
78: // split key/value pairs
79: int index = s.indexOf('=');
80: if (index != -1) {
81: String key = s.substring(0, index);
82: String value = s.substring(index + 1, s.length());
83: map.put(key, value);
84: }
85: }
86:
87: return map;
88: }
89: }
|