1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package javax.mail.internet;
19
20 import java.util.Collections;
21 import java.util.Enumeration;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Map;
25 import java.util.StringTokenizer;
26
27 // Represents lists in things like
28 // Content-Type: text/plain;charset=klingon
29 //
30 // The ;charset=klingon is the parameter list, may have more of them with ';'
31
32 /**
33 * @version $Rev: 54266 $ $Date: 2004-10-10 14:02:50 -0700 (Sun, 10 Oct 2004) $
34 */
35 public class ParameterList {
36 private Map _parameters = new HashMap();
37
38 public ParameterList() {
39 }
40
41 public ParameterList(String list) throws ParseException {
42 if (list == null) {
43 return;
44 } else {
45 StringTokenizer tokenizer = new StringTokenizer(list, ";");
46 while (tokenizer.hasMoreTokens()) {
47 String parameter = tokenizer.nextToken();
48 int eq = parameter.indexOf("=");
49 if (eq == -1) {
50 throw new ParseException(parameter);
51 } else {
52 String name = parameter.substring(0, eq);
53 String value = parameter.substring(eq + 1);
54 set(name, value);
55 }
56 }
57 }
58 }
59
60 public int size() {
61 return _parameters.size();
62 }
63
64 public String get(String name) {
65 return (String) _parameters.get(name);
66 }
67
68 public void set(String name, String value) {
69 _parameters.put(name.trim(), value.trim());
70 }
71
72 public void remove(String name) {
73 _parameters.remove(name);
74 }
75
76 public Enumeration getNames() {
77 return Collections.enumeration(_parameters.keySet());
78 }
79
80 public String toString() {
81 Iterator it = _parameters.entrySet().iterator();
82 StringBuffer result = new StringBuffer();
83 while (it.hasNext()) {
84 Map.Entry entry = (Map.Entry) it.next();
85 result.append(";");
86 result.append(entry.getKey());
87 result.append("=");
88 result.append(entry.getValue());
89 }
90 return result.toString();
91 // TODO Return in same list as parsed format
92 }
93
94 public String toString(int lineBreak) {
95 // figure out where to break the line
96 String answer = toString();
97 if (answer.length() > lineBreak) {
98 // convert it to substring
99 // TODO Implement
100 return "";
101 } else {
102 return answer;
103 }
104 }
105 }