Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bd9333f920a7de55cb197e3f622d9ef50535a99d (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
273
274
275
276
277
278
279
280
281
/*******************************************************************************
 * Copyright (c) 2000, 2007 QNX Software Systems 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:
 *     IBM Corporation - initial API and implementation
 *     QNX Software Systems - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/

package org.eclipse.cdt.internal.core;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.resources.IPathEntryVariableChangeListener;
import org.eclipse.cdt.core.resources.IPathEntryVariableManager;
import org.eclipse.cdt.core.resources.PathEntryVariableChangeEvent;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.SafeRunner;



/**
 * Core's implementation of IPathEntryVariableManager. 
 */
public class PathEntryVariableManager implements IPathEntryVariableManager {

	private Set<IPathEntryVariableChangeListener> listeners;
	private Preferences preferences;

	static final String VARIABLE_PREFIX = "pathEntryVariable."; //$NON-NLS-1$

	/**
	 * Constructor for the class.
	 * 
	 * The current manager implementation is not used any more
	 * Instead the CdtVarPathEntryVariableManager is used that actually wraps the CdtVariables contributed at workspace level
	 * 
	 * NOTE: all PathEntryVariableManager functionality remains workable with the new
	 * CdtVarPathEntryVariableManager. We could either remove this class or copy the contents of the 
	 * CdtVarPathEntryVariableManager to this class to preserve internal class name for better backward compatibility.
	 * 
	 * 
	 */ 
	private PathEntryVariableManager() {
		this.listeners = Collections.synchronizedSet(new HashSet<IPathEntryVariableChangeListener>());
		this.preferences = CCorePlugin.getDefault().getPluginPreferences();
	}

	/**
	 * Note that if a user changes the key in the preferences file to be invalid
	 * and then calls #getValue using that key, they will get the value back for
	 * that. But then if they try and call #setValue using the same key it will throw
	 * an exception. We may want to revisit this behaviour in the future.
	 * 
	 * @see org.eclipse.cdt.core.resources.IPathEntryVariableManager#getValue(String)
	 */
	public IPath getValue(String varName) {
		String key = getKeyForName(varName);
		String value = preferences.getString(key);
		return value.length() == 0 ? null : Path.fromPortableString(value);
	}

	/**
	 * @see org.eclipse.cdt.core.resources.IPathEntryVariableManager#setValue(String, IPath)
	 */
	public void setValue(String varName, IPath newValue) throws CoreException {
		//if the location doesn't have a device, see if the OS will assign one
		if (newValue != null && newValue.isAbsolute() && newValue.getDevice() == null) {
			newValue = new Path(newValue.toFile().getAbsolutePath());
		}
		int eventType;
		// read previous value and set new value atomically in order to generate the right event		
		synchronized (this) {
			IPath currentValue = getValue(varName);
			boolean variableExists = currentValue != null;
			if (currentValue == null) {
				if (newValue == null) {
					return;
				}
			} else if (currentValue.equals(newValue)) {
				return;
			}
			if (newValue == null) {
				preferences.setToDefault(getKeyForName(varName));
				eventType = PathEntryVariableChangeEvent.VARIABLE_DELETED;
			} else {
				preferences.setValue(getKeyForName(varName), newValue.toPortableString());
				eventType = variableExists ? PathEntryVariableChangeEvent.VARIABLE_CHANGED : PathEntryVariableChangeEvent.VARIABLE_CREATED;
			}
		}
		// notify listeners from outside the synchronized block to avoid deadlocks
		fireVariableChangeEvent(varName, newValue, eventType);
	}

	/**
	 * Return a key to use in the Preferences.
	 */
	private String getKeyForName(String varName) {
		return VARIABLE_PREFIX + varName;
	}

	/**
	 * @see org.eclipse.cdt.core.resources.IPathEntryVariableManager#resolvePath(IPath)
	 */
	public IPath resolvePath(IPath path) {
		if (path == null || path.segmentCount() == 0) {
			return path;
		}
		String variable = path.toPortableString();
		if (variable.indexOf('$') == -1) {
			return path;
		}
		String value = expandVariable(variable);
		return (value == null || value.length() == 0) ? Path.EMPTY : new Path(value);
	}

	/**
	 * Fires a property change event corresponding to a change to the
	 * current value of the variable with the given name.
	 * 
	 * @param name the name of the variable, to be used as the variable
	 *      in the event object
	 * @param value the current value of the path variable or <code>null</code> if
	 *      the variable was deleted
	 * @param type one of <code>IPathVariableChangeEvent.VARIABLE_CREATED</code>,
	 *      <code>PathEntryVariableChangeEvent.VARIABLE_CHANGED</code>, or
	 *      <code>PathEntryVariableChangeEvent.VARIABLE_DELETED</code>
	 * @see PathEntryVariableChangeEvent
	 * @see PathEntryVariableChangeEvent#VARIABLE_CREATED
	 * @see PathEntryVariableChangeEvent#VARIABLE_CHANGED
	 * @see PathEntryVariableChangeEvent#VARIABLE_DELETED
	 */
	private void fireVariableChangeEvent(String name, IPath value, int type) {
		if (this.listeners.size() == 0)
			return;
		// use a separate collection to avoid interference of simultaneous additions/removals 
		Object[] listenerArray = this.listeners.toArray();
		final PathEntryVariableChangeEvent pve = new PathEntryVariableChangeEvent(this, name, value, type);
		for (int i = 0; i < listenerArray.length; ++i) {
			final IPathEntryVariableChangeListener l = (IPathEntryVariableChangeListener) listenerArray[i];
			ISafeRunnable job = new ISafeRunnable() {
				public void handleException(Throwable exception) {
					// already being logged in Platform#run()
				}

				public void run() throws Exception {
					l.pathVariableChanged(pve);
				}
			};
			SafeRunner.run(job);
		}
	}

	/**
	 * @see org.eclipse.core.resources.IPathVariableManager#getPathVariableNames()
	 */
	public String[] getVariableNames() {
		List<String> result = new LinkedList<String>();
		String[] names = preferences.propertyNames();
		for (String name : names) {
			if (name.startsWith(VARIABLE_PREFIX)) {
				String key = name.substring(VARIABLE_PREFIX.length());
				result.add(key);
			}
		}
		return result.toArray(new String[result.size()]);
	}

	/**
	 * @see org.eclipse.cdt.core.resources.
	 * IPathEntryVariableManager#addChangeListener(IPathEntryVariableChangeListener)
	 */
	public void addChangeListener(IPathEntryVariableChangeListener listener) {
		listeners.add(listener);
	}

	/**
	 * @see org.eclipse.cdt.core.resources.
	 * IPathEntryVariableManager#removeChangeListener(IPathEntryVariableChangeListener)
	 */
	public void removeChangeListener(IPathEntryVariableChangeListener listener) {
		listeners.remove(listener);
	}

	/**
	 * @see org.eclipse.core.resources.IPathVariableManager#isDefined(String)
	 */
	public boolean isDefined(String varName) {
		return getValue(varName) != null;
	}

	public void startup() {
	}

	public void shutdown() {
	}

	/**
	 * Expand the variable with the format ${key}. example:
	 * with variable HOME=/foobar
	 * ${HOME}/project
	 * The the return value will be /foobar/project.
	 */
	protected String expandVariable(String variable) {
		StringBuffer sb = new StringBuffer();
		StringBuffer param = new StringBuffer();
		char prev = '\n';
		char ch = prev;
		boolean inMacro = false;
		boolean inSingleQuote = false;
		
		for (int i = 0; i < variable.length(); i++) {
			ch = variable.charAt(i);
			switch (ch) {
			case '\'':
				if (prev != '\\') {
					inSingleQuote = !inSingleQuote;
				}
				break;
				
			case '$' :
				if (!inSingleQuote && prev != '\\') {
					if (i < variable.length() && variable.indexOf('}', i) > 0) {
						char c = variable.charAt(i + 1);
						if (c == '{') {
							param.setLength(0);
							inMacro = true;
							prev = ch;
							continue;
						}
					}
				}
				break;
				
			case '}' :
				if (inMacro) {
					inMacro = false;
					String p = param.toString();
					IPath path = getValue(p);
					if (path != null) {
						String v = path.toPortableString();
						if (v != null) {
							sb.append(v);
						}
					}
					param.setLength(0);
					/* Skip the trailing } */
					prev = ch;
					continue;
				}
				break;
			} /* switch */
			
			if (!inMacro) {
				sb.append(ch);
			} else {
				/* Do not had the '{' */
				if (!(ch == '{' && prev == '$')) {
					param.append(ch);
				}
			}
			prev = (ch == '\\' && prev == '\\') ? '\n' : ch;
		} /* for */
		return sb.toString();
	}


}

Back to the top