Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fad84474323e93bf972d9ca0ebf3fac798326cd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.framework.util;

import java.io.*;
import java.util.*;
import org.eclipse.osgi.framework.internal.core.Msg;
import org.osgi.framework.BundleException;

/**
 * Headers classes. This class implements a Dictionary that has
 * the following behaviour:
 * <ul>
 * <li>put and remove clear throw UnsupportedOperationException.
 * The Dictionary is thus read-only to others.
 * <li>The String keys in the Dictionary are case-preserved,
 * but the get operation is case-insensitive.
 * </ul>
 */
public class Headers extends Dictionary {
	/**
	 * Dictionary of keys: Lower case key => Case preserved key.
	 */
	protected Dictionary headers;

	/**
	 * Dictionary of values: Case preserved key => value.
	 */
	protected Dictionary values;

	/**
	 * Create an empty Headers dictionary.
	 *
	 * @param initialCapacity The initial capacity of this Headers object.
	 */
	public Headers(int initialCapacity) {
		super();

		headers = new Hashtable(initialCapacity);
		values = new Hashtable(initialCapacity);
	}

	/**
	 * Create a Headers dictionary from a Dictionary.
	 *
	 * @param values The initial dictionary for this Headers object.
	 * @exception IllegalArgumentException If a case-variant of the key is
	 * in the dictionary parameter.
	 */
	public Headers(Dictionary values) {
		super();

		headers = new Hashtable(values.size());
		this.values = values;

		/* initialize headers dictionary */
		Enumeration keys = values.keys();

		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();

			if (key instanceof String) {
				String header = ((String) key).toLowerCase();

				if (headers.put(header, key) != null) /* if case-variant already present */
				{
					throw new IllegalArgumentException(Msg.formatter.getString("HEADER_DUPLICATE_KEY_EXCEPTION", header)); //$NON-NLS-1$
				}
			}
		}
	}

	/**
	 * Case-preserved keys.
	 */
	public synchronized Enumeration keys() {
		return (values.keys());
	}

	/**
	 * Values.
	 */
	public synchronized Enumeration elements() {
		return (values.elements());
	}

	/**
	 * Support case-insensitivity for keys.
	 *
	 * @param key name.
	 */
	public synchronized Object get(Object key) {
		Object value = values.get(key);

		if ((value == null) && (key instanceof String)) {
			Object keyLower = headers.get(((String) key).toLowerCase());

			if (keyLower != null) {
				value = values.get(keyLower);
			}
		}

		return (value);
	}

	/**
	 * Set a header value.
	 *
	 * @param key Key name.
	 * @param value Value of the key or null to remove key.
	 * @return the previous value to which the key was mapped,
	 * or null if the key did not have a previous mapping.
	 *
	 * @exception IllegalArgumentException If a case-variant of the key is
	 * already present.
	 */
	public synchronized Object set(Object key, Object value) {
		String header = (key instanceof String) ? ((String) key).toLowerCase() : null;

		if (value == null) /* remove */
		{
			if (header != null) /* String key */
			{
				key = headers.remove(header);

				if (key != null) /* is String key in hashtable? */
				{
					value = values.remove(key);
				}
			} else /* non-String key */
			{
				value = values.remove(key);
			}

			return (value);
		} else /* put */
		{
			if (header != null) /* String key */
			{
				Object oldKey = headers.put(header, key);

				if ((oldKey != null) && !header.equals(oldKey)) /* if case-variant already present */
				{
					headers.put(header, oldKey); /* put old case-variant back */

					throw new IllegalArgumentException(Msg.formatter.getString("HEADER_DUPLICATE_KEY_EXCEPTION", header)); //$NON-NLS-1$
				}

			}

			return (values.put(key, value));
		}

	}

	/**
	 * Returns the number of entries (distinct keys) in this dictionary.
	 *
	 * @return  the number of keys in this dictionary.
	 */
	public synchronized int size() {
		return (values.size());
	}

	/**
	 * Tests if this dictionary maps no keys to value. The general contract
	 * for the <tt>isEmpty</tt> method is that the result is true if and only
	 * if this dictionary contains no entries.
	 *
	 * @return  <code>true</code> if this dictionary maps no keys to values;
	 *          <code>false</code> otherwise.
	 */
	public synchronized boolean isEmpty() {
		return (values.isEmpty());
	}

	/**
	 * Always throws UnsupportedOperationException.
	 *
	 * @param key header name.
	 * @param value header value.
	 * @throws UnsupportedOperationException
	 */
	public Object put(Object key, Object value) {
		throw new UnsupportedOperationException();
	}

	/**
	 * Always throws UnsupportedOperationException.
	 *
	 * @param key header name.
	 * @throws UnsupportedOperationException
	 */
	public Object remove(Object key) {
		throw new UnsupportedOperationException();
	}

	public String toString() {
		return (values.toString());
	}

	public static Headers parseManifest(InputStream in) throws BundleException {
		try {
			Headers headers = new Headers(10);
			BufferedReader br;
			try {
				br = new BufferedReader(new InputStreamReader(in, "UTF8")); //$NON-NLS-1$
			} catch (UnsupportedEncodingException e) {
				br = new BufferedReader(new InputStreamReader(in));
			}

			String header = null;
			StringBuffer value = new StringBuffer(256);
			boolean firstLine = true;

			while (true) {
				String line = br.readLine();
				/* The java.util.jar classes in JDK 1.3 use the value of the last
				 * encountered manifest header. So we do the same to emulate
				 * this behavior. We no longer throw a BundleException
				 * for duplicate manifest headers.
				 */

				if ((line == null) || (line.length() == 0)) /* EOF or empty line */
				{
					if (!firstLine) /* flush last line */
					{
						headers.set(header, null); /* remove old attribute,if present */
						headers.set(header, value.toString().trim());
					}
					break; /* done processing main attributes */
				}

				if (line.charAt(0) == ' ') /* continuation */
				{
					if (firstLine) /* if no previous line */
					{
						throw new BundleException(Msg.formatter.getString("MANIFEST_INVALID_SPACE", line)); //$NON-NLS-1$
					}
					value.append(line.substring(1));
					continue;
				}

				if (!firstLine) {
					headers.set(header, null); /* remove old attribute,if present */
					headers.set(header, value.toString().trim());
					value.setLength(0); /* clear StringBuffer */
				}

				int colon = line.indexOf(':');
				if (colon == -1) /* no colon */
				{
					throw new BundleException(Msg.formatter.getString("MANIFEST_INVALID_LINE_NOCOLON", line)); //$NON-NLS-1$
				}
				header = line.substring(0, colon).trim();
				value.append(line.substring(colon + 1));
				firstLine = false;
			}
			return headers;
		} catch (IOException e) {
			throw new BundleException(Msg.formatter.getString("MANIFEST_IOEXCEPTION"), e); //$NON-NLS-1$
		} finally {
			try {
				in.close();
			} catch (IOException ee) {
			}
		}
	}
}

Back to the top