1
/*******************************************************************************
2
* Copyright (c) 2007 Intel Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Intel Corporation - Initial API and implementation
10
* James Blackburn (Broadcom Corp.)
11
*******************************************************************************/
12
package org.eclipse.cdt.managedbuilder.internal.core;
14
import java.util.ArrayList;
15
import java.util.HashMap;
16
import java.util.Iterator;
17
import java.util.List;
19
import java.util.Map.Entry;
21
import org.eclipse.cdt.core.settings.model.ICStorageElement;
22
import org.eclipse.core.runtime.CoreException;
24
public class MapStorageElement implements ICStorageElement {
25
private HashMap<String, String> fMap;
27
private MapStorageElement fParent;
28
private static final String CHILDREN_KEY = "?children?"; //$NON-NLS-1$
29
private static final String NAME_KEY = "?name?"; //$NON-NLS-1$
30
private static final String VALUE_KEY = "?value?"; //$NON-NLS-1$
31
private List<MapStorageElement> fChildren = new ArrayList<MapStorageElement>();
32
private String fValue;
34
public MapStorageElement(String name, MapStorageElement parent){
37
fMap = new HashMap<String, String>();
40
public MapStorageElement(Map<String, String> map, MapStorageElement parent){
41
fName = map.get(getMapKey(NAME_KEY));
42
fValue = map.get(getMapKey(VALUE_KEY));
43
fMap = new HashMap<String, String>(map);
46
String children = map.get(getMapKey(CHILDREN_KEY));
48
List<String> childrenStrList = decodeList(children);
49
int size = childrenStrList.size();
51
for(int i = 0; i < size; i++){
52
Map<String, String> childMap = decodeMap(childrenStrList.get(i));
53
MapStorageElement child = createChildElement(childMap);
60
protected MapStorageElement createChildElement(Map<String, String> childMap){
61
return new MapStorageElement(childMap, this);
64
protected String getMapKey(String name){
68
public Map<String, String> toStringMap(){
69
Map<String, String> map = (Map<String, String>)fMap.clone();
71
map.put(getMapKey(NAME_KEY), fName);
73
map.remove(getMapKey(NAME_KEY));
76
map.put(getMapKey(VALUE_KEY), fValue);
78
map.remove(getMapKey(VALUE_KEY));
80
int size = fChildren.size();
82
List<String> childrenStrList = new ArrayList<String>(size);
83
for(int i = 0; i < size; i++){
84
MapStorageElement child = fChildren.get(i);
85
Map<String, String> childStrMap = child.toStringMap();
86
String str = encodeMap(childStrMap);
87
childrenStrList.add(str);
90
String childrenStr = encodeList(childrenStrList);
91
map.put(getMapKey(CHILDREN_KEY), childrenStr);
93
map.remove(getMapKey(CHILDREN_KEY));
99
protected boolean isSystemKey(String key){
100
return key.indexOf('?') == 0 && key.lastIndexOf('?') == key.length() - 1;
103
public void clear() {
107
public ICStorageElement createChild(String name) {
108
MapStorageElement child = createChildElement(name);
109
fChildren.add(child);
113
protected MapStorageElement createChildElement(String name){
114
return new MapStorageElement(name, this);
117
public String getAttribute(String name) {
118
Object o = fMap.get(getMapKey(name));
119
if(o instanceof String)
124
public boolean hasAttribute(String name) {
125
return fMap.containsKey(getMapKey(name));
128
public ICStorageElement[] getChildren() {
129
return fChildren.toArray(new MapStorageElement[fChildren.size()]);
132
public ICStorageElement[] getChildrenByName(String name) {
133
List<ICStorageElement> children = new ArrayList<ICStorageElement>();
134
for (ICStorageElement child : fChildren)
135
if (name.equals(child.getName()))
137
return new ICStorageElement[children.size()];
140
public boolean hasChildren() {
141
return !fChildren.isEmpty();
144
public String getName() {
148
public ICStorageElement getParent() {
152
public void removeChild(ICStorageElement child){
153
fChildren.remove(child);
154
if(child instanceof MapStorageElement){
155
((MapStorageElement)child).removed();
159
private void removed() {
163
public void removeAttribute(String name) {
164
fMap.remove(getMapKey(name));
167
public void setAttribute(String name, String value) {
168
fMap.put(getMapKey(name), value);
171
public static Map<String, String> decodeMap(String value) {
172
List<String> list = decodeList(value);
173
Map<String, String> map = new HashMap<String, String>();
174
char escapeChar = '\\';
176
for(int i = 0; i < list.size(); i++){
177
StringBuffer line = new StringBuffer(list.get(i));
179
while (lndx < line.length()) {
180
if (line.charAt(lndx) == '=') {
181
if (line.charAt(lndx - 1) == escapeChar) {
182
// escaped '=' - remove '\' and continue on.
183
line.deleteCharAt(lndx - 1);
190
map.put(line.substring(0, lndx), line.substring(lndx + 1));
197
public static List<String> decodeList(String value) {
198
List<String> list = new ArrayList<String>();
200
StringBuffer envStr = new StringBuffer(value);
201
String escapeChars = "|\\"; //$NON-NLS-1$
202
char escapeChar = '\\';
204
while (envStr.length() > 0) {
206
while (ndx < envStr.length()) {
207
if (escapeChars.indexOf(envStr.charAt(ndx)) != -1) {
208
if (envStr.charAt(ndx - 1) == escapeChar) {
209
// escaped '|' - remove '\' and continue on.
210
envStr.deleteCharAt(ndx - 1);
211
if (ndx == envStr.length()) {
215
if (envStr.charAt(ndx) == '|')
220
StringBuffer line = new StringBuffer(envStr.substring(0, ndx));
222
while (lndx < line.length()) {
223
if (line.charAt(lndx) == '=') {
224
if (line.charAt(lndx - 1) == escapeChar) {
225
// escaped '=' - remove '\' and continue on.
226
line.deleteCharAt(lndx - 1);
234
list.add(line.toString());
235
envStr.delete(0, ndx + 1);
237
} catch (StringIndexOutOfBoundsException e) {
243
public static String encodeMap(Map<String, String> values) {
244
Iterator<Entry<String, String>> entries = values.entrySet().iterator();
245
StringBuffer str = new StringBuffer();
246
while (entries.hasNext()) {
247
Entry<String, String> entry = entries.next();
248
str.append(escapeChars(entry.getKey(), "=|\\", '\\')); //$NON-NLS-1$
249
str.append("="); //$NON-NLS-1$
250
str.append(escapeChars(entry.getValue(), "|\\", '\\')); //$NON-NLS-1$
251
str.append("|"); //$NON-NLS-1$
253
return str.toString();
256
public static String encodeList(List<String> values) {
257
StringBuffer str = new StringBuffer();
258
Iterator<String> entries = values.iterator();
259
while (entries.hasNext()) {
260
String entry = entries.next();
261
str.append(escapeChars(entry, "|\\", '\\')); //$NON-NLS-1$
262
str.append("|"); //$NON-NLS-1$
264
return str.toString();
267
public static String escapeChars(String string, String escapeChars, char escapeChar) {
268
StringBuffer str = new StringBuffer(string);
269
for (int i = 0; i < str.length(); i++) {
270
if (escapeChars.indexOf(str.charAt(i)) != -1) {
271
str.insert(i, escapeChar);
275
return str.toString();
278
public String getValue() {
282
public void setValue(String value) {
286
public ICStorageElement importChild(ICStorageElement el)
287
throws UnsupportedOperationException {
289
throw new UnsupportedOperationException();
292
public String[] getAttributeNames() {
293
List<String> list = new ArrayList<String>(fMap.size());
294
for(Iterator iter = fMap.entrySet().iterator(); iter.hasNext();){
295
Map.Entry entry = (Map.Entry)iter.next();
296
String key = (String)entry.getKey();
297
if(!isSystemKey(key)){
302
return list.toArray(new String[list.size()]);
305
public ICStorageElement createCopy() throws UnsupportedOperationException, CoreException {
306
throw new UnsupportedOperationException();
309
public boolean equals(ICStorageElement other) {
310
throw new UnsupportedOperationException();