1
/****************************************************************
2
* Licensed to the Apache Software Foundation (ASF) under one *
3
* or more contributor license agreements. See the NOTICE file *
4
* distributed with this work for additional information *
5
* regarding copyright ownership. The ASF licenses this file *
6
* to you under the Apache License, Version 2.0 (the *
7
* "License"); you may not use this file except in compliance *
8
* with the License. You may obtain a copy of the License at *
10
* http://www.apache.org/licenses/LICENSE-2.0 *
12
* Unless required by applicable law or agreed to in writing, *
13
* software distributed under the License is distributed on an *
14
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15
* KIND, either express or implied. See the License for the *
16
* specific language governing permissions and limitations *
17
* under the License. *
18
****************************************************************/
20
package org.apache.james.mime4j.util;
22
import java.io.Serializable;
23
import java.util.ArrayList;
24
import java.util.Collection;
25
import java.util.Collections;
26
import java.util.Enumeration;
27
import java.util.HashMap;
28
import java.util.List;
30
import java.util.NoSuchElementException;
32
import org.apache.james.mime4j.message.Header;
33
import org.apache.james.mime4j.parser.ContentHandler;
34
import org.apache.james.mime4j.parser.Field;
37
* An object, which may be used to implement header, or parameter
38
* maps. The maps keys are the header or parameter names. The
39
* maps values are strings (single value), lists, or arrays.
41
* Note that this class is not directly used anywhere in Mime4j.
42
* Instead a user might choose to use it instead of {@link Header}
43
* and {@link Field} in a custom {@link ContentHandler} implementation.
46
public class StringArrayMap implements Serializable {
47
private static final long serialVersionUID = -5833051164281786907L;
48
private final Map<String, Object> map = new HashMap<String, Object>();
51
* <p>Converts the given object into a string. The object may be either of:
53
* <li>a string, which is returned without conversion</li>
54
* <li>a list of strings, in which case the first element is returned</li>
55
* <li>an array of strings, in which case the first element is returned</li>
58
public static String asString(Object pValue) {
62
if (pValue instanceof String) {
63
return (String) pValue;
65
if (pValue instanceof String[]) {
66
return ((String[]) pValue)[0];
68
if (pValue instanceof List) {
69
return (String) ((List<?>) pValue).get(0);
71
throw new IllegalStateException("Invalid parameter class: " + pValue.getClass().getName());
75
* <p>Converts the given object into a string array. The object may be either of:
77
* <li>a string, which is returned as an array with one element</li>
78
* <li>a list of strings, which is being converted into a string array</li>
79
* <li>an array of strings, which is returned without conversion</li>
82
public static String[] asStringArray(Object pValue) {
86
if (pValue instanceof String) {
87
return new String[]{(String) pValue};
89
if (pValue instanceof String[]) {
90
return (String[]) pValue;
92
if (pValue instanceof List) {
93
final List<?> l = (List<?>) pValue;
94
return l.toArray(new String[l.size()]);
96
throw new IllegalStateException("Invalid parameter class: " + pValue.getClass().getName());
100
* <p>Converts the given object into a string enumeration. The object may be either of:
102
* <li>a string, which is returned as an enumeration with one element</li>
103
* <li>a list of strings, which is being converted into a string enumeration</li>
104
* <li>an array of strings, which is being converted into a string enumeration</li>
107
public static Enumeration<String> asStringEnum(final Object pValue) {
108
if (pValue == null) {
111
if (pValue instanceof String) {
112
return new Enumeration<String>(){
113
private Object value = pValue;
114
public boolean hasMoreElements() {
115
return value != null;
117
public String nextElement() {
119
throw new NoSuchElementException();
121
final String s = (String) value;
127
if (pValue instanceof String[]) {
128
final String[] values = (String[]) pValue;
129
return new Enumeration<String>() {
131
public boolean hasMoreElements() {
132
return offset < values.length;
134
public String nextElement() {
135
if (offset >= values.length) {
136
throw new NoSuchElementException();
138
return values[offset++];
142
if (pValue instanceof List) {
143
@SuppressWarnings("unchecked")
144
final List<String> stringList = (List<String>) pValue;
145
return Collections.enumeration(stringList);
147
throw new IllegalStateException("Invalid parameter class: " + pValue.getClass().getName());
151
* Converts the given map into a string array map: The map values
154
public static Map<String, String[]> asMap(final Map<String, Object> pMap) {
155
Map<String, String[]> result = new HashMap<String, String[]>(pMap.size());
156
for (Map.Entry<String, Object> entry : pMap.entrySet()) {
157
final String[] value = asStringArray(entry.getValue());
158
result.put(entry.getKey(), value);
160
return Collections.unmodifiableMap(result);
164
* Adds a value to the given map.
166
protected void addMapValue(Map<String, Object> pMap, String pName, String pValue) {
167
Object o = pMap.get(pName);
170
} else if (o instanceof String) {
171
final List<Object> list = new ArrayList<Object>();
175
} else if (o instanceof List) {
176
@SuppressWarnings("unchecked")
177
final List<String> stringList = (List<String>) o;
178
stringList.add(pValue);
179
} else if (o instanceof String[]) {
180
final List<String> list = new ArrayList<String>();
181
final String[] arr = (String[]) o;
182
for (String str : arr) {
188
throw new IllegalStateException("Invalid object type: " + o.getClass().getName());
194
* Lower cases the given name.
196
protected String convertName(String pName) {
197
return pName.toLowerCase();
201
* Returns the requested value.
203
public String getValue(String pName) {
204
return asString(map.get(convertName(pName)));
208
* Returns the requested values as a string array.
210
public String[] getValues(String pName) {
211
return asStringArray(map.get(convertName(pName)));
215
* Returns the requested values as an enumeration.
217
public Enumeration<String> getValueEnum(String pName) {
218
return asStringEnum(map.get(convertName(pName)));
222
* Returns the set of registered names as an enumeration.
223
* @see #getNameArray()
225
public Enumeration<String> getNames() {
226
return Collections.enumeration(map.keySet());
230
* Returns an unmodifiable map of name/value pairs. The map keys
231
* are the lower cased parameter/header names. The map values are
234
public Map<String, String[]> getMap() {
239
* Adds a new name/value pair.
241
public void addValue(String pName, String pValue) {
242
addMapValue(map, convertName(pName), pValue);
246
* Returns the set of registered names.
249
public String[] getNameArray() {
250
final Collection<String> c = map.keySet();
251
return c.toArray(new String[c.size()]);