Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6f458aefcdcb6846037d48ce4cf1b10d3c5f27c4 (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
/*******************************************************************************
 * Copyright (c) 2005, 2016 Intel Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.utils.envvar;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.cdt.core.envvar.EnvironmentVariable;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;


/**
 * This is an utility class that implements environment variable operations
 * functionality: append, prepend, replace and remove
 *
 * @since 3.0
 */
public class EnvVarOperationProcessor {
	/**
	 * performs the environment variable operation given an initial variable and
	 * a variable representing an operation to be performed
	 * Returns a new variable the represents the result of a performed operation
	 *
	 * @param initial the initial variable
	 * @param added the variable that specifies an operation to be performed on the
	 * initial variable value
	 * @return the new variable the represents the result of a performed operation
	 */
	static public IEnvironmentVariable performOperation(IEnvironmentVariable initial, IEnvironmentVariable added){
		if(initial == null){
			return added;
		}
		if(added == null)
			return initial;

		String name = added.getName();

		switch(added.getOperation()){
		case IEnvironmentVariable.ENVVAR_REMOVE:
			return new EnvironmentVariable(name,null,IEnvironmentVariable.ENVVAR_REMOVE,null);
		case IEnvironmentVariable.ENVVAR_APPEND:{
				String delimiter = added.getDelimiter();
				return new EnvironmentVariable(name,
						performAppend(initial.getValue(),added.getValue(),delimiter),
//						IEnvironmentVariable.ENVVAR_APPEND,
						delimiter);
			}
		case IEnvironmentVariable.ENVVAR_PREPEND:{
				String delimiter = added.getDelimiter();
				return new EnvironmentVariable(name,
						performPrepend(initial.getValue(),added.getValue(),delimiter),
//						IEnvironmentVariable.ENVVAR_PREPEND,
						delimiter);
			}
		case IEnvironmentVariable.ENVVAR_REPLACE:
		default:
			return new EnvironmentVariable(added.getName(),added.getValue(),added.getDelimiter());
		}
	}

	/**
	 * performs append or prepend given an initial String, a string to be appended/prepended and a delimiter
	 * Returns a String representing the result of the operation
	 * @param initialValue
	 * @param addValue
	 * @param delimiter
	 * @param prepend
	 * @return String
	 */
	static public String performAppendPrepend(String initialValue, String addValue, String delimiter, boolean prepend){
		if(initialValue == null)
			return addValue;
		if(addValue == null)
			return initialValue;

		if(delimiter == null || delimiter.isEmpty()){
			return prepend ? addValue + initialValue : initialValue + addValue;
		}

		List<String> value = convertToList(initialValue, delimiter);
		List<String> added = convertToList(addValue, delimiter);

		value = removeDuplicates(value, added);

		if(prepend)
			value.addAll(0,added);
		else
			value.addAll(added);

		return convertToString(value, delimiter);
	}

	/**
	 * performs append given an initial String, a string to be appended and a delimiter
	 * Returns a String representing the result of the operation
	 * @param initialValue
	 * @param addValue
	 * @param delimiter
	 * @return String
	 */
	static public String performAppend(String initialValue, String addValue, String delimiter){
		return performAppendPrepend(initialValue,addValue,delimiter,false);
	}

	/**
	 * performs prepend given an initial String, a string to be prepended and a delimiter
	 * Returns a String representing the result of the operation
	 * @param initialValue
	 * @param addValue
	 * @param delimiter
	 * @return String
	 */
	static public String performPrepend(String initialValue, String addValue, String delimiter){
		return performAppendPrepend(initialValue,addValue,delimiter,true);
	}

	/**
	 * performs an environment variable operation
	 * Returns String representing the result of the operation
	 * @param initialValue
	 * @param newValue
	 * @param delimiter
	 * @param op
	 * @return String
	 */
	static public String performOperation(String initialValue, String newValue, String delimiter, int op){
		switch(op){
		case IEnvironmentVariable.ENVVAR_REMOVE:
			return null;
		case IEnvironmentVariable.ENVVAR_PREPEND:
			return performPrepend(initialValue,newValue,delimiter);
		case IEnvironmentVariable.ENVVAR_APPEND:
			return performAppend(initialValue,newValue,delimiter);
		case IEnvironmentVariable.ENVVAR_REPLACE:
		default:
			return initialValue;
		}
	}

	/**
	 * Converts a given value to string using a delimiter passed to this method
	 * @param value
	 * @param delimiter
	 */
	static public List<String> convertToList(String value, String delimiter){
		if (value == null)
			value = ""; //$NON-NLS-1$
		List<String> list = new ArrayList<String>();
		int delLength = delimiter.length();
		int valLength = value.length();

		if(delLength == 0){
			list.add(value);
		}
		else{
			int start = 0;
			int stop;
			while(start < valLength){
				stop = value.indexOf(delimiter,start);
				if(stop == -1)
					stop = valLength;
				String subst = value.substring(start,stop);
				list.add(subst);
				start = stop + delLength;
			}
		}

		return list;
	}

	/**
	 * removes duplicates
	 */
	static public List<String> removeDuplicates(List<String> value, List<String> duplicates){
		List<String> list = new ArrayList<String>();
		Iterator<String> valueIter = value.iterator();
		while(valueIter.hasNext()){
			String curVal = valueIter.next();
			boolean duplFound = false;
			Iterator<String> duplicatesIter = duplicates.iterator();
			while(duplicatesIter.hasNext()){
				String curDupl = duplicatesIter.next();
				if(curVal.equals(curDupl)){
					duplFound = true;
					break;
				}
			}
			if(!duplFound)
				list.add(curVal);
		}
		return list;
	}

	/**
	 * Converts list to a single String using a given delimiter to separate
	 * the list value in the resulting String
	 * @param list
	 * @param delimiter
	 * @return String
	 */
	static public String convertToString(List<String> list, String delimiter){
		Iterator<String> iter = list.iterator();
		StringBuilder buffer = new StringBuilder();

		while(iter.hasNext()){
			buffer.append(iter.next());

			if(iter.hasNext())
				buffer.append(delimiter);
		}

		return buffer.toString();
	}

	/**
	 * normalizes the variable name. That is: removes prepended and appended spaces
	 * and converts the name to upper-case for Win32 systems
	 * @return the normalized name or <code>null</code> in case the name is not valid
	 */
	static public String normalizeName(String name){
		if(name == null)
			return null;
		if("".equals(name = name.trim()))   //$NON-NLS-1$
			return null;
		if(!EnvironmentVariableManager.getDefault().isVariableCaseSensitive())
			name = name.toUpperCase();
		return name;
	}

	static public IEnvironmentVariable[] filterVariables(IEnvironmentVariable variables[], String remove[]){

		if(variables == null || variables.length == 0)
			return variables;

		IEnvironmentVariable filtered[] = new IEnvironmentVariable[variables.length];
		int filteredNum = 0;
		for (IEnvironmentVariable var : variables) {
			String name = null;
			if(var != null && (name = normalizeName(var.getName())) != null){
				boolean skip = false;
				if(remove != null && remove.length > 0){
					for (String element : remove) {
						if(element != null && element.equals(name)){
							skip = true;
							break;
						}
					}
				}
				if(!skip)
					filtered[filteredNum++] = var;
			}
		}

		if(filteredNum != filtered.length){
			IEnvironmentVariable vars[] = new IEnvironmentVariable[filteredNum];
			for(int i = 0; i < filteredNum; i++)
				vars[i] = filtered[i];
			filtered = vars;
		}
		return filtered;
	}
}

Back to the top