Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 56ad1bd8ac54002a787b839d1ba2ad6aa83b24c0 (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
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.ui.launchVariables;


import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationsMessages;

/**
 * Expands a resource type variable into the desired
 * result format.
 * <p>
 * This class is not intended to be extended by clients.
 * </p>
 */
public class ResourceExpander extends DefaultVariableExpander {

	/**
	 * Expands the variable to a resource.
	 */
	protected IResource expand(String varValue, ExpandVariableContext context) {
		if (varValue != null && varValue.length() > 0) {
			return expandToMember(varValue);
		} else {
			return expandUsingContext(context);
		}
	}
	
	/**
	 * Expands using the current context information.
	 * By default, return the selected resource of the
	 * context.
	 */
	protected IResource expandUsingContext(ExpandVariableContext context) {
		return context.getSelectedResource();
	}
	
	/**
	 * Expands the variable value to a resource. The value
	 * will not be <code>null</code> nor empty. By default,
	 * lookup the member from the workspace root.
	 */
	protected IResource expandToMember(String varValue) {
		return getWorkspaceRoot().findMember(varValue);
	}
	
	/**
	 * @see IVariableExpander#getPath(String, String, ExpandVariableContext)
	 */
	public IPath getPath(String varTag, String varValue, ExpandVariableContext context) throws CoreException {
		IResource resource = expand(varValue, context);
		if (resource != null) {
			if (isPathVariable(varTag)) {
				return resource.getFullPath();
			} else {
				return resource.getLocation();
			}
		}
		throwExpansionException(varTag, LaunchConfigurationsMessages.getString("ResourceExpander.No_resource_selected._1")); //$NON-NLS-1$
		return null;
	}
	
	/**
	 * Returns whether the given variable tag is a known path
	 * variable tag. Path variable tags represent variables that
	 * expand to paths relative to the workspace root.
	 */
	private boolean isPathVariable(String varTag) {
		return varTag.equals(IVariableConstants.VAR_CONTAINER_PATH) ||
				varTag.equals(IVariableConstants.VAR_PROJECT_PATH) ||
				varTag.equals(IVariableConstants.VAR_RESOURCE_PATH);
	}

	/**
	 * @see IVariableExpander#getResources(String, String, ExpandVariableContext)
	 */
	public IResource[] getResources(String varTag, String varValue, ExpandVariableContext context) throws CoreException {
		IResource resource = expand(varValue, context);
		if (resource != null) {
			return new IResource[] {resource};
		}
		throwExpansionException(varTag, LaunchConfigurationsMessages.getString("ResourceExpander.No_resource_selected._2")); //$NON-NLS-1$
		return null;
	}
	
	/**
	 * Returns the workspace root resource.
	 */
	protected final IWorkspaceRoot getWorkspaceRoot() {
		return ResourcesPlugin.getWorkspace().getRoot();
	}
	
	/**
	 * Returns a string representation of the path to a file or directory
	 * for the given variable tag and value or <code>null</code>.
	 * 
	 * @see IVariableExpander#getText(String, String, ExpandVariableContext)
	 */
	public String getText(String varTag, String varValue, ExpandVariableContext context) throws CoreException {
		IPath path= getPath(varTag, varValue, context);
		if (path != null) {
			return path.toOSString();
		}
		throwExpansionException(varTag, LaunchConfigurationsMessages.getString("ResourceExpander.No_resource_selected._3")); //$NON-NLS-1$
		return null;
	}

}

Back to the top