Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7906c168476c9325fef383fa3c7fc9ed99d9bc48 (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
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.core;

import java.net.URL;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.variables.IDynamicVariable;
import org.eclipse.core.variables.IDynamicVariableResolver;

/**
 * Resolves common system variables. The current set of variables
 * supported (referenced as an argument to this variable) are:
 * <ul>
 * <li>ARCH - value of <code>BootLoader.getOSArch()</code></li>
 * <li>ECLIPSE_HOME - location of the Eclipse installation</li>
 * <li>NL - value of <code>BootLoader.getNL()</code></li>
 * <li>OS - value of <code>BootLoader.getOS()</code></li>
 * <li>WS - value of <code>BootLoader.getWS()</code></li>
 * </ul>
 * @since 3.0
 */
public class SystemVariableResolver implements IDynamicVariableResolver {
	/* (non-Javadoc)
	 * @see org.eclipse.core.variables.IDynamicVariableResolver#resolveValue(org.eclipse.core.variables.IDynamicVariable, java.lang.String)
	 */
	@Override
	public String resolveValue(IDynamicVariable variable, String argument) throws CoreException {
		if ("ARCH".equals(argument)) { //$NON-NLS-1$
			return Platform.getOSArch();
		} else if ("ECLIPSE_HOME".equals(argument)) { //$NON-NLS-1$
			URL installURL = Platform.getInstallLocation().getURL();
			IPath ppath = new Path(installURL.getFile()).removeTrailingSeparator();
			return getCorrectPath(ppath.toOSString());
		} else if ("NL".equals(argument)) { //$NON-NLS-1$
			return Platform.getNL();
		} else if ("OS".equals(argument)) { //$NON-NLS-1$
			return Platform.getOS();
		} else if ("WS".equals(argument)) { //$NON-NLS-1$
			return Platform.getWS();
		}
		return null;
	}

	private static String getCorrectPath(String path) {
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < path.length(); i++) {
			char c = path.charAt(i);
			if (Platform.getOS().equals("win32")) { //$NON-NLS-1$
				if (i == 0 && c == '/') {
					continue;
				}
			}
			// Some VMs may return %20 instead of a space
			if (c == '%' && i + 2 < path.length()) {
				char c1 = path.charAt(i + 1);
				char c2 = path.charAt(i + 2);
				if (c1 == '2' && c2 == '0') {
					i += 2;
					buf.append(" "); //$NON-NLS-1$
					continue;
				}
			}
			buf.append(c);
		}
		return buf.toString();
	}
}

Back to the top