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
9
* Intel Corporation - Initial API and implementation
10
* James Blackburn (Broadcom Corp.)
11
*******************************************************************************/
12
package org.eclipse.cdt.utils.envvar;
14
import java.util.HashMap;
15
import java.util.Iterator;
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;
25
* This class represents the set of environment variables that could be loaded
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;
44
* @return the live {@link IEnvironmentVariable} map
46
Map<String, IEnvironmentVariable> getMap(){
47
if(fVariables == null)
48
fVariables = new HashMap<String, IEnvironmentVariable>();
57
public StorableEnvironment(IEnvironmentVariable variables[], boolean isReadOnly) {
58
setVariales(variables);
59
fIsReadOnly = isReadOnly;
63
* Create new empty StorableEnvironment
66
public StorableEnvironment(boolean isReadOnly) {
67
fIsReadOnly = isReadOnly;
73
* Creates a new StorableEnvironment from an existing StorableEnvironment. Settings
74
* are copied wholesale from the previous enviornment.
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
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();
92
* Initialize the StorableEnvironment from an ICStorageElement tree
96
public StorableEnvironment(ICStorageElement element, boolean isReadOnly) {
98
fIsReadOnly = isReadOnly;
102
* Load the preferences from an {@link ICStorageElement}
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));
114
String append = element.getAttribute(ATTRIBUTE_APPEND);
115
fAppend = append != null ? Boolean.valueOf(append).booleanValue()
118
append = element.getAttribute(ATTRIBUTE_APPEND_CONTRIBUTED);
119
fAppendContributedEnv = append != null ? Boolean.valueOf(append).booleanValue()
127
* Serialize the Storable enviornment into the ICStorageElement
129
* NB assumes that any variables part of the ISerializeInfo will continue to be serialized
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);
148
* Add the environment variable to the map
152
void addVariable(Map<String, IEnvironmentVariable> map, IEnvironmentVariable var){
153
String name = getNameForMap(var.getName());
159
public IEnvironmentVariable createVariable(String name, String value, int op, String delimiter){
161
throw ExceptionFactory.createIsReadOnlyException();
163
if(name == null || "".equals(name = name.trim())) //$NON-NLS-1$
166
IEnvironmentVariable var = checkVariable(name,value,op,delimiter);
168
var = new StorableEnvVar(name, value, op, delimiter);
169
addVariable(getMap(), var);
176
public IEnvironmentVariable createVariable(String name){
177
return createVariable(name,null,IEnvironmentVariable.ENVVAR_REPLACE,null);
180
public IEnvironmentVariable createVariable(String name, String value){
181
return createVariable(name,value,IEnvironmentVariable.ENVVAR_REPLACE,null);
184
public IEnvironmentVariable createVariable(String name, String value, String delimiter){
186
throw ExceptionFactory.createIsReadOnlyException();
187
return createVariable(name,value,IEnvironmentVariable.ENVVAR_REPLACE,delimiter);
190
public IEnvironmentVariable checkVariable(String name, String value, int op, String delimiter){
191
IEnvironmentVariable var = getVariable(name);
193
&& checkStrings(var.getValue(),value)
194
&& var.getOperation() == op
195
&& checkStrings(var.getDelimiter(),delimiter))
200
private boolean checkStrings(String str1, String str2){
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.
212
* The dirty state is automatically set to <code>false</code> when the environment is serialized
213
* by calling the serialize() method
216
public boolean isDirty(){
221
* sets the "dirty" state of the environment
222
* @param dirty represents the new state
224
public void setDirty(boolean dirty){
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
236
public boolean isChanged(){
241
* sets the "change" state of the environment
242
* @param changed represents the new "change" state
244
public void setChanged(boolean changed){
246
throw ExceptionFactory.createIsReadOnlyException();
247
fIsChanged = changed;
252
* @return the environment variable with the given name, or null
254
public IEnvironmentVariable getVariable(String name){
255
name = getNameForMap(name);
258
return getMap().get(name);
262
* Set the enviornment variables in this {@link StorableEnvironment}
265
public void setVariales(IEnvironmentVariable vars[]){
267
throw ExceptionFactory.createIsReadOnlyException();
268
if(vars == null || vars.length == 0)
271
if (getMap().size() != 0) {
272
Iterator<IEnvironmentVariable> iter = getMap().values().iterator();
273
while(iter.hasNext()){
274
IEnvironmentVariable v = iter.next();
276
for(i = 0 ; i < vars.length; i++){
277
if(v.getName().equals(vars[i].getName()))
281
deleteVariable(v.getName());
284
createVriables(vars);
288
public void createVriables(IEnvironmentVariable vars[]){
290
throw ExceptionFactory.createIsReadOnlyException();
291
for(int i = 0; i < vars.length; i++)
292
createVariable(vars[i].getName(),
294
vars[i].getOperation(),
295
vars[i].getDelimiter());
299
* @return cloned map of all variables set on this storable environment runtime variables + backing store vars
301
Map<String, IEnvironmentVariable> getAllVariablesMap() {
302
Map<String, IEnvironmentVariable> vars = new HashMap<String, IEnvironmentVariable>();
303
vars.putAll(getMap());
307
public IEnvironmentVariable[] getVariables(){
308
Map<String, IEnvironmentVariable> vars = getAllVariablesMap();
309
return vars.values().toArray(new IEnvironmentVariable[vars.size()]);
313
* Returns the unique canonical form of the variable name for storage in the Maps.
315
* The name will be trimmed, and, if the var manager isn't case sensitive, made upper case
318
* @return canonical name, or null
320
String getNameForMap(String name) {
321
if (name == null || (name = name.trim()).length() == 0)
323
if (!EnvironmentVariableManager.getDefault().isVariableCaseSensitive())
324
return name.toUpperCase();
328
public IEnvironmentVariable deleteVariable(String name){
330
throw ExceptionFactory.createIsReadOnlyException();
331
name = getNameForMap(name);
335
IEnvironmentVariable var = getMap().remove(name);
344
public boolean deleteAll(){
346
throw ExceptionFactory.createIsReadOnlyException();
347
Map<String, IEnvironmentVariable> map = getMap();
358
public boolean isReadOnly(){
362
public boolean appendEnvironment(){
366
public void setAppendEnvironment(boolean append){
367
if(fAppend == append)
371
throw ExceptionFactory.createIsReadOnlyException();
377
public boolean appendContributedEnvironment(){
378
return fAppendContributedEnv;
381
public void setAppendContributedEnvironment(boolean append){
382
if(fAppendContributedEnv == append)
386
throw ExceptionFactory.createIsReadOnlyException();
388
fAppendContributedEnv = append;
392
public void restoreDefaults(){
394
fAppend = DEFAULT_APPEND;
395
fAppendContributedEnv = DEFAULT_APPEND;