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

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/envvar/StorableEnvironment.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) 2005, 2009 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.utils.envvar;
 
13
 
 
14
import java.util.HashMap;
 
15
import java.util.Iterator;
 
16
import java.util.Map;
 
17
 
 
18
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
 
19
import org.eclipse.cdt.core.settings.model.ICStorageElement;
 
20
import org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;
 
21
import org.eclipse.cdt.internal.core.settings.model.ExceptionFactory;
 
22
import org.eclipse.cdt.utils.envvar.StorableEnvironmentLoader.ISerializeInfo;
 
23
 
 
24
/**
 
25
 * This class represents the set of environment variables that could be loaded
 
26
 * and stored in XML
 
27
 * 
 
28
 * @since 3.0
 
29
 */
 
30
public class StorableEnvironment {
 
31
        public static final String ENVIRONMENT_ELEMENT_NAME = "environment"; //$NON-NLS-1$
 
32
        static final String ATTRIBUTE_APPEND = "append";  //$NON-NLS-1$
 
33
        static final String ATTRIBUTE_APPEND_CONTRIBUTED = "appendContributed";  //$NON-NLS-1$
 
34
        static final boolean DEFAULT_APPEND = true;
 
35
        /** The map of in-flight environment variables */
 
36
        Map<String, IEnvironmentVariable> fVariables;
 
37
        private boolean fIsDirty = false;
 
38
        boolean fIsChanged = false;
 
39
        final boolean fIsReadOnly;
 
40
        boolean fAppend = DEFAULT_APPEND;
 
41
        boolean fAppendContributedEnv = DEFAULT_APPEND;
 
42
 
 
43
        /** 
 
44
         * @return the live {@link IEnvironmentVariable} map
 
45
         */
 
46
        Map<String, IEnvironmentVariable> getMap(){
 
47
                if(fVariables == null)
 
48
                        fVariables = new HashMap<String, IEnvironmentVariable>();
 
49
                return fVariables;
 
50
        }
 
51
 
 
52
        /**
 
53
         * 
 
54
         * @param variables
 
55
         * @param isReadOnly
 
56
         */
 
57
        public StorableEnvironment(IEnvironmentVariable variables[], boolean isReadOnly) {
 
58
                setVariales(variables);
 
59
                fIsReadOnly = isReadOnly;
 
60
        }
 
61
 
 
62
        /**
 
63
         * Create new empty StorableEnvironment
 
64
         * @param isReadOnly
 
65
         */
 
66
        public StorableEnvironment(boolean isReadOnly) {
 
67
                fIsReadOnly = isReadOnly;
 
68
        }
 
69
 
 
70
        /**
 
71
         * Copy constructor.
 
72
         *
 
73
         * Creates a new StorableEnvironment from an existing StorableEnvironment. Settings
 
74
         * are copied wholesale from the previous enviornment. 
 
75
         *
 
76
         * Note that the previous environment's {@link ISerializeInfo} isn't copied 
 
77
         * over, as it's expected this environment's settings will be stored elsewhere
 
78
         *
 
79
         * @param env
 
80
         * @param isReadOnly
 
81
         */
 
82
        public StorableEnvironment(StorableEnvironment env, boolean isReadOnly) {
 
83
                if(env.fVariables != null)
 
84
                        fVariables = env.getAllVariablesMap();
 
85
                fAppend = env.fAppend;
 
86
                fAppendContributedEnv = env.fAppendContributedEnv;
 
87
                fIsReadOnly = isReadOnly;
 
88
                fIsDirty = env.isDirty();
 
89
        }
 
90
 
 
91
        /**
 
92
         * Initialize the StorableEnvironment from an ICStorageElement tree
 
93
         * @param element
 
94
         * @param isReadOnly
 
95
         */
 
96
        public StorableEnvironment(ICStorageElement element, boolean isReadOnly) {
 
97
                load(element);
 
98
                fIsReadOnly = isReadOnly;
 
99
        }
 
100
 
 
101
        /**
 
102
         * Load the preferences from an {@link ICStorageElement}
 
103
         * @param element
 
104
         */
 
105
        private void load(ICStorageElement element){
 
106
                ICStorageElement children[] = element.getChildren();
 
107
                for (int i = 0; i < children.length; ++i) {
 
108
                        ICStorageElement node = children[i];
 
109
                        if (node.getName().equals(StorableEnvVar.VARIABLE_ELEMENT_NAME)) {
 
110
                                addVariable(getMap(), new StorableEnvVar(node));
 
111
                        }
 
112
                }
 
113
                
 
114
                String append = element.getAttribute(ATTRIBUTE_APPEND);
 
115
                fAppend = append != null ? Boolean.valueOf(append).booleanValue()
 
116
                                : DEFAULT_APPEND;
 
117
                
 
118
                append = element.getAttribute(ATTRIBUTE_APPEND_CONTRIBUTED);
 
119
                fAppendContributedEnv = append != null ? Boolean.valueOf(append).booleanValue()
 
120
                                : DEFAULT_APPEND;
 
121
                
 
122
                fIsDirty = false;
 
123
                fIsChanged = false;
 
124
        }
 
125
 
 
126
        /**
 
127
         * Serialize the Storable enviornment into the ICStorageElement
 
128
         * 
 
129
         * NB assumes that any variables part of the ISerializeInfo will continue to be serialized
 
130
         * @param element
 
131
         */
 
132
        public void serialize(ICStorageElement element){
 
133
                element.setAttribute(ATTRIBUTE_APPEND, Boolean.valueOf(fAppend).toString());
 
134
                element.setAttribute(ATTRIBUTE_APPEND_CONTRIBUTED, Boolean.valueOf(fAppendContributedEnv).toString());
 
135
                if(fVariables != null){
 
136
                        Iterator<IEnvironmentVariable> iter = fVariables.values().iterator();
 
137
                        while(iter.hasNext()){
 
138
                                StorableEnvVar var = (StorableEnvVar)iter.next();
 
139
                                ICStorageElement varEl = element.createChild(StorableEnvVar.VARIABLE_ELEMENT_NAME);
 
140
                                var.serialize(varEl);
 
141
                        }
 
142
                }
 
143
 
 
144
                fIsDirty = false;
 
145
        }
 
146
 
 
147
        /**
 
148
         * Add the environment variable to the map
 
149
         * @param map
 
150
         * @param var
 
151
         */
 
152
        void addVariable(Map<String, IEnvironmentVariable> map, IEnvironmentVariable var){
 
153
                String name = getNameForMap(var.getName());
 
154
                if (name == null)
 
155
                        return;
 
156
                map.put(name,var);
 
157
        }
 
158
 
 
159
        public IEnvironmentVariable createVariable(String name, String value, int op, String delimiter){
 
160
                if(fIsReadOnly)
 
161
                        throw ExceptionFactory.createIsReadOnlyException();
 
162
                
 
163
                if(name == null || "".equals(name = name.trim())) //$NON-NLS-1$
 
164
                        return null;
 
165
 
 
166
                IEnvironmentVariable var = checkVariable(name,value,op,delimiter);
 
167
                if(var == null){
 
168
                        var = new StorableEnvVar(name, value, op, delimiter);
 
169
                        addVariable(getMap(), var);
 
170
                        fIsDirty = true;
 
171
                        fIsChanged = true;
 
172
                }
 
173
                return var;
 
174
        }
 
175
 
 
176
        public IEnvironmentVariable createVariable(String name){
 
177
                return createVariable(name,null,IEnvironmentVariable.ENVVAR_REPLACE,null);
 
178
        }
 
179
        
 
180
        public IEnvironmentVariable createVariable(String name, String value){
 
181
                return createVariable(name,value,IEnvironmentVariable.ENVVAR_REPLACE,null);     
 
182
        }
 
183
        
 
184
        public IEnvironmentVariable createVariable(String name, String value, String delimiter){
 
185
                if(fIsReadOnly)
 
186
                        throw ExceptionFactory.createIsReadOnlyException();
 
187
                return createVariable(name,value,IEnvironmentVariable.ENVVAR_REPLACE,delimiter);        
 
188
        }
 
189
        
 
190
        public IEnvironmentVariable checkVariable(String name, String value, int op, String delimiter){
 
191
                IEnvironmentVariable var = getVariable(name);
 
192
                if(var != null 
 
193
                                && checkStrings(var.getValue(),value)
 
194
                                && var.getOperation() == op
 
195
                                && checkStrings(var.getDelimiter(),delimiter))
 
196
                        return var;
 
197
                return null;
 
198
        }
 
199
        
 
200
        private boolean checkStrings(String str1, String str2){
 
201
                if(str1 != null &&
 
202
                                str1.equals(str2))
 
203
                        return true;
 
204
                return str1 == str2;
 
205
        }
 
206
        
 
207
        /**
 
208
         * Returns the "dirty" state of the environment.
 
209
         * If the dirty state is <code>true</code>, that means that the environment 
 
210
         * is out of synch with the repository and the environment needs to be serialized.
 
211
         * <br><br>
 
212
         * The dirty state is automatically set to <code>false</code> when the environment is serialized
 
213
         * by calling the serialize() method  
 
214
         * @return boolean 
 
215
         */
 
216
        public boolean isDirty(){
 
217
                return fIsDirty;
 
218
        }
 
219
        
 
220
        /**
 
221
         * sets the "dirty" state of the environment
 
222
         * @param dirty represents the new state
 
223
         */
 
224
        public void setDirty(boolean dirty){
 
225
                fIsDirty = dirty;
 
226
        }
 
227
        
 
228
        /**
 
229
         * Returns the "change" state of the environment.
 
230
         * The "change" state represents whether the environment was changed or not.
 
231
         * This state is not reset when the serialize() method is called
 
232
         * Users can use this state to monitor whether the environment was changed or not.
 
233
         * This state can be reset to <code>false</code> only by calling the setChanged(false) method 
 
234
         * @return boolean
 
235
         */
 
236
        public boolean isChanged(){
 
237
                return fIsChanged;
 
238
        }
 
239
        
 
240
        /**
 
241
         * sets the "change" state of the environment
 
242
         * @param changed represents the new "change" state
 
243
         */
 
244
        public void setChanged(boolean changed){
 
245
                if(fIsReadOnly)
 
246
                        throw ExceptionFactory.createIsReadOnlyException();
 
247
                fIsChanged = changed;
 
248
        }
 
249
 
 
250
        /**
 
251
         * @param name
 
252
         * @return the environment variable with the given name, or null
 
253
         */
 
254
        public IEnvironmentVariable getVariable(String name){
 
255
                name = getNameForMap(name);
 
256
                if (name == null)
 
257
                        return null;
 
258
                return getMap().get(name);
 
259
        }
 
260
        
 
261
        /**
 
262
         * Set the enviornment variables in this {@link StorableEnvironment}
 
263
         * @param vars
 
264
         */
 
265
        public void setVariales(IEnvironmentVariable vars[]){
 
266
                if(fIsReadOnly)
 
267
                        throw ExceptionFactory.createIsReadOnlyException();
 
268
                if(vars == null || vars.length == 0)
 
269
                        deleteAll();
 
270
                else{
 
271
                        if (getMap().size() != 0) {
 
272
                                Iterator<IEnvironmentVariable> iter = getMap().values().iterator();
 
273
                                while(iter.hasNext()){
 
274
                                        IEnvironmentVariable v = iter.next();
 
275
                                        int i;
 
276
                                        for(i = 0 ; i < vars.length; i++){
 
277
                                                if(v.getName().equals(vars[i].getName()))
 
278
                                                        break;
 
279
                                        }
 
280
                                        if(i == vars.length)
 
281
                                                deleteVariable(v.getName());
 
282
                                }
 
283
                        }
 
284
                        createVriables(vars);
 
285
                }
 
286
        }
 
287
        
 
288
        public void createVriables(IEnvironmentVariable vars[]){
 
289
                if(fIsReadOnly)
 
290
                        throw ExceptionFactory.createIsReadOnlyException();
 
291
                for(int i = 0; i < vars.length; i++)
 
292
                        createVariable(vars[i].getName(),
 
293
                                        vars[i].getValue(),
 
294
                                        vars[i].getOperation(),
 
295
                                        vars[i].getDelimiter());
 
296
        }
 
297
 
 
298
        /**
 
299
         * @return cloned map of all variables set on this storable environment runtime variables + backing store vars
 
300
         */
 
301
        Map<String, IEnvironmentVariable> getAllVariablesMap() {
 
302
                Map<String, IEnvironmentVariable> vars = new HashMap<String, IEnvironmentVariable>();
 
303
                vars.putAll(getMap());
 
304
                return vars;
 
305
        }
 
306
 
 
307
        public IEnvironmentVariable[] getVariables(){
 
308
                Map<String, IEnvironmentVariable> vars = getAllVariablesMap();
 
309
                return vars.values().toArray(new IEnvironmentVariable[vars.size()]);
 
310
        }
 
311
 
 
312
        /**
 
313
         * Returns the unique canonical form of the variable name for storage in the Maps.
 
314
         *
 
315
         * The name will be trimmed, and, if the var manager isn't case sensitive, made upper case
 
316
         *
 
317
         * @param name
 
318
         * @return canonical name, or null
 
319
         */
 
320
        String getNameForMap(String name) {
 
321
                if (name == null || (name = name.trim()).length() == 0)
 
322
                        return null;
 
323
                if (!EnvironmentVariableManager.getDefault().isVariableCaseSensitive())
 
324
                        return name.toUpperCase();
 
325
                return name;
 
326
        }
 
327
 
 
328
        public IEnvironmentVariable deleteVariable(String name){
 
329
                if(fIsReadOnly)
 
330
                        throw ExceptionFactory.createIsReadOnlyException();
 
331
                name = getNameForMap(name);
 
332
                if(name == null)
 
333
                        return null;
 
334
 
 
335
                IEnvironmentVariable var = getMap().remove(name);
 
336
                if(var != null){
 
337
                        fIsDirty = true;
 
338
                        fIsChanged = true;
 
339
                }
 
340
 
 
341
                return var;
 
342
        }
 
343
        
 
344
        public boolean deleteAll(){
 
345
                if(fIsReadOnly)
 
346
                        throw ExceptionFactory.createIsReadOnlyException();
 
347
                Map<String, IEnvironmentVariable> map = getMap();
 
348
                if(map.size() > 0) {
 
349
                        fIsDirty = true;
 
350
                        fIsChanged = true;
 
351
                        map.clear();
 
352
                        return true;
 
353
                }
 
354
                
 
355
                return false;
 
356
        }
 
357
        
 
358
        public boolean isReadOnly(){
 
359
                return fIsReadOnly;
 
360
        }
 
361
        
 
362
        public boolean appendEnvironment(){
 
363
                return fAppend;
 
364
        }
 
365
 
 
366
        public void setAppendEnvironment(boolean append){
 
367
                if(fAppend == append)
 
368
                        return;
 
369
 
 
370
                if(fIsReadOnly)
 
371
                        throw ExceptionFactory.createIsReadOnlyException();
 
372
 
 
373
                fAppend = append;
 
374
                fIsDirty = true;
 
375
        }
 
376
 
 
377
        public boolean appendContributedEnvironment(){
 
378
                return fAppendContributedEnv;
 
379
        }
 
380
 
 
381
        public void setAppendContributedEnvironment(boolean append){
 
382
                if(fAppendContributedEnv == append)
 
383
                        return;
 
384
 
 
385
                if(fIsReadOnly)
 
386
                        throw ExceptionFactory.createIsReadOnlyException();
 
387
                
 
388
                fAppendContributedEnv = append;
 
389
                fIsDirty = true;
 
390
        }
 
391
        
 
392
        public void restoreDefaults(){
 
393
                deleteAll();
 
394
                fAppend = DEFAULT_APPEND;
 
395
                fAppendContributedEnv = DEFAULT_APPEND;
 
396
        }
 
397
 
 
398
}