KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > main > MessageOptionParser


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
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 JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
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 JavaDoc[] tokenizeList(CharSequence JavaDoc input) {
33         if (input == null)
34             throw new IllegalArgumentException JavaDoc("input == null");
35
36         List JavaDoc<CharSequence JavaDoc> result = new ArrayList JavaDoc<CharSequence JavaDoc>();
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 JavaDoc[]) result.toArray(new CharSequence JavaDoc[0]);
65     }
66
67     public static Map JavaDoc<String JavaDoc,String JavaDoc> parse(String JavaDoc in) {
68         if (in == null)
69             throw new IllegalArgumentException JavaDoc("in == null");
70
71         Hashtable JavaDoc<String JavaDoc, String JavaDoc> map = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
72         CharSequence JavaDoc[] sequence = tokenizeList(in);
73         for (int i = 0; i < sequence.length; i++) {
74             String JavaDoc s = (String JavaDoc) 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 JavaDoc key = s.substring(0, index);
82                 String JavaDoc value = s.substring(index + 1, s.length());
83                 map.put(key, value);
84             }
85         }
86
87         return map;
88     }
89 }
90
Popular Tags