Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 66f07285bb85ae790c9e3acf2d5eb65dc989bcad (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*******************************************************************************
 * Copyright (c) 2007, 2016 Intel Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Intel Corporation - Initial API and implementation
 * James Blackburn (Broadcom Corp.)
 * IBM Corporation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.cdt.core.settings.model.ICStorageElement;
import org.eclipse.cdt.internal.core.SafeStringInterner;
import org.eclipse.core.runtime.CoreException;

public class MapStorageElement implements ICStorageElement {
	private HashMap<String, String> fMap;
	private String fName;
	private MapStorageElement fParent;
	private static final String CHILDREN_KEY = "?children?"; //$NON-NLS-1$
	private static final String NAME_KEY = "?name?"; //$NON-NLS-1$
	private static final String VALUE_KEY = "?value?"; //$NON-NLS-1$
	private List<MapStorageElement> fChildren = new ArrayList<MapStorageElement>();
	private String fValue;

	public MapStorageElement(String name, MapStorageElement parent) {
		fName = name;
		fParent = parent;
		fMap = new HashMap<String, String>();
	}

	public MapStorageElement(Map<String, String> map, MapStorageElement parent) {
		fName = map.get(getMapKey(NAME_KEY));
		fValue = map.get(getMapKey(VALUE_KEY));
		fMap = new HashMap<String, String>(map);
		fParent = parent;

		String children = map.get(getMapKey(CHILDREN_KEY));
		if (children != null) {
			List<String> childrenStrList = decodeList(children);
			int size = childrenStrList.size();
			if (size != 0) {
				for (int i = 0; i < size; i++) {
					Map<String, String> childMap = decodeMap(childrenStrList.get(i));
					MapStorageElement child = createChildElement(childMap);
					fChildren.add(child);
				}
			}
		}
	}

	protected MapStorageElement createChildElement(Map<String, String> childMap) {
		return new MapStorageElement(childMap, this);
	}

	protected String getMapKey(String name) {
		return name;
	}

	public Map<String, String> toStringMap() {
		@SuppressWarnings("unchecked")
		Map<String, String> map = (Map<String, String>) fMap.clone();
		if (fName != null)
			map.put(getMapKey(NAME_KEY), fName);
		else
			map.remove(getMapKey(NAME_KEY));

		if (fValue != null)
			map.put(getMapKey(VALUE_KEY), fValue);
		else
			map.remove(getMapKey(VALUE_KEY));

		int size = fChildren.size();
		if (size != 0) {
			List<String> childrenStrList = new ArrayList<String>(size);
			for (int i = 0; i < size; i++) {
				MapStorageElement child = fChildren.get(i);
				Map<String, String> childStrMap = child.toStringMap();
				String str = encodeMap(childStrMap);
				childrenStrList.add(str);
			}

			String childrenStr = encodeList(childrenStrList);
			map.put(getMapKey(CHILDREN_KEY), childrenStr);
		} else {
			map.remove(getMapKey(CHILDREN_KEY));
		}

		return map;
	}

	protected boolean isSystemKey(String key) {
		return key.indexOf('?') == 0 && key.lastIndexOf('?') == key.length() - 1;
	}

	@Override
	public void clear() {
		fMap.clear();
	}

	@Override
	public ICStorageElement createChild(String name) {
		MapStorageElement child = createChildElement(name);
		fChildren.add(child);
		return child;
	}

	protected MapStorageElement createChildElement(String name) {
		return new MapStorageElement(name, this);
	}

	@Override
	public String getAttribute(String name) {
		Object o = fMap.get(getMapKey(name));
		if (o instanceof String)
			return (String) o;
		return null;
	}

	@Override
	public boolean hasAttribute(String name) {
		return fMap.containsKey(getMapKey(name));
	}

	@Override
	public ICStorageElement[] getChildren() {
		return fChildren.toArray(new MapStorageElement[fChildren.size()]);
	}

	@Override
	public ICStorageElement[] getChildrenByName(String name) {
		List<ICStorageElement> children = new ArrayList<ICStorageElement>();
		for (ICStorageElement child : fChildren)
			if (name.equals(child.getName()))
				children.add(child);
		return new ICStorageElement[children.size()];
	}

	@Override
	public boolean hasChildren() {
		return !fChildren.isEmpty();
	}

	@Override
	public String getName() {
		return fName;
	}

	@Override
	public ICStorageElement getParent() {
		return fParent;
	}

	@Override
	public void removeChild(ICStorageElement child) {
		fChildren.remove(child);
		if (child instanceof MapStorageElement) {
			((MapStorageElement) child).removed();
		}
	}

	private void removed() {
		fParent = null;
	}

	@Override
	public void removeAttribute(String name) {
		fMap.remove(getMapKey(name));
	}

	@Override
	public void setAttribute(String name, String value) {
		fMap.put(getMapKey(name), value);
	}

	public static HashMap<String, String> decodeMap(String value) {
		List<String> list = decodeList(value);
		HashMap<String, String> map = new HashMap<String, String>();
		char escapeChar = '\\';

		for (int i = 0; i < list.size(); i++) {
			StringBuilder line = new StringBuilder(list.get(i));
			int lndx = 0;
			while (lndx < line.length()) {
				if (line.charAt(lndx) == '=') {
					if (line.charAt(lndx - 1) == escapeChar) {
						// escaped '=' - remove '\' and continue on.
						line.deleteCharAt(lndx - 1);
					} else {
						break;
					}
				}
				lndx++;
			}
			map.put(SafeStringInterner.safeIntern(line.substring(0, lndx)),
					SafeStringInterner.safeIntern(line.substring(lndx + 1)));
		}

		return map;

	}

	public static List<String> decodeList(String value) {
		List<String> list = new ArrayList<String>();
		if (value != null) {
			StringBuilder envStr = new StringBuilder(value);
			String escapeChars = "|\\"; //$NON-NLS-1$
			char escapeChar = '\\';
			try {
				while (envStr.length() > 0) {
					int ndx = 0;
					while (ndx < envStr.length()) {
						if (escapeChars.indexOf(envStr.charAt(ndx)) != -1) {
							if (envStr.charAt(ndx - 1) == escapeChar) {
								// escaped '|' - remove '\' and continue on.
								envStr.deleteCharAt(ndx - 1);
								if (ndx == envStr.length()) {
									break;
								}
							}
							if (envStr.charAt(ndx) == '|')
								break;
						}
						ndx++;
					}
					StringBuilder line = new StringBuilder(envStr.substring(0, ndx));
					/*					int lndx = 0;
										while (lndx < line.length()) {
											if (line.charAt(lndx) == '=') {
												if (line.charAt(lndx - 1) == escapeChar) {
													// escaped '=' - remove '\' and continue on.
													line.deleteCharAt(lndx - 1);
												} else {
													break;
												}
											}
											lndx++;
										}
					*/
					list.add(SafeStringInterner.safeIntern(line.toString()));
					envStr.delete(0, ndx + 1);
				}
			} catch (StringIndexOutOfBoundsException e) {
			}
		}
		return list;
	}

	public static String encodeMap(Map<String, String> values) {
		Iterator<Entry<String, String>> entries = values.entrySet().iterator();
		StringBuilder str = new StringBuilder();
		while (entries.hasNext()) {
			Entry<String, String> entry = entries.next();
			str.append(escapeChars(entry.getKey(), "=|\\", '\\')); //$NON-NLS-1$
			str.append("="); //$NON-NLS-1$
			str.append(escapeChars(entry.getValue(), "|\\", '\\')); //$NON-NLS-1$
			str.append("|"); //$NON-NLS-1$
		}
		return str.toString();
	}

	public static String encodeList(List<String> values) {
		StringBuilder str = new StringBuilder();
		Iterator<String> entries = values.iterator();
		while (entries.hasNext()) {
			String entry = entries.next();
			str.append(escapeChars(entry, "|\\", '\\')); //$NON-NLS-1$
			str.append("|"); //$NON-NLS-1$
		}
		return str.toString();
	}

	public static String escapeChars(String string, String escapeChars, char escapeChar) {
		StringBuilder str = new StringBuilder(string);
		for (int i = 0; i < str.length(); i++) {
			if (escapeChars.indexOf(str.charAt(i)) != -1) {
				str.insert(i, escapeChar);
				i++;
			}
		}
		return str.toString();
	}

	@Override
	public String getValue() {
		return fValue;
	}

	@Override
	public void setValue(String value) {
		fValue = value;
	}

	@Override
	public ICStorageElement importChild(ICStorageElement el) throws UnsupportedOperationException {
		// TODO
		throw new UnsupportedOperationException();
	}

	@Override
	public String[] getAttributeNames() {
		List<String> list = new ArrayList<String>(fMap.size());
		Set<Entry<String, String>> entrySet = fMap.entrySet();
		for (Entry<String, String> entry : entrySet) {
			String key = entry.getKey();
			if (!isSystemKey(key)) {
				list.add(key);
			}
		}

		return list.toArray(new String[list.size()]);
	}

	@Override
	public ICStorageElement createCopy() throws UnsupportedOperationException, CoreException {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean equals(ICStorageElement other) {
		throw new UnsupportedOperationException();
	}
}

Back to the top