~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/MapStorageElement.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
7
 
 *
8
 
 * Contributors:
9
 
 * Intel Corporation - Initial API and implementation
10
 
 * James Blackburn (Broadcom Corp.)
11
 
 *******************************************************************************/
12
 
package org.eclipse.cdt.managedbuilder.internal.core;
13
 
 
14
 
import java.util.ArrayList;
15
 
import java.util.HashMap;
16
 
import java.util.Iterator;
17
 
import java.util.List;
18
 
import java.util.Map;
19
 
import java.util.Map.Entry;
20
 
 
21
 
import org.eclipse.cdt.core.settings.model.ICStorageElement;
22
 
import org.eclipse.core.runtime.CoreException;
23
 
 
24
 
public class MapStorageElement implements ICStorageElement {
25
 
        private HashMap<String, String> fMap;
26
 
        private String fName;
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;
33
 
 
34
 
        public MapStorageElement(String name, MapStorageElement parent){
35
 
                fName = name;
36
 
                fParent = parent;
37
 
                fMap = new HashMap<String, String>();
38
 
        }
39
 
 
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);
44
 
                fParent = parent;
45
 
                
46
 
                String children = map.get(getMapKey(CHILDREN_KEY));
47
 
                if(children != null){
48
 
                        List<String> childrenStrList = decodeList(children);
49
 
                        int size = childrenStrList.size();
50
 
                        if(size != 0){
51
 
                                for(int i = 0; i < size; i++){
52
 
                                        Map<String, String> childMap = decodeMap(childrenStrList.get(i));
53
 
                                        MapStorageElement child = createChildElement(childMap);
54
 
                                        fChildren.add(child);
55
 
                                }
56
 
                        }
57
 
                }
58
 
        }
59
 
        
60
 
        protected MapStorageElement createChildElement(Map<String, String> childMap){
61
 
                return new MapStorageElement(childMap, this);
62
 
        }
63
 
        
64
 
        protected String getMapKey(String name){
65
 
                return name;
66
 
        }
67
 
        
68
 
        public Map<String, String> toStringMap(){
69
 
                Map<String, String> map = (Map<String, String>)fMap.clone();
70
 
                if(fName != null)
71
 
                        map.put(getMapKey(NAME_KEY), fName);
72
 
                else
73
 
                        map.remove(getMapKey(NAME_KEY));
74
 
                
75
 
                if(fValue != null)
76
 
                        map.put(getMapKey(VALUE_KEY), fValue);
77
 
                else
78
 
                        map.remove(getMapKey(VALUE_KEY));
79
 
                
80
 
                int size = fChildren.size();
81
 
                if(size != 0){
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);
88
 
                        }
89
 
                        
90
 
                        String childrenStr = encodeList(childrenStrList);
91
 
                        map.put(getMapKey(CHILDREN_KEY), childrenStr);
92
 
                } else {
93
 
                        map.remove(getMapKey(CHILDREN_KEY));
94
 
                }
95
 
                
96
 
                return map;
97
 
        }
98
 
        
99
 
        protected boolean isSystemKey(String key){
100
 
                return key.indexOf('?') == 0 && key.lastIndexOf('?') == key.length() - 1;
101
 
        }
102
 
 
103
 
        public void clear() {
104
 
                fMap.clear();
105
 
        }
106
 
 
107
 
        public ICStorageElement createChild(String name) {
108
 
                MapStorageElement child = createChildElement(name);
109
 
                fChildren.add(child);
110
 
                return child;
111
 
        }
112
 
        
113
 
        protected MapStorageElement createChildElement(String name){
114
 
                return new MapStorageElement(name, this); 
115
 
        }
116
 
 
117
 
        public String getAttribute(String name) {
118
 
                Object o = fMap.get(getMapKey(name));
119
 
                if(o instanceof String)
120
 
                        return (String)o;
121
 
                return null;
122
 
        }
123
 
        
124
 
        public boolean hasAttribute(String name) {
125
 
                return fMap.containsKey(getMapKey(name));
126
 
        }
127
 
 
128
 
        public ICStorageElement[] getChildren() {
129
 
                return fChildren.toArray(new MapStorageElement[fChildren.size()]);
130
 
        }
131
 
        
132
 
        public ICStorageElement[] getChildrenByName(String name) {
133
 
                List<ICStorageElement> children = new ArrayList<ICStorageElement>();
134
 
                for (ICStorageElement child : fChildren)
135
 
                        if (name.equals(child.getName()))
136
 
                                children.add(child);
137
 
                return new ICStorageElement[children.size()];
138
 
        }
139
 
        
140
 
        public boolean hasChildren() {
141
 
                return !fChildren.isEmpty();
142
 
        }
143
 
 
144
 
        public String getName() {
145
 
                return fName;
146
 
        }
147
 
 
148
 
        public ICStorageElement getParent() {
149
 
                return fParent;
150
 
        }
151
 
 
152
 
        public void removeChild(ICStorageElement child){
153
 
                fChildren.remove(child);
154
 
                if(child instanceof MapStorageElement){
155
 
                        ((MapStorageElement)child).removed();
156
 
                }
157
 
        }
158
 
        
159
 
        private void removed() {
160
 
                fParent = null;
161
 
        }
162
 
 
163
 
        public void removeAttribute(String name) {
164
 
                fMap.remove(getMapKey(name));
165
 
        }
166
 
 
167
 
        public void setAttribute(String name, String value) {
168
 
                fMap.put(getMapKey(name), value);
169
 
        }
170
 
        
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 = '\\';
175
 
 
176
 
                for(int i = 0; i < list.size(); i++){
177
 
                        StringBuffer line = new StringBuffer(list.get(i));
178
 
                        int lndx = 0;
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);
184
 
                                        } else {
185
 
                                                break;
186
 
                                        }
187
 
                                }
188
 
                                lndx++;
189
 
                        }
190
 
                        map.put(line.substring(0, lndx), line.substring(lndx + 1));
191
 
                }
192
 
                
193
 
                return map;
194
 
 
195
 
        }
196
 
        
197
 
        public static List<String> decodeList(String value) {
198
 
                List<String> list = new ArrayList<String>();
199
 
                if (value != null) {
200
 
                        StringBuffer envStr = new StringBuffer(value);
201
 
                        String escapeChars = "|\\"; //$NON-NLS-1$
202
 
                        char escapeChar = '\\';
203
 
                        try {
204
 
                                while (envStr.length() > 0) {
205
 
                                        int ndx = 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()) {
212
 
                                                                        break;
213
 
                                                                }
214
 
                                                        }
215
 
                                                        if (envStr.charAt(ndx) == '|')
216
 
                                                                break;
217
 
                                                }
218
 
                                                ndx++;
219
 
                                        }
220
 
                                        StringBuffer line = new StringBuffer(envStr.substring(0, ndx));
221
 
/*                                      int lndx = 0;
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);
227
 
                                                        } else {
228
 
                                                                break;
229
 
                                                        }
230
 
                                                }
231
 
                                                lndx++;
232
 
                                        }
233
 
*/
234
 
                                        list.add(line.toString());
235
 
                                        envStr.delete(0, ndx + 1);
236
 
                                }
237
 
                        } catch (StringIndexOutOfBoundsException e) {
238
 
                        }
239
 
                }
240
 
                return list;
241
 
        }
242
 
        
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$
252
 
                }
253
 
                return str.toString();
254
 
        }
255
 
        
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$
263
 
                }
264
 
                return str.toString();
265
 
        }
266
 
 
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);
272
 
                                i++;
273
 
                        }
274
 
                }
275
 
                return str.toString();
276
 
        }
277
 
 
278
 
        public String getValue() {
279
 
                return fValue;
280
 
        }
281
 
 
282
 
        public void setValue(String value) {
283
 
                fValue = value;
284
 
        }
285
 
 
286
 
        public ICStorageElement importChild(ICStorageElement el)
287
 
                        throws UnsupportedOperationException {
288
 
                // TODO
289
 
                throw new UnsupportedOperationException();
290
 
        }
291
 
 
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)){
298
 
                                list.add(key);
299
 
                        }
300
 
                }
301
 
                
302
 
                return list.toArray(new String[list.size()]);
303
 
        }
304
 
        
305
 
        public  ICStorageElement createCopy() throws UnsupportedOperationException, CoreException {
306
 
                throw new UnsupportedOperationException();
307
 
        }
308
 
        
309
 
        public boolean equals(ICStorageElement other) {
310
 
                throw new UnsupportedOperationException();
311
 
        }
312
 
}